Can merge tiles while moving left/right
This commit is contained in:
parent
1e9e8ac303
commit
52bec0e312
5 changed files with 101 additions and 22 deletions
4
Makefile
4
Makefile
|
|
@ -34,9 +34,9 @@ SOURCE_FILES = $(shell test -e src/ && find src -type f)
|
|||
COMPILER = rustc
|
||||
|
||||
# For release:
|
||||
COMPILER_FLAGS = -O
|
||||
# COMPILER_FLAGS = -O
|
||||
# For debugging:
|
||||
# COMPILER_FLAGS = -g
|
||||
COMPILER_FLAGS = -g
|
||||
|
||||
RUSTDOC = rustdoc
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ use graphics::*;
|
|||
use piston::*;
|
||||
|
||||
use board::Board;
|
||||
use settings;
|
||||
|
||||
pub struct App {
|
||||
board: Board,
|
||||
|
|
@ -50,3 +49,4 @@ impl Game for App {
|
|||
_asset_store: &mut AssetStore
|
||||
) {}
|
||||
}
|
||||
|
||||
|
|
|
|||
112
src/board.rs
112
src/board.rs
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
use std::iter::range_step;
|
||||
use collections::hashmap::HashSet;
|
||||
use rand::random;
|
||||
use graphics::*;
|
||||
use piston::*;
|
||||
|
|
@ -25,7 +26,7 @@ impl Board {
|
|||
return;
|
||||
}
|
||||
|
||||
'generating: loop {
|
||||
loop {
|
||||
let x = (random::<uint>() % settings::TILE_WIDTH as uint) as int;
|
||||
let y = (random::<uint>() % settings::TILE_HEIGHT as uint) as int;
|
||||
if self.get_tile(x, y).is_none() {
|
||||
|
|
@ -39,6 +40,41 @@ impl Board {
|
|||
for tile in self.tiles.mut_iter() {
|
||||
tile.update(dt);
|
||||
}
|
||||
|
||||
if self.is_locking() {
|
||||
return;
|
||||
}
|
||||
let mut tiles_need_removed = HashSet::<uint>::new();
|
||||
let mut tiles_need_added = Vec::<Tile>::new();
|
||||
for i in range(0, self.tiles.len()) {
|
||||
let tile1 = self.tiles.get(i);
|
||||
if tile1.status != TileStatic {
|
||||
continue;
|
||||
}
|
||||
for j in range(i + 1, self.tiles.len()) {
|
||||
let tile2 = self.tiles.get(j);
|
||||
if tile2.status != TileStatic
|
||||
|| tile1.tile_x != tile2.tile_x
|
||||
|| tile1.tile_y != tile2.tile_y {
|
||||
continue;
|
||||
}
|
||||
|
||||
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));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if tiles_need_removed.len() > 0 {
|
||||
let mut tiles = Vec::<Tile>::new();
|
||||
for i in range(0, self.tiles.len()) {
|
||||
if !tiles_need_removed.contains(&i) {
|
||||
tiles.push(*self.tiles.get(i));
|
||||
}
|
||||
}
|
||||
self.tiles = tiles.append(tiles_need_added.as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&self, c: &Context, gl: &mut Gl) {
|
||||
|
|
@ -68,24 +104,64 @@ impl Board {
|
|||
return;
|
||||
}
|
||||
|
||||
// move all tiles to right place
|
||||
for row in range(0, settings::TILE_HEIGHT) {
|
||||
for col in range_step(x_start, x_end, x_step) {
|
||||
match self.get_mut_tile(col, row) {
|
||||
None => {
|
||||
match self.get_mut_next_tile(col, row, x_step, 0) {
|
||||
Some(ref mut tile) => {
|
||||
tile.start_moving(col, row);
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
loop {
|
||||
// move all tiles to right place
|
||||
for row in range(0, settings::TILE_HEIGHT) {
|
||||
for col in range_step(x_start, x_end, x_step) {
|
||||
match self.get_mut_tile(col, row) {
|
||||
None => {
|
||||
match self.get_mut_next_tile(col, row, x_step, 0) {
|
||||
Some(ref mut tile) => {
|
||||
tile.start_moving(col, row);
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// merge
|
||||
// merge
|
||||
let mut did_merged = false;
|
||||
for row in range(0, settings::TILE_HEIGHT) {
|
||||
let mut found = false;
|
||||
let mut sx = 0;
|
||||
let mut sy = 0;
|
||||
let mut dx = 0;
|
||||
let mut dy = 0;
|
||||
for col in range_step(x_start, x_end, x_step) {
|
||||
match self.get_tile(col, row) {
|
||||
Some(ref d_tile) => {
|
||||
match self.get_next_tile(col, row, x_step, 0) {
|
||||
Some(ref s_tile) if d_tile.score == s_tile.score => {
|
||||
found = true;
|
||||
dx = d_tile.tile_x;
|
||||
dy = d_tile.tile_y;
|
||||
sx = s_tile.tile_x;
|
||||
sy = s_tile.tile_y;
|
||||
break;
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if found {
|
||||
did_merged = true;
|
||||
let mut tile = self.get_mut_tile(sx, sy);
|
||||
let mut tile = tile.get_mut_ref();
|
||||
tile.start_moving(dx, dy);
|
||||
}
|
||||
}
|
||||
|
||||
if !did_merged {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
self.generate_tile();
|
||||
}
|
||||
|
|
@ -190,8 +266,8 @@ impl Board {
|
|||
|
||||
let mut x = settings::BOARD_PADDING + settings::TILE_PADDING;
|
||||
let mut y = settings::BOARD_PADDING + settings::BOARD_OFFSET_Y + settings::TILE_PADDING;
|
||||
for row in range(0, settings::TILE_HEIGHT) {
|
||||
for col in range(0, settings::TILE_WIDTH) {
|
||||
for _ in range(0, settings::TILE_HEIGHT) {
|
||||
for _ in range(0, settings::TILE_WIDTH) {
|
||||
c.view()
|
||||
.rect(x, y, settings::TILE_SIZE, settings::TILE_SIZE)
|
||||
.rgba(settings::TILES_COLOR[0][0],
|
||||
|
|
|
|||
|
|
@ -46,3 +46,4 @@ 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];
|
||||
|
||||
|
|
|
|||
|
|
@ -3,13 +3,14 @@ use graphics::*;
|
|||
use piston::*;
|
||||
use settings;
|
||||
|
||||
#[deriving(Eq)]
|
||||
#[deriving(Clone, Eq)]
|
||||
pub enum TileState {
|
||||
TileStatic,
|
||||
/// (t, x, y)
|
||||
TileMoving(f64, f64, f64),
|
||||
}
|
||||
|
||||
#[deriving(Clone)]
|
||||
pub struct Tile {
|
||||
pub score: int,
|
||||
pub tile_x: int,
|
||||
|
|
@ -81,3 +82,4 @@ impl Tile {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue