Skip to content

Commit

Permalink
Added Sprint Tracking (#434)
Browse files Browse the repository at this point in the history
* Added sprinting component to player

* Added sprint tracking

* Added sprint component and event

* Added sprint component

* Added sprint event import

* Added sprint event

* Sprint tracking in plugin example

* Reloacted a comment to a more appropriate postion

* Refactored Sprinting Detection

Refactored the sprint detection to reduce code duplication with the assistance of Defman

* Refactor Reverted Due to Problem

The desired refactor didn't work because the method attempted couldn't receive the sprint state from the player

* Re-Refactor of Sprint Detection

Turned out the macro actually expected the packet id and the `EntityActionKind` to be reversed

Co-authored-by: linuxNeko <[email protected]>
  • Loading branch information
ThisNekoGuy and linuxNeko authored Sep 6, 2021
1 parent d919b96 commit 9008090
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 8 deletions.
3 changes: 2 additions & 1 deletion feather/common/src/entities/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::bail;
use base::EntityKind;
use ecs::{EntityBuilder, SysResult};
use quill_common::{
components::{CreativeFlying, Sneaking},
components::{CreativeFlying, Sneaking, Sprinting},
entities::Player,
};

Expand All @@ -12,6 +12,7 @@ pub fn build_default(builder: &mut EntityBuilder) {
.add(Player)
.add(CreativeFlying(false))
.add(Sneaking(false))
.add(Sprinting(false))
.add(EntityKind::Player);
}

Expand Down
18 changes: 12 additions & 6 deletions feather/server/src/packet_handlers/entity_action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use common::Game;
use ecs::{Entity, SysResult};
use protocol::packets::client::{EntityAction, EntityActionKind};
use quill_common::{components::Sneaking, events::SneakEvent};
use quill_common::{
components::{Sneaking, Sprinting},
events::{SneakEvent, SprintEvent},
};

/// From [wiki](https://wiki.vg/Protocol#Entity_Action)
/// Sent by the client to indicate that it has performed certain actions:
Expand Down Expand Up @@ -35,11 +38,14 @@ pub fn handle_entity_action(game: &mut Game, player: Entity, packet: EntityActio
// and all players are kicked out of the bed. We have to seperatly send out
// a notice that bed state might have changed.
}
EntityActionKind::StartSprinting => {
//TODO issue #423
}
EntityActionKind::StopSprinting => {
//TODO issue #423
EntityActionKind::StartSprinting | EntityActionKind::StopSprinting => {
let start_sprinting = matches!(packet.action_id, EntityActionKind::StartSprinting);
let is_sprinting = game.ecs.get_mut::<Sprinting>(player)?.0;
if is_sprinting != start_sprinting {
game.ecs
.insert_entity_event(player, SprintEvent::new(start_sprinting))?;
game.ecs.get_mut::<Sprinting>(player)?.0 = start_sprinting;
}
}
EntityActionKind::StartHorseJump => {
//TODO issue #423
Expand Down
3 changes: 3 additions & 0 deletions quill/common/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ host_component_enum! {
CreativeFlyingEvent = 1010,
Sneaking = 1011,
SneakEvent = 1012,
Sprinting = 1013,
SprintEvent = 1014,


}
Expand Down Expand Up @@ -352,3 +354,4 @@ bincode_component_impl!(BlockPlacementEvent);
bincode_component_impl!(BlockInteractEvent);
bincode_component_impl!(CreativeFlyingEvent);
bincode_component_impl!(SneakEvent);
bincode_component_impl!(SprintEvent);
10 changes: 10 additions & 0 deletions quill/common/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,13 @@ bincode_component_impl!(CreativeFlying);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Sneaking(pub bool);
bincode_component_impl!(Sneaking);

/// A component on players that tracks if they are sprinting or not.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Sprinting(pub bool);
impl Sprinting {
pub fn new(value: bool) -> Self {
Sprinting(value)
}
}
bincode_component_impl!(Sprinting);
2 changes: 1 addition & 1 deletion quill/common/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ mod change;
mod interact_entity;

pub use block_interact::{BlockInteractEvent, BlockPlacementEvent};
pub use change::{CreativeFlyingEvent, SneakEvent};
pub use change::{CreativeFlyingEvent, SneakEvent, SprintEvent};
pub use interact_entity::InteractEntityEvent;
13 changes: 13 additions & 0 deletions quill/common/src/events/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ impl SneakEvent {
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SprintEvent {
pub is_sprinting: bool,
}

impl SprintEvent {
pub fn new(changed_to: bool) -> Self {
Self {
is_sprinting: changed_to,
}
}
}
10 changes: 10 additions & 0 deletions quill/example-plugins/observe-creativemode-flight-event/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ flying.
*/

use quill::{
components::Sprinting,
events::{CreativeFlyingEvent, SneakEvent},
Game, Plugin, Setup,
};
Expand All @@ -16,6 +17,7 @@ impl Plugin for FlightPlugin {
fn enable(_game: &mut Game, setup: &mut Setup<Self>) -> Self {
setup.add_system(flight_observer_system);
setup.add_system(sneak_observer_system);
setup.add_system(sprinting_observer_system);
FlightPlugin {}
}

Expand All @@ -41,3 +43,11 @@ fn sneak_observer_system(_plugin: &mut FlightPlugin, game: &mut Game) {
}
}
}

fn sprinting_observer_system(_plugin: &mut FlightPlugin, game: &mut Game) {
for (player, sprinting) in game.query::<&Sprinting>() {
if sprinting.0 {
player.send_message("Are you sprinting?");
}
}
}

0 comments on commit 9008090

Please sign in to comment.