Exemplo n.º 1
0
void GameController::erase( Node* node )
{
	if(node)
	{
		auto body = node->getPhysicsBody();
		auto type = body->getCollisionBitmask();
		switch ( type )
		{
		case 1://player
			break;
		case 2: //enemy
			enemies.eraseObject( (EnemyCB*) node );
			break;
		case 3: //arrow
			//GameController::_player->nrOfArrows -= 1;
			bullets.eraseObject( (Bullet*) node );
			break;
		case 4: // bullet
			//bullets.eraseObject( (Bullet*) node );
			break;
		}
		node->removeFromParentAndCleanup( true );
	}
}
bool gameLevel2::onContactBegins(PhysicsContact &contact) {

	auto userDefault = CCUserDefault::getInstance()->sharedUserDefault();
	auto nodeA = contact.getShapeA()->getBody();
	auto nodeB = contact.getShapeB()->getBody();

	
	if (nodeA->getContactTestBitmask() == 5 && nodeB->getContactTestBitmask() == 9 || nodeA->getContactTestBitmask() == 9 && nodeB->getContactTestBitmask() == 5)
	{
		isEnemyReady = false;

		if (soundState)CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Sounds/explosionships.mp3");


		gameLevel2::shipExplosions(enemy->getPosition(),false);	//BOOOOOM


		nodeA->getNode()->removeFromParentAndCleanup(true);
		nodeB->getNode()->removeFromParentAndCleanup(true);

		
		scoreLbl->setString(to_string(score += enemyScore));
		scoreLbl->setPosition(Vec2(scoreIcon->getPositionX() + scoreLbl->getContentSize().width + 10, origin.y + visibleSize.height - 40));

		enemyCountLbl->setString(to_string(--enemyCount));

		if (enemyCount == 2) gameLevel2::createSecondEnemy();
		if (enemyCount == 1) gameLevel2::createThirdEnemy();
		if (enemyCount == 0)
		{
			this->stopAllActionsByTag(1);
			this->stopAllActionsByTag(2);

			isShipAlive = false;

			if (soundState)CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Sounds/shipexit.mp3");

			ship->runAction(Sequence::create(MoveTo::create(0.5, Vec2(origin.x + visibleSize.width / 2, ship->getPositionY())), MoveTo::create(0.5, Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height)),NULL));


			userDefault->setIntegerForKey("score", score);

			if (score > userDefault->getIntegerForKey("highscore",0))
				userDefault->setIntegerForKey("highscore", score);

			userDefault->setIntegerForKey("level", 2);

			userDefault->flush();
			Director::getInstance()->replaceScene(TransitionFade::create(2.2, gameWinScene::createScene(), Color3B(0, 0, 0)));
		}

	}

	if (nodeA->getContactTestBitmask() == 8 && nodeB->getContactTestBitmask() == 1 || nodeA->getContactTestBitmask() == 1 && nodeB->getContactTestBitmask() == 8)
	{
		if (soundState)CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Sounds/wav/explosion.wav");

		if (--shipHealt == 0)
		{
			isShipAlive = false;
			this->stopAllActionsByTag(1);
			this->stopAllActionsByTag(2);

			gameLevel2::shipExplosions(ship->getPosition(),false);	//BOOOOOM

			nodeA->getNode()->removeFromParentAndCleanup(true);
			nodeB->getNode()->removeFromParentAndCleanup(true);

			if (soundState)CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Sounds/explosionships.mp3");

			userDefault->setIntegerForKey("score", score);

			if (score > userDefault->getIntegerForKey("highscore",0))
				userDefault->setIntegerForKey("highscore", score);

			userDefault->flush();
			
			Director::getInstance()->replaceScene(TransitionFade::create(1, gameOverScene::createScene(), Color3B(0, 0, 0)));
		}
		else
		{

			if (nodeA->getCollisionBitmask() == 4)
			{
				gameLevel2::shipExplosions(nodeA->getNode()->getPosition(), true);	//LITTLE BOOOM
				nodeA->getNode()->removeFromParentAndCleanup(true);
			}
			else
			{
				gameLevel2::shipExplosions(nodeB->getNode()->getPosition(), true);	//LITTLE BOOOM
				nodeB->getNode()->removeFromParentAndCleanup(true);
			}
				

			

			healtLbl->setString(to_string(shipHealt));
		}
	}
	
	return true;
}