Skip to content

Commit

Permalink
Win/loss mechanics, more sounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
twetzel59 committed Aug 5, 2017
1 parent 530bf7e commit 62f3b84
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 26 deletions.
Binary file added res/die.flac
Binary file not shown.
Binary file added res/win.flac
Binary file not shown.
50 changes: 32 additions & 18 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate sfml;

use spaceshooter::*;

use sfml::audio::Sound;
use sfml::graphics::*;
use sfml::system::Clock;
use sfml::window::{Event, Key};
Expand All @@ -22,23 +23,34 @@ fn main() {

let mut scoreboard = Scoreboard::new(&res, &size);

let mut clock = Clock::start();
let mut won: WinStatus = WinStatus::Playing;

let mut win_sound = Sound::with_buffer(res.win());
let mut die_sound = Sound::with_buffer(res.die());

let mut won: bool = false;
let mut clock = Clock::start();
'game: loop {
let delta_t = clock.restart().as_seconds();

if collision::handle_collisions(&mut enemy_mgr, &mut bullet_mgr) {
if collision::handle_bullets(&mut enemy_mgr, &mut bullet_mgr) {
scoreboard.score();
}
bullet_mgr.update(delta_t);
match enemy_mgr.update(delta_t) {
WinStatus::Won => {
won = true;
scoreboard.show_win();
won = WinStatus::Won;
win_sound.play();
break 'game;
}
_ => {},
}
if collision::handle_ship(&ship, &enemy_mgr) {
scoreboard.show_loss();
won = WinStatus::Lost;
die_sound.play();
break 'game;
}

win.draw(&back);
for i in &bullet_mgr {
Expand Down Expand Up @@ -73,21 +85,23 @@ fn main() {
//ship.update();
}

if won {
scoreboard.show_win();

'scorescreen: loop {
win.draw(&back);
win.draw(&scoreboard);
win.display();

while let Some(e) = win.poll_event() {
match e {
Event::Closed | Event::KeyPressed { code: Key::Escape, .. } =>
break 'scorescreen,
_ => {},
match won {
WinStatus::Won | WinStatus::Lost => {
'scorescreen: loop {
win.draw(&back);
win.draw(&scoreboard);
win.display();
while let Some(e) = win.poll_event() {
match e {
Event::Closed | Event::KeyPressed { code: Key::Escape, .. } =>
break 'scorescreen,
_ => {},
}
}
}
}
},

_ => {},
}
}
13 changes: 12 additions & 1 deletion src/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use attackable::Attackable;
use enemies::*;
use bullets::*;
use ship::*;

pub fn handle_collisions(enemies: &mut EnemyManager, bullets: &mut BulletManager) -> bool {
pub fn handle_bullets(enemies: &mut EnemyManager, bullets: &mut BulletManager) -> bool {
let mut hit = false;

for e in &mut *enemies {
Expand Down Expand Up @@ -36,3 +37,13 @@ pub fn handle_collisions(enemies: &mut EnemyManager, bullets: &mut BulletManager
bullets.register_dead(&dead_bullets);
*/
}

pub fn handle_ship(ship: &Ship, enemies: &EnemyManager) -> bool {
for e in enemies {
if let Some(_) = e.bounds().intersection(&ship.bounds()) {
return true;
}
}

false
}
7 changes: 1 addition & 6 deletions src/enemies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sfml::system::Vector2u;
use attackable::*;
use resources::Resources;
use soundfilter;
use winstatus::WinStatus;

const MAX_ENEMY_NUMBER: u32 = 8;
const WIN_SIDE_PADDING: u32 = 64;
Expand Down Expand Up @@ -103,12 +104,6 @@ impl<'s> Drawable for Enemy<'s> {
}
}

#[derive(Clone, Copy)]
pub enum WinStatus {
Won,
Playing,
}

pub struct EnemyManager<'s> {
active: Vec<Enemy<'s>>,
inactive: Vec<Enemy<'s>>,
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use sfml::window::{ContextSettings, style, VideoMode};

pub use attackable::Attackable;
pub use background::Background;
pub use enemies::WinStatus;
pub use resources::Resources;
pub use ship::Ship;
pub use scoreboard::Scoreboard;
pub use winstatus::WinStatus;

pub mod background;
pub mod bullets;
Expand All @@ -20,6 +20,7 @@ pub mod resources;
pub mod scoreboard;
pub mod ship;
pub mod soundfilter;
pub mod winstatus;

mod attackable;

Expand Down
12 changes: 12 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct Resources {
font: Font,
shoot: SoundBuffer,
kill: SoundBuffer,
win: SoundBuffer,
die: SoundBuffer,
}

impl Resources {
Expand All @@ -21,6 +23,8 @@ impl Resources {
font: Font::from_file("res/UbuntuMono-B.ttf").unwrap(),
shoot: SoundBuffer::from_file("res/shoot.flac").unwrap(),
kill: SoundBuffer::from_file("res/kill.flac").unwrap(),
win: SoundBuffer::from_file("res/win.flac").unwrap(),
die: SoundBuffer::from_file("res/die.flac").unwrap(),
}
}

Expand Down Expand Up @@ -51,4 +55,12 @@ impl Resources {
pub fn kill(&self) -> &SoundBufferRef {
&self.kill
}

pub fn win(&self) -> &SoundBufferRef {
&self.win
}

pub fn die(&self) -> &SoundBufferRef {
&self.die
}
}
5 changes: 5 additions & 0 deletions src/scoreboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl<'s> Scoreboard<'s> {
self.text.set_character_size(26);
}

pub fn show_loss(&mut self) {
self.text.set_fill_color(&Color::rgb(16, 100, 255));
self.text.set_character_size(26);
}

fn render(&mut self) {
self.text.set_string(&format!("{}", self.score));
}
Expand Down
6 changes: 6 additions & 0 deletions src/winstatus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(Clone, Copy)]
pub enum WinStatus {
Won,
Lost,
Playing,
}

0 comments on commit 62f3b84

Please sign in to comment.