C++ 与cocos2d-x-4.0完成太空飞机大战 (三)

动画演示

游戏画面演示

飞机炸弹精灵编码:BombSprite.cpp


#include "BombSprite.h"
#include "ActionBase.h"
#include "Util.h"

namespace SpaceWar {

	BombSprite::BombSprite()
	{
	}

	BombSprite::~BombSprite()
	{
	}

	void BombSprite::update(float dt)
	{
	}
}

飞机炸弹精灵编码:BombSprite.h


#pragma once
#include "cocos2d.h"
#include "SpriteBase.h"

using namespace std;
using namespace cocos2d;

namespace SpaceWar
{
	class ActionBase;

	class BombSprite : public SpriteBase
	{
	public:
		BombSprite();
		virtual ~BombSprite();

		void update(float dt);

		ActionBase* actBase = nullptr;

		/*玩家标识,0:玩家1,1:玩家2*/
		int playerTag = 0;

		/*炸弹类型*/
		int bombType = 0;

		/*炸弹速度*/
		int bombSpeed = 4;

		/*炸弹威力*/
		int bombPower = 6;

		/*碰撞次数*/
		int collisionCount = 0;

		/*最大碰撞次数*/
		int maxCollisionCount = 0;

		/*碰撞类型掩码*/
		int category = 0b111;

		/*碰撞掩码*/
		int collision = 0b000;

		/*碰撞事件掩码*/
		int contact = 0b110;

		/*是否允许重力*/
		bool gravityEnable = false;

		/*是否允许碰撞*/
		bool collisionEnable = false;
	};
}



飞机子弹精灵编码:BulletSprite.cpp


#include "BulletSprite.h"
#include "ActionBase.h"
#include "Util.h"

namespace SpaceWar {

	BulletSprite::BulletSprite()
	{
	}

	BulletSprite::~BulletSprite()
	{
	}

	void BulletSprite::update(float dt)
	{
		if (isTrack)
		{
			Vec2 pointStart = this->getPosition();
			Node* trackNode = Util::findNode(&actBase->enemyNodes, trackKey);
			if (trackNode != nullptr)
			{
				trackPoint = trackNode->getPosition();
				moveSpeed = Util::getTrackPosition(bulletSpeed, pointStart, trackPoint);
			}
			Vec2 newPosition = pointStart + moveSpeed;
			float deltaRotation = Util::getTrackRotation(pointStart, newPosition);
			this->setPosition(newPosition);
			this->setRotation(deltaRotation);
			outSceneRemove();
		}
	}

	void BulletSprite::outSceneRemove()
	{
		if (Util::isOutScene(this, actBase->vSize))
		{
			this->stopAllActions();
			this->removeFromParentAndCleanup(true);
		}
	}
}


飞机子弹精灵编码:BulletSprite.h


#pragma once
#include <map>
#include <string>
#include "cocos2d.h"
#include "SpriteBase.h"

using namespace std;
using namespace cocos2d;

namespace SpaceWar
{
	class ActionBase;

	class BulletSprite : public SpriteBase
	{
	public:
		BulletSprite();
		virtual ~BulletSprite();

		void update(float dt);

		void outSceneRemove();

		ActionBase* actBase = nullptr;

		/*玩家标识,0:玩家1,1:玩家2*/
		int playerTag = 0;

		/*子弹类型*/
		int bulletType = 0;

		/*子弹速度*/
		float bulletSpeed = 4.0f;

		/*子弹威力*/
		int bulletPower = 2;

		/*是否是追踪弹*/
		bool isTrack = false;

		/*追踪对象键*/
		string trackKey = "";

		/*追踪对象位置*/
		Vec2 trackPoint = Vec2(0, 0);

		/*每帧移动速度*/
		Vec2 moveSpeed = Vec2(0, 0);

		/*碰撞次数*/
		int collisionCount = 0;

		/*最大碰撞次数*/
		int maxCollisionCount = 0;

		/*碰撞类型掩码*/
		int category = 0b111;

		/*碰撞掩码*/
		int collision = 0b000;

		/*碰撞事件掩码*/
		int contact = 0b110;

		/*是否允许重力*/
		bool gravityEnable = false;

