summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-07-07 11:45:49 -0500
committertom barrett <spalf0@gmail.com>2019-07-07 11:45:49 -0500
commitde4e8c1f0b82627fd1980401ae63472f49c9d89c (patch)
treebcbffde08ac2e5bf843abee0dc61c2d7e20fd03b
parentbfdaef7850b6ac17bb88f1b314236fb5014aac8e (diff)
reorganized
-rw-r--r--README.md5
-rw-r--r--src/animations.rs5
-rw-r--r--src/cell.rs59
-rw-r--r--src/layer.rs2
-rw-r--r--src/lib.rs2
-rw-r--r--src/map.rs6
-rw-r--r--src/property.rs57
-rw-r--r--src/tile.rs114
-rw-r--r--src/tileset.rs68
9 files changed, 157 insertions, 161 deletions
diff --git a/README.md b/README.md
index e074b21..90e9eca 100644
--- a/README.md
+++ b/README.md
@@ -5,8 +5,3 @@
* collision
* fat binary (include-bytes!)
* textbox (with dialog options)
-
-* have tile use the animation struct
-* have the tileset have the translated ids within the hashmap
-* merge the Tile struct and the Frame struct?
-* rename property to properties
diff --git a/src/animations.rs b/src/animations.rs
index 5e5740a..be10f31 100644
--- a/src/animations.rs
+++ b/src/animations.rs
@@ -5,7 +5,8 @@ use std::time::Instant;
use crate::constants;
use crate::entity::Action;
-use crate::tileset::{Tile, Tileset};
+use crate::tile::Tile;
+use crate::tileset::Tileset;
#[derive(Debug, Clone, PartialEq)]
pub struct Animation {
@@ -29,7 +30,7 @@ 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.property.delay {
+ if let Some(delay) = self.current.properties.delay {
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();
diff --git a/src/cell.rs b/src/cell.rs
new file mode 100644
index 0000000..13d095f
--- /dev/null
+++ b/src/cell.rs
@@ -0,0 +1,59 @@
+use ggez::graphics::spritebatch::SpriteBatch;
+use ggez::nalgebra::Point2;
+
+use crate::animations::Animation;
+use crate::constants;
+use crate::entity::Operable;
+use crate::tileset::Tileset;
+
+#[derive(Debug, Clone)]
+pub struct Cell {
+ pub id: usize,
+ pub animation: Animation,
+ pub destination: Point2<f32>,
+}
+
+impl Operable for Cell {
+ fn update(&mut self) {
+ self.animation.update();
+ }
+
+ fn draw(&self, spritebatch: &mut SpriteBatch) {
+ self.animation.draw(spritebatch, self.destination);
+ }
+}
+
+impl Cell {
+ pub fn new(text: &str, i: usize, tileset: &Tileset, dimensions: (usize, usize)) -> Cell {
+ let id = text.parse::<usize>().unwrap();
+
+ /*
+ let (source, rotation) = match (flip_d, flip_h, flip_v) {
+ (true, true, true) => (flip(tileset.get(id)), convert_angle_to_rad(90.0)),
+ (true, true, false) => (tileset.get(id), convert_angle_to_rad(90.0)),
+ (true, false, true) => (tileset.get(id), convert_angle_to_rad(270.0)),
+ //(true, false, false) => (),
+ (false, true, true) => (tileset.get(id), convert_angle_to_rad(180.0)),
+ (false, true, false) => (flip(tileset.get(id)), 0.0),
+ //(false, false, true) => (),
+ //(false, false, false) => (),
+ _ => (tileset.get(id), 0.0),
+ };
+ */
+
+ let x = i as f32 % dimensions.0 as f32;
+ let y = (i as f32 / dimensions.1 as f32).floor();
+ //let offset = (constants::TILE_WIDTH / 2.0) * constants::TILE_SCALE;
+
+ let destination = Point2::new(
+ constants::TILE_WIDTH * constants::TILE_SCALE * x, //+ offset,
+ constants::TILE_HEIGHT * constants::TILE_SCALE * y, //+ offset,
+ );
+
+ Cell {
+ id,
+ animation: tileset.get_animation(id),
+ destination,
+ }
+ }
+}
diff --git a/src/layer.rs b/src/layer.rs
index 2ae9dc2..05e4d1a 100644
--- a/src/layer.rs
+++ b/src/layer.rs
@@ -1,7 +1,7 @@
use ggez::graphics::spritebatch::SpriteBatch;
+use crate::cell::Cell;
use crate::entity::Operable;
-use crate::tile::Cell;
use crate::tileset::Tileset;
#[derive(Debug, Clone)]
diff --git a/src/lib.rs b/src/lib.rs
index e00c17c..4ecd991 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,12 +1,12 @@
pub mod animations;
pub mod camera;
+pub mod cell;
pub mod constants;
pub mod entity;
pub mod layer;
pub mod map;
pub mod npc;
pub mod player;
-pub mod property;
pub mod state;
pub mod tile;
pub mod tileset;
diff --git a/src/map.rs b/src/map.rs
index 7042f58..8ef6f4e 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -7,7 +7,8 @@ use xml::reader::XmlEvent::Characters;
use crate::constants;
use crate::entity::Operable;
use crate::layer::Layer;
-use crate::tileset::{Tile, Tileset};
+use crate::tile::Tile;
+use crate::tileset::Tileset;
use crate::xmlelements::XMLElements;
#[derive(Clone)]
@@ -79,7 +80,8 @@ impl Map {
for cell in layer.cells.iter() {
for (id, tile) in spawn_tiles.iter() {
if id == &cell.id {
- spawn_points.push((tile.property.spawn.clone().unwrap(), cell.destination));
+ spawn_points
+ .push((tile.properties.spawn.clone().unwrap(), cell.destination));
}
}
}
diff --git a/src/property.rs b/src/property.rs
deleted file mode 100644
index b57d54c..0000000
--- a/src/property.rs
+++ /dev/null
@@ -1,57 +0,0 @@
-use xml::reader::XmlEvent;
-
-use crate::xmlelements::XMLElements;
-
-#[derive(Debug, Clone, PartialEq)]
-pub struct Property {
- pub entity: Option<String>,
- pub rotation: f32,
- pub keyframe: Option<usize>,
- pub delay: Option<usize>,
- pub spawn: Option<String>,
- pub visible: Option<bool>,
-}
-
-impl Property {
- pub fn new(property_elements: Vec<XmlEvent>) -> Property {
- let entity = match XMLElements::get_attribute_value(&property_elements, "entity") {
- Ok(entity) => entity.parse().ok(),
- Err(_) => None,
- };
- let keyframe = match XMLElements::get_attribute_value(&property_elements, "keyframe") {
- Ok(keyframe) => keyframe.parse().ok(),
- Err(_) => None,
- };
- let delay = match XMLElements::get_attribute_value(&property_elements, "delay") {
- Ok(delay) => delay.parse().ok(),
- Err(_) => None,
- };
- let spawn = XMLElements::get_attribute_value(&property_elements, "spawn").ok();
- let visible = match XMLElements::get_attribute_value(&property_elements, "visible") {
- Ok(visible) => visible.parse().ok(),
- Err(_) => None,
- };
-
- Property {
- rotation: 0.0,
- entity,
- keyframe,
- delay,
- spawn,
- visible,
- }
- }
-}
-
-impl Default for Property {
- fn default() -> Property {
- Property {
- rotation: 0.0,
- entity: None,
- keyframe: None,
- delay: None,
- spawn: None,
- visible: None,
- }
- }
-}
diff --git a/src/tile.rs b/src/tile.rs
index 13d095f..de67cdc 100644
--- a/src/tile.rs
+++ b/src/tile.rs
@@ -1,59 +1,81 @@
-use ggez::graphics::spritebatch::SpriteBatch;
-use ggez::nalgebra::Point2;
-
-use crate::animations::Animation;
-use crate::constants;
-use crate::entity::Operable;
-use crate::tileset::Tileset;
-
-#[derive(Debug, Clone)]
-pub struct Cell {
- pub id: usize,
- pub animation: Animation,
- pub destination: Point2<f32>,
+use ggez::graphics::Rect;
+use xml::reader::XmlEvent;
+
+use crate::xmlelements::XMLElements;
+
+#[derive(Clone, Debug, PartialEq)]
+pub struct Tile {
+ pub source: Rect,
+ pub properties: Properties,
}
-impl Operable for Cell {
- fn update(&mut self) {
- self.animation.update();
+impl Tile {
+ pub fn new(source: Rect, properties: Properties) -> Tile {
+ Tile { source, properties }
}
- fn draw(&self, spritebatch: &mut SpriteBatch) {
- self.animation.draw(spritebatch, self.destination);
+ pub fn flip(&mut self) {
+ self.source.x *= -1.0;
+ self.source.x -= self.source.w;
}
}
-impl Cell {
- pub fn new(text: &str, i: usize, tileset: &Tileset, dimensions: (usize, usize)) -> Cell {
- let id = text.parse::<usize>().unwrap();
-
- /*
- let (source, rotation) = match (flip_d, flip_h, flip_v) {
- (true, true, true) => (flip(tileset.get(id)), convert_angle_to_rad(90.0)),
- (true, true, false) => (tileset.get(id), convert_angle_to_rad(90.0)),
- (true, false, true) => (tileset.get(id), convert_angle_to_rad(270.0)),
- //(true, false, false) => (),
- (false, true, true) => (tileset.get(id), convert_angle_to_rad(180.0)),
- (false, true, false) => (flip(tileset.get(id)), 0.0),
- //(false, false, true) => (),
- //(false, false, false) => (),
- _ => (tileset.get(id), 0.0),
- };
- */
+impl Default for Tile {
+ fn default() -> Tile {
+ Tile::new(Rect::zero(), Properties::default())
+ }
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct Properties {
+ pub entity: Option<String>,
+ pub rotation: f32,
+ pub keyframe: Option<usize>,
+ pub delay: Option<usize>,
+ pub spawn: Option<String>,
+ pub visible: Option<bool>,
+}
- let x = i as f32 % dimensions.0 as f32;
- let y = (i as f32 / dimensions.1 as f32).floor();
- //let offset = (constants::TILE_WIDTH / 2.0) * constants::TILE_SCALE;
+impl Properties {
+ pub fn new(properties_elements: Vec<XmlEvent>) -> Properties {
+ let entity = match XMLElements::get_attribute_value(&properties_elements, "entity") {
+ Ok(entity) => entity.parse().ok(),
+ Err(_) => None,
+ };
+ let keyframe = match XMLElements::get_attribute_value(&properties_elements, "keyframe") {
+ 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 destination = Point2::new(
- constants::TILE_WIDTH * constants::TILE_SCALE * x, //+ offset,
- constants::TILE_HEIGHT * constants::TILE_SCALE * y, //+ offset,
- );
+ Properties {
+ rotation: 0.0,
+ entity,
+ keyframe,
+ delay,
+ spawn,
+ visible,
+ }
+ }
+}
- Cell {
- id,
- animation: tileset.get_animation(id),
- destination,
+impl Default for Properties {
+ fn default() -> Properties {
+ Properties {
+ rotation: 0.0,
+ entity: None,
+ keyframe: None,
+ delay: None,
+ spawn: None,
+ visible: None,
}
}
}
diff --git a/src/tileset.rs b/src/tileset.rs
index f576e08..bb64c12 100644
--- a/src/tileset.rs
+++ b/src/tileset.rs
@@ -5,39 +5,9 @@ use std::f32::consts::PI;
use crate::animations::Animation;
use crate::constants::{self, FLIP_A, FLIP_D, FLIP_H, FLIP_V};
-use crate::property::Property;
+use crate::tile::{Properties, Tile};
use crate::xmlelements::XMLElements;
-#[derive(Clone, Debug, PartialEq)]
-pub struct Tile {
- pub source: Rect,
- pub property: Property,
-}
-
-impl Tile {
- pub fn new(source: Rect, property: Property) -> Tile {
- Tile { source, property }
- }
-
- pub fn flip(&mut self) {
- self.source.x *= -1.0;
- self.source.x -= self.source.w;
- }
-}
-
-fn flip(tile: Tile) -> Tile {
- let mut t = tile.clone();
- t.source.x *= -1.0;
- t.source.x -= t.source.w;
- t
-}
-
-impl Default for Tile {
- fn default() -> Tile {
- Tile::new(Rect::zero(), Property::default())
- }
-}
-
pub struct Tileset {
tiles: HashMap<usize, Tile>,
}
@@ -79,14 +49,14 @@ impl Tileset {
== id
});
- let property = match tile_element {
+ let properties = match tile_element {
Some(tile_element) => {
- Property::new(elements.get_children(&tile_element, "property"))
+ Properties::new(elements.get_children(&tile_element, "property"))
}
- None => Property::default(),
+ None => Properties::default(),
};
- tiles.insert(id, Tile::new(Rect::new(x, y, w, h), property));
+ tiles.insert(id, Tile::new(Rect::new(x, y, w, h), properties));
id += 1;
}
}
@@ -117,7 +87,7 @@ impl Tileset {
self.tiles
.clone()
.into_iter()
- .filter(|(_, t)| t.property.spawn.is_some())
+ .filter(|(_, t)| t.properties.spawn.is_some())
.collect()
}
@@ -130,12 +100,12 @@ impl Tileset {
.1
.clone();
- if first_tile.property.entity.is_some() {
+ if first_tile.properties.entity.is_some() {
Animation::new(
self.tiles
.values()
.cloned()
- .filter(|t| t.property.entity == first_tile.property.entity)
+ .filter(|t| t.properties.entity == first_tile.properties.entity)
.collect(),
)
} else {
@@ -144,20 +114,24 @@ impl Tileset {
}
pub fn get_tile_by_entity_keyframe(&self, entity: &str, keyframe: usize) -> Tile {
- let tile_id = self
- .tiles
- .iter()
- .find(|(_, t)| {
- t.property.entity == Some(entity.to_string())
- && Some(keyframe) == t.property.keyframe
+ self.tiles
+ .values()
+ .find(|t| {
+ t.properties.entity == Some(entity.to_string())
+ && Some(keyframe) == t.properties.keyframe
})
.unwrap()
- .0;
-
- self.tiles.get(tile_id).unwrap().clone()
+ .clone()
}
}
pub fn convert_angle_to_rad(angle: f32) -> f32 {
angle * (PI / 180.0)
}
+
+fn flip(tile: Tile) -> Tile {
+ let mut t = tile.clone();
+ t.source.x *= -1.0;
+ t.source.x -= t.source.w;
+ t
+}