summaryrefslogtreecommitdiff
path: root/game/src/lib.rs
blob: 6e09a371880af220d475fd1c667cf4d47f3dd746 (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
use std::rc::Rc;

use dungeon::{
	Dungeon, UpdateResult, entity::Item, map::Tile, player::PLAYER_INVENTORY_SIZE_USIZE,
	player_input::PlayerInput, pos::Direction,
};
use graphics::{Key, Window};

use crate::music::Music;

mod music;

macro_rules! play_sound {
	($self:expr, $sound:expr) => {
		$self.window.audio().schedule(
			Rc::clone(&$sound.track),
			$sound.priority,
			$sound.looping,
		);
	};
}

/// The `Error` type used within this crate
pub type Error = Box<dyn std::error::Error>;

/// The `Result` type used witin this crate
pub type Result<T> = std::result::Result<T, crate::Error>;

pub struct Game {
	window: Window,
	dungeon: Dungeon,
	music: Music,
	// to ensure the most recently-pressed direction key is used:
	current_dir: Option<(Direction, Key)>,
}

impl Game {
	pub fn new(window: Window, seed: Option<u64>) -> Result<Self> {
		let music = Music::load()?;
		let dungeon = match seed {
			Some(s) => Dungeon::new(s),
			None => Dungeon::random(),
		};
		let game = Self {
			window,
			dungeon,
			music,
			current_dir: None,
		};
		Ok(game)
	}

	fn player_dir(&mut self) -> Option<Direction> {
		const MOVE_KEYS: [(Direction, [Key; 2]); 4] = [
			(Direction::North, [Key::Up, Key::W]),
			(Direction::West, [Key::Left, Key::A]),
			(Direction::South, [Key::Down, Key::S]),
			(Direction::East, [Key::Right, Key::D]),
		];

		// if a key was just pressed, use it for the new direction to move in
		for (dir, keys) in MOVE_KEYS {
			for key in keys {
				if self.window.is_key_pressed(key) {
					self.current_dir = Some((dir, key));
					return Some(dir);
				}
			}
		}
		// otherwise, use existing direction, so long as the key is still down
		match self.current_dir {
			Some((dir, key)) if self.window.is_key_down(key) => return Some(dir),
			_ => self.current_dir = None,
		}
		// otherwise, use any key that is already down
		for (dir, keys) in MOVE_KEYS {
			for key in keys {
				if self.window.is_key_down(key) {
					self.current_dir = Some((dir, key));
					return Some(dir);
				}
			}
		}
		// otherwise, no direction key is pressed, so return None
		None
	}

	fn get_player_input(&mut self) -> PlayerInput {
		let direction = self.player_dir();
		let interact = self.window.is_key_pressed(Key::Return);
		let use_item = self.window.is_key_pressed(Key::E);
		let attack = self.window.is_key_pressed(Key::F);
		let drop = self.window.is_key_pressed(Key::Q);
		let inv_slot = (0..PLAYER_INVENTORY_SIZE_USIZE)
			.find(|n| self.window.is_key_pressed(Key::Number(n + 1)));
		PlayerInput {
			direction,
			interact,
			use_item,
			attack,
			drop,
			inv_slot,
		}
	}

	pub fn run(&mut self) {
		play_sound!(self, &self.music.background);

		// Main game loop
		while self.window.is_open() {
			// Handle debug keys
			if self.window.is_key_pressed(Key::F3) {
				self.window.toggle_debug();
			}
			if self.window.is_key_pressed(Key::F4) {
				*self.dungeon.floor.get_mut(self.dungeon.player.entity.pos) =
					Tile::Chest(Some(Item::Bomb));
			}

			// Update game state
			let inputs = self.get_player_input();
			let result = self
				.dungeon
				.update(inputs, self.window.delta_time().as_secs_f32());
			match result {
				UpdateResult::Default(action) => {
					// items
					if action.bomb {
						play_sound!(self, &self.music.use_bomb);
					} else if action.potion {
						play_sound!(self, &self.music.use_potion);
					} else if action.drop_item {
						play_sound!(self, &self.music.drop_item);
					} else if action.pickup_item {
						play_sound!(self, &self.music.pickup_item);
					}

					// actions
					if action.attack {
						play_sound!(self, &self.music.attack);
					} else if action.walk {
						// TODO: do we want walking sounds? (were a ghost)
					}
				}
				UpdateResult::NextFloor => {
					play_sound!(self, &self.music.discover);
				}
				UpdateResult::MessageUpdated(changed) => {
					if changed {
						play_sound!(self, &self.music.speak);
					}
				}
				UpdateResult::GameOver => {
					self.window.audio().clear();
					play_sound!(self, &self.music.game_over);
				}
				UpdateResult::Nothing => {}
			}

			// Draw a single frame
			self.window.draw_frame(&self.dungeon);

			// Update audio
			self.window.audio().update();
		}
	}
}