Skip to main content

rust_warrior/engine/systems/
sludge.rs

1//! contains system for sludge enemy AI
2
3use std::cmp;
4
5use crate::{engine::world::World, unit::UnitType};
6
7/// This system acts as an enemy AI, attacking the player if a sludge
8/// exists and is in range of the [`Warrior`](crate::warrior::Warrior).
9pub fn sludge_system(world: &mut World) -> Vec<String> {
10    let mut events = Vec::new();
11
12    let (wx, _) = world.warrior.position;
13
14    let mut sludges = Vec::new();
15    for unit in &world.other_units {
16        if unit.unit_type == UnitType::Sludge || unit.unit_type == UnitType::ThickSludge {
17            sludges.push(unit.clone());
18        }
19    }
20
21    for sludge in sludges {
22        let (sx, _) = sludge.position;
23        let (hp, _) = sludge.hp;
24
25        let in_range = (wx - sx).abs() <= 1;
26
27        if hp > 0 && in_range {
28            events.push(format!(
29                "{sludge:?} attacks {warrior}",
30                sludge = sludge.unit_type,
31                warrior = &world.player_name
32            ));
33            let (current, max) = world.warrior.hp;
34            let remaining = cmp::max(current - sludge.atk, 0);
35            events.push(format!(
36                "{warrior} takes {atk} damage, {remaining} HP left",
37                warrior = &world.player_name,
38                atk = sludge.atk,
39                remaining = remaining
40            ));
41            world.warrior.hp = (remaining, max);
42        }
43    }
44
45    events
46}