cocos2dx 로 간단 한 단기 어로 달인 게임 (4)

7427 단어 cocos2dx
물고기 가 다 썼 습 니 다. 총알 을 쓰기 시 작 했 습 니 다. 총알 과 물고기의 디자인 모델 은 대략 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
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 BulletLive;//   
	std::listBulletDead;//   
};

#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()
{
}

좋은 웹페이지 즐겨찾기