This days I’m refactoring code and one of the things I want to improve is my entity manager code. More precisely, the update funcion where entities are updated. My engine is a 2D tile based engine that I’m using for an iphone game. The idea I had was to have the entity update splitted in several tasks because I’m willing to move to component based entities when I have the time. This is a Physic task, a collision detection task, AI task, etc…
For long time I have been using a simple update() function for my entity update that was called by the entity manager in a loop. Now I have come across another way to do it (in the end it is the same) but gives me some advantages like for example differnt ticks for the different tasks (Physics can run at a differnt refresh rate than collisions, etc…)
Here is the source code. I would be really happy if anyone could comment, give opinions, or improve it. Sorry ti is ObjC but I think it is pretty easy to understand:
//Run the update process o nall entities.
for(int i=0; i< numGameObjects;++i)
{
GameObject* go=_entities[i];
//If this game object was marked to be delted don't do any process with it.
//It will be released when update loop is finished.
if(! [go isToBeDeleted])
{
//Player does its physics (Movement, etc...)
//We save old position to let the onMapCollision know
//The previous position before physics were applied.
CGPoint oldPos=[go position];
[go doPhysics];
if([go collisionEnabled])
{
//Check collisions against map
CollisionMask collisionMask=[go collisionMask];
if(collisionMask & MapEntity)
{
bool collision=[self checkEntityVsMapCollision:go];
if(collision==true)
[go onMapCollision:oldPos];
}
//Check collisions against other entities (just the ones that have not been checked previously)
for(int j=i+1; j < numGameObjects; ++j)
{
GameObject* otherGO= _entities[j];
EntityType type=[go type];
if( (collisionMask & type) && [go collidesWith:otherGO])
{
[go onEntityCollision:otherGO];
[otherGO onEntityCollision:go];
}
}
}
//Execute AI for this game object
[go doLogic];
}
}
There could be some sintactical errors in the code but I just wanted to show the idea. Sorry for the long question, but I thought I had to explain everything as clear as I could.
Thanks in advance.
So it looks like a fine loop. I wouldn’t see any reason to change it unless you are having problems. But since you posted here I am going to assume you are having performance problems.
This chunk:
Perhaps I am missing something, it does you oldPos, but does this have anything to do with an individual game object? Can [self checkEntityVsMapCollision] just be called once before the loop?
You could also cache of if an object has moved. If something hasn’t moved it doesn’t need to do a collision check. Every other object will still check if it hit (so your inner for loop will need to start from 0) but then you save the full collision check for non-moving objects.
Overall it looks like good code, you already have Physics and logic broken out, so you can update the logic in a separate loop that is called less often as you mentioned in the post.