コード例 #1
0
        /*
            Modified from original.
            Now invisible sprites may still collide.
        */
		void Engine::TestForCollisions()
		{
			bool didCollisionOccur = false;
			std::list<Entity*>::iterator first;
			std::list<Entity*>::iterator second;
			Sprite *sprite1;
			Sprite *sprite2;
		
			first = p_entities.begin();
			while (first != p_entities.end() )
			{
				//we only care about sprite collisions
				if ( (*first)->getRenderType() == RENDER_2D )
				{
					//point local sprite to sprite contained in the list
					sprite1 = (Sprite*) *first;
					
					//if this entity is alive and visible...
					//if ( sprite1->getAlive() && sprite1->getVisible() && sprite1->isCollidable() )
                    if ( sprite1->getAlive() && sprite1->isCollidable() )
					{
						//test all other entities for collision
						second = p_entities.begin();
						while (second != p_entities.end() )
						{
							//point local sprite to sprite contained in the list
							sprite2 = (Sprite*) *second;
							
							//if other entity is active and not same as first entity...
							//if ( sprite2->getAlive() && sprite2->getVisible() && sprite2->isCollidable() && sprite1 != sprite2 ) 
                            if ( sprite2->getAlive() && sprite2->isCollidable() && sprite1 != sprite2 ) 
							{
								//test for collision
								if ( collision(sprite1, sprite2 ) ) 
								{
									//notify game of collision
									game_entityCollision( sprite1, sprite2 );
									didCollisionOccur = true;
									break;
								}
	
							}
							//go to the next sprite in the list
							second++;
						}
			
					} 
					//go to the next sprite in the list
					if (didCollisionOccur) break;
					first++;
				}//render2d
			} //while
		} 
コード例 #2
0
		void Engine::TestForCollisions()
		{
			std::list<Entity*>::iterator first;
			std::list<Entity*>::iterator second;
			Sprite *sprite1;
			Sprite *sprite2;
		
			first = p_entities.begin();
			while (first != p_entities.end() )
			{
				//we only care about sprite collisions
				if ( (*first)->getRenderType() == RENDER2D )
				{
					//point local sprite to sprite contained in the list
					sprite1 = (Sprite*) *first;
					
//*****CHAPTER 9 -- ADDED COLLISION PROPERTY, NEED TO UPDATE CHAPTER 3
					//if this entity is alive and visible...
//***optimization--collider property
					if ( sprite1->getAlive() && sprite1->getVisible() && sprite1->isCollidable() && sprite1->isCollider() )
					{
						//test all other entities for collision
						second = p_entities.begin();
						while (second != p_entities.end() )
						{
							//point local sprite to sprite contained in the list
							sprite2 = (Sprite*) *second;
							
//*****CHAPTER 9 -- ADDED COLLISION PROPERTY, NEED TO UPDATE CHAPTER 3
							//if other entity is active and not same as first entity...
							if ( sprite2->getAlive() && sprite2->getVisible() && sprite2->isCollidable() && sprite1 != sprite2 )
							{
								//test for collision
								if ( collision(sprite1, sprite2 ) ) {
								
									//notify game of collision
									game_entityCollision( sprite1, sprite2 );
								}
	
							}
							//go to the next sprite in the list
							second++;
						}
			
					} 
					//go to the next sprite in the list
					first++;
				}//render2d
			} //while
		}