From ada9131dfb915111cb40a540f6e47625e07e13c7 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Fri, 9 Aug 2019 11:01:26 -0500 Subject: [PATCH] ../.. --- src/animations.rs | 11 +++++++++-- src/game.rs | 16 +++++++++------- src/level/block.rs | 1 + src/level/mod.rs | 13 ++++++++++--- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/animations.rs b/src/animations.rs index 8ad9090..b3283df 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -1,7 +1,8 @@ use std::collections::HashMap; +use std::time::Duration; -type BlockOffsets = HashMap; -type AnimationFn = Box BlockOffsets>; +pub type BlockOffsets = HashMap; +pub type AnimationFn = Box BlockOffsets>; #[derive(Default)] pub struct AnimationState { @@ -19,4 +20,10 @@ impl AnimationState { self.progress = 0.0; self.progress_function = Some(f); } + + pub fn make_progress(&mut self, delta: Duration) {} + + pub fn is_done(&self) -> bool { + self.progress > 1.0 + } } diff --git a/src/game.rs b/src/game.rs index c161959..fbd6312 100644 --- a/src/game.rs +++ b/src/game.rs @@ -105,8 +105,10 @@ impl<'a> Game<'a> { } if self.animations.is_animating { - let delta_ms = delta.as_millis(); if self.animations.last_move_success { + if self.animations.is_done() { + self.animations.make_progress(delta); + } } else { } } else { @@ -122,17 +124,17 @@ impl<'a> Game<'a> { // failed a move if !self.animations.last_move_success { - self.animations - .begin_transition(Box::new(|mut offsets, prog| { - offsets.insert(0, (0, 0)); - offsets - })); + let func = |mut offsets: HashMap<_, _>, prog| { + offsets.insert(0, (0, 0)); + offsets + }; + self.animations.begin_transition(Box::new(func)); } } } pub fn render(&self, renderer: &mut Renderer) { let level = self.get_current_level(); - level.render(renderer); + level.render(renderer, &self.animations.block_offsets); } } diff --git a/src/level/block.rs b/src/level/block.rs index eaed205..67d7267 100644 --- a/src/level/block.rs +++ b/src/level/block.rs @@ -39,6 +39,7 @@ impl Block { .collect(); let orientation = data.orientation.into(); let color = Color::from_rgb_u32(data.color.0, data.color.1, data.color.2); + Block { index, movable, diff --git a/src/level/mod.rs b/src/level/mod.rs index 65e9591..c6a683a 100644 --- a/src/level/mod.rs +++ b/src/level/mod.rs @@ -4,6 +4,7 @@ mod player; use std::collections::{HashMap, VecDeque}; +use crate::animations::BlockOffsets; use crate::color::Color; use crate::data::LevelData; use crate::enums::{Board, Orientation, PushDir, Shape}; @@ -197,7 +198,7 @@ impl Level { Some(()) } - pub fn render(&self, renderer: &mut Renderer) { + pub fn render(&self, renderer: &mut Renderer, block_offsets: &BlockOffsets) { // board positioning calculations 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; @@ -215,10 +216,16 @@ impl Level { (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 right_off = ( offset.0 + (4 + self.dimensions.0 as i32) * scale,