summaryrefslogtreecommitdiff
path: root/src/entity.rs
diff options
context:
space:
mode:
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,