-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:
Mergul 2018-09-16 14:16:18 +02:00
parent a066a97f01
commit 8fdb56e840
3 changed files with 43 additions and 18 deletions

View file

@ -21,6 +21,7 @@ struct Entity
{
EntityManager.EntitiesBlock* block = gEM.getMetaData(&this);
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]);
}
}

View file

@ -1,22 +1,23 @@
module ecs.manager;
import std.experimental.allocator.mallocator : Mallocator, AlignedMallocator;
import std.experimental.allocator;
import std.traits;
import std.algorithm : max;
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.string;
import ecs.system;
import ecs.entity;
import ecs.vector;
import ecs.entity_allocator;
import ecs.hash_map;
import ecs.id_manager;
import ecs.entity_allocator;
import ecs.system;
import ecs.vector;
alias gEM = EntityManager.instance;
alias SerializeVector = ecs.vector.Vector!ubyte;
class EntityManager
{
@ -410,7 +411,7 @@ class EntityManager
{
ushort min = new_ids[i];
int pos = i;
foreach(int j;0..new_ids.length)
foreach(int j;i..new_ids.length)
{
if(new_ids[j] < min)
{
@ -562,6 +563,11 @@ class EntityManager
}
void removeEntity(EntityID id)
{
entities_to_remove.add(id);
}
void __removeEntity(EntityID id)
{
//get entity and block meta data pointers
Entity* entity = id_manager.getEntityPointer(id);
@ -571,7 +577,6 @@ class EntityManager
id_manager.releaseID(id); //release id from manager
removeEntityNoID(entity, block, true);
}
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)));
}
void removeEntites()
{
foreach(id;entities_to_remove)
{
__removeEntity(id);
}
entities_to_remove.clear();
}
void begin()
{
}
void end()
{
removeEntites();
}
struct ComponentInfo
{
ushort size;
@ -710,6 +734,8 @@ class EntityManager
IDManager id_manager;
EntityAllocator allocator;
Vector!EntityID entities_to_remove;
HashMap!(ushort[], EntityInfo*) entities_infos;
HashMap!(string, uint) components_map;
Vector!System systems;

View file

@ -1,29 +1,27 @@
module tests.tests;
import ecs.manager;
import ecs.events;
import ecs.system;
import ecs.entity;
import ecs.events;
import ecs.manager;
import ecs.system;
import core.time;
import std.stdio;
int main()
{
alias SerializeVector = ubyte[];
struct TestComp
{
__gshared ushort component_id;
int a;
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 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)
{
}