		/*是否允许碰撞*/
		bool collisionEnable = false;

	};
}



精灵基类编码:SpriteBase.cpp


#include "SpriteBase.h"

namespace SpaceWar {

	SpriteBase::SpriteBase()
	{
	}

	SpriteBase::~SpriteBase()
	{
	}

	SpriteBase* SpriteBase::createWithTexture(SpriteBase* sprite, Texture2D *texture)
	{
		if (sprite && sprite->initWithTexture(texture))
		{
			sprite->autorelease();
			return sprite;
		}
		CC_SAFE_DELETE(sprite);
		return nullptr;
	}

	SpriteBase* SpriteBase::createWithTexture(SpriteBase* sprite, Texture2D *texture, const Rect& rect, bool rotated)
	{
		if (sprite && sprite->initWithTexture(texture, rect, rotated))
		{
			sprite->autorelease();
			return sprite;
		}
		CC_SAFE_DELETE(sprite);
		return nullptr;
	}

	SpriteBase* SpriteBase::create(SpriteBase* sprite, const std::string& filename)
	{
		if (sprite && sprite->initWithFile(filename))
		{
			sprite->autorelease();
			return sprite;
		}
		CC_SAFE_DELETE(sprite);
		return nullptr;
	}

	SpriteBase* SpriteBase::create(SpriteBase* sprite, const PolygonInfo& info)
	{
		if (sprite && sprite->initWithPolygon(info))
		{
			sprite->autorelease();
			return sprite;
		}
		CC_SAFE_DELETE(sprite);
		return nullptr;
	}

	SpriteBase* SpriteBase::create(SpriteBase* sprite, const std::string& filename, const Rect& rect)
	{
		if (sprite && sprite->initWithFile(filename, rect))
		{
			sprite->autorelease();
			return sprite;
		}
		CC_SAFE_DELETE(sprite);
		return nullptr;
	}

	SpriteBase* SpriteBase::createWithSpriteFrame(SpriteBase* sprite, SpriteFrame *spriteFrame)
	{
		if (sprite && spriteFrame && sprite->initWithSpriteFrame(spriteFrame))
		{
			sprite->autorelease();
			return sprite;
		}
		CC_SAFE_DELETE(sprite);
		return nullptr;
	}

	SpriteBase* SpriteBase::createWithSpriteFrameName(SpriteBase* sprite, const std::string& spriteFrameName)
	{
		SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);

#if COCOS2D_DEBUG > 0
		char msg[256] = { 0 };
		sprintf(msg, "Invalid spriteFrameName: %s", spriteFrameName.c_str());
		CCASSERT(frame != nullptr, msg);
#endif

		return createWithSpriteFrame(sprite, frame);
	}

	SpriteBase* SpriteBase::create(SpriteBase* sprite)
	{
		if (sprite && sprite->init())
		{
			sprite->autorelease();
			return sprite;
		}
		CC_SAFE_DELETE(sprite);
		return nullptr;
	}
}


精灵基类编码:SpriteBase.h


#pragma once
#include "cocos2d.h"

using namespace std;
using namespace cocos2d;


namespace SpaceWar
{
	class SpriteBase : public Sprite
	{
	public:
		SpriteBase();
		virtual ~SpriteBase();

		static const int INDEX_NOT_INITIALIZED = -1;

		static SpriteBase* create(SpriteBase* sprite);

		static SpriteBase* create(SpriteBase* sprite, const std::string& filename);

		static SpriteBase* create(SpriteBase* sprite, const PolygonInfo& info);

		static SpriteBase* create(SpriteBase* sprite, const std::string& filename, const Rect& rect);

		static SpriteBase* createWithTexture(SpriteBase* sprite, Texture2D *texture);

		static SpriteBase* createWithTexture(SpriteBase* sprite, Texture2D *texture, const Rect& rect, bool rotated = false);

		static SpriteBase* createWithSpriteFrame(SpriteBase* sprite, SpriteFrame *spriteFrame);

		static SpriteBase* createWithSpriteFrameName(SpriteBase* sprite, const std::string& spriteFrameName);

		/*类型 0: 玩家, 1:敌人*/
		int spriteType = 0;
	};
}



C++ 与cocos2d-x-4.0完成太空飞机大战 (四)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值