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
Show file tree
Hide file tree
Changes from 9 commits
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
5 changes: 5 additions & 0 deletions client/src/graphics/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ impl Window {
sim.toggle_no_clip();
}
}
KeyCode::KeyB if state == ElementState::Pressed => {
if let Some(sim) = self.sim.as_mut() {
sim.debug_spawn_blinker();
}
}
KeyCode::F1 if state == ElementState::Pressed => {
self.gui_state.toggle_gui();
}
Expand Down
1 change: 1 addition & 0 deletions client/src/prediction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ mod tests {
movement: na::Vector3::x(),
jump: false,
no_clip: true,
debug_spawn_blinker: false,
block_update: None,
};

Expand Down
13 changes: 13 additions & 0 deletions client/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct Sim {
no_clip: bool,
/// Whether no_clip will be toggled next step
toggle_no_clip: bool,
debug_spawn_blinker: bool,
/// Whether the current step starts with a jump
is_jumping: bool,
/// Whether the jump button has been pressed since the last step
Expand Down Expand Up @@ -100,6 +101,7 @@ impl Sim {
average_movement_input: na::zero(),
no_clip: true,
toggle_no_clip: false,
debug_spawn_blinker: false,
is_jumping: false,
jump_pressed: false,
jump_held: false,
Expand Down Expand Up @@ -144,6 +146,11 @@ impl Sim {
self.toggle_no_clip = true;
}

pub fn debug_spawn_blinker(&mut self) {
// Note: the blinker currently does nothing but update internal state
self.debug_spawn_blinker = true;
}

pub fn set_jump_held(&mut self, jump_held: bool) {
self.jump_held = jump_held;
self.jump_pressed = jump_held || self.jump_pressed;
Expand Down Expand Up @@ -367,6 +374,9 @@ impl Sim {
node = Some(x.node);
builder.add(x);
}
Blinker(x) => {
builder.add(x);
}
};
}
let entity = self.world.spawn(builder.build());
Expand All @@ -392,6 +402,7 @@ impl Sim {
movement: sanitize_motion_input(orientation * self.average_movement_input),
jump: self.is_jumping,
no_clip: self.no_clip,
debug_spawn_blinker: self.debug_spawn_blinker,
block_update: self.get_local_character_block_update(),
};
let generation = self
Expand All @@ -404,6 +415,7 @@ impl Sim {
character_input,
orientation: self.local_character_controller.orientation(),
});
self.debug_spawn_blinker = false;
}

fn update_view_position(&mut self) {
Expand All @@ -425,6 +437,7 @@ impl Sim {
/ (self.since_input_sent.as_secs_f32() / self.cfg.step_interval.as_secs_f32()),
jump: self.is_jumping,
no_clip: self.no_clip,
debug_spawn_blinker: self.debug_spawn_blinker,
block_update: None,
};
character_controller::run_character_step(
Expand Down
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod plane;
pub mod proto;
mod sim_config;
pub mod terraingen;
pub mod ticker;
pub mod traversal;
pub mod voxel_math;
pub mod world;
Expand Down
6 changes: 4 additions & 2 deletions common/src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};

use crate::{
dodeca, graph::NodeId, node::ChunkId, voxel_math::Coords, world::Material, EntityId, SimConfig,
Step,
dodeca, graph::NodeId, node::ChunkId, ticker::Blinker, voxel_math::Coords,
world::Material, EntityId, SimConfig, Step,
};

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -70,6 +70,7 @@ pub struct CharacterInput {
pub movement: na::Vector3<f32>,
pub jump: bool,
pub no_clip: bool,
pub debug_spawn_blinker: bool,
pub block_update: Option<BlockUpdate>,
}

Expand All @@ -90,6 +91,7 @@ pub struct SerializedVoxelData {
pub enum Component {
Character(Character),
Position(Position),
Blinker(Blinker),
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
18 changes: 18 additions & 0 deletions common/src/ticker.rs
blockmath marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Blinker {
pub on: bool,
}

impl Blinker {
pub fn new() -> Self {
Self { on: false }
}
}

impl Default for Blinker {
fn default() -> Self {
Blinker::new()
}
}
33 changes: 30 additions & 3 deletions server/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use common::{
Character, CharacterInput, CharacterState, ClientHello, Command, Component, FreshNode,
Position, Spawns, StateDelta,
},
ticker::Blinker,
traversal::{ensure_nearby, nearby_nodes},
worldgen::ChunkParams,
EntityId, SimConfig, Step,
Expand All @@ -30,8 +31,8 @@ use crate::postcard_helpers::{self, SaveEntity};

pub struct Sim {
cfg: Arc<SimConfig>,
rng: SmallRng,
step: Step,
rng: SmallRng,
blockmath marked this conversation as resolved.
Show resolved Hide resolved
entity_ids: FxHashMap<EntityId, Entity>,
world: hecs::World,
graph: Graph,
Expand All @@ -51,8 +52,8 @@ pub struct Sim {
impl Sim {
pub fn new(cfg: Arc<SimConfig>, save: &save::Save) -> Self {
let mut result = Self {
rng: SmallRng::from_entropy(),
step: 0,
rng: SmallRng::from_entropy(),
entity_ids: FxHashMap::default(),
world: hecs::World::new(),
graph: Graph::new(cfg.chunk_size),
Expand Down Expand Up @@ -205,6 +206,7 @@ impl Sim {
movement: na::Vector3::zeros(),
jump: false,
no_clip: true,
debug_spawn_blinker: false,
block_update: None,
};
let entity = self.world.spawn((id, position, character, initial_input));
Expand Down Expand Up @@ -272,6 +274,16 @@ impl Sim {
let span = error_span!("step", step = self.step);
let _guard = span.enter();

for (_entity, blinker) in self.world.query::<&mut Blinker>().iter() {
blinker.on = !blinker.on;

if blinker.on {
tracing::info!("Blinked ON");
} else {
tracing::info!("Blinked OFF");
}
}

let mut pending_block_updates: Vec<BlockUpdate> = vec![];

// Extend graph structure
Expand Down Expand Up @@ -325,13 +337,19 @@ impl Sim {
}
}

let mut pending_blinker_spawns: Vec<(Position, Blinker)> = Vec::new();

// Simulate
for (entity, (position, character, input)) in self
.world
.query::<(&mut Position, &mut Character, &CharacterInput)>()
.iter()
{
let prev_node = position.node;
if input.debug_spawn_blinker {
let blinker: Blinker = Blinker::new();
pending_blinker_spawns.push((*position, blinker));
}
character_controller::run_character_step(
&self.cfg,
&self.graph,
Expand All @@ -350,6 +368,15 @@ impl Sim {
self.dirty_nodes.insert(position.node);
}

for (position, ticker) in pending_blinker_spawns {
patowen marked this conversation as resolved.
Show resolved Hide resolved
let id = self.new_id();
let entity = self.world.spawn((id, position, ticker));
self.graph_entities.insert(position.node, entity);
self.entity_ids.insert(id, entity);
self.spawns.push(entity);
self.dirty_nodes.insert(position.node);
}

let mut accepted_block_updates: Vec<BlockUpdate> = vec![];

for block_update in pending_block_updates.into_iter() {
Expand Down Expand Up @@ -412,7 +439,7 @@ impl Sim {
(spawns, delta)
}

fn new_id(&mut self) -> EntityId {
pub fn new_id(&mut self) -> EntityId {
blockmath marked this conversation as resolved.
Show resolved Hide resolved
loop {
let id = self.rng.gen();
if !self.entity_ids.contains_key(&id) {
Expand Down