summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--src/animations.rs5
-rw-r--r--src/layer.rs26
-rw-r--r--src/lib.rs1
-rw-r--r--src/map.rs27
-rw-r--r--src/math.rs37
-rw-r--r--src/npc.rs8
-rw-r--r--src/tile.rs13
-rw-r--r--src/tileset.rs30
9 files changed, 70 insertions, 78 deletions
diff --git a/README.md b/README.md
index 5cfb4d1..f511a69 100644
--- a/README.md
+++ b/README.md
@@ -7,3 +7,4 @@
* 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?
diff --git a/src/animations.rs b/src/animations.rs
index f1ec902..e308f1d 100644
--- a/src/animations.rs
+++ b/src/animations.rs
@@ -1,6 +1,7 @@
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect};
use ggez::nalgebra::{Point2, Vector2};
use std::collections::HashMap;
+use std::f32::consts::PI;
use std::time::Instant;
use crate::constants;
@@ -143,3 +144,7 @@ pub fn flip(frame: Frame) -> Frame {
f.source.x -= frame.source.w;
f
}
+
+pub fn convert_angle_to_rad(angle: f32) -> f32 {
+ angle * (PI / 180.0)
+}
diff --git a/src/layer.rs b/src/layer.rs
index 0247d81..ef21059 100644
--- a/src/layer.rs
+++ b/src/layer.rs
@@ -11,6 +11,20 @@ pub struct Layer {
height: usize,
}
+impl Operable for Layer {
+ fn update(&mut self) {
+ for tile in self.tiles.iter_mut() {
+ tile.update();
+ }
+ }
+
+ fn draw(&self, spritebatch: &mut SpriteBatch) {
+ for tile in self.tiles.iter() {
+ tile.draw(spritebatch);
+ }
+ }
+}
+
impl Layer {
pub fn new(text: &str, tileset: &Tileset, width: usize, height: usize) -> Layer {
Layer {
@@ -24,16 +38,4 @@ impl Layer {
height,
}
}
-
- pub fn update(&mut self) {
- for tile in self.tiles.iter_mut() {
- tile.update();
- }
- }
-
- pub fn draw(&self, spritebatch: &mut SpriteBatch) {
- for tile in self.tiles.iter() {
- tile.draw(spritebatch);
- }
- }
}
diff --git a/src/lib.rs b/src/lib.rs
index ed8948f..e00c17c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,7 +4,6 @@ pub mod constants;
pub mod entity;
pub mod layer;
pub mod map;
-pub mod math;
pub mod npc;
pub mod player;
pub mod property;
diff --git a/src/map.rs b/src/map.rs
index fe51242..a3ddfb3 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -4,6 +4,7 @@ use ggez::nalgebra::Point2;
use xml::reader::XmlEvent::Characters;
use crate::constants;
+use crate::entity::Operable;
use crate::layer::Layer;
use crate::tileset::Tileset;
use crate::xmlelements::XMLElements;
@@ -16,6 +17,20 @@ pub struct Map {
spawns: Vec<(String, Point2<f32>)>,
}
+impl Operable for Map {
+ fn draw(&self, spritebatch: &mut SpriteBatch) {
+ for layer in self.layers.iter() {
+ layer.draw(spritebatch);
+ }
+ }
+
+ fn update(&mut self) {
+ for layer in self.layers.iter_mut() {
+ layer.update();
+ }
+ }
+}
+
impl Map {
pub fn new(file: File, tileset: &Tileset) -> Map {
let elements = XMLElements::new(file);
@@ -72,18 +87,6 @@ impl Map {
spawn_points
}
- pub fn draw(&self, spritebatch: &mut SpriteBatch) {
- for layer in self.layers.iter() {
- layer.draw(spritebatch);
- }
- }
-
- pub fn update(&mut self) {
- for layer in self.layers.iter_mut() {
- layer.update();
- }
- }
-
pub fn get_spawns(&self) -> Vec<(String, Point2<f32>)> {
self.spawns.clone()
}
diff --git a/src/math.rs b/src/math.rs
deleted file mode 100644
index ff652ea..0000000
--- a/src/math.rs
+++ /dev/null
@@ -1,37 +0,0 @@
-use ggez::graphics::Rect;
-use ggez::nalgebra::Point2;
-use rand::Rng;
-use std::f32::consts::PI;
-use std::time::Instant;
-
-pub fn convert_angle_to_rad(angle: f32) -> f32 {
- angle * (PI / 180.0)
-}
-
-pub fn flip(rect: Rect) -> Rect {
- let mut r = rect;
- r.x *= -1.0;
- r.x -= rect.w;
- r
-}
-
-pub fn next_source(source: Rect, animation: &[(usize, Rect)], timer: Instant) -> (Rect, Instant) {
- if let Some(mut i) = animation.iter().position(|a| a.1 == source) {
- if timer.elapsed().as_millis() > animation[i].0 as u128 {
- i = if i == animation.len() - 1 { 0 } else { i + 1 };
- (animation[i].1, Instant::now())
- } else {
- (source, timer)
- }
- } else if !animation.is_empty() {
- (animation[0].1, timer)
- } else {
- (source, timer)
- }
-}
-
-pub fn random_nearby_point(origin: Point2<f32>, within_radius: f32) -> Point2<f32> {
- let w = within_radius * rand::thread_rng().gen_range(0.0, 1.0);
- let t = 2.0 * PI * rand::thread_rng().gen_range(0.0, 1.0);
- Point2::new(origin.x + w * t.cos(), origin.y + w * t.sin())
-}
diff --git a/src/npc.rs b/src/npc.rs
index 9316e69..c31f305 100644
--- a/src/npc.rs
+++ b/src/npc.rs
@@ -1,5 +1,7 @@
use ggez::graphics::spritebatch::SpriteBatch;
use ggez::nalgebra::Point2;
+use rand::Rng;
+use std::f32::consts::PI;
use crate::animations::Animations;
use crate::entity::{Entity, Operable};
@@ -83,3 +85,9 @@ enum Behavior {
//Waiting(Instant),
Wandering(Option<Point2<f32>>),
}
+
+pub fn random_nearby_point(origin: Point2<f32>, within_radius: f32) -> Point2<f32> {
+ let w = within_radius * rand::thread_rng().gen_range(0.0, 1.0);
+ let t = 2.0 * PI * rand::thread_rng().gen_range(0.0, 1.0);
+ Point2::new(origin.x + w * t.cos(), origin.y + w * t.sin())
+}
diff --git a/src/tile.rs b/src/tile.rs
index 144e764..b00f55e 100644
--- a/src/tile.rs
+++ b/src/tile.rs
@@ -1,10 +1,9 @@
use ggez::graphics::spritebatch::SpriteBatch;
use ggez::nalgebra::Point2;
-use crate::animations::{Animation, Frame};
+use crate::animations::{convert_angle_to_rad, flip, Animation, Frame};
use crate::constants;
use crate::entity::Operable;
-use crate::math::{convert_angle_to_rad, flip};
use crate::tileset::Tileset;
#[derive(Debug, Clone)]
@@ -37,6 +36,7 @@ impl Tile {
pub fn new(text: &str, i: usize, tileset: &Tileset, width: usize, height: usize) -> Tile {
let id = text.parse::<usize>().unwrap();
+ /*
let flip_d = (id & constants::FLIP_DIAGONAL_FLAG) == constants::FLIP_DIAGONAL_FLAG;
let flip_h = (id & constants::FLIP_HORIZONTAL_FLAG) == constants::FLIP_HORIZONTAL_FLAG;
let flip_v = (id & constants::FLIP_VERTICAL_FLAG) == constants::FLIP_VERTICAL_FLAG;
@@ -58,6 +58,7 @@ impl Tile {
//(false, false, false) => (),
_ => (tileset.get(id), 0.0),
};
+ */
let x = i as f32 % width as f32;
let y = (i as f32 / height as f32).floor();
@@ -68,12 +69,16 @@ impl Tile {
constants::TILE_HEIGHT * constants::TILE_SCALE * y, //+ offset,
);
- let mut animation = Animation::new(Frame::new(source, None, rotation));
+ //let mut animation = Animation::new(Frame::new(source, None, rotation));
+ /*
+ let frame = tileset.get_frame(id);
+ let mut animation = Animation::default();
animation.give_frames(tileset.get_frames(id));
+ */
Tile {
id,
- animation,
+ animation: tileset.get_animation(id),
destination,
}
}
diff --git a/src/tileset.rs b/src/tileset.rs
index ee7128f..70095b8 100644
--- a/src/tileset.rs
+++ b/src/tileset.rs
@@ -2,13 +2,13 @@ use ggez::filesystem::File;
use ggez::graphics::Rect;
use std::collections::HashMap;
-use crate::animations::Frame;
+use crate::animations::{Animation, Frame};
use crate::constants;
use crate::property::Property;
use crate::xmlelements::XMLElements;
pub struct Tileset {
- tiles: HashMap<usize, Rect>,
+ tiles: HashMap<usize, Frame>,
properties: Vec<Property>,
}
@@ -30,7 +30,7 @@ impl Tileset {
let rows = height / (constants::TILE_HEIGHT as usize);
let mut tiles = HashMap::new();
- tiles.insert(0, Rect::zero());
+ tiles.insert(0, Frame::default());
let w = 1.0 / columns as f32;
let h = 1.0 / rows as f32;
@@ -39,7 +39,7 @@ impl Tileset {
for c in 0..columns {
let x = c as f32 / columns as f32;
let y = r as f32 / rows as f32;
- tiles.insert(key, Rect::new(x, y, w, h));
+ tiles.insert(key, Frame::new(Rect::new(x, y, w, h), None, 0.0));
key += 1;
}
}
@@ -65,14 +65,12 @@ impl Tileset {
.collect();
for i in invisible {
- *tiles.get_mut(&i).unwrap() = Rect::zero();
+ *tiles.get_mut(&i).unwrap() = Frame::default();
}
- Tileset { tiles, properties }
- }
+ for tile in &tiles {}
- pub fn get(&self, id: usize) -> Rect {
- *self.tiles.get(&id).unwrap()
+ Tileset { tiles, properties }
}
pub fn get_spawn_tiles(&self) -> Vec<(String, usize)> {
@@ -84,18 +82,24 @@ impl Tileset {
.collect()
}
+ /*
pub fn get_frames(&self, tile_id: usize) -> Vec<Frame> {
if let Some(property) = self.properties.iter().find(|p| p.tile_id == tile_id) {
self.properties
.clone()
.into_iter()
.filter(|p| p.entity == property.entity && p.entity.is_some())
- .map(|p| Frame::new(self.get(p.tile_id), p.delay, 0.0))
+ .map(|p| Frame::new(*self.tiles.get(&p.tile_id).unwrap(), p.delay, 0.0))
.collect()
} else {
Vec::new()
}
}
+ */
+
+ pub fn get_animation(&self, tile_id: usize) -> Animation {
+ Animation::default()
+ }
pub fn get_frame_by_entity_keyframe(&self, entity: &str, keyframe: usize) -> Frame {
let tile_id = &self
@@ -105,6 +109,7 @@ impl Tileset {
.unwrap()
.tile_id;
+ /*
let delay = self
.properties
.iter()
@@ -112,8 +117,9 @@ impl Tileset {
.unwrap()
.delay;
- let source = self.tiles.get(tile_id).unwrap();
-
Frame::new(*source, delay, 0.0)
+ */
+
+ self.tiles.get(tile_id).unwrap().clone()
}
}