Example #1
0
void ItemFactory::addFood()
{
	//check if the food still exist
	if (m_pFood)
		return;

	//check the empty grid
	Snake* snake = dynamic_cast<Snake*>(getParent()->getChildByTag(eID_Snake));
	if (!snake)
		return;
	int left = m_pSnakeMap->getMovableNumbers() - snake->getLength();

	//no empty grids left, you win
	if (left <= 0)
		return;

	//set the food index
	int index = (int)(rand() % left + 1);
	auto mapIndex = m_pSnakeMap->getEmptyGridIndex(index);

	//create food model
	m_pFood = Food::create();
	this->addChild(m_pFood, 1, eID_Food);
	m_pFood->setPosition(VisibleRect::getVisibleRect().origin + VisibleRect::getHalfGridVec() + VisibleRect::getGridLength()*mapIndex);
	m_pFood->setIndex(mapIndex);

	//set the grid type
	m_pSnakeMap->setGridType(mapIndex, eType_Food);
}
Example #2
0
void ItemFactory::addDoor()
{
	//check if the doors exist
	if (m_pDoors.first != nullptr || m_pDoors.second != nullptr)
		return;

	//check the empty grid
	Snake* snake = m_pSnakeMap->getSnake();
	if (!snake)
		return;
	int left = m_pSnakeMap->getMovableNumbers() - snake->getLength();

	//no empty grids left
	if (left <= 0)
		return;

	//set the door index
	int index = (int)(rand() % left + 1);
	auto mapIndex = m_pSnakeMap->getEmptyGridIndex(index);

	//create door model
	m_pDoors.first = Door::create();
	this->addChild(m_pDoors.first, 1, eID_Door1);
	m_pDoors.first->setPosition(VisibleRect::getVisibleRect().origin + VisibleRect::getHalfGridVec() + VisibleRect::getGridLength()*mapIndex);
	m_pDoors.first->setIndex(mapIndex);

	//set the door transfer direction
	auto transferDir = randomDirection();
	m_pDoors.first->setTransferDirection(transferDir);
	//actual door direction is opposite to transfer direction
	m_pDoors.first->setRotation(arcByDirection(oppositeDirection(transferDir)));

	//set the grid type
	m_pSnakeMap->setGridType(mapIndex, eType_Door);

	//create the second door
	index = (int)(rand() % left + 1);
	mapIndex = m_pSnakeMap->getEmptyGridIndex(index);
	m_pDoors.second = Door::create();
	this->addChild(m_pDoors.second, 1, eID_Door2);
	m_pDoors.second->setPosition(VisibleRect::getVisibleRect().origin + VisibleRect::getHalfGridVec() + VisibleRect::getGridLength()*mapIndex);
	m_pDoors.second->setIndex(mapIndex);

	transferDir = randomDirection();
	m_pDoors.second->setTransferDirection(transferDir);
	m_pDoors.second->setRotation(arcByDirection(oppositeDirection(transferDir)));

	m_pSnakeMap->setGridType(mapIndex, eType_Door);

	//connect the two doors
	m_pDoors.first->setOtherDoor(m_pDoors.second);
	m_pDoors.second->setOtherDoor(m_pDoors.first);
}
Example #3
0
void ItemFactory::addBall(float dt)
{
	//check if the ball exist
	if (m_pBall)
		return;

	//check if the random time has finished
	if (m_fTimeToAddBall > 0)
	{
		m_fTimeToAddBall -= dt;
		return;
	}

	// it's time to add ball, check the empty grid
	Snake* snake = m_pSnakeMap->getSnake();
	if (!snake)
		return;
	int left = m_pSnakeMap->getMovableNumbers() - snake->getLength();

	//no empty grids left
	if (left <= 0)
		return;

	//set the star index
	int index = (int)(rand() % left + 1);
	auto mapIndex = m_pSnakeMap->getEmptyGridIndex(index);

	//create star model
	m_pBall = Ball::create();
	this->addChild(m_pBall, 1, eID_Ball);
	m_pBall->setPosition(VisibleRect::getVisibleRect().origin + VisibleRect::getHalfGridVec() + VisibleRect::getGridLength()*mapIndex);
	m_pBall->setIndex(mapIndex);

	//add a scale animation
	m_pBall->setScale(0.1f);
	auto scaleBig = ScaleTo::create(0.7f, 1.2f);
	auto scaleBack = ScaleTo::create(0.3f, 1.0f);
	auto sequence = Sequence::create(scaleBig, scaleBack, nullptr);
	m_pBall->runAction(sequence);

	//set the grid type
	m_pSnakeMap->setGridType(mapIndex, eType_Ball);

	//set the duration from [15, 25] seconds
	auto duration = rand() % 16 + 10.0f;
	m_pBall->setDuration(duration);
}