Exemplo n.º 1
0
void SCollision::update(Map * world, float frameTime)
{
	EntityManager * entM = world->getEntityManager();

	// if entity manager has changed, ( happens when switching worlds ), cache new entity manager's entities who have hitboxes
	if( this->entM != entM )
		startUp( entM );

	auto deletedlist = entM->getDeletedEntities();
	auto newlist = entM->getNewEntities();

	PPosition * pos1, * pos2;
	PBoundingBox * hitbox1, * hitbox2;
	PImage * img1, * img2;

	// query entity manager for newly created entities and
	// update system's vectors with new entities if new entities exist
	acknowledgeInserts( newlist, entM );

	for(size_t cache_index = 0; cache_index < pboxes.size(); cache_index++)
	{	
		pos1 = pposes[cache_index]; 
		hitbox1 = pboxes[cache_index];
		img1 = images[cache_index];
		
		// clear this entity of the previous pass's colliding entities.
		if ( cache_index == 0 )
			hitbox1->collidingEntities.clear();

		// if this entity was marked for deletion...
		if( removeDelEnt( deletedlist, hitbox1->entity_id, cache_index ) )
		{
			// continue on to next entity
			cache_index--;
			continue;
		}

		for( size_t cache_index_inner = cache_index+1; cache_index_inner < pboxes.size(); cache_index_inner++ )
		{
			pos2 = pposes[cache_index_inner];
			hitbox2 = pboxes[cache_index_inner];			
			img2 = images[cache_index_inner];

			if ( cache_index == 0 )
				hitbox2->collidingEntities.clear();

			// if this entity was marked for deletion...
			if( removeDelEnt( deletedlist, hitbox2->entity_id, cache_index_inner ) )
			{
				// continue on to next entity
				cache_index_inner--;
				continue;
			}			
			
			if(hitbox1->active && hitbox2->active)
			{	
				if(collisionOccurring(entM, pos1, hitbox1, pos2, hitbox2))
				{
					hitbox1->collidingEntities.push_back(hitbox2->entity_id);
					hitbox2->collidingEntities.push_back(hitbox1->entity_id);

					// notify scripts of collision event
					LuaDoa::fireEventMulti(ON_COLLIDE, hitbox1->entity_id, hitbox2->entity_id);
					LuaDoa::fireEventMulti(ON_COLLIDE, hitbox2->entity_id, hitbox1->entity_id);
				}		
			}
		}
	}
	// acknowledge the deletions so that the entity manager may remove marked entities from store
	for(auto it = deletedlist.begin(); it != deletedlist.end(); it++)
	{
		if(!entM->acknowledgedDelete[this->sys_id][(*it)])
			entM->acknowledgeDelete(this->sys_id, (*it));
	}
}