summaryrefslogtreecommitdiff
path: root/src/entity.rs
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-07-04 07:39:51 -0500
committertom barrett <spalf0@gmail.com>2019-07-04 07:39:51 -0500
commit83ff2ad49e6d3bf83fd85fcfee68a454372bf22a (patch)
tree029ab8bd9d73652f1fabd9e34e970f16f93116b5 /src/entity.rs
parentc9ce92e1830505269b631ba1fdd864a173bacf48 (diff)
moved animation file, moved player function to enitity, tile now uses animation, move drawing from npc tile player to animation
Diffstat (limited to 'src/entity.rs')
-rw-r--r--src/entity.rs49
1 files changed, 48 insertions, 1 deletions
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,