From 068f98cec100772defce8ba966e5b917558b191c Mon Sep 17 00:00:00 2001 From: troido Date: Sat, 4 Apr 2020 23:48:07 +0200 Subject: draw the room after new entities have been added --- src/components/mod.rs | 14 ++++---------- src/playerstate.rs | 1 + src/pos.rs | 1 + src/purgatory.rs | 10 +++------- src/resources/ground.rs | 30 ++++++++++++++++++------------ src/room.rs | 36 +++++++++++++++++------------------- src/systems/clear.rs | 19 +++++++++++++++++++ src/systems/mod.rs | 4 +++- src/systems/moving.rs | 21 +++++++-------------- src/systems/registernew.rs | 19 ++++++++++++------- src/systems/remove.rs | 2 +- src/systems/view.rs | 36 ++++++++++-------------------------- todo.md | 6 +++++- 13 files changed, 101 insertions(+), 98 deletions(-) create mode 100644 src/systems/clear.rs diff --git a/src/components/mod.rs b/src/components/mod.rs index b7e3875..1a590b2 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -33,7 +33,6 @@ use specs::{ DenseVecStorage, VecStorage, HashMapStorage, - FlaggedStorage, NullStorage, Component, Entity @@ -50,7 +49,8 @@ use crate::{ Timestamp }; -#[derive(Debug, Clone)] +#[derive(Component, Debug, Clone)] +#[storage(VecStorage)] pub struct Position{ pub pos: Pos } @@ -60,19 +60,13 @@ impl Position { } } -impl Component for Position { - type Storage = FlaggedStorage>; -} - -#[derive(Debug, Clone)] +#[derive(Component, Debug, Clone)] +#[storage(VecStorage)] pub struct Visible { pub sprite: Sprite, pub height: f64, pub name: String } -impl Component for Visible { - type Storage = FlaggedStorage>; -} #[derive(Component, Debug)] pub struct Controller { diff --git a/src/playerstate.rs b/src/playerstate.rs index f146cd2..f05332b 100644 --- a/src/playerstate.rs +++ b/src/playerstate.rs @@ -30,6 +30,7 @@ use crate::{ }; #[derive(Debug, Clone)] +#[allow(dead_code)] pub enum RoomPos { Pos(Pos), Name(String), diff --git a/src/pos.rs b/src/pos.rs index d95c6f7..0ea6435 100644 --- a/src/pos.rs +++ b/src/pos.rs @@ -18,6 +18,7 @@ impl Pos { Pos {x, y} } + #[allow(dead_code)] pub fn clamp(self, smaller: Pos, larger: Pos) -> Pos { Pos { x: clamp(self.x, smaller.x, larger.x), diff --git a/src/purgatory.rs b/src/purgatory.rs index 04024db..e5803d0 100644 --- a/src/purgatory.rs +++ b/src/purgatory.rs @@ -11,9 +11,7 @@ use crate::{ roomtemplate::RoomTemplate, systems::{ Move, - RegisterNew, ControlInput, - View, Remove, Create, Volate, @@ -29,14 +27,12 @@ pub fn purgatory_id() -> RoomId { pub fn create_purgatory<'a, 'b>(encyclopedia: &Encyclopedia) -> Room<'a, 'b> { let dispatcher = DispatcherBuilder::new() .with(Volate, "volate", &[]) - .with(RegisterNew::default(), "registernew", &[]) - .with(UpdateCooldowns, "cool_down", &["registernew"]) + .with(UpdateCooldowns, "cool_down", &[]) .with(ControlInput, "controlinput", &["cool_down"]) .with(ControlAI, "controlai", &["cool_down"]) .with(Move, "move", &["controlinput", "controlai"]) - .with(View::default(), "view", &["move", "volate"]) - .with(Create, "create", &["view"]) - .with(Remove, "remove", &["view", "move"]) + .with(Create, "create", &["move", "volate"]) + .with(Remove, "remove", &["volate", "move"]) .build(); let mut room = Room::new(purgatory_id(), encyclopedia.clone(), dispatcher); room.load_from_template(&RoomTemplate::from_json(&json!({ diff --git a/src/resources/ground.rs b/src/resources/ground.rs index 8194a44..7411a15 100644 --- a/src/resources/ground.rs +++ b/src/resources/ground.rs @@ -8,27 +8,33 @@ use specs::{ }; use crate::{ - components::{Visible, Removed, Flags, Flag}, + components::{Visible, Flags, Flag}, Pos }; #[derive(Default)] pub struct Ground { - pub cells: HashMap> + pub cells: HashMap>, + pub changes: HashSet } impl Ground { - pub fn components_on<'a, C: Component>(&self, pos: Pos, component_type: &'a ReadStorage, removals: &'a ReadStorage) -> Vec<&'a C> { - self.cells - .get(&pos) - .unwrap_or(&HashSet::new()) - .iter() - .filter(|e| !removals.contains(**e)) - .filter_map(|e| component_type.get(*e)) - .collect() + + pub fn insert(&mut self, pos: Pos, ent: Entity){ + self.cells.entry(pos).or_insert_with(HashSet::new).insert(ent); + self.changes.insert(pos); + } + + pub fn remove(&mut self, pos: &Pos, ent: Entity) -> bool{ + if let Some(cell) = self.cells.get_mut(pos) { + self.changes.insert(*pos); + cell.remove(&ent) + } else { + false + } } - pub fn all_components_on<'a, C: Component>(&self, pos: Pos, component_type: &'a ReadStorage) -> Vec<&'a C> { + pub fn components_on<'a, C: Component>(&self, pos: Pos, component_type: &'a ReadStorage) -> Vec<&'a C> { self.cells .get(&pos) .unwrap_or(&HashSet::new()) @@ -51,6 +57,6 @@ impl Ground { } pub fn flags_on<'a>(&self, pos: Pos, flags: &'a ReadStorage) -> HashSet { - self.all_components_on::(pos, flags).into_iter().fold(HashSet::new(), |a, b| &a | &b.0) + self.components_on::(pos, flags).into_iter().fold(HashSet::new(), |a, b| &a | &b.0) } } diff --git a/src/room.rs b/src/room.rs index fa46d47..2b4fb7e 100644 --- a/src/room.rs +++ b/src/room.rs @@ -6,7 +6,6 @@ use specs::{ WorldExt, DispatcherBuilder, Dispatcher, - Builder, Join, Entity }; @@ -30,7 +29,6 @@ use crate::{ Player, Inventory, Health, - New, Removed }, Encyclopedia, @@ -66,17 +64,17 @@ use crate::{ Spawn, Interact, DropLoot, - Growth + Growth, + Clear } }; pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> { DispatcherBuilder::new() .with(Volate, "volate", &[]) - .with(RegisterNew::default(), "registernew", &[]) - .with(Growth, "growth", &["registernew"]) - .with(UpdateCooldowns, "cool_down", &["registernew"]) - .with(Spawn, "spawn", &["registernew"]) + .with(Growth, "growth", &[]) + .with(UpdateCooldowns, "cool_down", &[]) + .with(Spawn, "spawn", &[]) .with(ControlInput, "controlinput", &["cool_down"]) .with(ControlAI, "controlai", &["cool_down"]) .with(Take, "take", &["controlinput", "controlai"]) @@ -85,20 +83,20 @@ pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> { .with(Move, "move", &["controlinput", "controlai"]) .with(Trapping, "trapping", &["move"]) .with(Fight, "fight", &["move"]) - .with(Heal, "heal", &["registernew"]) + .with(Heal, "heal", &[]) .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact"]) .with(Die, "die", &["attacking"]) .with(DropLoot, "droploot", &["attacking"]) - .with(View::default(), "view", &["move", "attacking", "volate", "die"]) - .with(Migrate, "migrate", &["view"]) - .with(Create, "create", &["view", "spawn", "droploot", "growth"]) - .with(Remove, "remove", &["view", "move", "droploot"]) + .with(Migrate, "migrate", &["move", "attacking", "volate", "die"]) + .with(Create, "create", &["migrate", "spawn", "droploot", "growth"]) + .with(Remove, "remove", &["migrate", "move", "droploot"]) .build() } pub struct Room<'a, 'b> { world: World, dispatcher: Dispatcher<'a, 'b>, + view_dispatcher: Dispatcher<'a, 'b>, pub id: RoomId, places: HashMap } @@ -129,6 +127,11 @@ impl <'a, 'b>Room<'a, 'b> { Room { world, dispatcher, + view_dispatcher: DispatcherBuilder::new() + .with(RegisterNew, "registernew", &[]) + .with(View, "view", &["registernew"]) + .with(Clear, "clear", &["view"]) + .build(), id, places: HashMap::new() } @@ -170,6 +173,7 @@ impl <'a, 'b>Room<'a, 'b> { self.world.fetch_mut::