Skip to content

Commit

Permalink
Save non-character entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Jan 10, 2024
1 parent cf13236 commit e60d4ce
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions server/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hecs::Entity;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use save::ComponentType;
use tracing::{error_span, info, trace};
use tracing::{error, error_span, info, trace};

use common::{
character_controller, dodeca,
Expand Down Expand Up @@ -99,25 +99,28 @@ impl Sim {
fn snapshot_node(&self, node: NodeId) -> save::EntityNode {
let mut entities = Vec::new();
for &entity in self.graph_entities.get(node) {
// TODO: Handle entities other than characters
let mut q = self
.world
.query_one::<(&EntityId, &Position, &Character)>(entity)
.unwrap();
let Some((id, pos, ch)) = q.get() else {
let Ok(entity) = self.world.entity(entity) else {
error!("stale graph entity {:?}", entity);
continue;
};
let Some(id) = entity.get::<&EntityId>() else {
continue;
};
let mut components = Vec::new();
if let Some(pos) = entity.get::<&Position>() {
components.push((
ComponentType::Position as u64,
postcard::to_stdvec(pos.local.as_ref()).unwrap(),
));
}
if let Some(ch) = entity.get::<&Character>() {
components.push((ComponentType::Name as u64, ch.name.as_bytes().into()));
}
let mut repr = Vec::new();
postcard_helpers::serialize(
&SaveEntity {
entity: id.to_bits().to_le_bytes(),
components: vec![
(
ComponentType::Position as u64,
postcard::to_stdvec(pos.local.as_ref()).unwrap(),
),
(ComponentType::Name as u64, ch.name.as_bytes().into()),
],
components,
},
&mut repr,
)
Expand Down

0 comments on commit e60d4ce

Please sign in to comment.