Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Sprint Tracking #434

Merged
merged 11 commits into from
Sep 6, 2021
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!(EntityActionKind::StartSprinting, packet.action_id);
let is_sprinting = game.ecs.get_mut::<Sprinting>(player)?.0;
ThisNekoGuy marked this conversation as resolved.
Show resolved Hide resolved
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.
ambeeeeee marked this conversation as resolved.
Show resolved Hide resolved
#[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?");
}
}
}