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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
//! The `entity` module contains structures of all entities including players and enimies.
use rand::Rng;
use crate::{Direction, FPos, Floor, Pos, astar, const_pos};
/// `PLAYER_FULL_HEALTH` is the starting health of the player entity
pub const PLAYER_FULL_HEALTH: u32 = 10;
/// `PLAYER_INVENTORY_SIZE` is the maximum size of the inventory
pub const PLAYER_INVENTORY_SIZE: u16 = 5;
/// `PLAYER_INVENTORY_SIZE_USIZE` is the maximum size of the inventory
pub const PLAYER_INVENTORY_SIZE_USIZE: usize = PLAYER_INVENTORY_SIZE as usize;
/// The `Item` type represents any item an entity may be using
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Item {
Potion { heal: u32 },
Weapon { atack: u32 },
Armor { defense: u32 },
}
/// The `EntityKind` represents what kind of entity this is.
#[derive(Clone, Debug, PartialEq)]
pub enum EntityKind {
/// The main player
Player,
Zombie(EnemyMoveState),
/// An item (not in an inventory) on the floor of the dungeon
Item(Item),
}
impl EntityKind {
// Tiles/s
pub fn move_speed(&self) -> f32 {
match &self {
Self::Player => 2.,
Self::Zombie(_) => 4.,
_ => 0.,
}
}
}
pub const MIN_ROAM_DIST: u16 = 1;
pub const MAX_ROAM_DIST: u16 = 4;
pub const ZOMBIE_HEALTH: u32 = 5;
pub const ENEMY_VISION_RADIUS: u16 = 10;
pub const IDLE_WAIT: f32 = 1.;
#[derive(Clone, Debug, PartialEq)]
pub enum EnemyMoveState {
Idle(f32),
Roam(Vec<Pos>),
Attack(Vec<Pos>),
}
impl EnemyMoveState {
pub const fn new_idle() -> Self {
Self::Idle(0.)
}
pub fn new_attack(starting_pos: Pos, player_pos: Pos, floor: &Floor) -> Option<Self> {
if player_pos.manhattan(starting_pos) < ENEMY_VISION_RADIUS {
let data = astar::astar(starting_pos, player_pos, floor)?;
let mut path = data.0;
path.reverse();
return Some(Self::Attack(path));
}
None
}
pub fn new_roam(starting_pos: Pos, floor: &mut Floor) -> Self {
let mut loop_index = 0;
loop {
let dir = Direction::random(floor.rand());
let dist = floor.rand().random_range(MIN_ROAM_DIST..=MIN_ROAM_DIST);
if let Some(p) = starting_pos.step_by(dir, dist) {
if !floor.get(p).is_wall() {
if let Some(data) = astar::astar(starting_pos, p, floor) {
let mut path = data.0;
path.reverse();
return Self::Roam(path);
}
}
}
// Safety check
loop_index += 1;
if loop_index >= 100 {
return Self::new_idle();
}
}
}
}
/// The `Entity` kind represents the main player, or any other
/// ai autonomous character that can move freely across the
/// dungeon.
#[derive(Clone, Debug, PartialEq)]
pub struct Entity {
/// The fixed grid position of the entity
pub pos: Pos,
/// The floating (real) current position of the entity
pub fpos: FPos,
/// Which direction this entity is facing
pub dir: Direction,
/// Which kind this entity is (along with entity kind specific data)
pub kind: EntityKind,
/// The amount of health this entity has (None if this Entity does not use health)
pub health: Option<u32>,
}
impl Entity {
/// Creates a new `Entity` at a given `Pos`, `Direction`, and `EntityKind`.
///
/// # Examples
///
/// ```
/// use dungeon::{Pos, Direction, Entity, EntityKind};
///
/// let pos = Pos::new(0, 0).unwrap();
/// let dir = Direction::North;
/// let kind = EntityKind::Player;
/// let health = Some(10);
/// let entity = Entity::new(pos, dir, kind, health);
/// ```
#[must_use]
pub const fn new(
pos: Pos,
dir: Direction,
kind: EntityKind,
health: Option<u32>,
) -> Self {
let fpos = FPos::from_pos(pos);
Self {
pos,
fpos,
dir,
kind,
health,
}
}
/// Creates the Player version of the `Entity`
///
/// # Examples
///
/// ```
/// use dungeon::{Pos, Entity};
///
/// let pos = Pos::new(0, 0).unwrap();
/// let player = Entity::player(pos);
/// ```
#[must_use]
pub const fn player(pos: Pos) -> Self {
let dir = Direction::East;
let kind = EntityKind::Player;
let health = Some(PLAYER_FULL_HEALTH);
Self::new(pos, dir, kind, health)
}
/// Creates the Zombie version of the `Entity`
///
/// # Examples
///
/// ```
/// use dungeon::{Pos, Entity};
///
/// let pos = Pos::new(0, 0).unwrap();
/// let player = Entity::zombie(pos);
/// ```
pub const fn zombie(pos: Pos) -> Self {
let dir = Direction::East;
let kind = EntityKind::Zombie(EnemyMoveState::new_idle());
let health = Some(ZOMBIE_HEALTH);
Self::new(pos, dir, kind, health)
}
pub fn move_by_dir(&mut self, dir: Direction, delta_time: f32) {
if let Some(fp) = self.fpos.step_by(dir, delta_time * self.kind.move_speed()) {
// TODO: collision
self.fpos = fp;
}
}
pub fn handle_movement(&mut self, player_pos: Pos, floor: &mut Floor, delta_time: f32) {
match &self.kind {
EntityKind::Zombie(move_state) => {
self.zombie_movement(move_state.clone(), player_pos, delta_time, floor);
}
EntityKind::Player => {}
_ => {}
}
}
pub fn zombie_movement(
&mut self,
move_state: EnemyMoveState,
player_pos: Pos,
delta_time: f32,
floor: &mut Floor,
) {
// Check if player is in range
if !matches!(move_state, EnemyMoveState::Attack { .. })
&& let Some(m_state) = EnemyMoveState::new_attack(self.pos, player_pos, floor)
{
self.kind = EntityKind::Zombie(m_state);
return;
}
match move_state {
EnemyMoveState::Idle(idle_accum) => {
if idle_accum < IDLE_WAIT {
self.kind =
EntityKind::Zombie(EnemyMoveState::Idle(idle_accum + delta_time));
return;
}
self.kind = EntityKind::Zombie(EnemyMoveState::new_roam(self.pos, floor));
}
EnemyMoveState::Roam(mut moves) => {
let p = if let Some(p) = moves.last() {
p
} else {
self.kind = EntityKind::Zombie(EnemyMoveState::new_idle());
return;
};
Self::movement_helper(self, *p, &mut moves, delta_time);
self.kind = EntityKind::Zombie(EnemyMoveState::Roam(moves));
}
EnemyMoveState::Attack(mut moves) => {
// Stop at one move left so the enemy is flush with the player tile
if moves.len() == 1 {
return;
}
Self::movement_helper(
self,
*moves.last().unwrap_or(&Pos::default()),
&mut moves,
delta_time,
);
self.kind = EntityKind::Zombie(EnemyMoveState::Attack(moves));
}
}
}
fn movement_helper(
entity: &mut Self,
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;
entity.fpos = FPos::from_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;
entity.fpos = FPos::from_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;
entity.fpos = FPos::from_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;
entity.fpos = FPos::from_pos(p);
}
}
};
}
}
/// The `Player` type represents the main player entity
#[derive(Clone, Debug, PartialEq)]
pub struct Player {
pub entity: Entity,
pub inventory: Vec<Item>,
}
impl Player {
/// Instantiates the game player at a given `Pos`
///
/// # Examples
///
/// ```
/// use dungeon::{Pos, Player};
///
/// let pos = Pos::new(1, 2).unwrap();
/// let player = Player::new(pos);
/// ```
pub fn new(pos: Pos) -> Self {
let entity = Entity::player(pos);
let inventory = vec![];
Self { entity, inventory }
}
}
impl Default for Player {
fn default() -> Self {
let pos = const_pos!(1, 1);
Self::new(pos)
}
}
|