Skip to content

Commit

Permalink
Merge pull request #36 from waynenilsen/timer
Browse files Browse the repository at this point in the history
feat: added the timer
  • Loading branch information
Vinatorul committed Sep 30, 2015
2 parents 0a80921 + 6d12d50 commit df2b7ba
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ keywords = ["minesweeper", "game"]
piston_window = "~0.19.0"
clap = "^1.2"
rand = "^0.3"
find_folder = "^0.2"
find_folder = "^0.2"
chrono = "^0.2"
30 changes: 26 additions & 4 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ui::UI;
use ui::EndMessage;
use piston_window::*;
use common::{ParamType, MoveDestination, GameEndState};
use chrono::{DateTime, UTC, Duration};

pub struct Game<'a> {
field: Field,
Expand All @@ -13,7 +14,9 @@ pub struct Game<'a> {
mouse_y: f64,
game_ended: bool,
panel_width: u32,
in_ui: bool
in_ui: bool,
game_start : Option<DateTime<UTC>>,
game_end : Option<DateTime<UTC>>,
}

impl<'a> Game<'a> {
Expand All @@ -27,7 +30,9 @@ impl<'a> Game<'a> {
mouse_y: 0.0,
game_ended: false,
panel_width: 350,
in_ui: false
in_ui: false,
game_start: None,
game_end : None,
}
}

Expand All @@ -38,7 +43,16 @@ impl<'a> Game<'a> {
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 dur = match self.game_start {
Some(game_start) => match self.game_end {
Some(game_end) => game_end - game_start,
None => UTC::now() - game_start,
},
None => Duration::seconds(0),
};

self.ui.draw(c, g, ui_rect, &mut self.glyphs, self.field.total_mines(), self.field.count_marked(), dur);

let msg_rect = self.get_msg_rect(window);
self.msg.draw(c, g, msg_rect, &mut self.glyphs);
Expand Down Expand Up @@ -189,7 +203,10 @@ impl<'a> Game<'a> {
if self.game_ended {
return;
}

if self.game_start == None {
self.game_start = Some(UTC::now());
}

if !self.field.revealed(i) {
self.check_reveal(i);
} else {
Expand Down Expand Up @@ -239,6 +256,7 @@ impl<'a> Game<'a> {
self.field.reveal_all();
self.game_ended = true;
self.field.set_killer(i);
self.game_end = Some(UTC::now());
self.msg.show( GameEndState::Lose );
println!("Game over :(");
},
Expand All @@ -248,13 +266,15 @@ impl<'a> Game<'a> {
self.msg.show( GameEndState::Win );
println!("You win :)");
self.game_ended = true;
self.game_end = Some(UTC::now());
}
}
Content::Number(_i) => {
if self.field.is_victory() {
self.msg.show( GameEndState::Win );
println!("You win :)");
self.game_ended = true;
self.game_end = Some(UTC::now());
}
}
}
Expand All @@ -268,6 +288,8 @@ impl<'a> Game<'a> {
fn restart(&mut self) {
self.game_ended = false;
self.msg.hide();
self.game_start = None;
self.game_end = None;
self.field.restart();
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate piston_window;
#[macro_use]
extern crate clap;
extern crate chrono;
extern crate rand;
extern crate find_folder;

Expand Down
22 changes: 19 additions & 3 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

use piston_window::*;
use common::{ParamType, MoveDestination, GameEndState};
use chrono::Duration;

struct Block<'a> {
name: &'a str,
Expand All @@ -9,7 +11,7 @@ struct Block<'a> {
selected: bool,
min_val: u32,
max_val: u32,
param_type: ParamType
param_type: ParamType,
}

impl<'a> Block<'a> {
Expand All @@ -28,7 +30,7 @@ impl<'a> Block<'a> {
selected: false,
min_val: min_val,
max_val: max_val,
param_type: param_type
param_type: param_type,
}
}

Expand Down Expand Up @@ -125,7 +127,8 @@ impl<'a> UI<'a> {
mut rect: [u32; 4],
glyps: &mut Glyphs,
mines_total: u32,
mines_marked: u32)
mines_marked: u32,
duration : Duration)
{
for b in self.blocks.iter() {
b.draw(context, graphics, &mut rect, glyps);
Expand All @@ -139,6 +142,19 @@ impl<'a> UI<'a> {
glyps,
transform,
graphics);

let transform = context.transform.trans((rect[0]+10) as f64,
(rect[1]+27*2) as f64);
let total_seconds = duration.num_seconds();
let mins = total_seconds / 60;
let rem_seconds = total_seconds - mins*60;

text([1.0, 1.0, 1.0, 1.0],
20,
&*format!("{}:{}", mins, rem_seconds),
glyps,
transform,
graphics);
}

pub fn proc_key(&mut self, block: ParamType) -> Option<u32> {
Expand Down

0 comments on commit df2b7ba

Please sign in to comment.