Skip to content
Snippets Groups Projects
Commit 47476b6c authored by radow's avatar radow
Browse files

sounds

parent 1f2cdf89
No related branches found
No related tags found
No related merge requests found
File added
File added
File added
......@@ -72,7 +72,7 @@ impl GameState {
impl State for GameState {
fn update(&mut self, ctx: &mut Context) -> tetra::Result {
if self.handle_user_turn(ctx) {
self.run_enemy_turn();
self.run_enemy_turn(ctx);
self.finish_turn();
}
......@@ -97,32 +97,32 @@ impl State for GameState {
// SOUNDS
impl GameState {
fn play_sound_player_attack(&mut self) {
fn play_sound_player_attack(&mut self, ctx: &mut Context) {
self.sounds.hit.play(ctx);
}
fn play_sound_enemy_attack(&mut self) {
fn play_sound_enemy_attack(&mut self, ctx: &mut Context) {
self.sounds.hit.play(ctx);
}
fn play_sound_killed_enemy(&mut self) {
fn play_sound_killed_enemy(&mut self, ctx: &mut Context) {
self.sounds.kill.play(ctx);
}
fn play_sound_death(&mut self) {
fn play_sound_death(&mut self, ctx: &mut Context) {
self.sounds.gameover.play(ctx);
}
fn play_sound_chest(&mut self) {
fn play_sound_chest(&mut self, ctx: &mut Context) {
self.sounds.chest.play(ctx);
}
fn play_sound_level_up(&mut self) {
fn play_sound_level_up(&mut self, ctx: &mut Context) {
self.sounds.fanfare.play(ctx);
}
fn play_sound_exit(&mut self) {
fn play_sound_exit(&mut self, ctx: &mut Context) {
self.sounds.exit.play(ctx);
}
}
......@@ -146,7 +146,7 @@ impl GameState {
Key::N => !is_player_alive && self.create_new_run(),
Key::W | Key::A | Key::S | Key::D | Key::Down | Key::Up | Key::Right | Key::Left => {
self.current_run.combat_log.level_up = false;
return is_player_alive && !is_treasure_choice && self.handle_user_movement(key);
return is_player_alive && !is_treasure_choice && self.handle_user_movement(key, ctx);
}
Key::Space => {
self.current_run.combat_log.level_up = false;
......@@ -203,7 +203,7 @@ impl GameState {
false
}
fn handle_user_movement(&mut self, key: Key) -> bool {
fn handle_user_movement(&mut self, key: Key, ctx: &mut Context) -> bool {
let player_position = self.current_run.level.get_player_position();
let target_position = GameState::calculate_target_position(player_position, key);
......@@ -218,50 +218,50 @@ impl GameState {
self.current_run.combat_log.player_action = Option::from(Move);
}
Tile::Wall => return false,
Tile::Enemy(id) => self.attack_enemy(id, target_position),
Tile::Exit => return self.progress_level(),
Tile::Treasure(a, b) => return self.open_chest(a, b, target_position),
Tile::Enemy(id) => self.attack_enemy(id, target_position, ctx),
Tile::Exit => return self.progress_level(ctx),
Tile::Treasure(a, b) => return self.open_chest(a, b, target_position, ctx),
_ => ()
}
true
}
fn open_chest(&mut self, option_a: TreasureType, option_b: TreasureType, position: Point) -> bool {
fn open_chest(&mut self, option_a: TreasureType, option_b: TreasureType, position: Point, ctx: &mut Context) -> bool {
self.current_run.level.move_player_to(position);
self.current_run.combat_log.treasure_choice = Option::from((option_a, option_b));
self.play_sound_chest();
self.play_sound_chest(ctx);
false
}
fn progress_level(&mut self) -> bool {
fn progress_level(&mut self, ctx: &mut Context) -> bool {
self.current_run.level_count += 1;
if self.current_run.player.current_hp < self.current_run.player.max_hp {
self.current_run.player.current_hp += config::LEVEL_HEAL;
}
self.current_run.level = Level::new(self.current_run.level_count);
self.current_run.combat_log.player_action = Option::from(PlayerAction::Level);
self.play_sound_exit();
self.play_sound_exit(ctx);
false
}
fn attack_enemy(&mut self, enemy_id: usize, enemy_position: Point) {
fn attack_enemy(&mut self, enemy_id: usize, enemy_position: Point, ctx: &mut Context) {
let damage = self.current_run.player.strength + GameState::roll_dice(self.current_run.player.weapon);
if let Some(enemy) = self.current_run.level.enemies.get_mut(enemy_id) {
if enemy.hp <= damage {
self.current_run.level.move_player_to(enemy_position);
self.current_run.combat_log.player_action = Option::from(PlayerAction::Attack(damage, 0));
self.play_sound_killed_enemy();
self.grant_xp();
self.play_sound_killed_enemy(ctx);
self.grant_xp(ctx);
} else {
enemy.hp -= damage;
self.current_run.combat_log.player_action = Option::from(PlayerAction::Attack(damage, enemy.hp));
self.play_sound_player_attack();
self.play_sound_player_attack(ctx);
}
}
}
fn grant_xp(&mut self) {
fn grant_xp(&mut self, ctx: &mut Context) {
self.current_run.player.current_xp += 1;
if self.current_run.player.current_xp == self.current_run.player.max_xp {
self.current_run.player.strength += 1;
......@@ -271,7 +271,7 @@ impl GameState {
self.current_run.player.max_xp += 2;
self.current_run.player.current_xp = 0;
self.current_run.combat_log.level_up = true;
self.play_sound_level_up();
self.play_sound_level_up(ctx);
}
}
......@@ -314,14 +314,14 @@ impl GameState {
// ENEMY TURN
impl GameState {
fn run_enemy_turn(&mut self) {
fn run_enemy_turn(&mut self, ctx: &mut Context) {
let player_position = self.current_run.level.get_player_position();
let mut total_damage = 0;
for (id, position) in self.current_run.level.get_current_enemies() {
if GameState::check_is_neighbor(player_position, position) {
total_damage += self.attack_player(id);
total_damage += self.attack_player(id, ctx);
} else {
self.move_enemy(id, position);
}
......@@ -360,17 +360,17 @@ impl GameState {
false
}
fn attack_player(&mut self, enemy_id: usize) -> usize {
fn attack_player(&mut self, enemy_id: usize, ctx: &mut Context) -> usize {
let mut damage = 0;
if let Some(enemy) = self.current_run.level.enemies.get(enemy_id) {
damage = enemy.strength + GameState::roll_dice(enemy.weapon_strength);
if self.current_run.player.current_hp <= damage {
self.current_run.player.current_hp = 0;
self.play_sound_death();
self.play_sound_death(ctx);
} else {
self.current_run.player.current_hp -= damage;
self.play_sound_enemy_attack();
self.play_sound_enemy_attack(ctx);
}
}
......
......@@ -5,6 +5,9 @@ pub struct Sounds {
pub hit: Sound,
pub fanfare: Sound,
pub gameover: Sound,
pub exit: Sound,
pub chest: Sound,
pub kill: Sound,
}
impl Sounds {
......@@ -14,6 +17,9 @@ impl Sounds {
hit: Sound::new("./resources/hit.wav")?,
fanfare: Sound::new("./resources/fanfare.wav")?,
gameover: Sound::new("./resources/gameover.wav")?,
exit: Sound::new("./resources/exit.mp3")?,
chest: Sound::new("./resources/chest.mp3")?,
kill: Sound::new("./resources/kill.wav")?,
})
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment