2020-01-09 16:23:20 +00:00
|
|
|
use super::utils::*;
|
2020-01-12 23:06:52 +00:00
|
|
|
use crate::comp::{ActionState, EcsStateData, MoveState, StateUpdate};
|
|
|
|
use crate::states::StateHandler;
|
2019-12-28 16:10:39 +00:00
|
|
|
|
2020-01-05 18:19:09 +00:00
|
|
|
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
|
2020-01-08 16:56:36 +00:00
|
|
|
pub struct State;
|
2019-12-26 14:43:59 +00:00
|
|
|
|
2020-01-08 16:56:36 +00:00
|
|
|
impl StateHandler for State {
|
2020-01-07 15:49:08 +00:00
|
|
|
fn new(_ecs_data: &EcsStateData) -> Self {
|
2020-01-05 23:17:22 +00:00
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:10:39 +00:00
|
|
|
fn handle(&self, ecs_data: &EcsStateData) -> StateUpdate {
|
|
|
|
let mut update = StateUpdate {
|
2019-12-26 14:43:59 +00:00
|
|
|
character: *ecs_data.character,
|
|
|
|
pos: *ecs_data.pos,
|
|
|
|
vel: *ecs_data.vel,
|
|
|
|
ori: *ecs_data.ori,
|
|
|
|
};
|
|
|
|
|
2019-12-26 18:01:19 +00:00
|
|
|
// Prevent action state handling
|
2020-01-08 19:31:42 +00:00
|
|
|
update.character.action_state = ActionState::Idle(None);
|
|
|
|
update.character.move_state = MoveState::Sit(None);
|
2019-12-26 18:01:19 +00:00
|
|
|
|
2019-12-28 16:10:39 +00:00
|
|
|
// Try to Fall
|
|
|
|
// ... maybe the ground disappears,
|
2019-12-26 14:43:59 +00:00
|
|
|
// suddenly maybe a water spell appears.
|
|
|
|
// Can't hurt to be safe :shrug:
|
|
|
|
if !ecs_data.physics.on_ground {
|
2019-12-28 16:10:39 +00:00
|
|
|
update.character.move_state = determine_fall_or_swim(ecs_data.physics);
|
|
|
|
return update;
|
2019-12-26 14:43:59 +00:00
|
|
|
}
|
2019-12-28 16:10:39 +00:00
|
|
|
// Try to jump
|
2019-12-26 14:43:59 +00:00
|
|
|
if ecs_data.inputs.jump.is_pressed() {
|
2020-01-08 19:31:42 +00:00
|
|
|
update.character.move_state = MoveState::Jump(None);
|
2019-12-26 14:43:59 +00:00
|
|
|
return update;
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:10:39 +00:00
|
|
|
// Try to Run
|
2019-12-26 14:43:59 +00:00
|
|
|
if ecs_data.inputs.move_dir.magnitude_squared() > 0.0 {
|
2020-01-08 19:31:42 +00:00
|
|
|
update.character.move_state = MoveState::Run(None);
|
2019-12-26 14:43:59 +00:00
|
|
|
return update;
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:10:39 +00:00
|
|
|
// Try to Stand
|
2019-12-26 14:43:59 +00:00
|
|
|
if ecs_data.inputs.sit.is_just_pressed() {
|
2020-01-08 19:31:42 +00:00
|
|
|
update.character.move_state = MoveState::Stand(None);
|
2019-12-26 14:43:59 +00:00
|
|
|
return update;
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:01:19 +00:00
|
|
|
// No move has occurred, keep sitting
|
2020-01-12 23:14:08 +00:00
|
|
|
update
|
2019-12-26 14:43:59 +00:00
|
|
|
}
|
|
|
|
}
|