IntervalSpawningSystem{} fix, and mobs randomly move

This commit is contained in:
Llywelwyn 2023-08-01 08:30:04 +01:00
parent 5276bb6e34
commit b5e3880a33
5 changed files with 61 additions and 28 deletions

View file

@ -22,30 +22,42 @@ impl<'a> System<'a> for BystanderAI {
for (entity, mut viewshed, _bystander, mut pos, _turn) in
(&entities, &mut viewshed, &bystander, &mut position, &turns).join()
{
// Try to move randomly
let mut x = pos.x;
let mut y = pos.y;
let move_roll = rng.roll_dice(1, 8);
match move_roll {
1 => x -= 1,
2 => x += 1,
3 => y -= 1,
4 => y += 1,
_ => {}
}
if x > 0 && x < map.width - 1 && y > 0 && y < map.height - 1 {
let dest_idx = map.xy_idx(x, y);
if !map.blocked[dest_idx] {
let idx = map.xy_idx(pos.x, pos.y);
map.blocked[idx] = false;
pos.x = x;
pos.y = y;
entity_moved.insert(entity, EntityMoved {}).expect("Unable to insert marker");
map.blocked[dest_idx] = true;
viewshed.dirty = true;
}
if try_move_randomly(&mut pos, &mut rng, &mut map, &mut viewshed) {
entity_moved.insert(entity, EntityMoved {}).expect("Unable to insert marker");
}
}
}
}
pub fn try_move_randomly(
pos: &mut Position,
rng: &mut rltk::RandomNumberGenerator,
map: &mut Map,
viewshed: &mut Viewshed,
) -> bool {
// Try to move randomly
let mut x = pos.x;
let mut y = pos.y;
let move_roll = rng.roll_dice(1, 8);
match move_roll {
1 => x -= 1,
2 => x += 1,
3 => y -= 1,
4 => y += 1,
_ => {}
}
if x > 0 && x < map.width - 1 && y > 0 && y < map.height - 1 {
let dest_idx = map.xy_idx(x, y);
if !map.blocked[dest_idx] {
let idx = map.xy_idx(pos.x, pos.y);
map.blocked[idx] = false;
pos.x = x;
pos.y = y;
map.blocked[dest_idx] = true;
viewshed.dirty = true;
return true;
}
}
return false;
}