Tiles can display numbers now!
This commit is contained in:
parent
6cc38951d8
commit
37af2957c4
7 changed files with 88 additions and 9 deletions
BIN
bin/assets/digits.png
Normal file
BIN
bin/assets/digits.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
12
src/app.rs
12
src/app.rs
|
|
@ -3,31 +3,35 @@ use graphics::*;
|
||||||
use piston::*;
|
use piston::*;
|
||||||
|
|
||||||
use board::Board;
|
use board::Board;
|
||||||
|
use number_renderer::NumberRenderer;
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
board: Board,
|
board: Board,
|
||||||
|
number_renderer: Option<NumberRenderer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new() -> App {
|
pub fn new() -> App {
|
||||||
App {
|
App {
|
||||||
board: Board::new(),
|
board: Board::new(),
|
||||||
|
number_renderer: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game for App {
|
impl Game for App {
|
||||||
|
fn load(&mut self, asset_store: &mut AssetStore) {
|
||||||
|
self.number_renderer = Some(NumberRenderer::new(asset_store));
|
||||||
|
}
|
||||||
|
|
||||||
fn render(&self, c: &Context, gl: &mut Gl) {
|
fn render(&self, c: &Context, gl: &mut Gl) {
|
||||||
self.board.render(c, gl);
|
self.board.render(self.number_renderer.get_ref(), 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);
|
self.board.update(dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load(&mut self, _asset_store: &mut AssetStore) {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn key_press(
|
fn key_press(
|
||||||
&mut self,
|
&mut self,
|
||||||
key: keyboard::Key,
|
key: keyboard::Key,
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ use collections::hashmap::HashSet;
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use graphics::*;
|
use graphics::*;
|
||||||
use piston::*;
|
use piston::*;
|
||||||
|
use number_renderer::NumberRenderer;
|
||||||
use settings;
|
use settings;
|
||||||
use tile::{
|
use tile::{
|
||||||
Tile,
|
Tile,
|
||||||
|
|
@ -87,9 +88,9 @@ impl Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, c: &Context, gl: &mut Gl) {
|
pub fn render(&self, number_renderer: &NumberRenderer, c: &Context, gl: &mut Gl) {
|
||||||
self.render_board(c, gl);
|
self.render_board(c, gl);
|
||||||
self.render_tiles(c, gl);
|
self.render_tiles(number_renderer, c, gl);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn merge_from_bottom_to_top(&mut self) {
|
pub fn merge_from_bottom_to_top(&mut self) {
|
||||||
|
|
@ -365,9 +366,9 @@ impl Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_tiles(&self, c: &Context, gl: &mut Gl) {
|
fn render_tiles(&self, number_renderer: &NumberRenderer, c: &Context, gl: &mut Gl) {
|
||||||
for tile in self.tiles.iter() {
|
for tile in self.tiles.iter() {
|
||||||
tile.render(c, gl);
|
tile.render(number_renderer, c, gl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ use piston::*;
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod board;
|
mod board;
|
||||||
|
mod number_renderer;
|
||||||
mod settings;
|
mod settings;
|
||||||
mod tile;
|
mod tile;
|
||||||
|
|
||||||
|
|
|
||||||
62
src/number_renderer.rs
Normal file
62
src/number_renderer.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
|
||||||
|
use graphics::*;
|
||||||
|
use piston::{
|
||||||
|
AssetStore,
|
||||||
|
Gl,
|
||||||
|
};
|
||||||
|
|
||||||
|
static DIGITS_WIDTH: f64 = 20.0;
|
||||||
|
static DIGITS_HEIGHT: f64 = 26.0;
|
||||||
|
|
||||||
|
pub struct NumberRenderer {
|
||||||
|
image: Image,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NumberRenderer {
|
||||||
|
pub fn new(asset_store: &mut AssetStore) -> NumberRenderer {
|
||||||
|
NumberRenderer {
|
||||||
|
image: asset_store.load_image("digits.png"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&self, number: u32, center_x: f64, center_y: f64, max_width: f64,
|
||||||
|
color: [f32, ..4], c: &Context, gl: &mut Gl) {
|
||||||
|
let digits = number_to_digits(number);
|
||||||
|
let total_width = DIGITS_WIDTH * digits.len() as f64;
|
||||||
|
let total_width = if total_width > max_width {
|
||||||
|
max_width
|
||||||
|
} else {
|
||||||
|
total_width
|
||||||
|
};
|
||||||
|
let mut x = center_x - total_width / 2.0;
|
||||||
|
let width = total_width / digits.len() as f64;
|
||||||
|
let height = width / DIGITS_WIDTH * DIGITS_HEIGHT;
|
||||||
|
let y = center_y - height / 2.0;
|
||||||
|
|
||||||
|
let mut image = self.image;
|
||||||
|
image.source_rect[2] = DIGITS_WIDTH as u32;
|
||||||
|
for digit in digits.iter() {
|
||||||
|
image.source_rect[0] = DIGITS_WIDTH as u32 * *digit;
|
||||||
|
c.view().rect(x, y, width, height)
|
||||||
|
.image(image).rgba(color[0], color[1], color[2], color[3])
|
||||||
|
.draw(gl);
|
||||||
|
x += width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn number_to_digits(number: u32) -> Vec<u32> {
|
||||||
|
let mut digits = Vec::<u32>::new();
|
||||||
|
if number == 0 {
|
||||||
|
digits.push(0);
|
||||||
|
return digits;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut n = number;
|
||||||
|
while n != 0 {
|
||||||
|
digits.unshift(n % 10);
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
digits
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -49,3 +49,6 @@ 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];
|
||||||
|
|
||||||
|
pub static TEXT_DARK_COLOR: [f32, ..4] = [119.0 / 255.0, 110.0 / 255.0, 101.0 / 255.0, 1.0];
|
||||||
|
pub static TEXT_LIGHT_COLOR: [f32, ..4] = [249.0 / 255.0, 246.0 / 255.0, 242.0 / 255.0, 1.0];
|
||||||
|
|
||||||
|
|
|
||||||
10
src/tile.rs
10
src/tile.rs
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
use graphics::*;
|
use graphics::*;
|
||||||
use piston::*;
|
use piston::*;
|
||||||
|
use number_renderer::NumberRenderer;
|
||||||
use settings;
|
use settings;
|
||||||
|
|
||||||
#[deriving(Clone, Eq)]
|
#[deriving(Clone, Eq)]
|
||||||
|
|
@ -96,7 +97,7 @@ impl Tile {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, c: &Context, gl: &mut Gl) {
|
pub fn render(&self, number_renderer: &NumberRenderer, c: &Context, gl: &mut Gl) {
|
||||||
let mut pos = Tile::tile_to_pos(self.tile_x, self.tile_y);
|
let mut pos = Tile::tile_to_pos(self.tile_x, self.tile_y);
|
||||||
let mut size = (settings::TILE_SIZE, settings::TILE_SIZE);
|
let mut size = (settings::TILE_SIZE, settings::TILE_SIZE);
|
||||||
match self.status {
|
match self.status {
|
||||||
|
|
@ -119,6 +120,13 @@ impl Tile {
|
||||||
y + settings::TILE_SIZE / 2.0,
|
y + settings::TILE_SIZE / 2.0,
|
||||||
w / 2.0, h / 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);
|
||||||
|
|
||||||
|
let color = if self.score >= 8 {
|
||||||
|
settings::TEXT_LIGHT_COLOR
|
||||||
|
} else {
|
||||||
|
settings::TEXT_DARK_COLOR
|
||||||
|
};
|
||||||
|
number_renderer.render(self.score as u32, x + settings::TILE_SIZE / 2.0, y + settings::TILE_SIZE / 2.0, settings::TILE_SIZE, color, c, gl);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_color(&self) -> [f32, ..4] {
|
fn get_color(&self) -> [f32, ..4] {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue