鱼写好了,开始写子弹,子弹和鱼的设计模式大概差不多
bullet.h
#ifndef _Bullet_H_
#define _Bullet_H_
#include"cocos2d.h"
using namespace cocos2d;
enum BULLETTYPE
{
BulletTypeOne,
BulletTypeTwo
};
class Bullet:public cocos2d::Sprite
{
public:
Bullet(BULLETTYPE t);
~Bullet();
bool init() override;
static Bullet*create(BULLETTYPE t);
//修改方向
void setDir(float dir)
{
this->dir = dir;
}
//获取子弹的类型
BULLETTYPE getBulletType()
{
return this->type;
}
void onEnter() override;
void onExit() override;
void setLive(bool isLive)
{
this->isLive = isLive;
}
double getAtt()
{
return att;
}
int getCostCoin()
{
return this->costCoin;
}
void bulletBack();//子弹死亡动画的回调函数
void DeadCartoonOne();//子弹一的死亡动画
void DeadCartoonTwo();//子弹二的死亡动画
bool koufei;//用于gamelayer中子弹发出扣金币
private:
BULLETTYPE type;//子弹种类
void update(float dt);//默认调度函数
float dir;//子弹的移动方向角度
double att;//子弹的攻击值
bool isLive;//是否存活
int speed;//子弹的速度
int costCoin;//子弹花费的金币
};
#endif // !_Bullet_H_
bullet.cpp
#include "Bullet.h"
#include"BulletManage.h"
Bullet::Bullet(BULLETTYPE t):
type(t)
{
switch (type)
{
case BulletTypeOne:
att = 4;//子弹的攻击力
costCoin = 7;//子弹花费的金币
break;
case BulletTypeTwo:
att = 5;//子弹的攻击力
costCoin = 1;//子弹花费的金币
break;
default:
break;
}
speed = 700;//子弹的速度
}
Bullet::~Bullet()
{
}
bool Bullet::init()
{
switch (type)
{
case BulletTypeOne:
if (!initWithFile("/bullet/bullet1.png"))
{
return false;
}
break;
case BulletTypeTwo:
if (!initWithFile("/bullet/bullet2.png"))
{
return false;
}
break;
default:
break;
}
//开启默认调度器
scheduleUpdate();
koufei=false;//用于gamelayer中子弹发出扣金币
return true;
}
Bullet * Bullet::create(BULLETTYPE t)
{
Bullet*ret = new(std::nothrow) Bullet(t);
if (ret&&ret->init())
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
return ret;
}
void Bullet::onEnter()
{
Node::onEnter();//
koufei = false;//用于gamelayer中子弹发出扣金币
//重新将子弹的攻击力和金币花费还原还原
switch (type)
{
case BulletTypeOne:
this->costCoin = 7;
this->att = 7;
break;
case BulletTypeTwo:
this->costCoin = 1;
this->att = 5;
break;
default:
break;
}
//开启默认调度器
scheduleUpdate();
isLive = true;
}
void Bullet::onExit()
{
Node::onExit();
}
void Bullet::bulletBack()
{
BulletManage::getInstance()->collection(this);
}
void Bullet::DeadCartoonOne()
{
//子弹的金币花费为0
this->costCoin = 0;
//子弹的攻击力赋为0
this->att = 0;
//1创建animation-ref
Animation* act = Animation::create();
//2添加精灵帧
act->addSpriteFrameWithFile("/bullet/bullet1bom.png");
//3设置动画的状态
act->setDelayPerUnit(0.3);//设置帧速率
act->setLoops(1);//设置循环的次数,-1表示一直循环
act->setRestoreOriginalFrame(true);//设置是否在动画结束后回到原始帧
//4.Animation添加到Animate中(创建Animate)
Animate* animate = Animate::create(act);
CallFunc* callFunc = CallFunc::create(this,
callfunc_selector(Bullet::bulletBack));//回调函数
Sequence* seqAct = Sequence::create(animate, callFunc, nullptr);//序列动作:...传无限多个参数,以nullptr结尾
this->runAction(seqAct);
}
void Bullet::DeadCartoonTwo()
{
//子弹的金币花费为0
this->costCoin = 0;
//子弹的攻击力赋为0
this->att = 0;
//1创建animation-ref
Animation* act = Animation::create();
//2添加精灵帧
act->addSpriteFrameWithFile("/bullet/bullet2bom.png");
//3设置动画的状态
act->setDelayPerUnit(0.3);//设置帧速率
act->setLoops(1);//设置循环的次数,-1表示一直循环
act->setRestoreOriginalFrame(true);//设置是否在动画结束后回到原始帧
//4.Animation添加到Animate中(创建Animate)
Animate* animate = Animate::create(act);
CallFunc* callFunc = CallFunc::create(this,
callfunc_selector(Bullet::bulletBack));//回调函数
Sequence* seqAct = Sequence::create(animate, callFunc, nullptr);//序列动作:...传无限多个参数,以nullptr结尾
this->runAction(seqAct);
}
void Bullet::update(float dt)
{
if (!isLive)//死亡
{
switch (type)
{
case BulletTypeOne:
DeadCartoonOne();//第一种子弹的死亡动画
break;
case BulletTypeTwo:
DeadCartoonTwo();//第二种子弹的死亡动画
break;
default:
break;
}
return;
}
Vec2 pos = getPosition();//获取原先的位置
Size winSize = Director::getInstance()->getWinSize();//屏幕的大小
/* 如果子弹碰了屏幕就开始反弹*/
if ( getPositionX()<0 || getPositionX()>winSize.width||getPositionY()<0)
{
this->dir = -(dir);//方向改变
this->setRotation(dir * 180 / 3.14159);//角度旋转
}
else if(getPositionY()>winSize.height)//子弹出了上边屏幕
{
if (dir>=0)
{
this->dir = dir + 3.14159 / 2;//方向改变
this->setRotation(dir * 180 / 3.14159);//角度旋转
}
else if(dir<0)
{
this->dir = dir - 3.14159 / 2;//方向改变
this->setRotation(dir * 180 / 3.14159);//角度旋转
}
}else if(getPositionY()<=20)//子弹出了下边屏幕
{
if (dir>=0)
{
this->dir = dir - 3.14159 / 2;//方向改变
this->setRotation(dir * 180 / 3.14159);//角度旋转
}
else if (dir<0)
{
this->dir = dir + 3.14159 / 2;//方向改变
this->setRotation(dir * 180 / 3.14159);//角度旋转
}
}
//子弹一直上升
Vec2 Dir = (Vec2(sin(dir), cos(dir)));
setPosition(getPosition() + Dir *dt*speed);
}
bulletFactory.h
#ifndef _BulletFactory_H_
#define _BulletFactory_H_
#include "Bullet.h"
class BulletFactory
{
public:
BulletFactory();
~BulletFactory();
static Bullet*create(BULLETTYPE type);
private:
};
#endif // !1
bulletFactory.cpp
#include "BulletFactory.h"
#include"BulletManage.h"
BulletFactory::BulletFactory()
{
}
BulletFactory::~BulletFactory()
{
}
Bullet * BulletFactory::create(BULLETTYPE type)
{
//定义一个子弹对象指针
Bullet*pBullet = nullptr;
//在管理类中查找是否有符合条件的子弹
pBullet = BulletManage::getInstance()->findDeath(type);
if (pBullet==nullptr)
{
//三种类型对应三个不同的攻击力
pBullet = Bullet::create(type);
}
//将引用计数+1,防止内存被释放
pBullet->retain();
//子弹添加到生存池
BulletManage::getInstance()->add(pBullet);
return pBullet;
}
bulletManage.h
#ifndef _BulletManage_H_
#define _BulletManage_H_
#include<list>
class Bullet;
enum BULLETTYPE;
class BulletManage
{
public:
~BulletManage();
static BulletManage*getInstance()
{
if (pInstance == nullptr)
{
pInstance = new BulletManage;
}
return pInstance;
}
void add(Bullet*bullet);//添加子弹
void collection(Bullet*bullet);//消除子弹
//根据类型查找子弹
Bullet*findDeath(BULLETTYPE type);
public:
BulletManage();
static BulletManage* pInstance;
std::list<Bullet*> BulletLive;//生存池
std::list<Bullet*>BulletDead;//死亡池
};
#endif // !_BulletManage_H_
bulletManage.cpp
#include "BulletManage.h"
#include"Bullet.h"
BulletManage*BulletManage::pInstance = nullptr;
BulletManage::~BulletManage()
{
//引用计数减一
for (Bullet*bullet : BulletLive)
bullet->release();
//清理链表
BulletDead.clear();
for (Bullet*bullet : BulletDead)
bullet->release();
BulletDead.clear();
}
void BulletManage::add(Bullet * bullet)
{
BulletLive.push_back(bullet);//子弹添加到生存池
}
void BulletManage::collection(Bullet * bullet)
{
bullet->removeFromParent();//子弹从父节点上移除
BulletLive.remove(bullet);//生存池移除
BulletDead.push_back(bullet);//死亡池添加
}
Bullet * BulletManage::findDeath(BULLETTYPE type)
{
for (Bullet*bullet:BulletDead)
{
if (type==bullet->getBulletType())
{
BulletDead.remove(bullet);
return bullet;
}
}
return nullptr;
}
BulletManage::BulletManage()
{
}