so finally end with something that I am satisfy with, will not explain details as it is internally very similar to the previous post :
one native array for all components :
1 2 3
| CMPLabel labels[]; CMPAppereance apperances[]; etc.... |
an entity array and a fast index acces Hastable
1 2
| Entity entities[]; Hastable <Integer,Integer> entityNativeIndex; |
the new point is that I added an EntityManager that is responsible of adding new entity and/or components, every component added to an entity is added by the entitymanager every new entity is added by the entity manager, this enable good integrity and nivce looking code and good eficiency,
code looks like :
1 2 3 4 5 6 7 8 9
| EntityManager em=...; em.loadEntityFromDB(102); em.addEntity(103); CMPLabel l=em.addComponentLabel(103); l.value="label103"; CMPColor c=em.addCMPColor(103); c.value=0xFFFFFF; |
than later
1
| CMPVisible visibles=em.getVisibles(); |
and still able to threat componnents as blocks for systems or DB saving with cross access to other components of the current entity
1 2 3 4 5 6 7
| for(int n=0;n<visibles.length;n++) { doVisibleSomething(visibles[n]); Entity e=visibles[n].getEntity(); e.getAnotherComponent().value=3; } |
the EntityManager is responsible to maintain coherence between component & entity, all storage (component/entity) is made in native arrays, the Entity class is only usefull to enable cross component access and fast entity loading (in DB it give for an entity all components it use => direct DB modifcation are then inded not alowed to keep DB integrity).
I personally tend to optimise entity management in each program i make. I find that I always do it different.
I understand what you mean and have similar feeling than you, it would have been a lot easier (faster) to make something more specific but I really wanted to find something more generic to let 'more open ways for the futur'
I am curious to know, what sort of game/application you were developing this for?
this is for an online game : MMO and we can say in a certain manner MMORPG (even if there wont be any magic, potion, dwarf or such...)
EDIT: the slower part is to get an entity by its ID (use of the hashtable) but it should not be used a lot, all other acces are direct acces : all component keep a direct link to its entity and all entity keep a direct link to each of its components