This commit is contained in:
Michael Zhang 2019-08-09 11:01:26 -05:00
parent 34e58f52d1
commit ada9131dfb
No known key found for this signature in database
GPG key ID: 5BAEFE5D04F0CE6C
4 changed files with 29 additions and 12 deletions

View file

@ -1,7 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::time::Duration;
type BlockOffsets = HashMap<usize, (u32, u32)>; pub type BlockOffsets = HashMap<usize, (u32, u32)>;
type AnimationFn = Box<Fn(BlockOffsets, f32) -> BlockOffsets>; pub type AnimationFn = Box<Fn(BlockOffsets, f32) -> BlockOffsets>;
#[derive(Default)] #[derive(Default)]
pub struct AnimationState { pub struct AnimationState {
@ -19,4 +20,10 @@ impl AnimationState {
self.progress = 0.0; self.progress = 0.0;
self.progress_function = Some(f); self.progress_function = Some(f);
} }
pub fn make_progress(&mut self, delta: Duration) {}
pub fn is_done(&self) -> bool {
self.progress > 1.0
}
} }

View file

@ -105,8 +105,10 @@ impl<'a> Game<'a> {
} }
if self.animations.is_animating { if self.animations.is_animating {
let delta_ms = delta.as_millis();
if self.animations.last_move_success { if self.animations.last_move_success {
if self.animations.is_done() {
self.animations.make_progress(delta);
}
} else { } else {
} }
} else { } else {
@ -122,17 +124,17 @@ impl<'a> Game<'a> {
// failed a move // failed a move
if !self.animations.last_move_success { if !self.animations.last_move_success {
self.animations let func = |mut offsets: HashMap<_, _>, prog| {
.begin_transition(Box::new(|mut offsets, prog| { offsets.insert(0, (0, 0));
offsets.insert(0, (0, 0)); offsets
offsets };
})); self.animations.begin_transition(Box::new(func));
} }
} }
} }
pub fn render(&self, renderer: &mut Renderer) { pub fn render(&self, renderer: &mut Renderer) {
let level = self.get_current_level(); let level = self.get_current_level();
level.render(renderer); level.render(renderer, &self.animations.block_offsets);
} }
} }

View file

@ -39,6 +39,7 @@ impl Block {
.collect(); .collect();
let orientation = data.orientation.into(); let orientation = data.orientation.into();
let color = Color::from_rgb_u32(data.color.0, data.color.1, data.color.2); let color = Color::from_rgb_u32(data.color.0, data.color.1, data.color.2);
Block { Block {
index, index,
movable, movable,

View file

@ -4,6 +4,7 @@ mod player;
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use crate::animations::BlockOffsets;
use crate::color::Color; use crate::color::Color;
use crate::data::LevelData; use crate::data::LevelData;
use crate::enums::{Board, Orientation, PushDir, Shape}; use crate::enums::{Board, Orientation, PushDir, Shape};
@ -197,7 +198,7 @@ impl Level {
Some(()) Some(())
} }
pub fn render(&self, renderer: &mut Renderer) { pub fn render(&self, renderer: &mut Renderer, block_offsets: &BlockOffsets) {
// board positioning calculations // board positioning calculations
let playfield_ratio = (2 * self.dimensions.0 + 6) as f32 / (self.dimensions.1 + 4) as f32; let playfield_ratio = (2 * self.dimensions.0 + 6) as f32 / (self.dimensions.1 + 4) as f32;
let screen_ratio = renderer.window.0 / renderer.window.1; let screen_ratio = renderer.window.0 / renderer.window.1;
@ -215,10 +216,16 @@ impl Level {
(scale, xoff, 0) (scale, xoff, 0)
}; };
self.render_boards(renderer, scale, (xoff, yoff)); self.render_boards(renderer, scale, (xoff, yoff), block_offsets);
} }
fn render_boards(&self, renderer: &mut Renderer, scale: i32, offset: (i32, i32)) { fn render_boards(
&self,
renderer: &mut Renderer,
scale: i32,
offset: (i32, i32),
block_offsets: &BlockOffsets,
) {
let left_off = (offset.0 + 2 * scale, offset.1 + 2 * scale); let left_off = (offset.0 + 2 * scale, offset.1 + 2 * scale);
let right_off = ( let right_off = (
offset.0 + (4 + self.dimensions.0 as i32) * scale, offset.0 + (4 + self.dimensions.0 as i32) * scale,