-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
584: Uuid Marker r=torkleyy a=jojolepro ## Checklist * [X] I've added tests for all code changes and additions (where applicable) * [ ] I've added a demonstration of the new feature to one or more examples * [ ] I've updated the book to reflect my changes * [X] Usage of new public items is shown in the API docs ## API changes Added UuidMarker and UuidMarkerAllocator. They work the same way their U64 counterparts do. Added "uuid_entity" feature gate. Co-authored-by: Joël Lupien (Jojolepro) <[email protected]>
- Loading branch information
Showing
7 changed files
with
173 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::collections::HashMap; | ||
|
||
use uuid::Uuid; | ||
|
||
use crate::{ | ||
join::Join, | ||
saveload::{Marker, MarkerAllocator}, | ||
storage::{ReadStorage, VecStorage}, | ||
world::{Component, EntitiesRes, Entity}, | ||
}; | ||
|
||
/// Basic marker uuid implementation usable for saving and loading. | ||
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct UuidMarker { | ||
uuid: Uuid, | ||
} | ||
|
||
impl Component for UuidMarker { | ||
type Storage = VecStorage<Self>; | ||
} | ||
|
||
impl Marker for UuidMarker { | ||
type Allocator = UuidMarkerAllocator; | ||
type Identifier = Uuid; | ||
|
||
fn id(&self) -> Uuid { | ||
self.uuid().clone() | ||
} | ||
} | ||
|
||
impl UuidMarker { | ||
/// Creates a new `UuidMarker` Component from the specified uuid. | ||
pub fn new(uuid: Uuid) -> Self { | ||
UuidMarker { uuid } | ||
} | ||
|
||
/// Creates a new `UuidMarker` Component with a random uuid. | ||
pub fn new_random() -> Self { | ||
let uuid = Uuid::new_v4(); | ||
UuidMarker { uuid } | ||
} | ||
|
||
/// Get the current uuid. | ||
pub fn uuid(&self) -> &Uuid { | ||
&self.uuid | ||
} | ||
} | ||
|
||
/// Basic marker allocator for uuid. | ||
#[derive(Clone, Debug)] | ||
pub struct UuidMarkerAllocator { | ||
mapping: HashMap<Uuid, Entity>, | ||
} | ||
|
||
impl Default for UuidMarkerAllocator { | ||
fn default() -> Self { | ||
UuidMarkerAllocator::new() | ||
} | ||
} | ||
|
||
impl UuidMarkerAllocator { | ||
/// Create new `UuidMarkerAllocator` which will yield `UuidMarker`s. | ||
pub fn new() -> Self { | ||
UuidMarkerAllocator { | ||
mapping: HashMap::new(), | ||
} | ||
} | ||
} | ||
|
||
impl MarkerAllocator<UuidMarker> for UuidMarkerAllocator { | ||
fn allocate(&mut self, entity: Entity, id: Option<Uuid>) -> UuidMarker { | ||
let marker = if let Some(id) = id { | ||
UuidMarker::new(id) | ||
} else { | ||
UuidMarker::new_random() | ||
}; | ||
self.mapping.insert(marker.uuid().clone(), entity); | ||
|
||
marker | ||
} | ||
|
||
fn retrieve_entity_internal(&self, id: Uuid) -> Option<Entity> { | ||
self.mapping.get(&id).cloned() | ||
} | ||
|
||
fn maintain(&mut self, entities: &EntitiesRes, storage: &ReadStorage<UuidMarker>) { | ||
// FIXME: may be too slow | ||
self.mapping = (entities, storage) | ||
.join() | ||
.map(|(e, m)| (m.uuid().clone(), e)) | ||
.collect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters