summaryrefslogtreecommitdiff
path: root/dungeon/src/enemy.rs
blob: 0d432dd5864d053c4d27bfab0d8641b80a7df7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use crate::{Direction, Entity, EntityMoveSpeed, Pos, Tile, astar};

pub const MIN_ROAM_DIST: u16 = 1;
pub const MAX_ROAM_DIST: u16 = 4;

pub const ZOMBIE_HEALTH: u32 = 50;
pub const ENEMY_VISION_RADIUS: u16 = 5;

pub const IDLE_WAIT: f32 = 1.;

#[derive(Clone, Debug, PartialEq)]
pub enum EnemyMoveState {
	Idle(f32),
	Roam(Vec<Pos>),
	Attack(Vec<Pos>),
}

impl EnemyMoveState {
	pub fn new_idle() -> Self {
		Self::Idle(0.)
	}

	pub fn new_attack(starting_pos: Pos, player_pos: Pos, tiles: &[Tile]) -> Option<Self> {
		if player_pos.manhattan(starting_pos) < ENEMY_VISION_RADIUS {
			let data = astar::find_path(tiles, starting_pos, player_pos)?;
			let mut path = data.0;
			path.reverse();
			return Some(Self::Attack(path));
		}
		None
	}

	pub fn new_roam(starting_pos: Pos, tiles: &[Tile]) -> Self {
		let mut rand_dir = Direction::get_random_dir();
		let mut rand_tiles = rand::random_range(MIN_ROAM_DIST..=MAX_ROAM_DIST);
		let mut loop_index = 0;
		loop {
			if let Some(p) = starting_pos.step_by(rand_dir, rand_tiles) {
				if !tiles[p.idx()].is_wall() {
					if let Some(data) = astar::find_path(tiles, starting_pos, p) {
						let mut path = data.0;
						path.reverse();
						return Self::Roam(path);
					}
				}
			}

			rand_dir = Direction::get_random_dir();
			rand_tiles = rand::random_range(MIN_ROAM_DIST..=MAX_ROAM_DIST);

			// Safety check
			loop_index += 1;
			assert!(
				loop_index < 100,
				"EnemyMoveState::new_roam couldn't find a valid spot around {starting_pos:?} in between {MIN_ROAM_DIST}-{MAX_ROAM_DIST} tiles",
			);
		}
	}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EnemyAttackType {
	Melee,
	Ranged,
}

#[derive(Clone, Debug, PartialEq, Eq, Copy)]
pub enum EnemyType {
	Zombie,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Enemy {
	pub entity: Entity,
	pub enemy_type: EnemyType,
	pub attack_type: EnemyAttackType,
	move_state: EnemyMoveState,
}

impl Enemy {
	pub fn new(enemy_type: EnemyType, pos: Pos) -> Self {
		match enemy_type {
			EnemyType::Zombie => Self::zombie(pos),
		}
	}

	fn _new(
		pos: Pos,
		health: u32,
		move_speed: EntityMoveSpeed,
		enemy_type: EnemyType,
		attack_type: EnemyAttackType,
	) -> Self {
		let entity = Entity::enemy(pos, move_speed, health);
		Self {
			entity,
			enemy_type,
			attack_type,
			move_state: EnemyMoveState::new_idle(),
		}
	}

	fn zombie(pos: Pos) -> Self {
		Self::_new(
			pos,
			ZOMBIE_HEALTH,
			EntityMoveSpeed::Slow,
			EnemyType::Zombie,
			EnemyAttackType::Melee,
		)
	}

	pub fn handle_movement(&mut self, player_pos: Pos, delta_time: f32, tiles: &[Tile]) {
		// Check if player is in range
		if !matches!(self.move_state, EnemyMoveState::Attack { .. })
			&& let Some(move_state) =
				EnemyMoveState::new_attack(self.entity.pos, player_pos, tiles)
		{
			self.move_state = move_state;
			return;
		}

		match &mut self.move_state {
			EnemyMoveState::Idle(idle_accum) => {
				*idle_accum += delta_time;
				if *idle_accum < IDLE_WAIT {
					return;
				}

				self.move_state = EnemyMoveState::new_roam(self.entity.pos, tiles);
			}
			EnemyMoveState::Roam(moves) => {
				let p = if let Some(p) = moves.last() {
					p
				} else {
					self.move_state = EnemyMoveState::new_idle();
					return;
				};

				Self::movement_helper(&mut self.entity, *p, moves, delta_time);
			}
			EnemyMoveState::Attack(moves) => {
				// Stop at one move left so the enemy is flush with the player tile
				if moves.len() == 1 {
					return;
				}

				Self::movement_helper(
					&mut self.entity,
					*moves.last().unwrap_or(&Pos::default()),
					moves,
					delta_time,
				);
			}
		}
	}

	fn movement_helper(
		entity: &mut Entity,
		next_move: Pos,
		moves: &mut Vec<Pos>,
		delta_time: f32,
	) {
		if entity.pos.y() > next_move.y() {
			entity.move_by_dir(Direction::North, delta_time);
			if entity.fpos.y() <= next_move.y() as f32 {
				if let Some(p) = moves.pop() {
					entity.pos = p;
				}
			}
		} else if entity.pos.y() < next_move.y() {
			entity.move_by_dir(Direction::South, delta_time);
			if entity.fpos.y() >= next_move.y() as f32 {
				if let Some(p) = moves.pop() {
					entity.pos = p;
				}
			}
		} else if entity.pos.x() < next_move.x() {
			entity.move_by_dir(Direction::East, delta_time);
			if entity.fpos.x() >= next_move.x() as f32 {
				if let Some(p) = moves.pop() {
					entity.pos = p;
				}
			}
		} else {
			entity.move_by_dir(Direction::West, delta_time);
			if entity.fpos.x() <= next_move.x() as f32 {
				if let Some(p) = moves.pop() {
					entity.pos = p;
				}
			}
		};
	}
}