summaryrefslogtreecommitdiff
path: root/dungeon/src/lib.rs
blob: 0c9e055b8d4597d10f063b149c344910f107aecf (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
//! The `dungon` crate contains the core functionality for
//! interacting with a `Dungeon` and its components.

pub mod astar;
pub mod bsp;
pub mod entity;
pub mod map;
pub mod msg;
pub mod player_input;
pub mod pos;

use rand::{
	SeedableRng, TryRngCore,
	rngs::{OsRng, SmallRng},
};

use crate::{
	entity::{Entity, Player},
	map::Floor,
	msg::Message,
	player_input::PlayerInput,
	pos::FPos,
};

/// Lets the caller know what has
/// changed in the game state this
/// tick
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpdateResult {
	/// Default, entities have moved
	EntityMovement,
	/// Message on screen updated.
	/// Contains if a char was added.
	MessageUpdated(bool),
}

/// The `Dungeon` type represents the game state of the
/// dungeon crawler.
#[derive(Debug, Clone)]
pub struct Dungeon {
	pub floor: Floor,
	pub player: Player,
	pub enemies: Vec<Entity>,
	pub msg: Message,
	seed: u64,
	rng: SmallRng,
}
impl Dungeon {
	/// Creates a new `Dungeon` with a provided seed.
	///
	/// # Examples
	///
	/// ```no_run
	/// use dungeon::Dungeon;
	///
	/// let seed = 234690523482u64;
	/// let dungeon = Dungeon::new(seed);
	/// ```
	#[must_use]
	pub fn new(seed: u64) -> Self {
		let mut rng = SmallRng::seed_from_u64(seed);
		let floor = bsp::generate(&mut rng);
		let player = Player::new(floor.player_start());
		// TODO: Randomize enemy positions/types
		let enemies = vec![Entity::zombie(floor.random_walkable_pos(&mut rng))];
		let msg = Message::empty();

		Self {
			floor,
			player,
			enemies,
			msg,
			seed,
			rng,
		}
	}

	/// Creates a new `Dungeon` with a random seed
	///
	/// # Examples
	///
	/// ```no_run
	/// use dungeon::Dungeon;
	///
	/// let dungeon = Dungeon::random();
	/// ```
	#[must_use]
	pub fn random() -> Self {
		let seed = OsRng.try_next_u64().unwrap_or_default();
		Self::new(seed)
	}

	/// Returns the current position of the camera (viewer)
	#[must_use]
	pub const fn camera(&self) -> FPos {
		self.player.entity.fpos
	}

	/// Returns the seed used to generate the map
	#[must_use]
	pub const fn seed(&self) -> u64 {
		self.seed
	}

	/// Returns the random number gen for the `Floor`
	#[must_use]
	pub const fn rng(&mut self) -> &mut SmallRng {
		&mut self.rng
	}

	pub fn update(&mut self, player_input: PlayerInput, delta_time: f32) -> UpdateResult {
		if self.msg.visible() {
			let changed = self.msg.update(player_input);
			UpdateResult::MessageUpdated(changed)
		} else {
			self.act_player(player_input, delta_time);
			self.act_non_players(delta_time);
			UpdateResult::EntityMovement
		}
	}

	fn act_player(&mut self, player_input: PlayerInput, delta_time: f32) {
		let mut move_distance = self.player.entity.kind.move_speed() * delta_time;
		// having this be a loop is a *little* unnecessary,
		// but is technically more correct if the entity is fast enough
		// to move more than one tile in a single frame
		// (plus, it was easier to write if i thought of this like a state machine)
		loop {
			match (self.player.moving_to, player_input.direction) {
				(Some(pos), _) => {
					move_distance -= self
						.player
						.entity
						.fpos
						.move_towards_manhattan(pos.into(), move_distance);
					if move_distance == 0.0 {
						// can't move any further
						break;
					}
					// otherwise, reached `pos`
					self.player.entity.pos = pos;
					self.player.moving_to = None;
					// continue moving
				}
				(None, Some(dir)) => {
					// set direction & find out next position
					self.player.entity.dir = dir;
					if let Some(pos) = self.player.entity.pos.step(dir)
						&& self.floor.get(pos).is_walkable()
					{
						self.player.moving_to = Some(pos);
					} else {
						break;
					}
				}
				(None, None) => break,
			}
		}
		// TODO: reuse a similar structure across all entities
	}

	fn act_non_players(&mut self, delta_time: f32) {
		for enemy in &mut self.enemies {
			enemy.handle_movement(
				self.player.entity.pos,
				&self.floor,
				&mut self.rng,
				delta_time,
			);
		}
	}
}