diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:20 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:20 +0100 |
commit | 381e7189ade1262573d993f0c1eae60791e924d0 (patch) | |
tree | da38593272f25691ba9dda63f737a1d759c763da | |
parent | cbe643d91fd3f47ea14ad94a2c0a3f0219a4518e (diff) |
refactor(types): remove old code
-rw-r--r-- | stockton-render/src/walk_bsp.rs | 26 | ||||
-rw-r--r-- | stockton-types/src/ent_map.rs | 50 | ||||
-rw-r--r-- | stockton-types/src/entity_store.rs | 134 | ||||
-rw-r--r-- | stockton-types/src/lib.rs | 7 | ||||
-rw-r--r-- | stockton-types/src/world.rs | 10 | ||||
-rw-r--r-- | stockton-types/tests/ent_map.rs | 115 | ||||
-rw-r--r-- | stockton-types/tests/helpers.rs | 76 | ||||
-rw-r--r-- | stockton-types/tests/world.rs | 71 |
8 files changed, 4 insertions, 485 deletions
diff --git a/stockton-render/src/walk_bsp.rs b/stockton-render/src/walk_bsp.rs deleted file mode 100644 index 3701da0..0000000 --- a/stockton-render/src/walk_bsp.rs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (C) 2019 Oscar Shrimpton - -// 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 <http://www.gnu.org/licenses/>. - -//! Walks a compiled BSP tree and renders it - -use crate::draw::RenderingContext; -use stockton_bsp::BSPFile; -use std::pin::Pin; -use std::boxed::Box; - -fn walk_tree<'a>(ctx: &RenderingContext<'a>, file: &Pin<Box<BSPFile>>) -> (){ - // TODO - -}
\ No newline at end of file diff --git a/stockton-types/src/ent_map.rs b/stockton-types/src/ent_map.rs deleted file mode 100644 index e2eedcc..0000000 --- a/stockton-types/src/ent_map.rs +++ /dev/null @@ -1,50 +0,0 @@ -// 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 <http://www.gnu.org/licenses/>. - -/// Convenience function for creating mappers for `World::new()`. -#[macro_export] -macro_rules! ent_map { - ( $ ( $name:expr => $type:ident [ $( $key:expr => $target:ident ),* ] ),* ) => { - { - use stockton_bsp::lumps::entities::Entity as BSPEntity; - use stockton_types::Entity; - |ent: &BSPEntity| -> Option<Box<dyn Entity>> { - $( - if ent.attributes["classname"] == $name { - let mut valid = true; - { - $(let mut $target = false;);* - for key in ent.attributes.keys() { - $(if key == &$key { - $target = true; - continue; - });* - } - $(if !$target { - valid = false; - });* - } - if valid { - return Some(Box::new($type { - $( $target : ent.attributes[$key].into() ),* - })); - } - } - );* - None - } - } - } -}
\ No newline at end of file diff --git a/stockton-types/src/entity_store.rs b/stockton-types/src/entity_store.rs deleted file mode 100644 index ae2db82..0000000 --- a/stockton-types/src/entity_store.rs +++ /dev/null @@ -1,134 +0,0 @@ -// 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 <http://www.gnu.org/licenses/>. -//! 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: Downcast { - - /// Should return the position of this entity in 3d space. - /// - /// This will likely just be `self.pos` - fn get_position(&self) -> Vector3; - -} - -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. -/// -/// Internally, this uses a vector to store the entities, and a hashmap to map names to indicies in that vector. -/// -/// An entity's index may change, so if you want to target an entity throughout frames you should store its name. -pub struct EntityStore { - entities: Vec<Box<dyn Entity>>, - name_to_index: HashMap<String, usize> -} - -/// Returned when an entity's name conflicts with an existing entity. -pub struct NameConflict; - -impl EntityStore { - /// Try to add the given entity with the given name. - /// - /// # Returns - /// The name & index of the added entity if successful. - /// If an entity already exists with the given name, NameConflict is returned. - pub fn add(&mut self, entity: Box<dyn Entity>, name: String) -> Result<usize, NameConflict> { - if self.name_to_index.contains_key(&name) { - return Err(NameConflict) - } - self.name_to_index.insert(name, self.entities.len()); - self.entities.push(entity); - - Ok(self.entities.len() - 1) - } - - /// Remove the entity with the given index, returning it. - /// - /// Takes O(2n - i) time. - pub fn remove_by_index(&mut self, index: usize) -> Option<Box<dyn Entity>> { - if index >= self.entities.len() { - return None; - } - self.name_to_index.retain(|_,v| *v != index); - Some(self.entities.remove(index)) - } - - /// Removes the entity with the given name, returning it. - /// - /// Takes O(2n - i) time. - pub fn remove_by_name(&mut self, name: &str) -> Option<Box<dyn Entity>> { - let mut index: usize = self.entities.len(); - - self.name_to_index.retain(|k,v| { - if k == name { - index = *v; - return false; - } - true - }); - - if index >= self.entities.len() { - return None; - } - - Some(self.entities.remove(index)) - } - - /// Make a new EntityStore from a list of entities & names. - /// - /// Returns None in case of name conflicts in list. - pub fn from_entities(entities: Vec<(Box<dyn Entity>, String)>) -> Option<EntityStore> { - let mut store = EntityStore { - entities: Vec::with_capacity(entities.len()), - name_to_index: HashMap::with_capacity(entities.len()) - }; - - for (entity, name) in entities { - if store.add(entity, name).is_err() { - return None; - } - } - - Some(store) - } -} - -/// Indexes the EntityStore for a specific index. -/// If you want to target an entity for longer than one tick, store its name, not an index. -impl Index<usize> for EntityStore { - type Output = dyn Entity; - fn index(&self, index: usize) -> &Self::Output { - self.entities[index].as_ref() - } -} - -/// Indexes the EntityStore for a specific name. -/// This is what you should use if you plan to target an entity for more than one tick. -impl Index<&str> for EntityStore { - type Output = dyn Entity; - fn index(&self, index: &str) -> &Self::Output { - self.entities[self.name_to_index[index]].as_ref() - } -}
\ No newline at end of file diff --git a/stockton-types/src/lib.rs b/stockton-types/src/lib.rs index 7cc5ac0..5a409d8 100644 --- a/stockton-types/src/lib.rs +++ b/stockton-types/src/lib.rs @@ -16,17 +16,10 @@ extern crate stockton_bsp; extern crate nalgebra_glm as na; -#[macro_use] -extern crate downcast_rs; - -pub mod entity_store; -pub use entity_store::{EntityStore, Entity}; pub mod world; pub use world::World; -pub mod ent_map; - /// Alias for convenience pub type Vector2 = na::Vec2; /// Alias for convenience diff --git a/stockton-types/src/world.rs b/stockton-types/src/world.rs index e538d68..2c677b7 100644 --- a/stockton-types/src/world.rs +++ b/stockton-types/src/world.rs @@ -15,18 +15,16 @@ //! The thing you play on and all the associated state. -use std::pin::Pin; - use stockton_bsp::BSPFile; /// A loaded world. -pub struct World<'a> { - pub map: Pin<Box<BSPFile<'a>>> +pub struct World { + pub map: BSPFile } -impl<'a> World<'a> { +impl World { /// Create a new world from a BSPFile. - pub fn new(bsp: Pin<Box<BSPFile<'a>>>) -> Option<World<'a>> { + pub fn new(bsp: BSPFile) -> Option<World> { Some(World { map: bsp }) diff --git a/stockton-types/tests/ent_map.rs b/stockton-types/tests/ent_map.rs deleted file mode 100644 index 6c799ff..0000000 --- a/stockton-types/tests/ent_map.rs +++ /dev/null @@ -1,115 +0,0 @@ -// 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 <http://www.gnu.org/licenses/>. - -#[macro_use] -extern crate stockton_types; -extern crate stockton_bsp; - -#[macro_use] -mod helpers; - -use stockton_bsp::lumps::entities::Entity as BSPEntity; - -use stockton_types::{Entity, Vector3}; - -#[derive(Debug, PartialEq)] -struct A; -impl Entity for A { - fn get_position(&self) -> Vector3 { - Vector3::new(0.0, 0.0, 0.0) - } -} - -#[derive(Debug, PartialEq)] -struct B { - data: String, -} -impl Entity for B { - fn get_position(&self) -> Vector3 { - Vector3::new(0.0, 0.0, 0.0) - } -} - -#[derive(Debug, PartialEq)] -struct CustomStruct { - one: i32, - two: i32, - three: i32 -} - -impl From<&str> for CustomStruct { - fn from(_: &str) -> CustomStruct { - CustomStruct { one: 1, two: 2, three: 3 } - } -} - -#[derive(Debug, PartialEq)] -struct C { - data2: String, - into: CustomStruct -} -impl Entity for C { - fn get_position(&self) -> Vector3 { - Vector3::new(0.0, 0.0, 0.0) - } -} - -#[test] -fn ent_map_macro() { - let map_func = ent_map![ - "A" => A [], - "B" => B [ - "data" => data - ], - "C" => C [ - "data" => data2, - "into" => into - ] - ]; - - assert_eq!(map_func(&BSPEntity { - attributes: map![ - "classname" => "A" - ] - }).unwrap().downcast_ref::<A>().unwrap(), &A); - - assert_eq!(map_func(&BSPEntity { - attributes: map![ - "classname" => "B", - "data" => "foobar" - ] - }).unwrap().downcast_ref::<B>().unwrap(), &B { data: "foobar".to_string() }); - - assert_eq!(map_func(&BSPEntity { - attributes: map![ - "classname" => "C", - "data" => "foobar", - "into" => "a b c" - ] - }).unwrap().downcast_ref::<C>().unwrap(), &C { data2: "foobar".to_string(), into: CustomStruct { one: 1, two: 2, three: 3 } }); - - assert!(map_func(&BSPEntity { - attributes: map![ - "classname" => "D" - ] - }).is_none()); - - assert!(map_func(&BSPEntity { - attributes: map![ - "classname" => "B", - "ebeb" => "foobar" - ] - }).is_none()); -}
\ No newline at end of file diff --git a/stockton-types/tests/helpers.rs b/stockton-types/tests/helpers.rs deleted file mode 100644 index a0215cb..0000000 --- a/stockton-types/tests/helpers.rs +++ /dev/null @@ -1,76 +0,0 @@ -// 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 <http://www.gnu.org/licenses/>. -#![allow(dead_code, unused_macros)] - -extern crate stockton_bsp; - -use std::pin::Pin; - -use stockton_bsp::BSPFile; -use stockton_bsp::lumps::*; -use stockton_bsp::lumps::entities::Entity as BSPEntity; -use stockton_bsp::directory::{DirEntry, Header}; - -macro_rules! map( - { $($key:expr => $value:expr),* } => { - { - let mut m = ::std::collections::HashMap::new(); - $( - m.insert($key, $value); - )* - m - } - }; -); - - -pub fn dummy_bspfile(entities: Vec<BSPEntity<'static>>) -> Pin<Box<BSPFile<'static>>> { - Box::pin(BSPFile { - directory: Header { - version: 1, - dir_entries: [DirEntry { offset: 0, length: 0 }; 17] - }, - entities: EntitiesLump { - string: "dummy", - entities - }, - textures: TexturesLump { - textures: vec![].into_boxed_slice() - }, - planes: PlanesLump { - planes: vec![].into_boxed_slice() - }, - lightvols: LightVolsLump { - vols: vec![].into_boxed_slice() - }, - lightmaps: LightmapsLump { - maps: vec![].into_boxed_slice() - }, - meshverts: MeshVertsLump { - meshverts: vec![].into_boxed_slice() - }, - vertices: VerticesLump { - vertices: vec![].into_boxed_slice() - }, - effects: EffectsLump::empty(), - brushes: BrushesLump::empty(), - faces: FaceLump::empty(), - tree: BSPTree::empty(), - visdata: VisDataLump { - vecs: vec![].into_boxed_slice() - }, - models: ModelsLump::empty() - }) -}
\ No newline at end of file diff --git a/stockton-types/tests/world.rs b/stockton-types/tests/world.rs deleted file mode 100644 index 380918b..0000000 --- a/stockton-types/tests/world.rs +++ /dev/null @@ -1,71 +0,0 @@ -// 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 <http://www.gnu.org/licenses/>. - -extern crate stockton_types; -extern crate stockton_bsp; - -#[macro_use] -mod helpers; -use crate::helpers::*; - -use stockton_bsp::lumps::entities::Entity as BSPEntity; - -use stockton_types::{World, Entity, Vector3}; - -#[derive(Debug, PartialEq)] -struct DummyEntity; - -impl Entity for DummyEntity { - fn get_position(&self) -> Vector3 { - Vector3::new(0.0, 0.0, 0.0) - } -} - -/// Test creating a world from a dummy BSPFile with a simple mapper. -#[test] -fn world_creation() { - - let file = dummy_bspfile(vec![ - BSPEntity { - attributes: map!( - "name" => "1" - ) - }, - BSPEntity { - attributes: map!( - "name" => "2" - ) - }, - BSPEntity { - attributes: map!( - "name" => "3" - ) - } - ]); - - let mut called_times = 0; - - let world = World::new(file, |ent: &BSPEntity| { - called_times += 1; - Some((Box::new(DummyEntity), ent.attributes.get("name").unwrap().clone().into())) - }).unwrap(); - - - assert_eq!(called_times, 3); - - world.live_entities["1"].downcast_ref::<DummyEntity>().unwrap(); - world.live_entities["2"].downcast_ref::<DummyEntity>().unwrap(); - world.live_entities["3"].downcast_ref::<DummyEntity>().unwrap(); -}
\ No newline at end of file |