Switch from specs to hecs
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
c0ce37aa38
commit
d9606e8b87
27 changed files with 514 additions and 673 deletions
92
src/systems/monster_ai.rs
Normal file
92
src/systems/monster_ai.rs
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
use crate::{
|
||||
components::{monster::*, *},
|
||||
resources::Resources,
|
||||
tile_data::TileType,
|
||||
};
|
||||
|
||||
use bracket_lib::{prelude::*, random::RandomNumberGenerator};
|
||||
use hecs::*;
|
||||
|
||||
pub fn run(world: &mut World, resources: &mut Resources) {
|
||||
let player_position = world
|
||||
.query::<(&Player, &Position)>()
|
||||
.iter()
|
||||
.map(|(_, (_, &pos))| pos)
|
||||
.collect::<Vec<_>>()
|
||||
.first()
|
||||
.cloned();
|
||||
|
||||
if let Some(player_pos) = player_position {
|
||||
let mut has_died = None;
|
||||
for (entity, (monster, position, renderable)) in
|
||||
&mut world.query::<(&mut Monster, &mut Position, &mut Renderable)>()
|
||||
{
|
||||
if resources.clock.has_ticked {
|
||||
monster.ticks_until_move -= 1;
|
||||
if monster.ticks_until_move <= 0 {
|
||||
// Change glyph
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
if let Some(glyph) =
|
||||
rng.random_slice_entry(&monster::glyphs_for_kind(monster.kind))
|
||||
{
|
||||
renderable.glyph = *glyph;
|
||||
}
|
||||
|
||||
// Move monster
|
||||
let (mut delta_x, mut delta_y) = (0, 0);
|
||||
if player_pos.x < position.x {
|
||||
delta_x = -1;
|
||||
}
|
||||
if player_pos.x > position.x {
|
||||
delta_x = 1;
|
||||
}
|
||||
if player_pos.y < position.y {
|
||||
delta_y = -1;
|
||||
}
|
||||
if player_pos.y > position.y {
|
||||
delta_y = 1;
|
||||
}
|
||||
let destination = Point {
|
||||
x: position.x + delta_x,
|
||||
y: position.y + delta_y,
|
||||
};
|
||||
|
||||
if let Some(e) = resources.map.get_tile_content_at(destination) {
|
||||
if let Ok(_player) = world.get::<Player>(e) {
|
||||
// TODO: Sound
|
||||
resources.map.clear_tile_content_at(Point::from(*position));
|
||||
resources.stats.take_gems(damage_for_kind(monster.kind));
|
||||
resources.sound_output.play_sound(sound_effect_for_kind(
|
||||
monster.kind,
|
||||
&resources.sound_effects,
|
||||
));
|
||||
has_died = Some(entity);
|
||||
}
|
||||
} else {
|
||||
let tile = resources.map.get_tile_at_mut(destination);
|
||||
match tile {
|
||||
TileType::Wall => {}
|
||||
TileType::Block => {
|
||||
// TODO: Sound
|
||||
*tile = TileType::Floor;
|
||||
has_died = Some(entity);
|
||||
}
|
||||
_ => {
|
||||
resources.map.clear_tile_content_at(Point::from(*position));
|
||||
position.x = destination.x;
|
||||
position.y = destination.y;
|
||||
resources.map.set_tile_content_at(destination, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
monster.ticks_until_move = ticks_for_kind(monster.kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = has_died {
|
||||
let _ = world.despawn(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue