summaryrefslogtreecommitdiff
path: root/src/player.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/player.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/player.rs')
-rw-r--r--src/player.rs57
1 files changed, 5 insertions, 52 deletions
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();