2013-06-04 17:38:43 +08:00
|
|
|
#include "EnemyController.h"
|
|
|
|
#include "SceneController.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
EnemyController::EnemyController(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = "EnemyController";
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EnemyController::~EnemyController(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EnemyController::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnemyController::onEnter()
|
|
|
|
{
|
|
|
|
// Determine where to spawn the target along the Y axis
|
2013-07-12 06:24:23 +08:00
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
2013-06-04 17:38:43 +08:00
|
|
|
float minY = getOwner()->getContentSize().height/2;
|
|
|
|
float maxY = winSize.height - getOwner()->getContentSize().height/2;
|
|
|
|
int rangeY = (int)(maxY - minY);
|
|
|
|
// srand( TimGetTicks() );
|
|
|
|
int actualY = ( rand() % rangeY ) + (int)minY;
|
|
|
|
|
|
|
|
// Create the target slightly off-screen along the right edge,
|
|
|
|
// and along a random position along the Y axis as calculated
|
2013-06-15 14:03:30 +08:00
|
|
|
_owner->setPosition(
|
2013-07-12 14:11:55 +08:00
|
|
|
Point(winSize.width + (getOwner()->getContentSize().width/2),
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->getVisibleOrigin().y + actualY) );
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Determine speed of the target
|
|
|
|
int minDuration = (int)2.0;
|
|
|
|
int maxDuration = (int)4.0;
|
|
|
|
int rangeDuration = maxDuration - minDuration;
|
|
|
|
// srand( TimGetTicks() );
|
|
|
|
int actualDuration = ( rand() % rangeDuration ) + minDuration;
|
|
|
|
|
|
|
|
// Create the actions
|
2013-06-20 14:17:10 +08:00
|
|
|
FiniteTimeAction* actionMove = MoveTo::create( (float)actualDuration,
|
2013-07-12 14:11:55 +08:00
|
|
|
Point(0 - getOwner()->getContentSize().width/2, actualY) );
|
2013-06-20 14:17:10 +08:00
|
|
|
FiniteTimeAction* actionMoveDone = CallFuncN::create(getOwner()->getParent()->getComponent("SceneController"),
|
2013-06-04 17:38:43 +08:00
|
|
|
callfuncN_selector(SceneController::spriteMoveFinished));
|
2013-06-20 14:17:10 +08:00
|
|
|
_owner->runAction( Sequence::create(actionMove, actionMoveDone, NULL) );
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnemyController::onExit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnemyController::update(float delta)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
EnemyController* EnemyController::create(void)
|
|
|
|
{
|
|
|
|
EnemyController * pRet = new EnemyController();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnemyController::die()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Component *com = _owner->getParent()->getComponent("SceneController");
|
|
|
|
cocos2d::Array *_targets = ((SceneController*)com)->getTargets();
|
2013-06-15 14:03:30 +08:00
|
|
|
_targets->removeObject(_owner);
|
|
|
|
_owner->removeFromParentAndCleanup(true);
|
2013-06-04 17:38:43 +08:00
|
|
|
((SceneController*)com)->increaseKillCount();
|
|
|
|
}
|
|
|
|
|