diff options
author | tom barrett <spalf0@gmail.com> | 2019-07-13 09:38:02 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-07-13 09:38:02 -0500 |
commit | 9b1b040fdd9cab23c6a3d074bfd0dcd29eb8a8eb (patch) | |
tree | 5a1e64f47f47fe712512d86214f9856012b57175 | |
parent | c61c5c4352a5fe4945ea7b14793879b84baf36de (diff) |
frame delay can now be randomized
-rw-r--r-- | resources/tileset.tsx | 2 | ||||
-rw-r--r-- | src/animations.rs | 25 | ||||
-rw-r--r-- | src/tile.rs | 49 |
3 files changed, 57 insertions, 19 deletions
diff --git a/resources/tileset.tsx b/resources/tileset.tsx index 44b6778..87c6689 100644 --- a/resources/tileset.tsx +++ b/resources/tileset.tsx @@ -4,6 +4,7 @@ <tile id="14"> <properties> <property name="delay" type="int" value="100"/> + <property name="scramble_delay" type="bool" value="true"/> <property name="entity" value="flame"/> <property name="keyframe" type="int" value="0"/> </properties> @@ -17,6 +18,7 @@ <tile id="24"> <properties> <property name="delay" type="int" value="100"/> + <property name="scramble_delay" type="bool" value="true"/> <property name="entity" value="flame"/> <property name="keyframe" type="int" value="1"/> </properties> diff --git a/src/animations.rs b/src/animations.rs index d19a8ff..e2cc009 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -1,9 +1,9 @@ -use ggez::graphics::{spritebatch::SpriteBatch, DrawParam}; -use ggez::nalgebra::{Point2, Vector2}; +use ggez::graphics::spritebatch::SpriteBatch; +use ggez::nalgebra::Point2; +use rand::Rng; use std::collections::HashMap; use std::time::Instant; -use crate::constants; use crate::entity::Action; use crate::tile::{flip, Tile}; use crate::tileset::Tileset; @@ -30,7 +30,13 @@ impl Animation { pub fn update(&mut self) { if let Some(mut i) = self.frames.iter().position(|a| a == &self.current) { - if let Some(delay) = self.current.properties.delay { + if let Some(mut delay) = self.current.properties.delay { + if let Some(scramble_delay) = self.current.properties.scramble_delay { + if scramble_delay { + delay = rand::thread_rng().gen_range(delay as f32 * 0.6, delay as f32 * 1.4) + as usize; + } + } if self.timer.elapsed().as_millis() > delay as u128 { i = if i == self.frames.len() - 1 { 0 } else { i + 1 }; self.current = self.frames[i].clone(); @@ -43,16 +49,7 @@ impl Animation { } pub fn draw(&self, spritebatch: &mut SpriteBatch, position: Point2<f32>) { - if self.current.properties.visible.is_none() { - spritebatch.add( - DrawParam::default() - .src(self.current.source) - .rotation(self.current.properties.rotation) - .offset(Point2::new(0.5, 0.5)) - .dest(position) - .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)), - ); - } + self.current.draw(spritebatch, position); } } diff --git a/src/tile.rs b/src/tile.rs index 9e92fef..c86706f 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,7 +1,9 @@ -use ggez::graphics::Rect; +use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; +use ggez::nalgebra::{Point2, Vector2}; use std::f32::consts::PI; use xml::reader::XmlEvent; +use crate::constants; use crate::xmlelements::XMLElements; #[derive(Clone, Debug, PartialEq)] @@ -14,6 +16,24 @@ impl Tile { pub fn new(source: Rect, properties: Properties) -> Tile { Tile { source, properties } } + + pub fn draw(&self, spritebatch: &mut SpriteBatch, position: Point2<f32>) { + let draw = match self.properties.visible { + Some(draw) => draw, + None => true, + }; + + if draw { + spritebatch.add( + DrawParam::default() + .src(self.source) + .rotation(self.properties.rotation) + .offset(Point2::new(0.5, 0.5)) + .dest(position) + .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)), + ); + } + } } impl Default for Tile { @@ -28,6 +48,7 @@ pub struct Properties { pub rotation: f32, pub keyframe: Option<usize>, pub delay: Option<usize>, + pub scramble_delay: Option<bool>, pub spawn: Option<String>, pub visible: Option<bool>, } @@ -42,21 +63,38 @@ impl Properties { Ok(keyframe) => keyframe.parse().ok(), Err(_) => None, }; - let delay = match XMLElements::get_attribute_value(&properties_elements, "delay") { - Ok(delay) => delay.parse().ok(), - Err(_) => None, - }; let spawn = XMLElements::get_attribute_value(&properties_elements, "spawn").ok(); let visible = match XMLElements::get_attribute_value(&properties_elements, "visible") { Ok(visible) => visible.parse().ok(), Err(_) => None, }; + let delay = match XMLElements::get_attribute_value(&properties_elements, "delay") { + Ok(delay) => delay.parse().ok(), + Err(_) => None, + }; + let scramble_delay = + match XMLElements::get_attribute_value(&properties_elements, "scramble_delay") { + Ok(scramble_delay) => scramble_delay.parse().ok(), + Err(_) => None, + }; + + /* + if scramble_delay { + println!("in"); + let mut rng = rand::thread_rng(); + let d = delay.unwrap() as f32; + let normal = Normal::new(d, d * 0.50).unwrap(); + delay = Some(normal.sample(&mut rng) as usize); + println!("{:?}", delay); + } + */ Properties { rotation: 0.0, entity, keyframe, delay, + scramble_delay, spawn, visible, } @@ -70,6 +108,7 @@ impl Default for Properties { entity: None, keyframe: None, delay: None, + scramble_delay: None, spawn: None, visible: None, } |