Jump to content
  • Advertisement
Sign in to follow this  
HexDump

Entity manager loop opinions

This topic is 2499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

//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.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!