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

Blinker POC. Possible framework for constantly-updating entities. #406

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
The blinker now syncs to client.
  • Loading branch information
blockmath committed Jun 24, 2024
commit bd3589aa1da70314196f00648c9856b6f05b93f4
18 changes: 18 additions & 0 deletions client/src/sim.rs
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ use common::{
self, BlockUpdate, Character, CharacterInput, CharacterState, Command, Component, Position,
},
sanitize_motion_input,
ticker::Blinker,
world::Material,
EntityId, GraphEntities, SimConfig, Step,
};
@@ -251,6 +252,9 @@ impl Sim {
for &(id, ref new_state) in &msg.character_states {
self.update_character_state(id, new_state);
}
for &(id, ref new_blinker) in &msg.blinker_states {
self.update_blinker_state(id, new_blinker);
}
self.reconcile_prediction(msg.latest_input);
}
}
@@ -286,6 +290,20 @@ impl Sim {
}
}

fn update_blinker_state(&mut self, id: EntityId, new_blinker: &Blinker) {
match self.entity_ids.get(&id) {
None => debug!(%id, "blinker state update for unknown entity"),
Some(&entity) => match self.world.get::<&mut Blinker>(entity) {
Ok(mut blinker) => {
blinker.on = new_blinker.on;
blockmath marked this conversation as resolved.
Show resolved Hide resolved
}
Err(e) => {
error!(%id, "blinker state update error: {}", e)
}
},
}
}

fn reconcile_prediction(&mut self, latest_input: u16) {
let id = self.local_character_id;
let Some(&entity) = self.entity_ids.get(&id) else {
1 change: 1 addition & 0 deletions common/src/proto.rs
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ pub struct StateDelta {
pub latest_input: u16,
pub positions: Vec<(EntityId, Position)>,
pub character_states: Vec<(EntityId, CharacterState)>,
pub blinker_states: Vec<(EntityId, Blinker)>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
2 changes: 1 addition & 1 deletion common/src/ticker.rs
blockmath marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Blinker {
pub on: bool,
}
9 changes: 9 additions & 0 deletions server/src/sim.rs
Original file line number Diff line number Diff line change
@@ -433,6 +433,12 @@ impl Sim {
.iter()
.map(|(_, (&id, ch))| (id, ch.state.clone()))
.collect(),
blinker_states: self
.world
.query::<(&EntityId, &Blinker)>()
.iter()
.map(|(_, (&id, blinker))| (id, blinker.clone()))
.collect(),
};

self.step += 1;
@@ -457,5 +463,8 @@ fn dump_entity(world: &hecs::World, entity: Entity) -> Vec<Component> {
if let Ok(x) = world.get::<&Character>(entity) {
components.push(Component::Character((*x).clone()));
}
if let Ok(x) = world.get::<&Blinker>(entity) {
components.push(Component::Blinker((*x).clone()));
}
components
}