Added Uid marker component

Former-commit-id: b0aae7a8e6dd075996f3bee6cc7be29763f6cdaa
This commit is contained in:
Joshua Barretto 2019-03-04 00:06:01 +00:00
parent 2fffe21bc7
commit ef333877ae
4 changed files with 73 additions and 5 deletions

View File

@ -5,7 +5,7 @@ authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>", "Maciej Ćwięka <mc
edition = "2018"
[dependencies]
specs = "0.14"
specs = { version = "0.14", features = ["serde"] }
shred = "0.7"
vek = "0.9"
dot_vox = "1.0"

View File

@ -1,9 +1,12 @@
pub mod phys;
pub mod uid;
// External
use specs::World as EcsWorld;
pub fn register_local_components(ecs_world: &mut EcsWorld) {
ecs_world.register::<uid::Uid>();
ecs_world.register::<phys::Pos>();
ecs_world.register::<phys::Vel>();
ecs_world.register::<phys::Dir>();

65
common/src/comp/uid.rs Normal file
View File

@ -0,0 +1,65 @@
use std::{
collections::HashMap,
ops::Range,
};
use specs::{
saveload::{Marker, MarkerAllocator},
world::EntitiesRes,
Component,
VecStorage,
Entity,
Join,
ReadStorage,
};
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Uid {
id: u64,
seq: u64,
}
impl Component for Uid {
type Storage = VecStorage<Self>;
}
impl Marker for Uid {
type Identifier = u64;
type Allocator = UidNode;
fn id(&self) -> u64 { self.id }
fn update(&mut self, update: Self) {
assert_eq!(self.id, update.id);
self.seq = update.seq;
}
}
pub struct UidNode {
pub(crate) range: Range<u64>,
pub(crate) mapping: HashMap<u64, Entity>,
}
impl MarkerAllocator<Uid> for UidNode {
fn allocate(&mut self, entity: Entity, id: Option<u64>) -> Uid {
let id = id.unwrap_or_else(|| {
self.range.next().expect("
Id range must be effectively endless.
Somehow, you ran this program for longer than the lifetime of the universe.
It's probably time to stop playing and prepare for your imminent extinction.
")
});
self.mapping.insert(id, entity);
Uid { id, seq: 0 }
}
fn retrieve_entity_internal(&self, id: u64) -> Option<Entity> {
self.mapping.get(&id).cloned()
}
fn maintain(&mut self, entities: &EntitiesRes, storage: &ReadStorage<Uid>) {
self.mapping = (&*entities, storage)
.join()
.map(|(e, m)| (m.id(), e))
.collect();
}
}

View File

@ -25,8 +25,8 @@ fn basic_run() {
scon.send(String::from("foo")).unwrap();
client.send(String::from("bar")).unwrap();
std::thread::sleep(std::time::Duration::from_millis(10));
assert_eq!("foo", client.recv_iter().next().unwrap());
assert_eq!("bar", scon.recv_iter().next().unwrap());
assert_eq!("foo", client.new_messages().next().unwrap());
assert_eq!("bar", scon.new_messages().next().unwrap());
}
#[test]
@ -40,7 +40,7 @@ fn huge_size_header() {
std::thread::sleep(std::time::Duration::from_millis(10));
client.write(&[0xffu8; 64]).unwrap();
std::thread::sleep(std::time::Duration::from_millis(10));
assert_eq!(scon.recv_iter().next(), None);
assert_eq!(scon.new_messages().next(), None);
}
#[test]
@ -66,7 +66,7 @@ fn disconnect() {
thread::sleep(Duration::from_millis(10));
match to_client.recv_iter().next() {
match to_client.new_messages().next() {
None => {},
_ => panic!("Unexpected message!"),
}