From 73d48fe3719b7bbef14a3cffaadabd55df4b4bf2 Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:19 +0100 Subject: feat(types): entity downcasting and fnmut in world creation --- stockton-types/src/entity_store.rs | 21 ++++++++++++++++++++- stockton-types/src/lib.rs | 16 ++++++++++++++++ stockton-types/src/world.rs | 19 +++++++++++++++++-- 3 files changed, 53 insertions(+), 3 deletions(-) (limited to 'stockton-types/src') diff --git a/stockton-types/src/entity_store.rs b/stockton-types/src/entity_store.rs index 9984c0a..375ae74 100644 --- a/stockton-types/src/entity_store.rs +++ b/stockton-types/src/entity_store.rs @@ -1,12 +1,29 @@ +// Copyright (C) Oscar Shrimpton 2019 + +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. + +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. + +// You should have received a copy of the GNU General Public License along +// with this program. If not, see . //! Stores entities in a world. use std::collections::HashMap; use std::boxed::Box; use std::ops::Index; + +use downcast_rs::Downcast; + use crate::Vector3; /// An entity, capable of recieving events. -pub trait Entity { +pub trait Entity: Downcast { /// Should return the position of this entity in 3d space. /// @@ -15,6 +32,8 @@ pub trait Entity { } +impl_downcast!(Entity); + /// Stores all the entities in a live world. The BSPFile only specifies the starting entities, /// whereas this is mutable and thus is used to represent the current state of the world's entities. /// diff --git a/stockton-types/src/lib.rs b/stockton-types/src/lib.rs index 0612e86..7e6c9ea 100644 --- a/stockton-types/src/lib.rs +++ b/stockton-types/src/lib.rs @@ -1,7 +1,23 @@ +// Copyright (C) Oscar Shrimpton 2019 + +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. + +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. + +// You should have received a copy of the GNU General Public License along +// with this program. If not, see . //! Common types for all stockton crates. extern crate stockton_bsp; extern crate nalgebra as na; +#[macro_use] +extern crate downcast_rs; pub mod entity_store; pub use entity_store::{EntityStore, Entity}; diff --git a/stockton-types/src/world.rs b/stockton-types/src/world.rs index f687f5e..18a0edf 100644 --- a/stockton-types/src/world.rs +++ b/stockton-types/src/world.rs @@ -1,3 +1,18 @@ +// Copyright (C) Oscar Shrimpton 2019 + +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. + +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. + +// You should have received a copy of the GNU General Public License along +// with this program. If not, see . + //! The thing you play on and all the associated state. use crate::{EntityStore, Entity}; @@ -19,8 +34,8 @@ impl<'a> World<'a> { /// Returns None if any entities in the map have name conflicts. /// /// `mapper` is called for each BSPEntity to map it to a concrete rust type. - pub fn new(bsp: Pin>>, mapper: F) -> Option> - where F: Fn(&BSPEntity) -> (Box, String) { + pub fn new(bsp: Pin>>, mut mapper: F) -> Option> + where F: FnMut(&BSPEntity) -> (Box, String) { let mut entities: Vec<(Box, String)> = Vec::with_capacity(bsp.entities.entities.len()); for bsp_ent in bsp.entities.entities.iter() { -- cgit v1.2.3