summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-06-19 07:49:33 -0500
committertom barrett <spalf0@gmail.com>2019-06-19 07:49:33 -0500
commitb4072b110df8886089d807bcd78891430ea7f974 (patch)
tree6ebd974fa52b308f21a0f3c95c74ff98290e7afb
parent8693a266c2c9a8448b70d87d487ed211c439cbc4 (diff)
can now move in angles
-rw-r--r--src/player.rs104
-rw-r--r--src/state.rs26
2 files changed, 121 insertions, 9 deletions
diff --git a/src/player.rs b/src/player.rs
index 66e7d28..976ab05 100644
--- a/src/player.rs
+++ b/src/player.rs
@@ -1,3 +1,4 @@
+use ggez::event::KeyCode;
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam};
use ggez::nalgebra::{Point2, Vector2};
@@ -6,12 +7,14 @@ use crate::tileset::Tileset;
pub struct Player {
pub position: Point2<f32>,
+ state: PlayerState,
}
impl Player {
pub fn new() -> Player {
Player {
position: Point2::new(0.0, 0.0),
+ state: PlayerState::Idle,
}
}
@@ -23,4 +26,105 @@ impl Player {
spritebatch.add(draw_param);
}
+
+ pub fn update(&mut self) {
+ match self.state {
+ PlayerState::MovingUp => self.position.y -= constants::PLAYER_SPEED,
+ PlayerState::MovingUpLeft => {
+ self.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
+ self.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
+ }
+ PlayerState::MovingUpRight => {
+ self.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt();
+ self.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
+ }
+ PlayerState::MovingLeft => self.position.x -= constants::PLAYER_SPEED,
+ PlayerState::MovingDown => self.position.y += constants::PLAYER_SPEED,
+ PlayerState::MovingDownLeft => {
+ self.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
+ self.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt();
+ }
+ PlayerState::MovingDownRight => {
+ self.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt();
+ self.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt();
+ }
+ PlayerState::MovingRight => self.position.x += constants::PLAYER_SPEED,
+ PlayerState::Idle => (),
+ }
+ }
+
+ pub fn give_key_down(&mut self, keycode: KeyCode) {
+ let original_state = self.state.clone();
+
+ self.state = match keycode {
+ KeyCode::W => match original_state {
+ PlayerState::MovingLeft => PlayerState::MovingUpLeft,
+ PlayerState::MovingRight => PlayerState::MovingUpRight,
+ _ => PlayerState::MovingUp,
+ },
+ KeyCode::A => match original_state {
+ PlayerState::MovingUp => PlayerState::MovingUpLeft,
+ PlayerState::MovingDown => PlayerState::MovingDownLeft,
+ _ => PlayerState::MovingLeft,
+ },
+ KeyCode::S => match original_state {
+ PlayerState::MovingLeft => PlayerState::MovingDownLeft,
+ PlayerState::MovingRight => PlayerState::MovingDownRight,
+ _ => PlayerState::MovingDown,
+ },
+ KeyCode::D => match original_state {
+ PlayerState::MovingUp => PlayerState::MovingUpRight,
+ PlayerState::MovingDown => PlayerState::MovingDownRight,
+ _ => PlayerState::MovingRight,
+ },
+ _ => original_state,
+ }
+ }
+
+ pub fn give_key_up(&mut self, keycode: KeyCode) {
+ let original_state = self.state.clone();
+
+ self.state = match keycode {
+ KeyCode::W => match original_state {
+ PlayerState::MovingUpLeft => PlayerState::MovingLeft,
+ PlayerState::MovingUpRight => PlayerState::MovingRight,
+ _ => PlayerState::Idle,
+ },
+ KeyCode::A => match original_state {
+ PlayerState::MovingUpLeft => PlayerState::MovingUp,
+ PlayerState::MovingDownLeft => PlayerState::MovingDown,
+ _ => PlayerState::Idle,
+ },
+ KeyCode::S => match original_state {
+ PlayerState::MovingDownLeft => PlayerState::MovingLeft,
+ PlayerState::MovingDownRight => PlayerState::MovingRight,
+ _ => PlayerState::Idle,
+ },
+ KeyCode::D => match original_state {
+ PlayerState::MovingUpRight => PlayerState::MovingUp,
+ PlayerState::MovingDownRight => PlayerState::MovingDown,
+ _ => PlayerState::Idle,
+ },
+ _ => original_state,
+ }
+ }
+}
+
+impl Default for Player {
+ fn default() -> Self {
+ Player::new()
+ }
+}
+
+#[derive(Clone)]
+enum PlayerState {
+ Idle,
+ MovingUp,
+ MovingDown,
+ MovingLeft,
+ MovingRight,
+ MovingUpLeft,
+ MovingUpRight,
+ MovingDownLeft,
+ MovingDownRight,
}
diff --git a/src/state.rs b/src/state.rs
index d63a763..299ecc9 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -3,7 +3,6 @@ use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Imag
use ggez::{filesystem, Context, GameResult};
use crate::camera::Camera;
-use crate::constants;
use crate::map::Map;
use crate::player::Player;
use crate::tileset::Tileset;
@@ -36,6 +35,7 @@ impl State {
impl EventHandler for State {
fn update(&mut self, _: &mut Context) -> GameResult {
+ self.player.update();
self.camera.give_center(self.player.position);
Ok(())
}
@@ -59,14 +59,22 @@ impl EventHandler for State {
Ok(())
}
- fn key_down_event(&mut self, context: &mut Context, keycode: KeyCode, _: KeyMods, _: bool) {
- match keycode {
- KeyCode::W => self.player.position.y -= constants::PLAYER_SPEED,
- KeyCode::A => self.player.position.x -= constants::PLAYER_SPEED,
- KeyCode::S => self.player.position.y += constants::PLAYER_SPEED,
- KeyCode::D => self.player.position.x += constants::PLAYER_SPEED,
- KeyCode::Q => context.continuing = false,
- _ => (),
+ fn key_down_event(
+ &mut self,
+ context: &mut Context,
+ keycode: KeyCode,
+ _: KeyMods,
+ repeat: bool,
+ ) {
+ if !repeat {
+ match keycode {
+ KeyCode::Q => context.continuing = false,
+ _ => self.player.give_key_down(keycode),
+ }
}
}
+
+ fn key_up_event(&mut self, _: &mut Context, keycode: KeyCode, _: KeyMods) {
+ self.player.give_key_up(keycode)
+ }
}