-fixed crash on getComponent when component doesn't exist
-fixed Components ids sorting -added deferred object removing -new EntityManager functions: begin(), end().
This commit is contained in:
parent
a066a97f01
commit
8fdb56e840
3 changed files with 43 additions and 18 deletions
|
|
@ -21,6 +21,7 @@ struct Entity
|
||||||
{
|
{
|
||||||
EntityManager.EntitiesBlock* block = gEM.getMetaData(&this);
|
EntityManager.EntitiesBlock* block = gEM.getMetaData(&this);
|
||||||
EntityManager.EntityInfo* info = block.type_data;
|
EntityManager.EntityInfo* info = block.type_data;
|
||||||
|
if(T.component_id >= info.deltas.length || info.deltas[T.component_id] == 0)return null;
|
||||||
return cast(T*)(cast(void*)&this + info.deltas[T.component_id]);
|
return cast(T*)(cast(void*)&this + info.deltas[T.component_id]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,23 @@
|
||||||
module ecs.manager;
|
module ecs.manager;
|
||||||
|
|
||||||
import std.experimental.allocator.mallocator : Mallocator, AlignedMallocator;
|
|
||||||
import std.experimental.allocator;
|
|
||||||
import std.traits;
|
|
||||||
import std.algorithm : max;
|
import std.algorithm : max;
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
|
import std.experimental.allocator;
|
||||||
|
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;
|
||||||
|
import std.traits;
|
||||||
|
|
||||||
import core.stdc.string;
|
|
||||||
import core.stdc.stdlib;
|
import core.stdc.stdlib;
|
||||||
|
import core.stdc.string;
|
||||||
|
|
||||||
import ecs.system;
|
|
||||||
import ecs.entity;
|
import ecs.entity;
|
||||||
import ecs.vector;
|
import ecs.entity_allocator;
|
||||||
import ecs.hash_map;
|
import ecs.hash_map;
|
||||||
import ecs.id_manager;
|
import ecs.id_manager;
|
||||||
import ecs.entity_allocator;
|
import ecs.system;
|
||||||
|
import ecs.vector;
|
||||||
|
|
||||||
alias gEM = EntityManager.instance;
|
alias gEM = EntityManager.instance;
|
||||||
|
alias SerializeVector = ecs.vector.Vector!ubyte;
|
||||||
|
|
||||||
class EntityManager
|
class EntityManager
|
||||||
{
|
{
|
||||||
|
|
@ -410,7 +411,7 @@ class EntityManager
|
||||||
{
|
{
|
||||||
ushort min = new_ids[i];
|
ushort min = new_ids[i];
|
||||||
int pos = i;
|
int pos = i;
|
||||||
foreach(int j;0..new_ids.length)
|
foreach(int j;i..new_ids.length)
|
||||||
{
|
{
|
||||||
if(new_ids[j] < min)
|
if(new_ids[j] < min)
|
||||||
{
|
{
|
||||||
|
|
@ -562,6 +563,11 @@ class EntityManager
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeEntity(EntityID id)
|
void removeEntity(EntityID id)
|
||||||
|
{
|
||||||
|
entities_to_remove.add(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __removeEntity(EntityID id)
|
||||||
{
|
{
|
||||||
//get entity and block meta data pointers
|
//get entity and block meta data pointers
|
||||||
Entity* entity = id_manager.getEntityPointer(id);
|
Entity* entity = id_manager.getEntityPointer(id);
|
||||||
|
|
@ -571,7 +577,6 @@ class EntityManager
|
||||||
id_manager.releaseID(id); //release id from manager
|
id_manager.releaseID(id); //release id from manager
|
||||||
|
|
||||||
removeEntityNoID(entity, block, true);
|
removeEntityNoID(entity, block, true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeEntityNoID(Entity* entity, EntitiesBlock* block,
|
private void removeEntityNoID(Entity* entity, EntitiesBlock* block,
|
||||||
|
|
@ -624,6 +629,25 @@ class EntityManager
|
||||||
return cast(EntitiesBlock*)(cast(size_t) pointer & (~cast(size_t)(page_size - 1)));
|
return cast(EntitiesBlock*)(cast(size_t) pointer & (~cast(size_t)(page_size - 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void removeEntites()
|
||||||
|
{
|
||||||
|
foreach(id;entities_to_remove)
|
||||||
|
{
|
||||||
|
__removeEntity(id);
|
||||||
|
}
|
||||||
|
entities_to_remove.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void begin()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void end()
|
||||||
|
{
|
||||||
|
removeEntites();
|
||||||
|
}
|
||||||
|
|
||||||
struct ComponentInfo
|
struct ComponentInfo
|
||||||
{
|
{
|
||||||
ushort size;
|
ushort size;
|
||||||
|
|
@ -710,6 +734,8 @@ class EntityManager
|
||||||
IDManager id_manager;
|
IDManager id_manager;
|
||||||
EntityAllocator allocator;
|
EntityAllocator allocator;
|
||||||
|
|
||||||
|
Vector!EntityID entities_to_remove;
|
||||||
|
|
||||||
HashMap!(ushort[], EntityInfo*) entities_infos;
|
HashMap!(ushort[], EntityInfo*) entities_infos;
|
||||||
HashMap!(string, uint) components_map;
|
HashMap!(string, uint) components_map;
|
||||||
Vector!System systems;
|
Vector!System systems;
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,27 @@
|
||||||
module tests.tests;
|
module tests.tests;
|
||||||
|
|
||||||
import ecs.manager;
|
|
||||||
import ecs.events;
|
|
||||||
import ecs.system;
|
|
||||||
import ecs.entity;
|
import ecs.entity;
|
||||||
|
import ecs.events;
|
||||||
|
import ecs.manager;
|
||||||
|
import ecs.system;
|
||||||
|
|
||||||
import core.time;
|
import core.time;
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
alias SerializeVector = ubyte[];
|
|
||||||
|
|
||||||
struct TestComp
|
struct TestComp
|
||||||
{
|
{
|
||||||
__gshared ushort component_id;
|
__gshared ushort component_id;
|
||||||
int a;
|
int a;
|
||||||
ulong b;
|
ulong b;
|
||||||
|
|
||||||
static void serializeComponent(ref TestComp comp, SerializeVector output)
|
static void serializeComponent(SerializeVector output)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deserializeComponent(ref TestComp comp, ubyte[] data)
|
static void deserializeComponent(ubyte[] data)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -52,12 +50,12 @@ int main()
|
||||||
uint gg; //good game
|
uint gg; //good game
|
||||||
uint bg; //bad game
|
uint bg; //bad game
|
||||||
|
|
||||||
static void serializeComponent(ref TestComp comp, SerializeVector output)
|
void serializeComponent(SerializeVector output)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deserializeComponent(ref TestComp comp, ubyte[] data)
|
void deserializeComponent(ubyte[] data)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue