summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs114
1 files changed, 84 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs
index cef5b84..0fa52ab 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,6 +10,7 @@ use rand::{
distributions::{Distribution, Standard},
Rng,
};
+use std::time::Instant;
pub const TILE_SCALE: f32 = 3.0;
pub const TILE_HEIGHT: f32 = 16.0;
@@ -27,7 +28,7 @@ enum Occupant {
Yellow,
Diamond,
Red,
- Explosion,
+ Explosion { frame: usize, timer: Instant },
}
impl Distribution<Occupant> for Standard {
@@ -91,11 +92,17 @@ impl Cell {
pub fn draw(&self, context: &mut Context, spritebatch: &mut SpriteBatch) -> GameResult {
let source = match self.occupant {
Occupant::None => None,
- Occupant::Explosion => None,
- Occupant::Green => Some(Rect::new(0.0, 0.0, 0.25, 1.0)),
- Occupant::Yellow => Some(Rect::new(0.25, 0.0, 0.25, 1.0)),
- Occupant::Diamond => Some(Rect::new(0.50, 0.0, 0.25, 1.0)),
- Occupant::Red => Some(Rect::new(0.75, 0.0, 0.25, 1.0)),
+ Occupant::Explosion { frame, .. } => match frame {
+ 0 => Some(Rect::new(0.0, 0.5, 0.25, 0.5)),
+ 1 => Some(Rect::new(0.25, 0.5, 0.25, 0.5)),
+ 2 => Some(Rect::new(0.50, 0.5, 0.25, 0.5)),
+ 3 => Some(Rect::new(0.75, 0.5, 0.25, 0.5)),
+ _ => None,
+ },
+ Occupant::Green => Some(Rect::new(0.0, 0.0, 0.25, 0.5)),
+ Occupant::Yellow => Some(Rect::new(0.25, 0.0, 0.25, 0.5)),
+ Occupant::Diamond => Some(Rect::new(0.50, 0.0, 0.25, 0.5)),
+ Occupant::Red => Some(Rect::new(0.75, 0.0, 0.25, 0.5)),
};
if let Some(source) = source {
@@ -154,7 +161,7 @@ impl Game {
let mut background = Image::new(context, "/background.png")?;
background.set_filter(FilterMode::Nearest);
- let mut image = Image::new(context, "/gem.png")?;
+ let mut image = Image::new(context, "/tileset.png")?;
image.set_filter(FilterMode::Nearest);
image.set_wrap(WrapMode::Mirror, WrapMode::Mirror);
@@ -180,32 +187,14 @@ impl Game {
background,
})
}
-}
-
-impl EventHandler for Game {
- fn update(&mut self, context: &mut Context) -> GameResult {
- for row in self.grid.iter_mut() {
- for cell in row.iter_mut() {
- cell.hover_off();
- }
- }
-
- let position = mouse::position(context);
-
- for row in self.grid.iter_mut() {
- for cell in row.iter_mut() {
- if cell.contains(position) {
- cell.hover_on();
- }
- }
- }
+ fn update_explosions(&mut self) {
let mut last = Occupant::None;
let mut connected = Vec::new();
for i in 0..COLUMNS {
let mut c = Vec::new();
for j in 0..ROWS {
- if self.grid[i][j].occupant == last {
+ if self.grid[i][j].occupant == last && last != Occupant::None {
c.push((i, j));
c.push((i, j - 1));
} else {
@@ -221,7 +210,10 @@ impl EventHandler for Game {
for c in connected.iter() {
if c.len() > 3 {
for (i, j) in c.iter() {
- self.grid[*i][*j].occupant = Occupant::Explosion;
+ self.grid[*i][*j].occupant = Occupant::Explosion {
+ frame: 0,
+ timer: Instant::now(),
+ };
}
}
}
@@ -230,7 +222,7 @@ impl EventHandler for Game {
for i in 0..COLUMNS {
let mut c = Vec::new();
for j in 0..ROWS {
- if self.grid[j][i].occupant == last {
+ if self.grid[j][i].occupant == last && last != Occupant::None {
c.push((j, i));
c.push((j - 1, i));
} else {
@@ -246,10 +238,72 @@ impl EventHandler for Game {
for c in connected.iter() {
if c.len() > 3 {
for (j, i) in c.iter() {
- self.grid[*j][*i].occupant = Occupant::Explosion;
+ self.grid[*j][*i].occupant = Occupant::Explosion {
+ frame: 0,
+ timer: Instant::now(),
+ };
+ }
+ }
+ }
+ }
+
+ fn update_dropping(&mut self) {
+ /*
+ for (i, row) in self.grid.iter_mut().enumerate() {
+ for (j, cell) in row.iter_mut().enumerate() {
+ println!("a");
+ }
+ }
+ */
+ }
+
+ fn update_frames(&mut self) {
+ for row in self.grid.iter_mut() {
+ for cell in row.iter_mut() {
+ let mut done = false;
+ if let Occupant::Explosion {
+ ref mut frame,
+ ref mut timer,
+ } = cell.occupant
+ {
+ if timer.elapsed().as_millis() > 500 {
+ if *frame < 4 {
+ *frame += 1;
+ *timer = Instant::now();
+ } else {
+ done = true;
+ }
+ }
+ }
+ if done {
+ cell.occupant = Occupant::None;
}
}
}
+ }
+}
+
+impl EventHandler for Game {
+ fn update(&mut self, context: &mut Context) -> GameResult {
+ for row in self.grid.iter_mut() {
+ for cell in row.iter_mut() {
+ cell.hover_off();
+ }
+ }
+
+ let position = mouse::position(context);
+
+ for row in self.grid.iter_mut() {
+ for cell in row.iter_mut() {
+ if cell.contains(position) {
+ cell.hover_on();
+ }
+ }
+ }
+
+ self.update_explosions();
+ self.update_frames();
+ self.update_dropping();
Ok(())
}