From 83ff2ad49e6d3bf83fd85fcfee68a454372bf22a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 4 Jul 2019 07:39:51 -0500 Subject: moved animation file, moved player function to enitity, tile now uses animation, move drawing from npc tile player to animation --- src/entity.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'src/entity.rs') 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, -- cgit v1.2.3