diff --git a/src/components/position.rs b/src/components/position.rs index 919ab1d..a6f7935 100644 --- a/src/components/position.rs +++ b/src/components/position.rs @@ -2,7 +2,7 @@ use bracket_lib::prelude::*; use specs::prelude::*; use specs_derive::Component; -#[derive(Component)] +#[derive(Component, Clone, Copy)] pub struct Position { pub x: i32, pub y: i32, @@ -13,3 +13,21 @@ impl PartialEq for Position { self.x == other.x && self.y == other.y } } + +impl From for Position { + fn from(point: Point) -> Self { + Self { + x: point.x, + y: point.y, + } + } +} + +impl From for Point { + fn from(position: Position) -> Self { + Self { + x: position.x, + y: position.y, + } + } +} diff --git a/src/resources/map.rs b/src/resources/map.rs index 4cbc00c..6628f77 100644 --- a/src/resources/map.rs +++ b/src/resources/map.rs @@ -182,6 +182,10 @@ impl Map { self.tile_content[index] = Some(entity); } + pub fn clear_tile_content(&mut self, index: usize) { + self.tile_content[index] = None; + } + pub fn get_tiles(&self) -> &Vec { &self.tiles } diff --git a/src/state.rs b/src/state.rs index 9327caa..f866e9c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -117,7 +117,7 @@ impl State { let mut positions = self.ecs.write_storage::(); let mut players = self.ecs.write_storage::(); let monsters = self.ecs.write_storage::(); - let map = self.ecs.read_resource::(); + let mut map = self.ecs.write_resource::(); let mut stats = self.ecs.write_resource::(); let mut sound_system = self.ecs.write_resource::(); let wants_to_whips = self.ecs.read_storage::(); @@ -148,6 +148,9 @@ impl State { } } + let index = map.point2d_to_index(Point::from(*pos)); + map.clear_tile_content(index); + pos.x = destination.x; pos.y = destination.y; @@ -155,6 +158,9 @@ impl State { player_pos.x = pos.x; player_pos.y = pos.y; + let index = map.point2d_to_index(destination); + map.set_tile_content(index, player_entity); + self.ecs.write_resource::().force_tick(); sound_system.play_sound(sound_effects.step.clone()); @@ -217,9 +223,6 @@ impl State { } fn run_systems(&mut self) { - let mut map_indexing_system = MapIndexingSystem {}; - map_indexing_system.run_now(&self.ecs); - let mut whip_system = WhipSystem {}; whip_system.run_now(&self.ecs); diff --git a/src/systems/map_indexing_system.rs b/src/systems/map_indexing_system.rs deleted file mode 100644 index 1339cf9..0000000 --- a/src/systems/map_indexing_system.rs +++ /dev/null @@ -1,27 +0,0 @@ -use bracket_lib::prelude::*; -use specs::prelude::*; - -use crate::{components::Position, resources::Map}; - -pub struct MapIndexingSystem {} - -impl<'a> System<'a> for MapIndexingSystem { - type SystemData = ( - WriteExpect<'a, Map>, - ReadStorage<'a, Position>, - Entities<'a>, - ); - - fn run(&mut self, data: Self::SystemData) { - let (mut map, position, entities) = data; - - map.clear_all_tile_content(); - for (entity, position) in (&entities, &position).join() { - let index = map.point2d_to_index(Point { - x: position.x, - y: position.y, - }); - map.set_tile_content(index, entity); - } - } -} diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 477f4e3..2845938 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -1,7 +1,5 @@ -pub mod map_indexing_system; pub mod monster_ai_system; pub mod whip_system; -pub use map_indexing_system::MapIndexingSystem; pub use monster_ai_system::MonsterAiSystem; pub use whip_system::WhipSystem; diff --git a/src/systems/monster_ai_system.rs b/src/systems/monster_ai_system.rs index 243aaeb..91694f0 100644 --- a/src/systems/monster_ai_system.rs +++ b/src/systems/monster_ai_system.rs @@ -17,7 +17,7 @@ impl<'a> System<'a> for MonsterAiSystem { Entities<'a>, ReadExpect<'a, Clock>, ReadExpect<'a, Point>, - ReadExpect<'a, Map>, + WriteExpect<'a, Map>, WriteExpect<'a, Stats>, WriteStorage<'a, Monster>, WriteStorage<'a, Position>, @@ -31,7 +31,7 @@ impl<'a> System<'a> for MonsterAiSystem { entities, clock, player_pos, - map, + mut map, mut stats, mut monsters, mut positions, @@ -88,8 +88,12 @@ impl<'a> System<'a> for MonsterAiSystem { let _ = entities.delete(*entity); } _ => { + let index = map.point2d_to_index(Point::from(**position)); + map.clear_tile_content(index); position.x = destination.x; position.y = destination.y; + let index = map.point2d_to_index(destination); + map.set_tile_content(index, *entity); } } }