#include “Bullet.h”
#define MOVE_SPEED 650
#define COUNT_BULLETS 5000
Bullet::Bullet()
{
this->shutingBullets=0;
this->nBullets = 0;
this->hasbeen = false;
}
Bullet::~Bullet()
{
this->shutingBullets=0;
this->hasbeen = false;
this->m_allBulletArray->release();
this->m_loadedArray->release();
}
bool Bullet::init()
{
if(!CCLayer::init())
{
return false;
}
initImage();
initIdleBulletSpool();
schedule(schedule_selector(Bullet::aotuLoadBullets),0);
schedule(schedule_selector(Bullet::shotBullets),0);
return true;
}
void Bullet::initImage()
{
const char* ImageList={“game/Bullets.png”};
for (int i=0;i<1;i++)
{
CCTextureCache::sharedTextureCache()->addImage(ImageList*);
}
}
void Bullet::initIdleBulletSpool()
{
this->winSize=CCDirector::sharedDirector()->getWinSize();
CCTexture2D* texture=CCTextureCache::sharedTextureCache()->textureForKey(“game/Bullets.png”);
m_bulletSprite = CCSpriteBatchNode::createWithTexture(texture,COUNT_BULLETS);
m_bulletSprite->setPosition(CCPointZero);
this->addChild(m_bulletSprite);
m_allBulletArray = CCArray::create();
m_allBulletArray->retain();
m_loadedArray=CCArray::create();
m_loadedArray->retain();
//批量渲染
for(int i =0;i<COUNT_BULLETS;i++)
{
CCSprite* newBullet=CCSprite::createWithTexture(m_bulletSprite->getTexture());
newBullet->setAnchorPoint(ccp(0,0));
newBullet->setPosition(ccp(0,230));
this->m_allBulletArray->addObject(newBullet);
newBullet->setVisible(false);
m_bulletSprite->addChild(newBullet);
}
}
void Bullet::aotuLoadBullets(float pt)
{
this->loadBullets(5);
}
void Bullet::loadBullets(int count)
{
if(!m_loadedArray->count())
{
getIdleBullet(count);
}
}
void Bullet::getIdleBullet(int count)
{
if (this->m_loadedArray->count() == 0)
{
int pair = count/2;
if(this->nBullets!=COUNT_BULLETS)
{
for (int i=0;i<count;++i)
{
CCSprite* newBullet =NULL;
newBullet = (CCSprite*)this->m_allBulletArray->objectAtIndex(this->nBullets );
this->nBullets ++;
if (i<=pair)
{newBullet->setRotation(10i);}
else if (i>pair)
{newBullet->setRotation(-(10(i-pair)));}
this->m_loadedArray->addObject(newBullet);
newBullet->setVisible(true);
}
}
else
{
this->shutingBullets=0;
for (int i=0;i<COUNT_BULLETS;i++)
{
CCSprite* oldBullets =(CCSprite*) m_allBulletArray->objectAtIndex(i);
this->m_allBulletArray->removeObjectAtIndex(i);
this->m_allBulletArray->retain();
this->nBullets=0;
}
}
}
}
void Bullet::shotBullets(float pt)
{
if(this->hasbeen){
for (int i=0;im_loadedArray->count();i++)
{
CCSprite* bullet = (CCSprite*)this->m_loadedArray->objectAtIndex(i);
float temhud=0;
float pointX =bullet->getPositionX();
float pointY =bullet->getPositionY();
if (i<=2)
{
temhud = -(8i3.14)/180;
}
else
{
temhud= ((8*(i-2)*3.14)/180);
}
bullet->setPosition(ccp(pointX+std::cos(temhud)MOVE_SPEEDpt,pointY+std::sin(temhud)MOVE_SPEEDpt));
if(bullet->getPositionX()>winSize.width)
{
this->m_loadedArray->removeObjectAtIndex(i);
this->m_bulletSprite->removeChild(bullet,false);
if (!this->m_loadedArray->count())
{
this->hasbeen =false;
}
}
}
}
}