Sanitise controller inputs

This commit is contained in:
Joshua Barretto 2021-06-21 21:13:49 +01:00
parent 90e5bac395
commit e930ff6779
3 changed files with 21 additions and 10 deletions

View File

@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Added a skill tree for mining, which gains xp from mining ores and gems.
- Added debug line info to release builds, enhancing the usefulness of panic backtraces
- NPCs and animals can now make sounds in response to certain events
@ -15,15 +16,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ability to toggle chat visibility
### Changed
- Entity-entity pushback is no longer applied in forced movement states like rolling and leaping.
- Updated audio library (rodio 0.13 -> 0.14).
- Improve entity-terrain physics performance by reducing the number of voxel lookups.
- Clay Golem uses shockwave only after specific fraction of health and other difficulty adjustments.
- Made strafing slightly slower
### Removed
- Enemies no more spawn in dungeon boss room
### Fixed
- Crafting Stations aren't exploadable anymore
- Cases where no audio output could be produced before.
- Significantly improved the performance of playing sound effects

View File

@ -215,6 +215,20 @@ pub struct Controller {
}
impl ControllerInputs {
/// Sanitize inputs to avoid clients sending bad data.
pub fn sanitize(&mut self) {
self.move_dir = if self.move_dir.map(|e| e.is_finite()).reduce_and() {
self.move_dir / self.move_dir.magnitude().max(1.0)
} else {
Vec2::zero()
};
self.move_z = if self.move_z.is_finite() {
self.move_z.clamped(-1.0, 1.0)
} else {
0.0
};
}
/// Updates Controller inputs with new version received from the client
pub fn update_with_new(&mut self, new: Self) {
self.climb = new.climb;

View File

@ -37,16 +37,8 @@ impl<'a> System<'a> for Sys {
let mut server_emitter = read_data.server_bus.emitter();
for (entity, controller) in (&read_data.entities, &mut controllers).join() {
let mut inputs = &mut controller.inputs;
// Update `inputs.move_dir`.
inputs.move_dir = if inputs.move_dir.magnitude_squared() > 1.0 {
// Cap move_dir to 1
inputs.move_dir.normalized()
} else {
inputs.move_dir
};
inputs.move_z = inputs.move_z.clamped(-1.0, 1.0);
// Sanitize inputs to avoid clients sending bad data
controller.inputs.sanitize();
// Process other controller events
for event in controller.events.drain(..) {