2013-06-04 17:38:43 +08:00
|
|
|
#include "ProjectileController.h"
|
|
|
|
#include "SceneController.h"
|
|
|
|
#include "EnemyController.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
2013-10-15 18:00:03 +08:00
|
|
|
using namespace cocostudio;
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
ProjectileController::ProjectileController(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = "ProjectileController";
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ProjectileController::~ProjectileController(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProjectileController::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectileController::onEnter()
|
|
|
|
{
|
2014-03-26 16:39:32 +08:00
|
|
|
ComController::onEnter();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto winSize = Director::getInstance()->getVisibleSize();
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
2013-07-12 14:11:55 +08:00
|
|
|
_owner->setPosition( Point(origin.x+20, origin.y+winSize.height/2) );
|
2013-06-15 14:03:30 +08:00
|
|
|
_owner->setTag(3);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto com = _owner->getParent()->getComponent("SceneController");
|
2013-12-28 14:34:52 +08:00
|
|
|
static_cast<SceneController*>(com)->getProjectiles().pushBack(_owner);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectileController::onExit()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectileController::update(float delta)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto com = _owner->getParent()->getComponent("SceneController");
|
|
|
|
auto _targets = ((SceneController*)com)->getTargets();
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto projectile = dynamic_cast<Sprite*>(_owner);
|
|
|
|
auto projectileRect = Rect(
|
2013-06-04 17:38:43 +08:00
|
|
|
projectile->getPosition().x - (projectile->getContentSize().width/2),
|
|
|
|
projectile->getPosition().y - (projectile->getContentSize().height/2),
|
|
|
|
projectile->getContentSize().width,
|
|
|
|
projectile->getContentSize().height);
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
std::vector<Node*> targetsToDelete;
|
|
|
|
targetsToDelete.reserve(20);
|
|
|
|
|
|
|
|
for (const auto& target : _targets)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
Rect targetRect = Rect(
|
2013-06-04 17:38:43 +08:00
|
|
|
target->getPosition().x - (target->getContentSize().width/2),
|
|
|
|
target->getPosition().y - (target->getContentSize().height/2),
|
|
|
|
target->getContentSize().width,
|
|
|
|
target->getContentSize().height);
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// if (Rect::RectIntersectsRect(projectileRect, targetRect))
|
2013-06-04 17:38:43 +08:00
|
|
|
if (projectileRect.intersectsRect(targetRect))
|
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
targetsToDelete.push_back(target);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
for (const auto& t : targetsToDelete)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
static_cast<EnemyController*>(t->getComponent("EnemyController"))->die();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
if (!targetsToDelete.empty())
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectileController* ProjectileController::create(void)
|
|
|
|
{
|
|
|
|
ProjectileController * pRet = new ProjectileController();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-07-25 05:02:36 +08:00
|
|
|
void freeFunction( Node *ignore )
|
|
|
|
{
|
|
|
|
log("hello");
|
|
|
|
}
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
void ProjectileController::move(float flocationX, float flocationY)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto winSize = Director::getInstance()->getVisibleSize();
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
2013-06-04 17:38:43 +08:00
|
|
|
// Determinie offset of location to projectile
|
2013-06-15 14:03:30 +08:00
|
|
|
float offX = flocationX - _owner->getPosition().x;
|
|
|
|
float offY = flocationY - _owner->getPosition().y;
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
// Bail out if we are shooting down or backwards
|
|
|
|
if (offX <= 0) return;
|
|
|
|
|
|
|
|
// Ok to add now - we've double checked position
|
|
|
|
|
|
|
|
|
|
|
|
// Determine where we wish to shoot the projectile to
|
2013-06-15 14:03:30 +08:00
|
|
|
float realX = origin.x + winSize.width + (_owner->getContentSize().width/2);
|
2013-06-04 17:38:43 +08:00
|
|
|
float ratio = offY / offX;
|
2013-06-15 14:03:30 +08:00
|
|
|
float realY = (realX * ratio) + _owner->getPosition().y;
|
2013-07-12 14:11:55 +08:00
|
|
|
Point realDest = Point(realX, realY);
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
// Determine the length of how far we're shooting
|
2013-06-15 14:03:30 +08:00
|
|
|
float offRealX = realX - _owner->getPosition().x;
|
|
|
|
float offRealY = realY - _owner->getPosition().y;
|
2013-06-04 17:38:43 +08:00
|
|
|
float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY));
|
|
|
|
float velocity = 480/1; // 480pixels/1sec
|
|
|
|
float realMoveDuration = length/velocity;
|
|
|
|
|
2013-07-25 05:02:36 +08:00
|
|
|
auto callfunc = CallFuncN::create(
|
|
|
|
CC_CALLBACK_1(
|
|
|
|
SceneController::spriteMoveFinished,
|
|
|
|
static_cast<SceneController*>( getOwner()->getParent()->getComponent("SceneController")
|
|
|
|
) ) );
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-07-25 05:02:36 +08:00
|
|
|
// Move projectile to actual endpoint
|
|
|
|
_owner->runAction(
|
|
|
|
Sequence::create(
|
|
|
|
MoveTo::create(realMoveDuration, realDest),
|
|
|
|
callfunc,
|
|
|
|
NULL)
|
|
|
|
);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectileController::die()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto com = _owner->getParent()->getComponent("SceneController");
|
2013-12-28 14:34:52 +08:00
|
|
|
auto& _projectiles = static_cast<SceneController*>(com)->getProjectiles();
|
|
|
|
_projectiles.eraseObject(_owner);
|
2013-06-15 14:03:30 +08:00
|
|
|
_owner->removeFromParentAndCleanup(true);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|