Example #1
0
bool CGameLayer::init()
{
	if (!Layer::init())
	{
		return false;
	}
	
	m_visibleSize = Director::getInstance()->getVisibleSize();
	m_origin = Director::getInstance()->getVisibleOrigin();

	auto playItem = MenuItemImage::create("psd_2_.png", "psd_back.png", CC_CALLBACK_1(CGameLayer::gotoMenuScene, this));
	playItem->setPosition(Point(m_visibleSize.width - 25, m_visibleSize.height - 25));

	auto menu = Menu::create(playItem, NULL);
	menu->setPosition(Point::ZERO);

	this->addChild(menu);

	m_screenSize = CCDirector::sharedDirector()->getWinSize();

	auto sprite = GameSprite::create("background.png");
	sprite->setPosition(ccp(m_screenSize.width * 0.5, m_screenSize.height* 0.5));
	this->addChild(sprite, -1);

	auto gateBody = PhysicsBody::createBox(Size(m_visibleSize.width, 1.0f));
	gateBody->setDynamic(false);
	gateBody->setContactTestBitmask(true);
	gateBody->setContactTestBitmask(BORDER_CONTACT_BITMASK);
	gateBody->setCollisionBitmask(BORDER_CONTACT_BITMASK);
	auto gateNode = make_cc<Node>();
	gateNode->setPosition(Point(m_visibleSize.width / 2.0f, -1.0f));
	gateNode->setPhysicsBody(gateBody);
	addChild(gateNode);
	

	Point coordinates = { 0.1f, 0.85f };
	for (int i = 1; i <= MAX_NUMBER_BLOCKS; ++i)
	{
		CBlock* spr = new (std::nothrow) CBlock();
		spr->init(this, ccp(coordinates.x, coordinates.y), TAG_START_BLOCK + i);
		m_blocks.push_back(spr);
		coordinates.x += 0.1f;
		if (i % 9 == 0)
		{
			coordinates = { 0.1f, coordinates.y - 0.03f };
		}
	}

	auto edgeBody = PhysicsBody::createEdgeBox(m_visibleSize, PhysicsMaterial(0, 0.5f, 0), 0);
	edgeBody->setDynamic(false);
	auto edgeNode = Node::create();
	edgeNode->setPosition(Point(m_visibleSize.width / 2 + m_origin.x, m_visibleSize.height / 2 + m_origin.y));
	edgeNode->setPhysicsBody(edgeBody);
	this->addChild(edgeNode);
	
	m_player = new (std::nothrow) CRacket();
	m_player->init(this);
	m_player->SetPosition(Vec2(m_visibleSize.height - 50.0f, 100.0f));

	CBall *_ball = new (std::nothrow) CBall();
	_ball->init(this, g_gameState->m_acc * g_gameState->m_level, m_player->GetPosition());
	//this->addChild(_ball);
	m_ball.push_back(_ball);
	
	m_labelScore = CCLabelTTF::create("You score", "Arial", SIZE_LABEL);
	m_labelScore->setPosition(ccp(60.0f + 70.0f, m_visibleSize.height - SIZE_LABEL));
	m_labelScore->setColor(COLOR_LABEL);
	m_labelScore->setTag(TAG_SCORE_LABEL);
	this->addChild(m_labelScore);

	char score_buffer[10];
	sprintf(score_buffer, "%i", g_gameState->m_score);
	
	m_playerScoreLabel = CCLabelTTF::create("", "Arial", SIZE_LABEL);
	m_playerScoreLabel->setPosition(ccp(135.0f + 70.0f, m_visibleSize.height - SIZE_LABEL));
	m_playerScoreLabel->setColor(COLOR_LABEL);
	m_playerScoreLabel->setTag(TAG_PLAYER_SCORE_LABEL);
	m_playerScoreLabel->setString(score_buffer);
	this->addChild(m_playerScoreLabel);

	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = CC_CALLBACK_2(CGameLayer::onTouchBegan, this);
	listener->onTouchMoved = CC_CALLBACK_2(CGameLayer::onTouchMoved, this);
	listener->onTouchEnded = CC_CALLBACK_2(CGameLayer::onTouchEnded, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

	auto contactListener = EventListenerPhysicsContact::create();
	contactListener->onContactBegin = CC_CALLBACK_1(CGameLayer::onContactBegin, this);
	this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);
	
	this->setTouchEnabled(true);
	this->scheduleUpdate();
	
	return true;
}