Skip to content

Commit

Permalink
imp: Display game result on GUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
Regentag committed Sep 14, 2015
1 parent 23979ea commit e99e267
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ pub enum MoveDestination {
Down,
Left,
Right
}
}

pub enum GameEndState {
Win,
Lose
}
31 changes: 29 additions & 2 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use field::{Field, Content};
use ui::UI;
use ui::EndMessage;
use piston_window::*;
use common::{ParamType, MoveDestination};
use common::{ParamType, MoveDestination, GameEndState};

pub struct Game<'a> {
field: Field,
ui: UI<'a>,
msg: EndMessage<'a>,
glyphs: Glyphs,
mouse_x: f64,
mouse_y: f64,
Expand All @@ -19,6 +21,7 @@ impl<'a> Game<'a> {
Game {
field: Field::new(width, height, mines),
ui: UI::new(width, height, mines),
msg: EndMessage::new(),
glyphs: glyphs,
mouse_x: 0.0,
mouse_y: 0.0,
Expand All @@ -33,8 +36,12 @@ impl<'a> Game<'a> {
clear([0.0, 0.0, 0.0, 1.0], g);
let field_rect = self.get_field_rect(window);
self.field.draw(c, g, field_rect, &mut self.glyphs);

let ui_rect = self.get_ui_rect(window);
self.ui.draw(c, g, ui_rect, &mut self.glyphs, self.field.total_mines(), self.field.count_marked());

let msg_rect = self.get_msg_rect(window);
self.msg.draw(c, g, msg_rect, &mut self.glyphs);
});
}

Expand All @@ -53,6 +60,19 @@ impl<'a> Game<'a> {
let h = window.size().height;
[field_w, 0, w, h]
}

fn get_msg_rect(&self, window: &PistonWindow) -> [u32; 4] {
let w_w = window.size().width;
let w_h = window.size().height;
let (m_w, m_h) = EndMessage::size();

[
( (w_w-m_w) / 2 ),
( (w_h-m_h) / 2 ),
m_w,
m_h
]
}

pub fn proc_key(&mut self, button: Button, window: &PistonWindow) {
if self.in_ui {
Expand All @@ -63,6 +83,7 @@ impl<'a> Game<'a> {
match self.ui.proc_key(ParamType::Height) {
Some(h) => {
self.in_ui = false;
self.msg.hide();
self.field.reinit_field(h, ParamType::Height);
}
_ => {}
Expand All @@ -72,6 +93,7 @@ impl<'a> Game<'a> {
match self.ui.proc_key(ParamType::Mines) {
Some(m) => {
self.in_ui = false;
self.msg.hide();
self.field.reinit_field(m, ParamType::Mines);
}
_ => {}
Expand All @@ -81,6 +103,7 @@ impl<'a> Game<'a> {
match self.ui.proc_key(ParamType::Width) {
Some(w) => {
self.in_ui = false;
self.msg.hide();
self.field.reinit_field(w, ParamType::Width);
}
_ => {}
Expand Down Expand Up @@ -215,18 +238,21 @@ impl<'a> Game<'a> {
Content::Mine(_) => {
self.field.reveal_all();
self.game_ended = true;
self.field.set_killer(i);
self.field.set_killer(i);
self.msg.show( GameEndState::Lose );
println!("Game over :(");
},
Content::None => {
self.field.chain_reveal(i);
if self.field.is_victory() {
self.msg.show( GameEndState::Win );
println!("You win :)");
self.game_ended = true;
}
}
Content::Number(_i) => {
if self.field.is_victory() {
self.msg.show( GameEndState::Win );
println!("You win :)");
self.game_ended = true;
}
Expand All @@ -241,6 +267,7 @@ impl<'a> Game<'a> {

fn restart(&mut self) {
self.game_ended = false;
self.msg.hide();
self.field.restart();
}
}
113 changes: 112 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use piston_window::*;
use common::{ParamType, MoveDestination};
use common::{ParamType, MoveDestination, GameEndState};

struct Block<'a> {
name: &'a str,
Expand Down Expand Up @@ -173,3 +173,114 @@ impl<'a> UI<'a> {
}
}
}

pub struct EndMessage<'a> {
visible: bool,
win: bool,
message: &'a str
}

static EM_TEXT_BIG : u32 = 40;
static EM_TEXT_SMALL : u32 = 20;
static EM_TEXT_PADDING : u32 = 5;
static EM_BORDER_WIDTH : u32 = 3;
static EM_BORDER_WIDTH_2: u32 = 6;

static RETRY_MSG: &'static str = "Press 'R' to retry.";

impl<'a> EndMessage<'a> {
pub fn new() -> EndMessage<'a> {
EndMessage {
visible: false,
win: false,
message: "Ready",
}
}

pub fn show( &mut self, end_state: GameEndState ) {
self.win = match end_state {
GameEndState::Win => true,
_ => false };
self.visible = true;
self.message = if self.win {
"You WIN :)"
} else {
"Game over :("
};
}

pub fn hide( &mut self ) {
self.visible = false;
}

/// Message Box's size. (width,height).
pub fn size() -> (u32,u32) {
(
350,
(EM_TEXT_BIG + EM_TEXT_SMALL + (EM_TEXT_PADDING*3) + (EM_BORDER_WIDTH*2))
)
}

pub fn draw(&self,
context: Context,
graphics: &mut G2d,
rect: [u32; 4],
glyps: &mut Glyphs)
{
if !self.visible
{
return;
}

let border_color = if self.win {
[0.0, 0.0, 1.0, 1.0] // Win : Blue
} else {
[1.0, 1.0, 0.0, 1.0] // Lose: yellow
};

// draw border
rectangle(border_color,
[
(rect[0]) as f64,
(rect[1]) as f64,
(rect[2]) as f64,
(rect[3]) as f64
],
context.transform,
graphics);

// draw background
rectangle( [1.0, 1.0, 1.0, 1.0],
[
(rect[0] +EM_BORDER_WIDTH) as f64,
(rect[1] +EM_BORDER_WIDTH) as f64,
(rect[2] -EM_BORDER_WIDTH_2) as f64,
(rect[3] -EM_BORDER_WIDTH_2) as f64
],
context.transform,
graphics);

// Draw game result message
let trans_msg = context.transform.trans(
(rect[0] + EM_BORDER_WIDTH + EM_TEXT_PADDING) as f64,
(rect[1] + EM_BORDER_WIDTH + EM_TEXT_PADDING + EM_TEXT_BIG) as f64);

text([0.0, 0.0, 0.0, 1.0],
EM_TEXT_BIG,
&*self.message,
glyps,
trans_msg,
graphics);

// Draw 'retry' message
let trans_retry = context.transform.trans(
(rect[0] + EM_BORDER_WIDTH + EM_TEXT_PADDING) as f64,
(rect[1] + EM_BORDER_WIDTH + EM_TEXT_PADDING + EM_TEXT_BIG + EM_TEXT_SMALL) as f64);
text([0.0, 0.0, 0.0, 1.0],
EM_TEXT_SMALL,
&*RETRY_MSG,
glyps,
trans_retry,
graphics);
}
}

0 comments on commit e99e267

Please sign in to comment.