Added more animations

This commit is contained in:
Coeuvre 2014-05-21 16:47:24 +08:00
parent 52bec0e312
commit ebdf6a5270
3 changed files with 70 additions and 26 deletions

View file

@ -61,7 +61,7 @@ impl Board {
tiles_need_removed.insert(i); tiles_need_removed.insert(i);
tiles_need_removed.insert(j); 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; break;
} }
} }
@ -112,6 +112,7 @@ impl Board {
None => { None => {
match self.get_mut_next_tile(col, row, x_step, 0) { match self.get_mut_next_tile(col, row, x_step, 0) {
Some(ref mut tile) => { Some(ref mut tile) => {
println!("move ({}, {}) to ({}, {})", tile.tile_x, tile.tile_y, col, row);
tile.start_moving(col, row); tile.start_moving(col, row);
}, },
_ => {}, _ => {},
@ -284,15 +285,8 @@ impl Board {
} }
fn render_tiles(&self, c: &Context, gl: &mut Gl) { fn render_tiles(&self, c: &Context, gl: &mut Gl) {
for row in range(0, settings::TILE_HEIGHT) { for tile in self.tiles.iter() {
for col in range(0, settings::TILE_WIDTH) {
match self.get_tile(col, row) {
Some(ref tile) => {
tile.render(c, gl); tile.render(c, gl);
},
_ => {},
}
}
} }
} }
} }

View file

@ -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], [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_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 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]; pub static BUTTON_COLOR: [f32, ..4] = [142.0 / 255.0, 122.0 / 255.0, 102.0 / 255.0, 1.0];

View file

@ -6,8 +6,12 @@ use settings;
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub enum TileState { pub enum TileState {
TileStatic, TileStatic,
/// (t, x, y) /// (t, x, y, origin_x, origin_x)
TileMoving(f64, f64, f64), TileMoving(f64, f64, f64, int, int),
/// (t, size)
TileNew(f64, f64),
/// (t, size)
TileCombine(f64, f64),
} }
#[deriving(Clone)] #[deriving(Clone)]
@ -24,7 +28,16 @@ impl Tile {
score: score, score: score,
tile_x: tile_x, tile_x: tile_x,
tile_y: tile_y, 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) { 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); match self.status {
self.status = TileMoving(settings::TILE_MOVE_TIME, x, y); 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_x = destination_tile_x;
self.tile_y = destination_tile_y; 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) { pub fn update(&mut self, dt: f64) {
match self.status { match self.status {
TileMoving(t, x, y) => { TileMoving(t, x, y, ox, oy) => {
if dt >= t { if dt >= t {
self.status = TileStatic; self.status = TileStatic;
} else { } else {
let (dx, dy) = Tile::tile_to_pos(self.tile_x, self.tile_y); let (dx, dy) = Tile::tile_to_pos(self.tile_x, self.tile_y);
let factor = dt / t; 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) { 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 { match self.status {
TileMoving(_, x, y) => { TileMoving(_, x, y, _, _) => {
pos = (x, y); pos = (x, y);
}, },
TileStatic => { TileNew(_, s) => {
pos = Tile::tile_to_pos(self.tile_x, self.tile_y); size = (s, s);
}, },
TileCombine(_, s) => {
size = (s, s);
},
_ => {},
} }
let (x, y) = pos; let (x, y) = pos;
let (w, h) = size;
let color = self.get_color(); let color = self.get_color();
c.view() 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); .rgba(color[0], color[1], color[2], color[3]).fill(gl);
} }