Skip to content

Commit

Permalink
Clean up text render code and add end-game screens
Browse files Browse the repository at this point in the history
  • Loading branch information
a5huynh committed Feb 2, 2018
1 parent e99584e commit 2894de8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/gfx/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod utils;
23 changes: 23 additions & 0 deletions src/gfx/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use graphics::{Context, DrawState, Transformed};
use graphics::text::Text;
use opengl_graphics::{GlGraphics, GlyphCache};

pub fn draw_text(txt: &str, pos: [f64; 2], size: u32, gc: &mut GlyphCache, c: &Context, gl: &mut GlGraphics) {

let transform = c.transform.trans(pos[0], pos[1]);

Text::new_color(::color::WHITE, size)
.draw(txt, gc, &DrawState::default(), transform, gl)
.unwrap();
}

// Draw text centered in the window
pub fn draw_center(txt: &str, size: u32, bounds: [f64; 2], gc: &mut GlyphCache, c: &Context, gl: &mut GlGraphics) {
let half_size = size as f64 / 2.0;
let num_chars = txt.len() as f64;

let x = (bounds[0] / 2.0) - (num_chars * half_size) / 2.0;
let y = (bounds[1] / 2.0) - half_size;

draw_text(txt, [x, y], size, gc, c, gl);
}
49 changes: 36 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use piston::window::Window;
mod color;
pub mod config;
mod geom;
mod gfx;
use gfx::utils::{draw_center, draw_text};

mod models;
use models::{GameObject};
Expand Down Expand Up @@ -81,6 +83,12 @@ impl<'a> App<'a> {
};
}

fn reset(&mut self) {
self.state.game_status = GameStatus::Normal;
self.score = 0;
self.enemies.clear();
}

pub fn input(&mut self, button: &Button, is_press: bool) {
if is_press {
if let Button::Keyboard(key) = *button {
Expand All @@ -100,14 +108,20 @@ impl<'a> App<'a> {
self.state.debug_mode = !self.state.debug_mode;
println!("Debug mode: {}", self.state.debug_mode);
},
// Reset game
Key::Return => {
match self.state.game_status {
GameStatus::Died => self.reset(),
GameStatus::Win => self.reset(),
_ => (),
}
}
_ => (),
}
}
} else {
if let Button::Keyboard(key) = *button {
match key {
// TODO: Can't do this because stop move on keyup may
// cancel keydown on another key.
Key::Up => self.player.stop_move(geom::Direction::NORTH),
Key::Down => self.player.stop_move(geom::Direction::SOUTH),
Key::Left => self.player.stop_move(geom::Direction::WEST),
Expand All @@ -131,27 +145,36 @@ impl<'a> App<'a> {
let bullets = &self.bullets;
let enemies = &self.enemies;
let player = &self.player;
let gc = &mut self.glyph_cache;
let state = &self.state;

let debug_mode = self.state.debug_mode;
let glyph_cache = &mut self.glyph_cache;
let score = self.score;
let size = self.window.settings.size();

// Render stuff.
self.window.gl.draw(args.viewport(), |c, gl| {
use graphics::*;

// Clear the screen.
clear(::color::BLACK, gl);

// Check game status
match state.game_status {
GameStatus::Died => {
draw_center("YOU DIED!", 32, [size.width as f64, size.height as f64], gc, &c, gl);
return;
},
GameStatus::Win => {
draw_center("YOU WIN!", 32, [size.width as f64, size.height as f64], gc, &c, gl);
return;
},
_ => (),
}

// Render the current score
text::Text::new_color(::color::WHITE, 12)
.draw(
format!("Score: {}", score).as_str(),
glyph_cache,
&DrawState::default(),
// Top left is (0.0, 0.0). Doesn't include the height
// of the text either.
c.transform.trans(0.0, 16.0),
gl
).unwrap();
let score_str = format!("Score: {}", score);
draw_text(score_str.as_str(), [0.0, 16.0], 16, gc, &c, gl);

// Render objects
for bullet in bullets.iter() {
Expand Down

0 comments on commit 2894de8

Please sign in to comment.