From ebdf6a52701c9c478efd75650276d8cb79451c21 Mon Sep 17 00:00:00 2001 From: Coeuvre Date: Wed, 21 May 2014 16:47:24 +0800 Subject: [PATCH] Added more animations --- src/board.rs | 14 +++------ src/settings.rs | 4 ++- src/tile.rs | 78 +++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 70 insertions(+), 26 deletions(-) diff --git a/src/board.rs b/src/board.rs index 77e849b..2124cc6 100644 --- a/src/board.rs +++ b/src/board.rs @@ -61,7 +61,7 @@ impl Board { tiles_need_removed.insert(i); tiles_need_removed.insert(j); - tiles_need_added.push(Tile::new(tile1.score + tile2.score, tile1.tile_x, tile1.tile_y)); + tiles_need_added.push(Tile::new_combined(tile1.score + tile2.score, tile1.tile_x, tile1.tile_y)); break; } } @@ -112,6 +112,7 @@ impl Board { None => { match self.get_mut_next_tile(col, row, x_step, 0) { Some(ref mut tile) => { + println!("move ({}, {}) to ({}, {})", tile.tile_x, tile.tile_y, col, row); tile.start_moving(col, row); }, _ => {}, @@ -284,15 +285,8 @@ impl Board { } fn render_tiles(&self, c: &Context, gl: &mut Gl) { - for row in range(0, settings::TILE_HEIGHT) { - for col in range(0, settings::TILE_WIDTH) { - match self.get_tile(col, row) { - Some(ref tile) => { - tile.render(c, gl); - }, - _ => {}, - } - } + for tile in self.tiles.iter() { + tile.render(c, gl); } } } diff --git a/src/settings.rs b/src/settings.rs index ddad0c2..6591592 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -42,7 +42,9 @@ pub static TILES_COLOR: [[f32, ..4], ..10] = [ [237.0 / 255.0, 200.0 / 255.0, 80.0 / 255.0, 1.0], ]; pub static TILE_UNKNOW_COLOR: [f32, ..4] = [0.8, 0.0, 0.0, 1.0]; -pub static TILE_MOVE_TIME: f64 = 0.08; +pub static TILE_MOVE_TIME: f64 = 0.1; +pub static TILE_NEW_TIME: f64 = 0.1; +pub static TILE_COMBINE_TIME: f64 = 0.1; pub static LABEL_COLOR: [f32, ..4] = [187.0 / 255.0, 173.0 / 255.0, 160.0 / 255.0, 1.0]; pub static BUTTON_COLOR: [f32, ..4] = [142.0 / 255.0, 122.0 / 255.0, 102.0 / 255.0, 1.0]; diff --git a/src/tile.rs b/src/tile.rs index bcc5a9d..d90231a 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -6,8 +6,12 @@ use settings; #[deriving(Clone, Eq)] pub enum TileState { TileStatic, - /// (t, x, y) - TileMoving(f64, f64, f64), + /// (t, x, y, origin_x, origin_x) + TileMoving(f64, f64, f64, int, int), + /// (t, size) + TileNew(f64, f64), + /// (t, size) + TileCombine(f64, f64), } #[deriving(Clone)] @@ -24,7 +28,16 @@ impl Tile { score: score, tile_x: tile_x, tile_y: tile_y, - status: TileStatic, + status: TileNew(settings::TILE_NEW_TIME, 0.0), + } + } + + pub fn new_combined(score: int, tile_x: int, tile_y: int) -> Tile { + Tile { + score: score, + tile_x: tile_x, + tile_y: tile_y, + status: TileCombine(settings::TILE_COMBINE_TIME, 1.2 * settings::TILE_SIZE), } } @@ -35,41 +48,76 @@ impl Tile { } pub fn start_moving(&mut self, destination_tile_x: int, destination_tile_y: int) { - let (x, y) = Tile::tile_to_pos(self.tile_x, self.tile_y); - self.status = TileMoving(settings::TILE_MOVE_TIME, x, y); - self.tile_x = destination_tile_x; - self.tile_y = destination_tile_y; + match self.status { + TileMoving(_, _, _, ox, oy) => { + let (x, y) = Tile::tile_to_pos(ox, oy); + self.status = TileMoving(settings::TILE_MOVE_TIME, x, y, ox, oy); + self.tile_x = destination_tile_x; + self.tile_y = destination_tile_y; + }, + TileStatic => { + let (x, y) = Tile::tile_to_pos(self.tile_x, self.tile_y); + self.status = TileMoving(settings::TILE_MOVE_TIME, x, y, self.tile_x, self.tile_y); + self.tile_x = destination_tile_x; + self.tile_y = destination_tile_y; + }, + _ => {}, + } } pub fn update(&mut self, dt: f64) { match self.status { - TileMoving(t, x, y) => { + TileMoving(t, x, y, ox, oy) => { if dt >= t { self.status = TileStatic; } else { let (dx, dy) = Tile::tile_to_pos(self.tile_x, self.tile_y); let factor = dt / t; - self.status = TileMoving(t - dt, x + factor * (dx - x), y + factor * (dy - y)); + self.status = TileMoving(t - dt, x + factor * (dx - x), y + factor * (dy - y), ox, oy); } }, - TileStatic => {}, + TileNew(t, size) => { + if dt >= t { + self.status = TileStatic; + } else { + let factor = dt / t; + self.status = TileNew(t - dt, size + factor * (settings::TILE_SIZE - size)); + } + }, + TileCombine(t, size) => { + if dt >= t { + self.status = TileStatic; + } else { + let factor = dt / t; + self.status = TileCombine(t - dt, size + factor * (settings::TILE_SIZE - size)); + } + } + _ => {}, } } pub fn render(&self, c: &Context, gl: &mut Gl) { - let mut pos = (0.0, 0.0); + let mut pos = Tile::tile_to_pos(self.tile_x, self.tile_y); + let mut size = (settings::TILE_SIZE, settings::TILE_SIZE); match self.status { - TileMoving(_, x, y) => { + TileMoving(_, x, y, _, _) => { pos = (x, y); }, - TileStatic => { - pos = Tile::tile_to_pos(self.tile_x, self.tile_y); + TileNew(_, s) => { + size = (s, s); }, + TileCombine(_, s) => { + size = (s, s); + }, + _ => {}, } let (x, y) = pos; + let (w, h) = size; let color = self.get_color(); c.view() - .rect(x, y, settings::TILE_SIZE, settings::TILE_SIZE) + .rect_centered(x + settings::TILE_SIZE / 2.0, + y + settings::TILE_SIZE / 2.0, + w / 2.0, h / 2.0) .rgba(color[0], color[1], color[2], color[3]).fill(gl); }