Fix monster/player collision issues

This commit is contained in:
Alex Page 2022-02-01 02:51:49 -05:00
parent d818c8f7a6
commit 73aa491d24
5 changed files with 26 additions and 19 deletions

View file

@ -61,7 +61,7 @@ fn main() -> BError {
ecs.register::<Player>(); ecs.register::<Player>();
ecs.register::<WantsToWhip>(); ecs.register::<WantsToWhip>();
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); map.spawn_entities(&mut ecs);
ecs.insert(map); ecs.insert(map);

View file

@ -80,9 +80,14 @@ impl Map {
} }
} }
pub fn spawn_entities(&self, ecs: &mut World) { pub fn spawn_entities(&mut self, ecs: &mut World) {
for (index, tile) in self.tiles.iter().enumerate() { for (index, tile) in self
let point = self.index_to_point2d(index); .tiles
.iter_mut()
.enumerate()
.collect::<Vec<(usize, &mut TileType)>>()
{
let point = Point::new(index % MAP_WIDTH, index / MAP_WIDTH);
match tile { match tile {
TileType::Player => { TileType::Player => {
let player_entity = ecs let player_entity = ecs
@ -102,6 +107,7 @@ impl Map {
.build(); .build();
ecs.insert(point); ecs.insert(point);
ecs.insert(player_entity); ecs.insert(player_entity);
self.tile_content[index] = Some(player_entity);
} }
TileType::Slow => { TileType::Slow => {
let mut rng = RandomNumberGenerator::new(); let mut rng = RandomNumberGenerator::new();
@ -174,15 +180,18 @@ impl Map {
}); });
} }
pub fn get_tile_content(&self, index: usize) -> Option<Entity> { pub fn get_tile_content_at(&self, point: Point) -> Option<Entity> {
let index = self.point2d_to_index(point);
self.tile_content[index] 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); 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; self.tile_content[index] = None;
} }

View file

@ -141,15 +141,14 @@ impl State {
if map.is_solid(destination) { if map.is_solid(destination) {
sound_system.play_sound(sound_effects.blocked.clone()); sound_system.play_sound(sound_effects.blocked.clone());
} else { } 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) { if let Some(monster) = monsters.get(e) {
stats.take_gems(damage_for_kind(monster.kind)); stats.take_gems(damage_for_kind(monster.kind));
let _ = entities.delete(e); let _ = entities.delete(e);
} }
} }
let index = map.point2d_to_index(Point::from(*pos)); map.clear_tile_content_at(Point::from(*pos));
map.clear_tile_content(index);
pos.x = destination.x; pos.x = destination.x;
pos.y = destination.y; pos.y = destination.y;
@ -158,8 +157,7 @@ impl State {
player_pos.x = pos.x; player_pos.x = pos.x;
player_pos.y = pos.y; player_pos.y = pos.y;
let index = map.point2d_to_index(destination); map.set_tile_content_at(destination, player_entity);
map.set_tile_content(index, player_entity);
self.ecs.write_resource::<Clock>().force_tick(); self.ecs.write_resource::<Clock>().force_tick();

View file

@ -74,9 +74,10 @@ impl<'a> System<'a> for MonsterAiSystem {
y: position.y + delta_y, 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) { if let Some(_player) = players.get(e) {
// TODO: Sound // TODO: Sound
map.clear_tile_content_at(Point::from(**position));
stats.take_gems(damage_for_kind(monster.kind)); stats.take_gems(damage_for_kind(monster.kind));
let _ = entities.delete(*entity); let _ = entities.delete(*entity);
} }
@ -88,12 +89,10 @@ impl<'a> System<'a> for MonsterAiSystem {
let _ = entities.delete(*entity); let _ = entities.delete(*entity);
} }
_ => { _ => {
let index = map.point2d_to_index(Point::from(**position)); map.clear_tile_content_at(Point::from(**position));
map.clear_tile_content(index);
position.x = destination.x; position.x = destination.x;
position.y = destination.y; position.y = destination.y;
let index = map.point2d_to_index(destination); map.set_tile_content_at(destination, *entity);
map.set_tile_content(index, *entity);
} }
} }
} }

View file

@ -25,7 +25,7 @@ impl<'a> System<'a> for WhipSystem {
fn run(&mut self, data: Self::SystemData) { fn run(&mut self, data: Self::SystemData) {
let ( let (
map, mut map,
mut stop_clock, mut stop_clock,
mut sound_output, mut sound_output,
sound_effects, sound_effects,
@ -92,9 +92,10 @@ impl<'a> System<'a> for WhipSystem {
}; };
if let Some(dest) = destination { 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) { if let Some(_monster) = monsters.get(e) {
let _ = entities.delete(e); let _ = entities.delete(e);
map.clear_tile_content_at(dest);
if let Some(sound) = &mut wants_to_whip.sound { if let Some(sound) = &mut wants_to_whip.sound {
sound.control::<oddio::Stop<_>, _>().stop(); sound.control::<oddio::Stop<_>, _>().stop();
} }