Inputs now remain queued even when the input is released, until the input is used.

This commit is contained in:
Sam 2022-10-29 23:32:52 -04:00
parent c8d0b337bb
commit d0a46ed82b
5 changed files with 20 additions and 12 deletions

View File

@ -1723,6 +1723,7 @@ impl Client {
entry entry
.or_insert_with(|| Controller { .or_insert_with(|| Controller {
inputs: inputs.clone(), inputs: inputs.clone(),
held_inputs: BTreeMap::new(),
queued_inputs: BTreeMap::new(), queued_inputs: BTreeMap::new(),
events: Vec::new(), events: Vec::new(),
actions: Vec::new(), actions: Vec::new(),

View File

@ -27,6 +27,7 @@ pub struct StateUpdate {
pub swap_equipped_weapons: bool, pub swap_equipped_weapons: bool,
pub should_strafe: bool, pub should_strafe: bool,
pub queued_inputs: BTreeMap<InputKind, InputAttr>, pub queued_inputs: BTreeMap<InputKind, InputAttr>,
pub used_inputs: Vec<InputKind>,
pub removed_inputs: Vec<InputKind>, pub removed_inputs: Vec<InputKind>,
} }
@ -57,6 +58,7 @@ impl From<&JoinData<'_>> for StateUpdate {
should_strafe: data.inputs.strafing, should_strafe: data.inputs.strafing,
character: data.character.clone(), character: data.character.clone(),
queued_inputs: BTreeMap::new(), queued_inputs: BTreeMap::new(),
used_inputs: Vec::new(),
removed_inputs: Vec::new(), removed_inputs: Vec::new(),
} }
} }

View File

@ -246,6 +246,7 @@ pub struct ControllerInputs {
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Controller { pub struct Controller {
pub inputs: ControllerInputs, pub inputs: ControllerInputs,
pub held_inputs: BTreeMap<InputKind, InputAttr>,
pub queued_inputs: BTreeMap<InputKind, InputAttr>, pub queued_inputs: BTreeMap<InputKind, InputAttr>,
// TODO: consider SmallVec // TODO: consider SmallVec
pub events: Vec<ControlEvent>, pub events: Vec<ControlEvent>,
@ -282,6 +283,7 @@ impl Controller {
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.inputs = Default::default(); self.inputs = Default::default();
self.queued_inputs = Default::default(); self.queued_inputs = Default::default();
self.held_inputs = Default::default();
} }
pub fn clear_events(&mut self) { self.events.clear(); } pub fn clear_events(&mut self) { self.events.clear(); }

View File

@ -1017,8 +1017,7 @@ pub fn attempt_glide_wield(
pub fn handle_jump( pub fn handle_jump(
data: &JoinData<'_>, data: &JoinData<'_>,
output_events: &mut OutputEvents, output_events: &mut OutputEvents,
// TODO: remove? update: &mut StateUpdate,
_update: &mut StateUpdate,
strength: f32, strength: f32,
) -> bool { ) -> bool {
(input_is_pressed(data, InputKind::Jump) && data.physics.on_ground.is_some()) (input_is_pressed(data, InputKind::Jump) && data.physics.on_ground.is_some())
@ -1029,6 +1028,7 @@ pub fn handle_jump(
data.entity, data.entity,
strength * impulse / data.mass.0 * data.stats.move_speed_modifier, strength * impulse / data.mass.0 * data.stats.move_speed_modifier,
)); ));
update.used_inputs.push(InputKind::Jump);
}) })
.is_some() .is_some()
} }
@ -1054,6 +1054,7 @@ fn handle_ability(data: &JoinData<'_>, update: &mut StateUpdate, input: InputKin
AbilityInfo::from_input(data, from_offhand, input, ability.ability_meta()), AbilityInfo::from_input(data, from_offhand, input, ability.ability_meta()),
data, data,
)); ));
update.used_inputs.push(input);
return true; return true;
} }
} }
@ -1109,6 +1110,7 @@ pub fn handle_block_input(data: &JoinData<'_>, update: &mut StateUpdate) -> bool
AbilityInfo::from_input(data, false, InputKind::Block, Default::default()), AbilityInfo::from_input(data, false, InputKind::Block, Default::default()),
data, data,
)); ));
update.used_inputs.push(InputKind::Block);
true true
} else { } else {
false false
@ -1129,6 +1131,7 @@ pub fn handle_dodge_input(data: &JoinData<'_>, update: &mut StateUpdate) -> bool
AbilityInfo::from_input(data, false, InputKind::Roll, Default::default()), AbilityInfo::from_input(data, false, InputKind::Roll, Default::default()),
data, data,
)); ));
update.used_inputs.push(InputKind::Roll);
if let CharacterState::Roll(roll) = &mut update.character { if let CharacterState::Roll(roll) = &mut update.character {
if let CharacterState::ComboMelee(c) = data.character { if let CharacterState::ComboMelee(c) = data.character {
roll.was_combo = c roll.was_combo = c
@ -1276,11 +1279,7 @@ pub fn get_buff_strength(data: &JoinData<'_>, ai: AbilityInfo) -> f32 {
} }
pub fn input_is_pressed(data: &JoinData<'_>, input: InputKind) -> bool { pub fn input_is_pressed(data: &JoinData<'_>, input: InputKind) -> bool {
data.controller.queued_inputs.contains_key(&input) data.controller.queued_inputs.contains_key(&input) || data.controller.held_inputs.contains_key(&input)
}
pub fn input_just_pressed(update: &StateUpdate, input: InputKind) -> bool {
update.queued_inputs.contains_key(&input)
} }
/// Checked `Duration` addition. Computes `timer` + `dt`, applying relevant stat /// Checked `Duration` addition. Computes `timer` + `dt`, applying relevant stat

View File

@ -244,7 +244,7 @@ impl<'a> System<'a> for Sys {
impl Sys { impl Sys {
fn publish_state_update( fn publish_state_update(
join: &mut JoinStruct, join: &mut JoinStruct,
mut state_update: StateUpdate, state_update: StateUpdate,
output_events: &mut OutputEvents, output_events: &mut OutputEvents,
) { ) {
// Here we check for equality with the previous value of these components before // Here we check for equality with the previous value of these components before
@ -269,12 +269,16 @@ impl Sys {
*join.vel = state_update.vel; *join.vel = state_update.vel;
*join.ori = state_update.ori; *join.ori = state_update.ori;
join.controller for (input, attr) in state_update.queued_inputs {
.queued_inputs join.controller.queued_inputs.insert(input, attr);
.append(&mut state_update.queued_inputs); join.controller.held_inputs.insert(input, attr);
for input in state_update.removed_inputs { }
for input in state_update.used_inputs {
join.controller.queued_inputs.remove(&input); join.controller.queued_inputs.remove(&input);
} }
for input in state_update.removed_inputs {
join.controller.held_inputs.remove(&input);
}
if state_update.swap_equipped_weapons { if state_update.swap_equipped_weapons {
output_events.emit_server(ServerEvent::InventoryManip( output_events.emit_server(ServerEvent::InventoryManip(
join.entity, join.entity,