2013-06-04 17:38:43 +08:00
|
|
|
#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)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = "SceneController";
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SceneController::~SceneController(void)
|
|
|
|
{
|
2013-08-22 11:12:09 +08:00
|
|
|
CC_SAFE_RELEASE(_targets);
|
|
|
|
CC_SAFE_RELEASE(_projectiles);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SceneController::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneController::onEnter()
|
|
|
|
{
|
|
|
|
_fAddTargetTime = 1.0f;
|
|
|
|
|
2013-08-22 11:12:09 +08:00
|
|
|
_targets = Array::createWithCapacity(10);
|
|
|
|
_projectiles = Array::createWithCapacity(10);
|
|
|
|
|
|
|
|
_targets->retain();
|
|
|
|
_projectiles->retain();
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
((ComAudio*)(_owner->getComponent("Audio")))->playBackgroundMusic("background-music-aac.wav", true);
|
|
|
|
((ComAttribute*)(_owner->getComponent("ComAttribute")))->setInt("KillCount", 0);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
Sprite *target = Sprite::create("components/Target.png", Rect(0,0,27,40));
|
2013-06-15 14:03:30 +08:00
|
|
|
_owner->addChild(target, 1, 2);
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
target->addComponent(EnemyController::create());
|
|
|
|
target->setTag(2);
|
|
|
|
_targets->addObject(target);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void SceneController::spriteMoveFinished(Node* sender)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite *sprite = (Sprite *)sender;
|
2013-06-15 14:03:30 +08:00
|
|
|
_owner->removeChild(sprite, true);
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
if (sprite->getTag() == 2) // target
|
|
|
|
{
|
|
|
|
_targets->removeObject(sprite);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto gameOverScene = GameOverScene::create();
|
2013-06-04 17:38:43 +08:00
|
|
|
gameOverScene->getLayer()->getLabel()->setString("You Lose :[");
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(gameOverScene);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
else if (sprite->getTag() == 3)
|
|
|
|
{
|
|
|
|
_projectiles->removeObject(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneController::increaseKillCount()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
int nProjectilesDestroyed = ((ComAttribute*)(_owner->getComponent("ComAttribute")))->getInt("KillCount");
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
ComAttribute *p = (ComAttribute*)(_owner->getComponent("ComAttribute"));
|
2013-06-04 17:38:43 +08:00
|
|
|
p->setInt("KillCount", ++nProjectilesDestroyed);
|
|
|
|
|
|
|
|
if (nProjectilesDestroyed >= 5)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto gameOverScene = GameOverScene::create();
|
2013-06-04 17:38:43 +08:00
|
|
|
gameOverScene->getLayer()->getLabel()->setString("You Win!");
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(gameOverScene);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|