From b4072b110df8886089d807bcd78891430ea7f974 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 19 Jun 2019 07:49:33 -0500 Subject: can now move in angles --- src/player.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) (limited to 'src/player.rs') 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, + 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, } -- cgit v1.2.3