That code snippet was from memory, and was a little wrong... here's something correct with the adding code. I have many other things going on in my update logic but here's the bare bones:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| Entity[] entities = new Entity[32]; int size = 0;
public void add( Entity e ) { if ( size >= entities.length ) { entities = Arrays.copyOf( entities, size + (size >> 1) ); } entites[ size++ ] = e; }
public void update( Entity e ) { int alive = 0; for (int i = 0; i < size; i++) { Entity e = entities[i]; e.update(); if ( e.isActive() ) { entities[ alive++ ] = e; } } while ( size > alive ) { entities[ --size ] = null; } } |
And yes, I've optimized this code because I always end up having thousands, often tens of thousands, sometimes a hundred thousand entities being managed at once...