mirror of https://github.com/axmolengine/axmol.git
148 lines
3.9 KiB
C++
148 lines
3.9 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
#include "SceneController.h"
|
|
#include "ComponentsTestScene.h"
|
|
#include "PlayerController.h"
|
|
#include "GameOverScene.h"
|
|
#include "EnemyController.h"
|
|
|
|
using namespace cocos2d;
|
|
using namespace cocos2d::extension;
|
|
|
|
SceneController::SceneController(void)
|
|
: _fAddTargetTime(0.0f)
|
|
, _fElapsedTime(0.0f)
|
|
, _targets(NULL)
|
|
, _projectiles(NULL)
|
|
{
|
|
m_strName = "SceneController";
|
|
}
|
|
|
|
SceneController::~SceneController(void)
|
|
{
|
|
if (_targets)
|
|
{
|
|
_targets->release();
|
|
_targets = NULL;
|
|
}
|
|
|
|
if (_projectiles)
|
|
{
|
|
_projectiles->release();
|
|
_projectiles = NULL;
|
|
}
|
|
}
|
|
|
|
bool SceneController::init()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
void SceneController::onEnter()
|
|
{
|
|
_fAddTargetTime = 1.0f;
|
|
|
|
_targets = new CCArray;
|
|
_projectiles = new CCArray;
|
|
|
|
((CCComAudio*)(m_pOwner->getComponent("Audio")))->playBackgroundMusic("background-music-aac.wav", true);
|
|
((CCComAttribute*)(m_pOwner->getComponent("ComAttribute")))->setInt("KillCount", 0);
|
|
}
|
|
|
|
void SceneController::onExit()
|
|
{
|
|
|
|
|
|
}
|
|
|
|
void SceneController::update(float delta)
|
|
{
|
|
// return;
|
|
_fElapsedTime += delta;
|
|
if (_fElapsedTime > _fAddTargetTime)
|
|
{
|
|
addTarget();
|
|
_fElapsedTime = 0.0f;
|
|
}
|
|
}
|
|
|
|
SceneController* SceneController::create(void)
|
|
{
|
|
SceneController * pRet = new SceneController();
|
|
if (pRet && pRet->init())
|
|
{
|
|
pRet->autorelease();
|
|
}
|
|
else
|
|
{
|
|
CC_SAFE_DELETE(pRet);
|
|
}
|
|
return pRet;
|
|
}
|
|
|
|
void SceneController::addTarget()
|
|
{
|
|
CCSprite *target = CCSprite::create("components/Target.png", CCRectMake(0,0,27,40));
|
|
m_pOwner->addChild(target, 1, 2);
|
|
|
|
target->addComponent(EnemyController::create());
|
|
target->setTag(2);
|
|
_targets->addObject(target);
|
|
}
|
|
|
|
void SceneController::spriteMoveFinished(CCNode* sender)
|
|
{
|
|
CCSprite *sprite = (CCSprite *)sender;
|
|
m_pOwner->removeChild(sprite, true);
|
|
|
|
if (sprite->getTag() == 2) // target
|
|
{
|
|
_targets->removeObject(sprite);
|
|
GameOverScene *gameOverScene = GameOverScene::create();
|
|
gameOverScene->getLayer()->getLabel()->setString("You Lose :[");
|
|
CCDirector::sharedDirector()->replaceScene(gameOverScene);
|
|
}
|
|
else if (sprite->getTag() == 3)
|
|
{
|
|
_projectiles->removeObject(sprite);
|
|
}
|
|
|
|
}
|
|
|
|
void SceneController::increaseKillCount()
|
|
{
|
|
int nProjectilesDestroyed = ((CCComAttribute*)(m_pOwner->getComponent("ComAttribute")))->getInt("KillCount");
|
|
|
|
CCComAttribute *p = (CCComAttribute*)(m_pOwner->getComponent("ComAttribute"));
|
|
p->setInt("KillCount", ++nProjectilesDestroyed);
|
|
|
|
if (nProjectilesDestroyed >= 5)
|
|
{
|
|
GameOverScene *gameOverScene = GameOverScene::create();
|
|
gameOverScene->getLayer()->getLabel()->setString("You Win!");
|
|
CCDirector::sharedDirector()->replaceScene(gameOverScene);
|
|
}
|
|
}
|