Example #1
0
void World::AddPlayer(int curTime)
{
	WorldPlayer p;
	p.color = sf::Color(rand() % 255, rand() % 255, rand() % 255, 255).toInteger();
	p.ID = players.size();
	p.joinTime = curTime;
	p.time = curTime;
	players.push_back(p);

	if (std::sqrt(players.size() + 0.1f) > balls.size() + 1.0f)
		AddBall(curTime);
}
Ball* BallManager::CreateBall(cocos2d::Node* parent, bool bypass)
{
	//or would you prefer templates?
	static const auto createBallFromType = [](int type)
	{
		Ball* newBall = nullptr;
		switch (type)
		{
		case Ball::type:		newBall = Ball::create(); break;
		case WalletBall::type:	newBall = WalletBall::create(); break;
		case BombBall::type:	newBall = BombBall::create(); break;
		case PowderBall::type:	newBall = PowderBall::create(); break;
		case OilBall::type:		newBall = OilBall::create(); break;
		case RocketBall::type:	newBall = RocketBall::create(); break;
		case BombOther::type:	newBall = BombOther::create(); break;
		}
		return newBall;
	};
	//random enabled ball based on chances
	//	example:
	//	Ball: 60, WalletBall: 5   -> _totalChance = 65
	//	random number up to total = 63
	//	not smaller than 60, take away the 60, leaving 3
	//	3 smaller than WalletBalls 5, to create a WalletBall
	int ballType = 0;
	float rnd = RandomHelper::random_real(0.0f, _totalChance);
	for (const auto enabledType : _enabledBalls)
	{
		float chance = _ballChance[enabledType];
		if (rnd <= chance)
		{
			ballType = enabledType;
			break;
		}
		rnd -= chance;
	}
	//create new ball
	Ball* newBall = createBallFromType(ballType);
	AddBall(parent, newBall);
	return newBall;
}
void BallDispencer::DropBall()
{
	Ball* toDrop = _containedBalls.front();
	Vec2 posRelativeToDispencer = toDrop->getPosition();
	Vec2 posRelativeToWorld = this->convertToWorldSpace(posRelativeToDispencer);
	toDrop->retain();
	toDrop->removeFromParentAndCleanup(false);
	getParent()->addChild(toDrop);
	toDrop->setPosition(posRelativeToWorld);
	toDrop->release();
	toDrop->Drop();
	toDrop->setZOrder(0);
	_containedBalls.erase(_containedBalls.begin());
	//move up next balls
	for (int i = 0; i < _containedBalls.size(); i++)
	{
		_containedBalls[i]->MoveToNext(_nodes[14 - i]->getPosition(), 14 - i);
	}
	//top up
	AddBall();
}