#include "Missile.h" #include "2d/CCSprite.h" #include "physics/CCPhysicsBody.h" #include "PhysicsShapeCache.h" #include "base/CCEventDispatcher.h" #include "Zombie.h" #include "SmokeEffect.h" #include "GameObjectBitmask.h" #include "Tank.h" USING_NS_CC; Missile::Missile() { static int num; log("missile Num %d", num); autorelease(); setName("Missile" + std::to_string(num++)); } Missile::~Missile() { } void Missile::CreateMissileSprite(const std::string & filename) { sprite = Sprite::createWithSpriteFrameName(filename); sprite->setAnchorPoint(Vec2{ 0.5f, 0.5f }); setContentSize(sprite->getContentSize()); setAnchorPoint(Vec2{ 0.5f, 0.5f }); sprite->setPosition(getAnchorPointInPoints()); addChild(sprite); } void Missile::CreatePhysicsBody(const std::string & filename) { physicsBody = PhysicsShapeCache::getInstance()->createBodyWithName(filename); int bitmask = GameObjectBitmask::MAP | GameObjectBitmask::ZOMBIE | GameObjectBitmask::ZOMBIE_POOL; physicsBody->setCategoryBitmask(GameObjectBitmask::MISSILE); physicsBody->setCollisionBitmask(bitmask); physicsBody->setContactTestBitmask(bitmask); addComponent(physicsBody); } void Missile::CreateContactEvent() { auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = [this](PhysicsContact & contact)->bool { auto nodeA{ contact.getShapeA()->getBody()->getNode() }; auto nodeB{ contact.getShapeB()->getBody()->getNode() }; log("This : %s", this->getName().c_str()); log("nodeA : %s", nodeA->getName().c_str()); log("nodeB : %s", nodeB->getName().c_str()); log("Missile Contact Position : %f, %f", this->getPosition().x, this->getPosition().y); /*Zombie* zombie{ zombie = dynamic_cast(nodeA) }; if (zombie == nullptr) { zombie = dynamic_cast(nodeB); } zombie->GetDamaged(this->GetDamage()); if (zombie->GetCurrentHP() <= 0) { zombie->removeFromParent(); }*/ SmokeEffect* smokeEffect{ new SmokeEffect{} }; smokeEffect->CreateSprite(); smokeEffect->CreateAnimation(); smokeEffect->setPosition(this->getPosition()); this->getParent()->addChild(smokeEffect); this->removeFromParent(); return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener->clone(), this); } void Missile::SetVelocity(float velocity) { physicsBody->setVelocity(direction * velocity); } void Missile::SetDamage(float damage) { this->damage = damage; } int Missile::GetDamage() const { return damage; } void Missile::SetDirection(const cocos2d::Vec2 & direction) { this->direction = direction.getNormalized(); setRotation(CC_RADIANS_TO_DEGREES(atan2(-direction.y, direction.x))); } void Missile::SetAngle(float angle) { setRotation(angle); float theta{ CC_DEGREES_TO_RADIANS(angle) }; direction.x = cos(theta); direction.y = sin(-theta); } void Missile::update(float delta) { }