format
This commit is contained in:
parent
b873398a79
commit
e2d5748733
41 changed files with 291 additions and 268 deletions
|
|
@ -8,13 +8,10 @@ pub struct Component<T: ComponentType> {
|
|||
component_type: T,
|
||||
}
|
||||
|
||||
pub trait ComponentType{}
|
||||
pub trait ComponentType {}
|
||||
|
||||
impl<T: ComponentType> Component<T> {
|
||||
pub fn new(id: usize, component_type: T) -> Self {
|
||||
Self {
|
||||
id,
|
||||
component_type
|
||||
}
|
||||
Self { id, component_type }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use crate::{entity, generational_index::{GenerationalIndex, GenerationalIndexAllocator}};
|
||||
use crate::{
|
||||
entity,
|
||||
generational_index::{GenerationalIndex, GenerationalIndexAllocator},
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
||||
pub struct Entity(GenerationalIndex);
|
||||
|
|
@ -13,7 +16,6 @@ impl Entity {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EntityAllocator(GenerationalIndexAllocator);
|
||||
|
||||
|
|
@ -33,4 +35,4 @@ impl EntityAllocator {
|
|||
pub fn is_live(&self, entity: Entity) -> bool {
|
||||
self.0.is_live(entity.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ impl GenerationalIndex {
|
|||
pub fn index(&self) -> usize {
|
||||
self.index
|
||||
}
|
||||
|
||||
|
||||
pub fn generation(&self) -> u64 {
|
||||
self.generation
|
||||
}
|
||||
|
|
@ -32,7 +32,7 @@ impl GenerationalIndexAllocator {
|
|||
}
|
||||
pub fn allocate(&mut self) -> GenerationalIndex {
|
||||
match self.free.pop() {
|
||||
Some(index) =>{
|
||||
Some(index) => {
|
||||
self.entries[index].generation += 1;
|
||||
self.entries[index].is_live = true;
|
||||
|
||||
|
|
@ -40,15 +40,15 @@ impl GenerationalIndexAllocator {
|
|||
index,
|
||||
generation: self.entries[index].generation,
|
||||
}
|
||||
},
|
||||
}
|
||||
None => {
|
||||
self.entries.push(AllocatorEntry {
|
||||
is_live: true,
|
||||
generation: 0,
|
||||
self.entries.push(AllocatorEntry {
|
||||
is_live: true,
|
||||
generation: 0,
|
||||
});
|
||||
|
||||
GenerationalIndex {
|
||||
index: self.entries.len() -1,
|
||||
index: self.entries.len() - 1,
|
||||
generation: 0,
|
||||
}
|
||||
}
|
||||
|
|
@ -67,7 +67,9 @@ impl GenerationalIndexAllocator {
|
|||
}
|
||||
|
||||
pub fn is_live(&self, index: GenerationalIndex) -> bool {
|
||||
index.index() < self.entries.len() && self.entries[index.index()].generation == index.generation && self.entries[index.index()].is_live
|
||||
index.index() < self.entries.len()
|
||||
&& self.entries[index.index()].generation == index.generation
|
||||
&& self.entries[index.index()].is_live
|
||||
}
|
||||
|
||||
pub fn max_allocated_index(&self) -> usize {
|
||||
|
|
@ -82,7 +84,7 @@ struct ArrayEntry<T> {
|
|||
|
||||
pub struct GenerationalIndexArray<T>(Vec<Option<ArrayEntry<T>>>);
|
||||
|
||||
impl <T> GenerationalIndexArray<T> {
|
||||
impl<T> GenerationalIndexArray<T> {
|
||||
pub fn new() -> GenerationalIndexArray<T> {
|
||||
GenerationalIndexArray(Vec::new())
|
||||
}
|
||||
|
|
@ -90,9 +92,9 @@ impl <T> GenerationalIndexArray<T> {
|
|||
pub fn clear(&mut self) {
|
||||
self.0.clear();
|
||||
}
|
||||
|
||||
|
||||
pub fn insert(&mut self, index: GenerationalIndex, value: T) {
|
||||
while self.0.len() <= index.index() {
|
||||
while self.0.len() <= index.index() {
|
||||
self.0.push(None);
|
||||
}
|
||||
|
||||
|
|
@ -105,9 +107,9 @@ impl <T> GenerationalIndexArray<T> {
|
|||
panic!("write an index from previous gen");
|
||||
}
|
||||
|
||||
self.0[index.index()] = Some(ArrayEntry {
|
||||
value,
|
||||
generation: index.generation(),
|
||||
self.0[index.index()] = Some(ArrayEntry {
|
||||
value,
|
||||
generation: index.generation(),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -123,12 +125,14 @@ impl <T> GenerationalIndexArray<T> {
|
|||
}
|
||||
|
||||
match &self.0[index.index()] {
|
||||
Some(entry) => if entry.generation == index.generation() {
|
||||
Some(&entry.value)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
None => None
|
||||
Some(entry) => {
|
||||
if entry.generation == index.generation() {
|
||||
Some(&entry.value)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
pub fn get_mut(&mut self, index: GenerationalIndex) -> Option<&mut T> {
|
||||
|
|
@ -137,12 +141,14 @@ impl <T> GenerationalIndexArray<T> {
|
|||
}
|
||||
|
||||
match &mut self.0[index.index()] {
|
||||
Some(entry) => if entry.generation == index.generation() {
|
||||
Some(&mut entry.value)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
None => None
|
||||
Some(entry) => {
|
||||
if entry.generation == index.generation() {
|
||||
Some(&mut entry.value)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,19 @@
|
|||
use std::{collections::HashMap, path::{Component, Components}};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
path::{Component, Components},
|
||||
};
|
||||
|
||||
use anymap::AnyMap;
|
||||
|
||||
use crate::{entity::{Entity, EntityAllocator}, generational_index::{GenerationalIndex, GenerationalIndexAllocator, GenerationalIndexArray}};
|
||||
use crate::{
|
||||
entity::{Entity, EntityAllocator},
|
||||
generational_index::{GenerationalIndex, GenerationalIndexAllocator, GenerationalIndexArray},
|
||||
};
|
||||
|
||||
pub mod component;
|
||||
pub mod entity;
|
||||
pub mod generational_index;
|
||||
pub mod world;
|
||||
pub mod entity;
|
||||
pub mod component;
|
||||
|
||||
|
||||
// based on: https://kyren.github.io/2018/09/14/rustconf-talk.html
|
||||
|
||||
|
|
@ -17,25 +22,25 @@ pub mod component;
|
|||
Game
|
||||
🠟
|
||||
Systems
|
||||
(RenderSystem, PhysicsSystem, EnemyAISystem, EnemyCollisionSystem,...)
|
||||
(RenderSystem, PhysicsSystem, EnemyAISystem, EnemyCollisionSystem,...)
|
||||
🠟
|
||||
Entity
|
||||
🠟
|
||||
🠟
|
||||
Components
|
||||
--------------------------------------
|
||||
--------------------------------------
|
||||
*/
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ECS {
|
||||
entities: EntityAllocator,
|
||||
components: AnyMap
|
||||
components: AnyMap,
|
||||
}
|
||||
|
||||
impl ECS {
|
||||
pub fn new() -> ECS {
|
||||
ECS {
|
||||
ECS {
|
||||
entities: EntityAllocator::new(),
|
||||
components: AnyMap::new(),
|
||||
components: AnyMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -47,9 +52,5 @@ impl ECS {
|
|||
self.entities.is_live(entity)
|
||||
}
|
||||
|
||||
pub fn register_component() {
|
||||
|
||||
}
|
||||
|
||||
pub fn register_component() {}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,21 +2,21 @@ use std::fmt::Error;
|
|||
|
||||
use anymap::AnyMap;
|
||||
|
||||
use crate::{Entity, ECS};
|
||||
use crate::{ECS, Entity};
|
||||
|
||||
/// stores Entitys, Components and resources
|
||||
/// provides methods to search for specific Entitys
|
||||
/// provides methods to search for specific Entitys
|
||||
#[derive(Debug)]
|
||||
pub struct World {
|
||||
ecs: ECS,
|
||||
resources: AnyMap
|
||||
resources: AnyMap,
|
||||
}
|
||||
|
||||
impl World {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
Self {
|
||||
ecs: ECS::new(),
|
||||
resources: AnyMap::new(),
|
||||
resources: AnyMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue