void GameLevelLayer::handleHazardCollisions(Player* pThePlayer)
{
    std::vector<tileInfo> tiles = getSurroundingTilesAtPosition(pThePlayer->getPosition(), m_pHazardLayer);
    
    for (int i=0; i<tiles.size();++i)
    {
        Rect tileRect = Rect(tiles[i].x,
                             tiles[i].y,
                             m_pMap->getTileSize().width,
                             m_pMap->getTileSize().height);
        
        Rect pRect = pThePlayer->getCollisionBoundBox();
        
        if (tiles[i].gid && pRect.intersectsRect(tileRect))
        {
            gameOver(false);
        }
    }
}
void CollisionEngine::checkForAndResolveCollisions(Trap *trap){

	CCArray *tiles = getSurroundingTilesAtPosition(trap->getPosition(), walls);

		trap->onGround = false;

		for (int tileIndx = 0; tileIndx < tiles->count(); tileIndx++) {

			CCRect pRect = trap->collisionBoundingBox();
			CCDictionary *dic = (CCDictionary *) tiles->objectAtIndex(tileIndx);
			CCXNumericData *tgidData = (CCXNumericData *) dic->objectForKey("gid");
			if (tgidData->getIntValue() > 0) {

				CCXNumericData *rectOrgXData = (CCXNumericData *) dic->objectForKey(
						"x");
				CCXNumericData *rectOrgYData = (CCXNumericData *) dic->objectForKey(
						"y");

				CCRect tileRect = CCRectMake(rectOrgXData->getFloatValue(),
						rectOrgYData->getFloatValue(), map->getTileSize().width,
						map->getTileSize().height);

				// the colision is not detected.
				if (trap->collisionBoundingBox().intersectsRect(tileRect)) {
					CCRect intersection = CCRectIntersection(pRect, tileRect);
					if (tileIndx == 0) {
						//tile is directly below player
						trap->desiredPosition = ccp(trap->desiredPosition.x,
								trap->desiredPosition.y
										+ intersection.size.height);
						trap->velocity = ccp(trap->velocity.x, 0.0);
						trap->onGround = true;
						//this->player->mightAsWellJump = false;
					} else if (tileIndx == 1) {
						//tile is directly above player
						trap->desiredPosition = ccp(trap->desiredPosition.x,
								trap->desiredPosition.y
										- intersection.size.height);
						trap->velocity = ccp(trap->velocity.x, 0.0);
					} else if (tileIndx == 2) {
						//tile is left of player
						trap->desiredPosition = ccp(
								trap->desiredPosition.x + intersection.size.width,
								trap->desiredPosition.y);
						//CCLOG("left collision stopAllActions %f\n",intersection.size.width);
						trap->velocity.x = 0;
					} else if (tileIndx == 3) {
						//tile is right of player
						trap->desiredPosition = ccp(
								trap->desiredPosition.x - intersection.size.width,
								trap->desiredPosition.y);
						trap->velocity.x = 0;
					} else {
						if (intersection.size.width > intersection.size.height) {
							//tile is diagonal, but resolving collision vertially
							trap->velocity = ccp(trap->velocity.x, 0.0); //////Here
							float resolutionHeight;
							if (tileIndx > 5) {
								resolutionHeight = intersection.size.height;
								trap->onGround = true; //////Here
								//this->player->mightAsWellJump = false;
							} else {
								resolutionHeight = -intersection.size.height;
							}
							trap->desiredPosition = ccp(trap->desiredPosition.x,
									trap->desiredPosition.y + resolutionHeight);

						} else {
							float resolutionWidth;
							if (tileIndx == 6 || tileIndx == 4) {
								resolutionWidth = intersection.size.width;
							} else {
								resolutionWidth = -intersection.size.width;
							}
							trap->desiredPosition = ccp(
									trap->desiredPosition.x + resolutionWidth,
									trap->desiredPosition.y);
						}
					}
				}
			}
		}
		trap->setPosition(trap->desiredPosition);

}
Ejemplo n.º 3
0
void		Enemy::checkForAndResolveCollisions()
{


	CCPoint playerPos = getPosition();
	CCArray* tiles = getSurroundingTilesAtPosition(playerPos);
	CCObject* obj;
	_onGround = false;

	bool isSideHit = false;
	CCARRAY_FOREACH(tiles,obj)	
	{
		CCDictionary* dict = (CCDictionary*)obj;



		Integer* x = (Integer*)dict->objectForKey("x");
		Integer* y = (Integer*)dict->objectForKey("y");
		float height = _map->getTileSize().height;
		float width = _map->getTileSize().width;
		//_debugDraw->appendRect(ccp(x->getValue(), y->getValue()), width, height);


		CCRect pRect = collisionBoundingBox();

		//_debugDraw->appendRect(ccp(pRect.getMinX(), pRect.getMinY()), pRect.getMaxX() - pRect.getMinX() , pRect.getMaxY() - pRect.getMinY(), 1.0f, 0.0f, 0.0f);
		Integer* gid = (Integer*)dict->objectForKey("gid");
		if(gid->getValue())
		{

			CCRect tileRect = CCRectMake((float)x->getValue(),(float)y->getValue(), _map->getTileSize().width, _map->getTileSize().height);
	
			if(pRect.intersectsRect(tileRect))
			{

				CCRect intersectRect = pRect.intersectsWithRect(tileRect);	
				int tileIndx = tiles->getIndexOfObject(obj);
				//CCLOG("tileIndx %d : " ,tileIndx);
				if(tileIndx == 0)
				{
					//CCLOG("0 intersect ");
					//_debugDraw->appendRect(ccp(intersectRect.getMinX(), intersectRect.getMinY()), intersectRect.getMaxX() - intersectRect.getMinX() , intersectRect.getMaxY() - intersectRect.getMinY(), 1.0f, 0.0f, 0.0f);
					_onGround = true;
					setVelocity(ccp(getVelocity().x, 0.0f));
					setDesiredPosition(ccp(getDesiredPosition().x, getDesiredPosition().y + intersectRect.size.height));
					//_player->setDesiredPosition(ccp(_player->getDesiredPosition().x, 13));
					//CCLOG("Player box %f", tileRect.getMaxY() - pRect.getMinY());
					//CCLOG("tile box %f", tileRect.getMaxY());
					//CCLOG("intersectRect box %f", intersectRect.size.height);
				}
				else if(tileIndx == 1)
				{
					//CCLOG("1 intersect ");
					
					setDesiredPosition(ccp(getDesiredPosition().x, getDesiredPosition().y - intersectRect.size.height));
				}	
				else if(tileIndx == 2)
				{
					//CCLOG("2 intersect ");
					isSideHit = true;
					setVelocity(ccp(0.0f,getVelocity().y));
					setDesiredPosition(ccp(getDesiredPosition().x  + intersectRect.size.width, getDesiredPosition().y));
				}
				else if(tileIndx == 3)
				{
					//CCLOG("3 intersect ");
					isSideHit = true;
					setVelocity(ccp(0.0f,getVelocity().y));
					setDesiredPosition(ccp(getDesiredPosition().x  - intersectRect.size.width, getDesiredPosition().y));
				}
				else
				{
					if(intersectRect.size.width > intersectRect.size.height)
					{
						float resolutionHeight;
						if(tileIndx > 5)
						{
							resolutionHeight  = intersectRect.size.height;
							setVelocity(ccp(getVelocity().x, 0.0f));
							_onGround = true;
						}
						else
						{
							resolutionHeight  = -intersectRect.size.height;
						}
						setDesiredPosition(ccp(getDesiredPosition().x, getDesiredPosition().y + resolutionHeight ));
					}
					else
					{
						float resolutionWidth;
						if (tileIndx == 6 || tileIndx == 4) 
						{
							resolutionWidth = intersectRect.size.width;
						}
						else
						{
							resolutionWidth = -intersectRect.size.width;
						}
						setDesiredPosition(ccp(getDesiredPosition().x + resolutionWidth, getDesiredPosition().y ));
					}
					//if((tileIndx == 5 || tileIndx == 4) && getVelocity().y > 0 && !isSideHit)

				}
			}
		}

	}
void GameLevelLayer::checkForAndResolveCollisions(Player *pThePlayer)
{
    std::vector<tileInfo> tiles = getSurroundingTilesAtPosition(pThePlayer->getPosition(), m_pWallLayer); //1
    
    if (m_isGameOver)
    {
        return;
    }
    
    pThePlayer->setOnGroundFlag(false);
    
    for (int i=0; i<tiles.size();++i)
    {
        Rect pRect = pThePlayer->getCollisionBoundBox(); //2
        
        int gid = tiles[i].gid; //3
        
        if (gid)
        {
            Rect tileRect = Rect(tiles[i].x,
                                 tiles[i].y,
                                 m_pMap->getTileSize().width,
                                 m_pMap->getTileSize().height);
            
            if (pRect.intersectsRect(tileRect))
            {
                // 5
                // This is replicating CGRectIntersection
                Rect intersection = Rect(std::max(pRect.getMinX(),tileRect.getMinX()),
                                    std::max(pRect.getMinY(),tileRect.getMinY()),
                                    0,
                                    0);
                
                intersection.size.width = std::min(pRect.getMaxX(),
                                                   tileRect.getMaxX()) - intersection.getMinX();
                intersection.size.height = std::min(pRect.getMaxY(),
                                                    tileRect.getMaxY()) - intersection.getMinY();
                
                int tileIndx = i; //6
                
                if (tileIndx == 0)
                {
                    //tile is directly below Koala
                    pThePlayer->setDesiredPosition(Vec2(pThePlayer->getDesiredPosition().x,
                                                        pThePlayer->getDesiredPosition().y + intersection.size.height));

                    pThePlayer->setVelocity(Vec2(pThePlayer->getGelocity().x, 0.0));
                    pThePlayer->setOnGroundFlag(true);
                }
                else if (tileIndx == 1)
                {
                    //tile is directly above Koala
                    pThePlayer->setDesiredPosition(Vec2(pThePlayer->getDesiredPosition().x, pThePlayer->getDesiredPosition().y - intersection.size.height));
                   pThePlayer->setVelocity(Vec2(pThePlayer->getGelocity().x, 0.0));
                }
                else if (tileIndx == 2)
                {
                    //tile is left of Koala
                    pThePlayer->setDesiredPosition(Vec2(pThePlayer->getDesiredPosition().x + intersection.size.width, pThePlayer->getDesiredPosition().y));
                }
                else if (tileIndx == 3)
                {
                    //tile is right of Koala
                    pThePlayer->setDesiredPosition(Vec2(pThePlayer->getDesiredPosition().x - intersection.size.width, pThePlayer->getDesiredPosition().y));
                }
                else
                {
                    if (intersection.size.width > intersection.size.height)
                    { //7
                        //tile is diagonal, but resolving collision vertically
                        pThePlayer->setVelocity(Vec2(pThePlayer->getGelocity().x, 0.0));
                        
                        float resolutionHeight;
                        if (tileIndx > 5)
                        {
                            resolutionHeight = intersection.size.height;
                            pThePlayer->setOnGroundFlag(true);
                        }
                        else
                        {
                            resolutionHeight = -intersection.size.height;
                        }
                        pThePlayer->setDesiredPosition(Vec2(pThePlayer->getDesiredPosition().x, pThePlayer->getDesiredPosition().y + intersection.size.height ));
                    }
                    else
                    {
                        //tile is diagonal, but resolving horizontally
                        float resolutionWidth;
                        
                        if (tileIndx == 6 || tileIndx == 4)
                        {
                            resolutionWidth = intersection.size.width;
                        }
                        else
                        {
                            resolutionWidth = -intersection.size.width;
                        }
                        pThePlayer->setDesiredPosition(Vec2(pThePlayer->getDesiredPosition().x, pThePlayer->getDesiredPosition().y + resolutionWidth));
                    } 
                } 
            }
        } 
    }
    pThePlayer->setPosition(pThePlayer->getDesiredPosition()); //7
}
Ejemplo n.º 5
0
void Level::checkForAndResolveCollisions(Player * p)
{
	__Array * tiles = getSurroundingTilesAtPosition(p->getPosition(), _walls);
	
	if (_gameOver) {
		return;
	}
	p->setOnGround(false);
	
	Ref* obj = nullptr;
	CCARRAY_FOREACH(tiles, obj)
	{
		Gid * dic = dynamic_cast<Gid*>(obj);
		Rect pRect = p->collisionBoundingBox();
		
		int gid = dic->_gid;
		
		if (gid) {
			Rect tileRect = Rect(dic->_x, dic->_y, _map->getTileSize().width, _map->getTileSize().height); //5
			if (pRect.intersectsRect(tileRect)) {
				
				Rect intersection = Rect(std::max(pRect.getMinX(), tileRect.getMinX()), std::max(pRect.getMinY(), tileRect.getMinY()), 0, 0);
				intersection.size.width = std::min(pRect.getMaxX(), tileRect.getMaxX()) - intersection.getMinX();
				intersection.size.height = std::min(pRect.getMaxY(), tileRect.getMaxY()) - intersection.getMinY();

				int tileIndx = tiles->getIndexOfObject(dic);
				
				if (tileIndx == 0) {
					//tile is directly below player
					p->_desiredPosition = Point(p->_desiredPosition.x, p->_desiredPosition.y + intersection.size.height);
					p->_velocity = Point(p->_velocity.x, 0.0);
					p->setOnGround(true);
				} else if (tileIndx == 1) {
					//tile is directly above player
					p->_desiredPosition = Point(p->_desiredPosition.x, p->_desiredPosition.y - intersection.size.height);
					p->_velocity = Point(p->_velocity.x, 0.0);
				} else if (tileIndx == 2) {
					//tile is left of player
					p->_desiredPosition = Point(p->_desiredPosition.x + intersection.size.width, p->_desiredPosition.y);
				} else if (tileIndx == 3) {
					//tile is right of player
					p->_desiredPosition = Point(p->_desiredPosition.x - intersection.size.width, p->_desiredPosition.y);
				} else {
					if (intersection.size.width > intersection.size.height) {
						//tile is diagonal, but resolving collision vertially
						p->_velocity = Point(p->_velocity.x, 0.0);
						float resolutionHeight;
						if (tileIndx > 5) {
							resolutionHeight = -intersection.size.height;
							p->setOnGround(true);
						} else {
							resolutionHeight = intersection.size.height;
						}
						
						p->_desiredPosition = Point(p->_desiredPosition.x, p->_desiredPosition.y + resolutionHeight );
						
					} else {
						float resolutionWidth;
						if (tileIndx == 6 || tileIndx == 4) {
							resolutionWidth = intersection.size.width;
						} else {
							resolutionWidth = -intersection.size.width;
						}
						p->_desiredPosition = Point(p->_desiredPosition.x + resolutionWidth , p->_desiredPosition.y);
						
					}
				}
			}
		}
	}