mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
update controller tick fn
This commit is contained in:
@ -15,81 +15,73 @@ pub enum ControlEvent {
|
|||||||
//Respawn,
|
//Respawn,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The various states an input can be in
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
||||||
pub enum InputState {
|
|
||||||
Pressed,
|
|
||||||
Unpressed,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether a key is pressed or unpressed
|
/// Whether a key is pressed or unpressed
|
||||||
/// and how long it has been in that state
|
/// and how long it has been in that state
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Input {
|
pub struct Input {
|
||||||
/// Should not be pub because duration should
|
/// Should not be pub because duration should
|
||||||
/// always be reset when state is updated
|
/// always be reset when state is updated
|
||||||
state: InputState,
|
state: bool,
|
||||||
/// Should only be updated by npc agents
|
/// Should only be updated by npc agents
|
||||||
/// through appropriate fn
|
/// through appropriate fn
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
/// Turned off first tick after switching states
|
/// How many update ticks the button has been in its current state for
|
||||||
just_changed: bool,
|
ticks_held: u32,
|
||||||
/// Set when `set_state` is called. Needed so
|
|
||||||
/// tick after change doesn't immediately unset `just_changed`
|
|
||||||
dirty: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Input {
|
impl Input {
|
||||||
fn tick(&mut self, dt: Duration) {
|
fn tick(&mut self, old: Input, dt: Duration) {
|
||||||
// Increase how long input has been in current state
|
// Increase how long input has been in current state
|
||||||
self.duration = self.duration.checked_add(dt).unwrap_or_default();
|
self.duration = self.duration.checked_add(dt).unwrap_or_default();
|
||||||
if self.dirty {
|
|
||||||
// Unset dirty first tick after changing into current state
|
match (self.is_pressed(), old.is_pressed()) {
|
||||||
self.dirty = false;
|
(false, true) | (true, false) => {
|
||||||
} else {
|
println!("{:?}", self);
|
||||||
// Otherwise, just changed is always false
|
self.duration = Duration::default();
|
||||||
self.just_changed = false;
|
self.ticks_held = 1;
|
||||||
}
|
println!("{:?}", self);
|
||||||
|
},
|
||||||
|
(_, _) => {
|
||||||
|
self.ticks_held += 1;
|
||||||
|
println!("____");
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether input is in `InputState::Pressed` state
|
/// Whether input is being pressed down
|
||||||
pub fn is_pressed(&self) -> bool { self.state == InputState::Pressed }
|
pub fn is_pressed(&self) -> bool { self.state == true }
|
||||||
|
|
||||||
/// Whether it's the first frame this input has been in
|
/// Whether it's the first frame this input has been pressed
|
||||||
/// its current state
|
pub fn is_just_pressed(&self) -> bool { self.is_pressed() && self.ticks_held == 1 }
|
||||||
pub fn is_just_pressed(&self) -> bool { self.just_changed && self.is_pressed() }
|
|
||||||
|
|
||||||
/// Whether input has been in current state longer than
|
/// Whether it's the first frame this input has been unpressed
|
||||||
|
pub fn is_just_unpressed(&self) -> bool { !self.is_pressed() && self.ticks_held == 1 }
|
||||||
|
|
||||||
|
/// Whether input has been pressed longer than
|
||||||
/// `DEFAULT_HOLD_DURATION`
|
/// `DEFAULT_HOLD_DURATION`
|
||||||
pub fn is_held_down(&self) -> bool {
|
pub fn is_held_down(&self) -> bool {
|
||||||
self.is_pressed() && self.duration >= DEFAULT_HOLD_DURATION
|
self.is_pressed() && self.duration >= DEFAULT_HOLD_DURATION
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether input has been pressed for longer than `threshold`
|
/// Whether input has been unpressed longer than
|
||||||
pub fn is_long_press(&self, threshold: Duration) -> bool {
|
/// `DEFAULT_HOLD_DURATION`
|
||||||
|
pub fn is_held_up(&self) -> bool {
|
||||||
|
!self.is_pressed() && self.duration >= DEFAULT_HOLD_DURATION
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether input has been pressed for longer than `threshold` duration
|
||||||
|
pub fn held_for_dur(&self, threshold: Duration) -> bool {
|
||||||
self.is_pressed() && self.duration >= threshold
|
self.is_pressed() && self.duration >= threshold
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles logic of updating state of Input
|
/// Whether input has been pressed for longer than `count` number of ticks
|
||||||
pub fn set_state(&mut self, new_state: bool) {
|
pub fn held_for_ticks(&self, count: u32) -> bool {
|
||||||
// Only update if state switches
|
self.is_pressed() && self.ticks_held >= count
|
||||||
match (self.is_pressed(), new_state) {
|
|
||||||
(false, true) => {
|
|
||||||
self.just_changed = true;
|
|
||||||
self.dirty = true;
|
|
||||||
self.state = InputState::Pressed;
|
|
||||||
self.duration = Duration::default();
|
|
||||||
},
|
|
||||||
(true, false) => {
|
|
||||||
self.just_changed = true;
|
|
||||||
self.dirty = true;
|
|
||||||
self.state = InputState::Unpressed;
|
|
||||||
self.duration = Duration::default();
|
|
||||||
},
|
|
||||||
(_, _) => {},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handles logic of updating state of Input
|
||||||
|
pub fn set_state(&mut self, new_state: bool) { self.state = new_state; }
|
||||||
|
|
||||||
/// Increases `input::duration` by `dur`
|
/// Increases `input::duration` by `dur`
|
||||||
pub fn inc_dur(&mut self, dur: Duration) {
|
pub fn inc_dur(&mut self, dur: Duration) {
|
||||||
self.duration = self.duration.checked_add(dur).unwrap_or_default();
|
self.duration = self.duration.checked_add(dur).unwrap_or_default();
|
||||||
@ -102,10 +94,9 @@ impl Input {
|
|||||||
impl Default for Input {
|
impl Default for Input {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
state: InputState::Unpressed,
|
state: false,
|
||||||
duration: Duration::default(),
|
duration: Duration::default(),
|
||||||
just_changed: false,
|
ticks_held: 0,
|
||||||
dirty: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,21 +130,21 @@ pub struct Controller {
|
|||||||
|
|
||||||
impl ControllerInputs {
|
impl ControllerInputs {
|
||||||
/// Updates all inputs, accounting for delta time
|
/// Updates all inputs, accounting for delta time
|
||||||
pub fn tick(&mut self, dt: Duration) {
|
pub fn calculate_change(&mut self, old: ControllerInputs, dt: Duration) {
|
||||||
self.primary.tick(dt);
|
self.primary.tick(old.primary, dt);
|
||||||
self.secondary.tick(dt);
|
self.secondary.tick(old.secondary, dt);
|
||||||
self.ability3.tick(dt);
|
self.ability3.tick(old.ability3, dt);
|
||||||
self.sit.tick(dt);
|
self.sit.tick(old.sit, dt);
|
||||||
self.jump.tick(dt);
|
self.jump.tick(old.jump, dt);
|
||||||
self.roll.tick(dt);
|
self.roll.tick(old.roll, dt);
|
||||||
self.glide.tick(dt);
|
self.glide.tick(old.glide, dt);
|
||||||
self.climb.tick(dt);
|
self.climb.tick(old.climb, dt);
|
||||||
self.climb_down.tick(dt);
|
self.climb_down.tick(old.climb_down, dt);
|
||||||
self.wall_leap.tick(dt);
|
self.wall_leap.tick(old.wall_leap, dt);
|
||||||
self.respawn.tick(dt);
|
self.respawn.tick(old.respawn, dt);
|
||||||
self.toggle_wield.tick(dt);
|
self.toggle_wield.tick(old.toggle_wield, dt);
|
||||||
self.swap_loadout.tick(dt);
|
self.swap_loadout.tick(old.swap_loadout, dt);
|
||||||
self.charge.tick(dt);
|
self.charge.tick(old.charge, dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn holding_ability_key(&self) -> bool {
|
pub fn holding_ability_key(&self) -> bool {
|
||||||
|
@ -25,8 +25,7 @@ pub use body::{
|
|||||||
};
|
};
|
||||||
pub use character_state::{Attacking, CharacterState, StateUpdate};
|
pub use character_state::{Attacking, CharacterState, StateUpdate};
|
||||||
pub use controller::{
|
pub use controller::{
|
||||||
ControlEvent, Controller, ControllerInputs, Input, InputState, InventoryManip, MountState,
|
ControlEvent, Controller, ControllerInputs, Input, InventoryManip, MountState, Mounting,
|
||||||
Mounting,
|
|
||||||
};
|
};
|
||||||
pub use energy::{Energy, EnergySource};
|
pub use energy::{Energy, EnergySource};
|
||||||
pub use inputs::CanBuild;
|
pub use inputs::CanBuild;
|
||||||
|
@ -8,6 +8,7 @@ use specs::{
|
|||||||
saveload::{Marker, MarkerAllocator},
|
saveload::{Marker, MarkerAllocator},
|
||||||
Entities, Join, Read, ReadStorage, System, WriteStorage,
|
Entities, Join, Read, ReadStorage, System, WriteStorage,
|
||||||
};
|
};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
// const CHARGE_COST: i32 = 200;
|
// const CHARGE_COST: i32 = 200;
|
||||||
// const ROLL_COST: i32 = 30;
|
// const ROLL_COST: i32 = 30;
|
||||||
@ -33,15 +34,23 @@ impl<'a> System<'a> for Sys {
|
|||||||
uid_allocator,
|
uid_allocator,
|
||||||
server_bus,
|
server_bus,
|
||||||
_local_bus,
|
_local_bus,
|
||||||
_dt,
|
read_dt,
|
||||||
mut controllers,
|
mut controllers,
|
||||||
mut character_states,
|
mut character_states,
|
||||||
uids,
|
uids,
|
||||||
): Self::SystemData,
|
): Self::SystemData,
|
||||||
) {
|
) {
|
||||||
let mut server_emitter = server_bus.emitter();
|
let mut server_emitter = server_bus.emitter();
|
||||||
for (entity, _uid, controller, character_state) in
|
let dt = Duration::from_secs_f32(read_dt.0);
|
||||||
(&entities, &uids, &mut controllers, &mut character_states).join()
|
|
||||||
|
for (entity, _uid, controller, character_state) in (
|
||||||
|
&entities,
|
||||||
|
&uids,
|
||||||
|
&mut controllers,
|
||||||
|
// &last_controllers,
|
||||||
|
&mut character_states,
|
||||||
|
)
|
||||||
|
.join()
|
||||||
{
|
{
|
||||||
let inputs = &mut controller.inputs;
|
let inputs = &mut controller.inputs;
|
||||||
|
|
||||||
|
@ -75,7 +75,6 @@ impl SessionState {
|
|||||||
impl SessionState {
|
impl SessionState {
|
||||||
/// Tick the session (and the client attached to it).
|
/// Tick the session (and the client attached to it).
|
||||||
fn tick(&mut self, dt: Duration) -> Result<TickAction, Error> {
|
fn tick(&mut self, dt: Duration) -> Result<TickAction, Error> {
|
||||||
self.inputs.tick(dt);
|
|
||||||
for event in self.client.borrow_mut().tick(
|
for event in self.client.borrow_mut().tick(
|
||||||
self.inputs.clone(),
|
self.inputs.clone(),
|
||||||
dt,
|
dt,
|
||||||
@ -327,7 +326,6 @@ impl PlayState for SessionState {
|
|||||||
self.inputs.toggle_wield.set_state(state);
|
self.inputs.toggle_wield.set_state(state);
|
||||||
},
|
},
|
||||||
Event::InputUpdate(GameInput::SwapLoadout, state) => {
|
Event::InputUpdate(GameInput::SwapLoadout, state) => {
|
||||||
println!("{:?}", state);
|
|
||||||
self.inputs.swap_loadout.set_state(state);
|
self.inputs.swap_loadout.set_state(state);
|
||||||
},
|
},
|
||||||
Event::InputUpdate(GameInput::Mount, true) => {
|
Event::InputUpdate(GameInput::Mount, true) => {
|
||||||
|
Reference in New Issue
Block a user