It seems that this question arises over and over again in my head. It involves handling the collision of entities.
I can think of two ways for handling it.
Option 1: First function to flag colliding entities. Second function to update their stats accordingly
Option 2: One function to check collision and set stats in same loop.
1.
Code: Select all
void Game::UpdateModel()
{
HandleCollision( entityA, entityB );
UpdateEntityStats( entityA, entityB );
}
void Game::HandleCollision( std::vector<Entity> &A, std::vector<Entity> &B )
{
for( A )
{
for( B )
{
if entity A collides with B;
{
A.is_colliding = true;
A.collides_with = B
B.is_colliding = true;
B.collides_with = A;
}
}
}
}
void Game::UpdateEntityStats( std::vector<Entity> &A, std::vector<Entity> &B )
{
for( A )
{
for( B )
{
if A.is_colliding
{
if A.collides_with == B
{
A.hp -= B.damage;
B.hp -= A.damage;
}
}
}
}
}
2.
Code: Select all
void Game::UpdateModel()
{
HandleCollision( entityA, entityB );
}
void Game::HandleCollision( std::vector<Entity> &A, std::vector<Entity> &B )
{
for( A )
{
for( B )
{
if entity A collides with B;
{
A.hp -= B.damage;
B.hp -= A.damage;
}
}
}
}
I think the second option is straight forward and fewer lines of code. However, the second option could be useful if perhaps you're concerned with encapsulation.
I figure you experienced programmers can set me straight in this trivial design desision. Thanks!