Added tile moving

This commit is contained in:
Coeuvre 2014-05-21 11:01:15 +08:00
parent 5d0f5fdb6e
commit 57b9c1d04d
4 changed files with 71 additions and 11 deletions

View file

@ -3,6 +3,7 @@ use graphics::*;
use piston::*;
use board::Board;
use settings;
pub struct App {
board: Board,
@ -21,15 +22,25 @@ impl Game for App {
self.board.render(c, gl);
}
fn update(&mut self, _dt: f64, _asset_store: &mut AssetStore) {}
fn update(&mut self, dt: f64, _asset_store: &mut AssetStore) {
self.board.update(dt);
}
fn load(&mut self, _asset_store: &mut AssetStore) {}
fn load(&mut self, _asset_store: &mut AssetStore) {
}
fn key_press(
&mut self,
_key: keyboard::Key,
key: keyboard::Key,
_asset_store: &mut AssetStore
) {}
) {
if key == keyboard::Left {
self.board.test_tile.start_moving(settings::TILE_MOVE_TIME, 0, 0);
}
if key == keyboard::Right {
self.board.test_tile.start_moving(settings::TILE_MOVE_TIME, 3, 0);
}
}
fn key_release(
&mut self,

View file

@ -7,6 +7,7 @@ use tile::Tile;
pub struct Board {
center: [f64, ..2],
tiles: [[Option<Tile>, ..settings::TILE_WIDTH], ..settings::TILE_HEIGHT],
pub test_tile: Tile,
}
impl Board {
@ -19,13 +20,18 @@ impl Board {
[None, None, None, None],
[None, None, None, None],
],
test_tile: Tile::new(16, 0, 0),
}
}
pub fn update(&mut self, dt: f64) {
self.test_tile.update(dt);
}
pub fn render(&self, c: &Context, gl: &mut Gl) {
self.render_board(c, gl);
self.render_tiles(c, gl);
Tile::new(16, 0, 0).render(c, gl);
self.test_tile.render(c, gl);
}
fn render_board(&self, c: &Context, gl: &mut Gl) {

View file

@ -42,6 +42,7 @@ 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 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];

View file

@ -5,8 +5,8 @@ use settings;
enum TileState {
TileStatic,
/// (x, y)
TileMoving(f64, f64),
/// (t, x, y, destination_tile_x, destination_tile_y)
TileMoving(f64, f64, f64, int, int),
}
pub struct Tile {
@ -26,12 +26,54 @@ impl Tile {
}
}
pub fn render(&self, c: &Context, gl: &mut Gl) {
let x = settings::BOARD_PADDING + self.tile_x as f64 * settings::TILE_SIZE + (self.tile_x + 1) as f64 * settings::TILE_PADDING;
let y = settings::BOARD_PADDING + settings::BOARD_OFFSET_Y + self.tile_y as f64 * settings::TILE_SIZE + (self.tile_y + 1) as f64 * settings::TILE_PADDING;
fn tile_to_pos(tile_x: int, tile_y: int) -> (f64, f64) {
let x = settings::BOARD_PADDING + tile_x as f64 * settings::TILE_SIZE + (tile_x + 1) as f64 * settings::TILE_PADDING;
let y = settings::BOARD_PADDING + settings::BOARD_OFFSET_Y + tile_y as f64 * settings::TILE_SIZE + (tile_y + 1) as f64 * settings::TILE_PADDING;
(x, y)
}
pub fn start_moving(&mut self, t: f64, destination_tile_x: int, destination_tile_y: int) {
match self.status {
TileStatic => {
let (x, y) = Tile::tile_to_pos(self.tile_x, self.tile_y);
self.status = TileMoving(t, x, y, destination_tile_x, destination_tile_y);
},
_ => {},
}
}
pub fn update(&mut self, dt: f64) {
match self.status {
TileMoving(t, x, y, dtx, dty) => {
if dt >= t {
self.tile_x = dtx;
self.tile_y = dty;
self.status = TileStatic;
} else {
let (dx, dy) = Tile::tile_to_pos(dtx, dty);
let factor = dt / t;
self.status = TileMoving(t - dt, x + factor * (dx - x), y + factor * (dy - y), dtx, dty);
}
},
TileStatic => {},
}
}
pub fn render(&self, c: &Context, gl: &mut Gl) {
let mut pos = (0.0, 0.0);
match self.status {
TileStatic => {
pos = Tile::tile_to_pos(self.tile_x, self.tile_y);
},
TileMoving(_, x, y, _, _) => {
pos = (x, y);
}
}
let (x, y) = pos;
let color = self.get_color();
c.view().rect(x, y, settings::TILE_SIZE, settings::TILE_SIZE).rgba(color[0], color[1], color[2], color[3]).fill(gl);
c.view()
.rect(x, y, settings::TILE_SIZE, settings::TILE_SIZE)
.rgba(color[0], color[1], color[2], color[3]).fill(gl);
}
fn get_color(&self) -> [f32, ..4] {