From 73aa491d24aa55566208e8e2c663c504ff3dd6e6 Mon Sep 17 00:00:00 2001 From: Alex Page Date: Tue, 1 Feb 2022 02:51:49 -0500 Subject: [PATCH] Fix monster/player collision issues --- src/main.rs | 2 +- src/resources/map.rs | 21 +++++++++++++++------ src/state.rs | 8 +++----- src/systems/monster_ai_system.rs | 9 ++++----- src/systems/whip_system.rs | 5 +++-- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index c0c6f0b..af06cc6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,7 @@ fn main() -> BError { ecs.register::(); ecs.register::(); - let map = Map::from_level(levels::get_level(starting_level)); + let mut map = Map::from_level(levels::get_level(starting_level)); map.spawn_entities(&mut ecs); ecs.insert(map); diff --git a/src/resources/map.rs b/src/resources/map.rs index 6628f77..1d753f9 100644 --- a/src/resources/map.rs +++ b/src/resources/map.rs @@ -80,9 +80,14 @@ impl Map { } } - pub fn spawn_entities(&self, ecs: &mut World) { - for (index, tile) in self.tiles.iter().enumerate() { - let point = self.index_to_point2d(index); + pub fn spawn_entities(&mut self, ecs: &mut World) { + for (index, tile) in self + .tiles + .iter_mut() + .enumerate() + .collect::>() + { + let point = Point::new(index % MAP_WIDTH, index / MAP_WIDTH); match tile { TileType::Player => { let player_entity = ecs @@ -102,6 +107,7 @@ impl Map { .build(); ecs.insert(point); ecs.insert(player_entity); + self.tile_content[index] = Some(player_entity); } TileType::Slow => { let mut rng = RandomNumberGenerator::new(); @@ -174,15 +180,18 @@ impl Map { }); } - pub fn get_tile_content(&self, index: usize) -> Option { + pub fn get_tile_content_at(&self, point: Point) -> Option { + let index = self.point2d_to_index(point); self.tile_content[index] } - pub fn set_tile_content(&mut self, index: usize, entity: Entity) { + pub fn set_tile_content_at(&mut self, point: Point, entity: Entity) { + let index = self.point2d_to_index(point); self.tile_content[index] = Some(entity); } - pub fn clear_tile_content(&mut self, index: usize) { + pub fn clear_tile_content_at(&mut self, point: Point) { + let index = self.point2d_to_index(point); self.tile_content[index] = None; } diff --git a/src/state.rs b/src/state.rs index f866e9c..e063062 100644 --- a/src/state.rs +++ b/src/state.rs @@ -141,15 +141,14 @@ impl State { if map.is_solid(destination) { sound_system.play_sound(sound_effects.blocked.clone()); } else { - if let Some(e) = map.get_tile_content(map.point2d_to_index(destination)) { + if let Some(e) = map.get_tile_content_at(destination) { if let Some(monster) = monsters.get(e) { stats.take_gems(damage_for_kind(monster.kind)); let _ = entities.delete(e); } } - let index = map.point2d_to_index(Point::from(*pos)); - map.clear_tile_content(index); + map.clear_tile_content_at(Point::from(*pos)); pos.x = destination.x; pos.y = destination.y; @@ -158,8 +157,7 @@ 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); + map.set_tile_content_at(destination, player_entity); self.ecs.write_resource::().force_tick(); diff --git a/src/systems/monster_ai_system.rs b/src/systems/monster_ai_system.rs index 91694f0..36ed68f 100644 --- a/src/systems/monster_ai_system.rs +++ b/src/systems/monster_ai_system.rs @@ -74,9 +74,10 @@ impl<'a> System<'a> for MonsterAiSystem { y: position.y + delta_y, }; - if let Some(e) = map.get_tile_content(map.point2d_to_index(destination)) { + if let Some(e) = map.get_tile_content_at(destination) { if let Some(_player) = players.get(e) { // TODO: Sound + map.clear_tile_content_at(Point::from(**position)); stats.take_gems(damage_for_kind(monster.kind)); let _ = entities.delete(*entity); } @@ -88,12 +89,10 @@ impl<'a> System<'a> for MonsterAiSystem { let _ = entities.delete(*entity); } _ => { - let index = map.point2d_to_index(Point::from(**position)); - map.clear_tile_content(index); + map.clear_tile_content_at(Point::from(**position)); position.x = destination.x; position.y = destination.y; - let index = map.point2d_to_index(destination); - map.set_tile_content(index, *entity); + map.set_tile_content_at(destination, *entity); } } } diff --git a/src/systems/whip_system.rs b/src/systems/whip_system.rs index 911c583..f9b5ff1 100644 --- a/src/systems/whip_system.rs +++ b/src/systems/whip_system.rs @@ -25,7 +25,7 @@ impl<'a> System<'a> for WhipSystem { fn run(&mut self, data: Self::SystemData) { let ( - map, + mut map, mut stop_clock, mut sound_output, sound_effects, @@ -92,9 +92,10 @@ impl<'a> System<'a> for WhipSystem { }; if let Some(dest) = destination { - if let Some(e) = map.get_tile_content(map.point2d_to_index(dest)) { + if let Some(e) = map.get_tile_content_at(dest) { if let Some(_monster) = monsters.get(e) { let _ = entities.delete(e); + map.clear_tile_content_at(dest); if let Some(sound) = &mut wants_to_whip.sound { sound.control::, _>().stop(); }