Update controller

This commit is contained in:
Adam Whitehurst 2019-12-09 14:45:10 +00:00 committed by Acrimon
parent 47b480eac6
commit 20575e0aab
5 changed files with 61 additions and 43 deletions

View File

@ -17,11 +17,24 @@ pub enum MovementState {
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum ActionState { pub enum ActionState {
Idle, Idle,
Wield { time_left: Duration }, Wield {
Attack { time_left: Duration, applied: bool }, time_left: Duration,
Block { time_active: Duration }, },
Roll { time_left: Duration }, Attack {
Charge { time_left: Duration }, time_left: Duration,
applied: bool,
},
Block {
time_active: Duration,
},
Roll {
time_left: Duration,
// Whether character was wielding before they started roll
was_wielding: bool,
},
Charge {
time_left: Duration,
},
//Carry, //Carry,
} }
@ -38,7 +51,7 @@ impl ActionState {
match self { match self {
Self::Wield { time_left } Self::Wield { time_left }
| Self::Attack { time_left, .. } | Self::Attack { time_left, .. }
| Self::Roll { time_left } | Self::Roll { time_left, .. }
| Self::Charge { time_left } => *time_left == Duration::default(), | Self::Charge { time_left } => *time_left == Duration::default(),
Self::Idle | Self::Block { .. } => false, Self::Idle | Self::Block { .. } => false,
} }

View File

@ -329,20 +329,17 @@ impl<'a> System<'a> for Sys {
continue; continue;
} }
inputs.update_look_dir();
inputs.update_move_dir();
match (character.action, character.movement) { match (character.action, character.movement) {
// Jumping, one frame state that calls jump server event // Jumping, one frame state that calls jump server event
(_, Jump) => { (_, Jump) => {
inputs.update_look_dir();
inputs.update_move_dir();
character.movement = get_state_from_move_dir(&inputs.move_dir);
character.movement = Fall; character.movement = Fall;
local_emitter.emit(LocalEvent::Jump(entity)); local_emitter.emit(LocalEvent::Jump(entity));
} }
// Charging + Any Movement, prioritizes finishing charge // Charging + Any Movement, prioritizes finishing charge
// over movement states // over movement states
(Charge { time_left }, _) => { (Charge { time_left }, _) => {
println!("{:?}", character);
inputs.update_move_dir(); inputs.update_move_dir();
if time_left == Duration::default() || vel.0.magnitude_squared() < 10.0 { if time_left == Duration::default() || vel.0.magnitude_squared() < 10.0 {
character.action = try_wield(stats); character.action = try_wield(stats);
@ -367,22 +364,35 @@ impl<'a> System<'a> for Sys {
} }
// Rolling + Any Movement, prioritizes finishing charge // Rolling + Any Movement, prioritizes finishing charge
// over movement states // over movement states
(Roll { time_left }, _) => { (
Roll {
time_left,
was_wielding,
},
_,
) => {
if time_left == Duration::default() { if time_left == Duration::default() {
character.action = try_wield(stats); if was_wielding {
character.action = try_wield(stats);
} else {
character.action = Idle;
}
} else { } else {
character.action = Roll { character.action = Roll {
time_left: time_left time_left: time_left
.checked_sub(Duration::from_secs_f32(dt.0)) .checked_sub(Duration::from_secs_f32(dt.0))
.unwrap_or_default(), .unwrap_or_default(),
was_wielding,
} }
} }
} }
// Any Action + Falling // Any Action + Falling
(action_state, Fall) => { (action_state, Fall) => {
inputs.update_move_dir(); // character.movement = get_state_from_move_dir(&inputs.move_dir);
inputs.update_look_dir(); if inputs.glide.is_pressed() && !inputs.glide.is_held_down() {
character.movement = get_state_from_move_dir(&inputs.move_dir); character.movement = Glide;
continue;
}
// Reset to Falling while not standing on ground, // Reset to Falling while not standing on ground,
// otherwise keep the state given above // otherwise keep the state given above
if !physics.on_ground { if !physics.on_ground {
@ -391,9 +401,8 @@ impl<'a> System<'a> for Sys {
} else { } else {
character.movement = Fall; character.movement = Fall;
} }
} } else {
if inputs.glide.is_pressed() && !inputs.glide.is_held_down() { character.movement = Stand;
character.movement = Glide;
continue; continue;
} }
@ -420,8 +429,6 @@ impl<'a> System<'a> for Sys {
} }
// Any Action + Swimming // Any Action + Swimming
(_action_state, Swim) => { (_action_state, Swim) => {
inputs.update_move_dir();
inputs.update_look_dir();
character.movement = get_state_from_move_dir(&inputs.move_dir); character.movement = get_state_from_move_dir(&inputs.move_dir);
if !physics.on_ground && physics.in_fluid { if !physics.on_ground && physics.in_fluid {
@ -451,7 +458,6 @@ impl<'a> System<'a> for Sys {
} }
// Blocking, restricted look_dir compared to other states // Blocking, restricted look_dir compared to other states
(Block { .. }, Stand) | (Block { .. }, Run) => { (Block { .. }, Stand) | (Block { .. }, Run) => {
inputs.update_move_dir();
character.movement = get_state_from_move_dir(&inputs.move_dir); character.movement = get_state_from_move_dir(&inputs.move_dir);
if !inputs.secondary.is_pressed() { if !inputs.secondary.is_pressed() {
@ -474,8 +480,6 @@ impl<'a> System<'a> for Sys {
} }
// Standing and Running states, typical states :shrug: // Standing and Running states, typical states :shrug:
(action_state, Run) | (action_state, Stand) => { (action_state, Run) | (action_state, Stand) => {
inputs.update_move_dir();
inputs.update_look_dir();
character.movement = get_state_from_move_dir(&inputs.move_dir); character.movement = get_state_from_move_dir(&inputs.move_dir);
// Try to sit // Try to sit
if inputs.sit.is_pressed() && physics.on_ground && body.is_humanoid() { if inputs.sit.is_pressed() && physics.on_ground && body.is_humanoid() {
@ -522,6 +526,7 @@ impl<'a> System<'a> for Sys {
{ {
character.action = Roll { character.action = Roll {
time_left: ROLL_DURATION, time_left: ROLL_DURATION,
was_wielding: character.action.is_wield(),
}; };
continue; continue;
} }
@ -552,6 +557,7 @@ impl<'a> System<'a> for Sys {
} }
Idle => { Idle => {
character.action = try_wield(stats); character.action = try_wield(stats);
continue;
} }
Charge { .. } | Roll { .. } | Block { .. } => {} Charge { .. } | Roll { .. } | Block { .. } => {}
} }
@ -579,8 +585,8 @@ impl<'a> System<'a> for Sys {
} }
} }
// Sitting // Sitting
(Idle, Sit) => { (_, Sit) => {
inputs.update_move_dir(); character.action = Idle;
character.movement = get_state_from_move_dir(&inputs.move_dir); character.movement = get_state_from_move_dir(&inputs.move_dir);
// character.movement will be Stand after updating when // character.movement will be Stand after updating when
@ -601,9 +607,6 @@ impl<'a> System<'a> for Sys {
(_, Glide) => { (_, Glide) => {
character.action = Idle; character.action = Idle;
inputs.update_look_dir();
inputs.update_move_dir();
if !inputs.glide.is_pressed() { if !inputs.glide.is_pressed() {
character.movement = Fall; character.movement = Fall;
} else if let Some(_wall_dir) = physics.on_wall { } else if let Some(_wall_dir) = physics.on_wall {
@ -617,22 +620,23 @@ impl<'a> System<'a> for Sys {
// Any Action + Climbing, shouldnt care about action, // Any Action + Climbing, shouldnt care about action,
// because should be Idle // because should be Idle
(_, Climb) => { (_, Climb) => {
character.action = Idle;
if let None = physics.on_wall { if let None = physics.on_wall {
if physics.on_ground { if inputs.jump.is_pressed() {
character.movement = Stand;
} else if inputs.jump.is_pressed() && !inputs.jump.is_held_down() {
character.movement = Jump; character.movement = Jump;
} else { } else {
character.movement = Fall; character.movement = Fall;
} }
} }
} if physics.on_ground {
// In case of adding new states character.movement = Stand;
(_, _) => { }
println!("UNKNOWN STATE"); } // In case of adding new states
character.action = Idle; // (_, _) => {
character.movement = Fall; // println!("UNKNOWN STATE");
} // character.action = Idle;
// character.movement = Fall;
// }
}; };
// Process other controller events // Process other controller events

View File

@ -12,7 +12,7 @@ use sphynx::Uid;
use std::time::Duration; use std::time::Duration;
use vek::*; use vek::*;
pub const ROLL_DURATION: Duration = Duration::from_millis(250); pub const ROLL_DURATION: Duration = Duration::from_millis(600);
const HUMANOID_ACCEL: f32 = 50.0; const HUMANOID_ACCEL: f32 = 50.0;
const HUMANOID_SPEED: f32 = 120.0; const HUMANOID_SPEED: f32 = 120.0;
@ -150,7 +150,7 @@ impl<'a> System<'a> for Sys {
(false, Glide) if vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0) => { (false, Glide) if vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0) => {
GLIDE_ACCEL GLIDE_ACCEL
} }
(false, Jump) (false, Fall) | (false, Jump)
if vel.0.magnitude_squared() < HUMANOID_AIR_SPEED.powf(2.0) => if vel.0.magnitude_squared() < HUMANOID_AIR_SPEED.powf(2.0) =>
{ {
HUMANOID_AIR_ACCEL HUMANOID_AIR_ACCEL

View File

@ -307,6 +307,7 @@ mod tests {
&CharacterState { &CharacterState {
action: ActionState::Roll { action: ActionState::Roll {
time_left: Duration::new(1, 0), time_left: Duration::new(1, 0),
was_wielding: false,
}, },
movement: MovementState::Run, movement: MovementState::Run,
}, },

View File

@ -197,7 +197,7 @@ impl FigureMgr {
} }
let target_base = match &character.movement { let target_base = match &character.movement {
Fall | Stand => anim::character::StandAnimation::update_skeleton( Stand => anim::character::StandAnimation::update_skeleton(
&CharacterSkeleton::new(), &CharacterSkeleton::new(),
(active_tool_kind, time), (active_tool_kind, time),
state.movement_time, state.movement_time,
@ -211,7 +211,7 @@ impl FigureMgr {
&mut movement_animation_rate, &mut movement_animation_rate,
skeleton_attr, skeleton_attr,
), ),
Jump => anim::character::JumpAnimation::update_skeleton( Jump | Fall => anim::character::JumpAnimation::update_skeleton(
&CharacterSkeleton::new(), &CharacterSkeleton::new(),
(active_tool_kind, time), (active_tool_kind, time),
state.movement_time, state.movement_time,