From 8cdf06e3e2a10c65001c07e5b00073d246fd0f9b Mon Sep 17 00:00:00 2001 From: Adam Whitehurst Date: Wed, 8 Jan 2020 05:17:36 -0800 Subject: [PATCH] Swim pulsing --- common/src/comp/controller.rs | 5 ++++ common/src/comp/states/mod.rs | 51 ++++++---------------------------- common/src/comp/states/swim.rs | 9 +++++- 3 files changed, 21 insertions(+), 44 deletions(-) diff --git a/common/src/comp/controller.rs b/common/src/comp/controller.rs index 1bfc01dcad..f3155a3fd3 100644 --- a/common/src/comp/controller.rs +++ b/common/src/comp/controller.rs @@ -69,6 +69,11 @@ impl Input { (self.is_pressed() && self.duration >= DEFAULT_HOLD_DURATION) } + // Whether input has been pressed for longer than `threshold` + pub fn is_long_press(&self, threshold: Duration) -> bool { + (self.is_pressed() && self.duration >= threshold) + } + /// Handles logic of updating state of Input pub fn set_state(&mut self, new_state: bool) { // Only update if state switches diff --git a/common/src/comp/states/mod.rs b/common/src/comp/states/mod.rs index 805c8e31dd..2d29505272 100644 --- a/common/src/comp/states/mod.rs +++ b/common/src/comp/states/mod.rs @@ -47,11 +47,8 @@ use super::{ /// /// ## Example Implementation: /// ``` -/// use crate::comp::{ -/// ClimbState, EcsStateData, GlideState, JumpState, MoveState::*, SitState, StateHandler, -/// StateUpdate, -/// }; -/// use crate::util::state_utils::* +/// use crate::util::state_utils::*; +/// /// #[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)] /// pub struct RunState { /// active_duration: Duration, @@ -72,7 +69,7 @@ use super::{ /// ori: *ecs_data.ori, /// }; /// -/// // Move player according to move_dir +/// // Update player's Vel /// update.vel.0 += Vec2::broadcast(ecs_data.dt.0) /// * ecs_data.inputs.move_dir /// * if update.vel.0.magnitude_squared() < HUMANOID_SPEED.powf(2.0) { @@ -81,50 +78,18 @@ use super::{ /// 0.0 /// }; /// -/// // Set direction based on move direction when on the ground -/// let ori_dir = if update.character.action_state.is_attacking() -/// || update.character.action_state.is_blocking() -/// { -/// Vec2::from(ecs_data.inputs.look_dir).normalized() -/// } else { -/// Vec2::from(update.vel.0) -/// }; -/// -/// if ori_dir.magnitude_squared() > 0.0001 -/// && (update.ori.0.normalized() - Vec3::from(ori_dir).normalized()).magnitude_squared() -/// > 0.001 -/// { -/// update.ori.0 = -/// vek::ops::Slerp::slerp(update.ori.0, ori_dir.into(), 9.0 * ecs_data.dt.0); -/// } -/// -/// // Try to sit -/// if can_sit(ecs_data.physics, ecs_data.inputs, ecs_data.body) { -/// update.character.move_state = Sit(Some(SitState)); -/// return update; -/// } -/// -/// // Try to climb -/// if can_climb(ecs_data.physics, ecs_data.inputs, ecs_data.body) { -/// update.character.move_state = Climb(Some(ClimbState)); -/// return update; -/// } +/// // -- snip -- +/// // Other updates; checks for gliding, climbing, etc. /// /// // Try to jump -/// if can_jump(ecs_data.physics, ecs_data.inputs) { -/// update.character.move_state = Jump(Some(JumpState)); -/// return update; -/// } -/// -/// // Try to glide -/// if can_glide(ecs_data.physics, ecs_data.inputs, ecs_data.body) { -/// update.character.move_state = Glide(Some(GlideState)); +/// if state_utils::can_jump(ecs_data.physics, ecs_data.inputs) { +/// update.character.move_state = Jump(None); /// return update; /// } /// /// // Update based on groundedness /// update.character.move_state = -/// determine_move_from_grounded_state(ecs_data.physics, ecs_data.inputs); +/// state_utils::determine_move_from_grounded_state(ecs_data.physics, ecs_data.inputs); /// /// return update; /// } diff --git a/common/src/comp/states/swim.rs b/common/src/comp/states/swim.rs index 29d1512bfe..1d739a7aec 100644 --- a/common/src/comp/states/swim.rs +++ b/common/src/comp/states/swim.rs @@ -1,5 +1,6 @@ use crate::comp::{EcsStateData, MoveState::*, StateHandler, StateUpdate}; use crate::sys::phys::GRAVITY; +use std::time::Duration; use vek::{Vec2, Vec3}; #[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)] @@ -50,7 +51,13 @@ impl StateHandler for SwimState { ); } - if ecs_data.inputs.jump.is_pressed() && !ecs_data.inputs.jump.is_held_down() { + // Force players to press jump in a slow rhythmic fashion to swim up + if ecs_data.inputs.jump.is_pressed() + && !ecs_data + .inputs + .jump + .is_long_press(Duration::from_millis(600)) + { update.vel.0.z = (update.vel.0.z + ecs_data.dt.0 * GRAVITY * 1.25).min(HUMANOID_WATER_SPEED); }