From ced37025a37051db896443d310f5f613f1281882 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 10 Jul 2019 09:43:48 -0500 Subject: animation now always works, added player spawn --- src/animations.rs | 18 ++++++++++-------- src/constants.rs | 2 ++ src/map.rs | 13 +++++++++---- src/npc.rs | 4 ++-- src/player.rs | 4 ++-- src/state.rs | 9 ++++++--- src/tileset.rs | 20 +++++++++++++++----- 7 files changed, 46 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/animations.rs b/src/animations.rs index 43f4326..58b6bf2 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -43,14 +43,16 @@ impl Animation { } pub fn draw(&self, spritebatch: &mut SpriteBatch, position: Point2) { - 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)), - ); + 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)), + ); + } } } diff --git a/src/constants.rs b/src/constants.rs index a1dd0eb..35ddda7 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -7,6 +7,8 @@ pub const ENTITY_SPEED: f32 = 2.5; pub const WANDER_DISTANCE: f32 = 150.0; pub const GOAL_DISTANCE: f32 = 6.0; +pub const FLOAT_PRECISION: f32 = 0.001; + pub const FLIP_H: usize = 0x8000_0000; pub const FLIP_V: usize = 0x4000_0000; pub const FLIP_D: usize = 0x2000_0000; diff --git a/src/map.rs b/src/map.rs index 8ef6f4e..124125a 100644 --- a/src/map.rs +++ b/src/map.rs @@ -61,7 +61,7 @@ impl Map { }) .collect(); - let spawns = Map::get_spawn_points(&layers, tileset.get_spawn_tiles()); + let spawns = Map::find_spawn_points(&layers, tileset.get_spawn_tiles()); Map { layers, @@ -70,7 +70,7 @@ impl Map { } } - fn get_spawn_points( + fn find_spawn_points( layers: &[Layer], spawn_tiles: HashMap, ) -> Vec<(String, Point2)> { @@ -90,8 +90,13 @@ impl Map { spawn_points } - pub fn get_spawns(&self) -> Vec<(String, Point2)> { - self.spawns.clone() + pub fn get_spawn_points(&self, name: &str) -> Vec> { + self.spawns + .clone() + .into_iter() + .filter(|s| s.0 == name) + .map(|s| s.1) + .collect() } pub fn get_dimensions(&self) -> (f32, f32) { diff --git a/src/npc.rs b/src/npc.rs index c31f305..316908b 100644 --- a/src/npc.rs +++ b/src/npc.rs @@ -72,8 +72,8 @@ impl NPC { pub fn build_npcs(tileset: &Tileset, map: &Map) -> Vec { let mut npcs = Vec::new(); - for (_name, position) in map.get_spawns() { - npcs.push(NPC::new(tileset, position, map.get_dimensions())); + for point in map.get_spawn_points("peasant") { + npcs.push(NPC::new(tileset, point, map.get_dimensions())); } npcs diff --git a/src/player.rs b/src/player.rs index 5993d21..025056c 100644 --- a/src/player.rs +++ b/src/player.rs @@ -23,9 +23,9 @@ impl Operable for Player { } impl Player { - pub fn new(tileset: &Tileset, dimensions: (f32, f32)) -> Player { + pub fn new(tileset: &Tileset, spawn: Point2, map_dimensions: (f32, f32)) -> Player { Player { - entity: Entity::new(Point2::new(0.0, 0.0), dimensions), + entity: Entity::new(spawn, map_dimensions), animations: Animations::new(tileset), } } diff --git a/src/state.rs b/src/state.rs index db72d10..98ad422 100644 --- a/src/state.rs +++ b/src/state.rs @@ -26,13 +26,16 @@ impl State { let tileset = Tileset::new(filesystem::open(context, "/tileset.tsx")?); let map = Map::new(filesystem::open(context, "/map.tmx")?, &tileset); - let map_dimensions = map.get_dimensions(); Ok(State { map: map.clone(), spritebatch: SpriteBatch::new(image), - camera: Camera::new(context, map_dimensions), - player: Player::new(&tileset, map_dimensions), + camera: Camera::new(context, map.get_dimensions()), + player: Player::new( + &tileset, + map.get_spawn_points("player")[0], + map.get_dimensions(), + ), npcs: NPC::build_npcs(&tileset, &map), }) } diff --git a/src/tileset.rs b/src/tileset.rs index 6a7ca42..effba3b 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -65,8 +65,8 @@ impl Tileset { for i in 1..8 { let (new_id, new_tile) = match i { 1 => ((id | FLIP_H), flip(tile.clone())), - 2 => ((id | FLIP_V), tile.clone()), - 3 => ((id | FLIP_D), tile.clone()), + //2 => ((id | FLIP_V), tile.clone()), + //3 => ((id | FLIP_D), tile.clone()), 4 => ((id | FLIP_D | FLIP_H), rotate(tile.clone(), 90.0)), 5 => ((id | FLIP_D | FLIP_V), rotate(tile.clone(), 270.0)), 6 => ((id | FLIP_H | FLIP_V), rotate(tile.clone(), 180.0)), @@ -95,7 +95,9 @@ impl Tileset { let first_tile = self .tiles .iter() - .find(|(id, _)| id == &&tile_id) + .find( + |(id, _)| id == &&tile_id + ) .unwrap() .1 .clone(); @@ -105,8 +107,13 @@ impl Tileset { self.tiles .values() .cloned() - .filter(|t| t.properties.entity == first_tile.properties.entity) - .collect(), + .filter(|t| { + t.properties.entity == first_tile.properties.entity + && (t.properties.rotation - first_tile.properties.rotation) < constants::FLOAT_PRECISION + && t.source.x.is_sign_positive() == first_tile.source.x.is_sign_positive() + && t.source.y.is_sign_positive() == first_tile.source.y.is_sign_positive() + }) + .collect() ) } else { Animation::new(vec![first_tile]) @@ -119,6 +126,9 @@ impl Tileset { .find(|t| { t.properties.entity == Some(entity.to_string()) && Some(keyframe) == t.properties.keyframe + && t.properties.rotation == 0.0 + && t.source.x > 0.0 + && t.source.y > 0.0 }) .unwrap() .clone() -- cgit v1.2.3