/*! * Determines initial territory size for this player based on the size of the screen and the location of the initial touch. * Sets player identifier based on initial territory. * * @param screenBox Size of the screen. */ void Player::initTerritory(CCRect screenBox) { this->territory = CCRectMake(screenBox.origin.x, screenBox.origin.y, screenBox.size.width, screenBox.size.height); territory.size.width /= 2; if (startingPoint.x < screenBox.getMidX()) { territory.origin.x = screenBox.origin.x / 2; if (GameManager::sharedManager()->tabletDevice()) { if (startingPoint.y > screenBox.getMidY()) { this->_identifier = GameManager::kPlayer1; } else { this->_identifier = GameManager::kPlayer3; } } else { this->_identifier = GameManager::kPlayer1; } } else { territory.origin.x = screenBox.origin.x / 2 + screenBox.size.width / 2; if (GameManager::sharedManager()->tabletDevice()) { if (startingPoint.y > screenBox.getMidY()) { this->_identifier = GameManager::kPlayer2; } else { this->_identifier = GameManager::kPlayer4; } } else { this->_identifier = GameManager::kPlayer2; } } }
bool CBoxBehaviorState::IsPutable(CCSprite* sprite, CCPoint touchPos, CCPoint& avaliablePos) { auto arr = CObjectManager::getInstance()->getBox2dSprite(); CCPoint setPos; bool bIsEnable = true; CCRect rect; for (int i = 0; i <= 20; i++) { for (int j = 0; j <= 20; j++) { CCRect r; r.setRect(i * 105 + CScrollManager::getInstance()->getDeltaPosition().x, j * 105 + CScrollManager::getInstance()->getDeltaPosition().y, 105, 105); if (r.containsPoint(touchPos)) { setPos = ccp(r.getMidX(), r.getMidY()); rect = r; } } } for (int i = 0; i < arr->getSize(); i++) { auto anothersprite = arr->getObjectAt(i)->getSpritePtr(); if (rect.intersectsRect(anothersprite->getBoundingBox())) bIsEnable = false; } avaliablePos = setPos; return bIsEnable; }
void Ball::collideWithPaddle(Paddle* paddle) { CCRect paddleRect = paddle->rect(); paddleRect.origin.x += paddle->getPosition().x; paddleRect.origin.y += paddle->getPosition().y; float lowY = paddleRect.getMinY(); float midY = paddleRect.getMidY(); float highY = paddleRect.getMaxY(); float leftX = paddleRect.getMinX(); float rightX = paddleRect.getMaxX(); if (getPosition().x > leftX && getPosition().x < rightX) { bool hit = false; float angleOffset = 0.0f; if (getPosition().y > midY && getPosition().y <= highY + radius()) { setPosition( CCPointMake(getPosition().x, highY + radius()) ); hit = true; angleOffset = (float)M_PI / 2; } else if (getPosition().y < midY && getPosition().y >= lowY - radius()) { setPosition( CCPointMake(getPosition().x, lowY - radius()) ); hit = true; angleOffset = -(float)M_PI / 2; } if (hit) { float hitAngle = ccpToAngle(ccpSub(paddle->getPosition(), getPosition())) + angleOffset; float scalarVelocity = ccpLength(m_velocity) * 1.05f; float velocityAngle = -ccpToAngle(m_velocity) + 0.5f * hitAngle; m_velocity = ccpMult(ccpForAngle(velocityAngle), scalarVelocity); } } }
bool GameLayer::isCollisionRight(CCRect roleBox, CCRect collisionBox) { CCPoint targetPoint = ccp(roleBox.getMaxX(), roleBox.getMidY()); return collisionBox.containsPoint(targetPoint); }