Fix monster/player collision issues
This commit is contained in:
parent
d818c8f7a6
commit
73aa491d24
5 changed files with 26 additions and 19 deletions
|
@ -61,7 +61,7 @@ fn main() -> BError {
|
|||
ecs.register::<Player>();
|
||||
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);
|
||||
ecs.insert(map);
|
||||
|
||||
|
|
|
@ -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::<Vec<(usize, &mut TileType)>>()
|
||||
{
|
||||
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<Entity> {
|
||||
pub fn get_tile_content_at(&self, point: Point) -> Option<Entity> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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::<Clock>().force_tick();
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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::<oddio::Stop<_>, _>().stop();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue