diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/animations.rs (renamed from src/animation.rs) | 48 | ||||
-rw-r--r-- | src/entity.rs | 49 | ||||
-rw-r--r-- | src/layer.rs | 1 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/npc.rs | 15 | ||||
-rw-r--r-- | src/player.rs | 57 | ||||
-rw-r--r-- | src/tile.rs | 66 | ||||
-rw-r--r-- | src/tileset.rs | 6 |
8 files changed, 133 insertions, 111 deletions
diff --git a/src/animation.rs b/src/animations.rs index bc7e493..f1ec902 100644 --- a/src/animation.rs +++ b/src/animations.rs @@ -1,11 +1,13 @@ -use ggez::graphics::Rect; +use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; +use ggez::nalgebra::{Point2, Vector2}; use std::collections::HashMap; use std::time::Instant; +use crate::constants; use crate::entity::Action; use crate::tileset::Tileset; -#[derive(Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct Frame { pub source: Rect, pub delay: Option<usize>, @@ -22,17 +24,29 @@ impl Frame { } } -#[derive(Clone, PartialEq)] +impl Default for Frame { + fn default() -> Frame { + Frame::new(Rect::zero(), None, 0.0) + } +} + +#[derive(Debug, Clone, PartialEq)] pub struct Animation { pub frames: Vec<Frame>, pub timer: Instant, pub current: Frame, } +impl Default for Animation { + fn default() -> Animation { + Animation::new(Frame::default()) + } +} + impl Animation { - pub fn new() -> Animation { + pub fn new(current: Frame) -> Animation { Animation { - current: Frame::new(Rect::zero(), None, 0.0), + current, timer: Instant::now(), frames: Vec::new(), } @@ -55,9 +69,18 @@ impl Animation { self.current = self.frames[0].clone(); } } + + pub fn draw(&self, spritebatch: &mut SpriteBatch, position: Point2<f32>) { + spritebatch.add( + DrawParam::default() + .src(self.current.source) + .dest(position) + .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)), + ); + } } -#[derive(Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Animations { pub available: HashMap<Action, Animation>, pub current: Animation, @@ -73,7 +96,7 @@ impl Animations { .source .h; - let mut animation = Animation::new(); + let mut animation = Animation::new(idle.clone()); animation.give_frames(vec![idle.clone()]); available.insert(Action::IdleLeft, animation.clone()); @@ -98,15 +121,20 @@ impl Animations { Animations { available, - current: Animation::new(), + current: Animation::default(), } } pub fn update(&mut self, action: &Action) { - let animation = self.available.get(&action).cloned().unwrap(); - self.current.give_frames(animation.frames); + if let Some(animation) = self.available.get(&action).cloned() { + self.current.give_frames(animation.frames); + } self.current.update(); } + + pub fn draw(&self, spritebatch: &mut SpriteBatch, position: Point2<f32>) { + self.current.draw(spritebatch, position) + } } pub fn flip(frame: Frame) -> Frame { diff --git a/src/entity.rs b/src/entity.rs index 8086b77..ae3a236 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -1,6 +1,8 @@ use ggez::graphics::spritebatch::SpriteBatch; use ggez::nalgebra::Point2; +use crate::constants; + pub trait Operable { fn update(&mut self); fn draw(&self, spritebatch: &mut SpriteBatch); @@ -23,9 +25,54 @@ impl Entity { map_dimensions, } } + + pub fn update(&mut self) { + self.move_position(); + } + + fn move_position(&mut self) { + match self.action { + Action::MovingUp => self.position.y -= constants::PLAYER_SPEED, + Action::MovingUpLeft => { + self.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt(); + self.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt(); + } + Action::MovingUpRight => { + self.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt(); + self.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt(); + } + Action::MovingLeft => self.position.x -= constants::PLAYER_SPEED, + Action::MovingDown => self.position.y += constants::PLAYER_SPEED, + Action::MovingDownLeft => { + self.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt(); + self.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt(); + } + Action::MovingDownRight => { + self.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt(); + self.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt(); + } + Action::MovingRight => self.position.x += constants::PLAYER_SPEED, + Action::IdleLeft | Action::IdleRight => (), + } + + let pixel_width = constants::TILE_WIDTH * constants::TILE_SCALE; + let pixel_height = constants::TILE_HEIGHT * constants::TILE_SCALE; + + if self.position.x < 0.0 { + self.position.x = 0.0; + } else if self.position.x + pixel_height > self.map_dimensions.0 { + self.position.x = self.map_dimensions.0 - pixel_width; + } + + if self.position.y < 0.0 { + self.position.y = 0.0; + } else if self.position.y + pixel_height > self.map_dimensions.1 { + self.position.y = self.map_dimensions.1 - pixel_height; + } + } } -#[derive(Clone, Hash, Eq, PartialEq)] +#[derive(Clone, Hash, Eq, PartialEq, Debug)] pub enum Action { IdleLeft, IdleRight, diff --git a/src/layer.rs b/src/layer.rs index ae645fb..0247d81 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -1,5 +1,6 @@ use ggez::graphics::spritebatch::SpriteBatch; +use crate::entity::Operable; use crate::tile::Tile; use crate::tileset::Tileset; @@ -1,4 +1,4 @@ -pub mod animation; +pub mod animations; pub mod camera; pub mod constants; pub mod entity; @@ -1,9 +1,7 @@ -use ggez::graphics::{spritebatch::SpriteBatch, DrawParam}; -use ggez::nalgebra::{/*distance,*/ Point2, Vector2}; -//use std::time::Instant; +use ggez::graphics::spritebatch::SpriteBatch; +use ggez::nalgebra::Point2; -use crate::animation::Animations; -use crate::constants; +use crate::animations::Animations; use crate::entity::{Entity, Operable}; use crate::map::Map; use crate::tileset::Tileset; @@ -17,12 +15,7 @@ pub struct NPC { impl Operable for NPC { fn draw(&self, spritebatch: &mut SpriteBatch) { - spritebatch.add( - DrawParam::default() - .src(self.animations.current.current.source) - .dest(self.entity.position) - .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)), - ); + self.animations.draw(spritebatch, self.entity.position); } fn update(&mut self) { diff --git a/src/player.rs b/src/player.rs index 2941934..5993d21 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,9 +1,8 @@ use ggez::event::KeyCode; -use ggez::graphics::{spritebatch::SpriteBatch, DrawParam}; -use ggez::nalgebra::{Point2, Vector2}; +use ggez::graphics::spritebatch::SpriteBatch; +use ggez::nalgebra::Point2; -use crate::animation::Animations; -use crate::constants; +use crate::animations::Animations; use crate::entity::{Action, Entity, Operable}; use crate::tileset::Tileset; @@ -14,16 +13,11 @@ pub struct Player { impl Operable for Player { fn draw(&self, spritebatch: &mut SpriteBatch) { - spritebatch.add( - DrawParam::default() - .src(self.animations.current.current.source) - .dest(self.entity.position) - .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)), - ); + self.animations.draw(spritebatch, self.get_position()); } fn update(&mut self) { - self.move_position(); + self.entity.update(); self.animations.update(&self.entity.action); } } @@ -40,47 +34,6 @@ impl Player { self.entity.position } - fn move_position(&mut self) { - match self.entity.action { - Action::MovingUp => self.entity.position.y -= constants::PLAYER_SPEED, - Action::MovingUpLeft => { - self.entity.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt(); - self.entity.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt(); - } - Action::MovingUpRight => { - self.entity.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt(); - self.entity.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt(); - } - Action::MovingLeft => self.entity.position.x -= constants::PLAYER_SPEED, - Action::MovingDown => self.entity.position.y += constants::PLAYER_SPEED, - Action::MovingDownLeft => { - self.entity.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt(); - self.entity.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt(); - } - Action::MovingDownRight => { - self.entity.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt(); - self.entity.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt(); - } - Action::MovingRight => self.entity.position.x += constants::PLAYER_SPEED, - Action::IdleLeft | Action::IdleRight => (), - } - - let pixel_width = constants::TILE_WIDTH * constants::TILE_SCALE; - let pixel_height = constants::TILE_HEIGHT * constants::TILE_SCALE; - - if self.entity.position.x < 0.0 { - self.entity.position.x = 0.0; - } else if self.entity.position.x + pixel_height > self.entity.map_dimensions.0 { - self.entity.position.x = self.entity.map_dimensions.0 - pixel_width; - } - - if self.entity.position.y < 0.0 { - self.entity.position.y = 0.0; - } else if self.entity.position.y + pixel_height > self.entity.map_dimensions.1 { - self.entity.position.y = self.entity.map_dimensions.1 - pixel_height; - } - } - pub fn give_key_down(&mut self, keycode: KeyCode) { let original_state = self.entity.action.clone(); diff --git a/src/tile.rs b/src/tile.rs index 1f3c2f7..144e764 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,19 +1,36 @@ -use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; -use ggez::nalgebra::{Point2, Vector2}; -use std::time::Instant; +use ggez::graphics::spritebatch::SpriteBatch; +use ggez::nalgebra::Point2; +use crate::animations::{Animation, Frame}; use crate::constants; -use crate::math::{convert_angle_to_rad, flip, next_source}; +use crate::entity::Operable; +use crate::math::{convert_angle_to_rad, flip}; use crate::tileset::Tileset; -#[derive(Clone, Debug)] +#[derive(Debug, Clone)] pub struct Tile { pub id: usize, - source: Rect, - animation: Vec<(usize, Rect)>, - timer: Instant, + pub animation: Animation, pub destination: Point2<f32>, - rotation: f32, +} + +impl Operable for Tile { + fn update(&mut self) { + self.animation.update(); + } + + fn draw(&self, spritebatch: &mut SpriteBatch) { + self.animation.draw(spritebatch, self.destination); + /* + DrawParam::default() + .src(self.animation.current.source) + .dest(self.destination) + .offset(Point2::new(0.5, 0.5)) + //.rotation(self.rotation) + .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)), + ); + */ + } } impl Tile { @@ -44,37 +61,20 @@ impl Tile { let x = i as f32 % width as f32; let y = (i as f32 / height as f32).floor(); - let offset = (constants::TILE_WIDTH / 2.0) * constants::TILE_SCALE; + //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, + constants::TILE_WIDTH * constants::TILE_SCALE * x, //+ offset, + constants::TILE_HEIGHT * constants::TILE_SCALE * y, //+ offset, ); + let mut animation = Animation::new(Frame::new(source, None, rotation)); + animation.give_frames(tileset.get_frames(id)); + Tile { id, - source, - animation: tileset.get_animation(id), - timer: Instant::now(), + animation, destination, - rotation, } } - - pub fn update(&mut self) { - let (source, timer) = next_source(self.source, &self.animation, self.timer); - self.source = source; - self.timer = timer; - } - - pub fn draw(&self, spritebatch: &mut SpriteBatch) { - let draw_param = DrawParam::default() - .src(self.source) - .dest(self.destination) - .offset(Point2::new(0.5, 0.5)) - .rotation(self.rotation) - .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)); - - spritebatch.add(draw_param); - } } diff --git a/src/tileset.rs b/src/tileset.rs index 975398d..ee7128f 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -2,7 +2,7 @@ use ggez::filesystem::File; use ggez::graphics::Rect; use std::collections::HashMap; -use crate::animation::Frame; +use crate::animations::Frame; use crate::constants; use crate::property::Property; use crate::xmlelements::XMLElements; @@ -84,13 +84,13 @@ impl Tileset { .collect() } - pub fn get_animation(&self, tile_id: usize) -> Vec<(usize, Rect)> { + 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| (p.delay.unwrap(), self.get(p.tile_id))) + .map(|p| Frame::new(self.get(p.tile_id), p.delay, 0.0)) .collect() } else { Vec::new() |