-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyBullet.cpp
86 lines (71 loc) · 1.86 KB
/
MyBullet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "MyBullet.h"
#include "EnemyPlane.h"
#include "CCBullet.h"
#include "cocos2d.h"
USING_NS_CC;
MyBullet *MyBullet::m_pSharedMyBullet = NULL;
const float MyBullet::DEFAULT_SPEED = 30.0f;
MyBullet *MyBullet::newMyBullet()
{
m_pSharedMyBullet = MyBullet::create();
return m_pSharedMyBullet;
}
MyBullet *MyBullet::getSharedMyBullet()
{
if (m_pSharedMyBullet == NULL)
m_pSharedMyBullet = MyBullet::create();
return m_pSharedMyBullet;
}
bool MyBullet::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
m_pArrayOfBullet = CCArray::create();
m_pArrayOfBullet->retain();
this->schedule(schedule_selector(MyBullet::move));
this->schedule(schedule_selector(MyBullet::hit));
bRet = true;
} while (0);
return bRet;
}
void MyBullet::isOver(CCBullet *pDelBullet)
{
m_pArrayOfBullet->removeObject(pDelBullet);
pDelBullet->isOver();
}
void MyBullet::addNewBullet(CCPoint from)
{
CCBullet *pNewBullet = CCBullet::createWithPowerAndId(DEFAULT_POWER, 1);
// Add to task team.
m_pArrayOfBullet->addObject(pNewBullet);
// Add to layer.
pNewBullet->setPosition(from);
this->addChild(pNewBullet);
}
void MyBullet::move(float dt)
{
CCDirector *pDirector = CCDirector::sharedDirector();
for (int i = 0; i < m_pArrayOfBullet->count(); )
{
CCBullet *pCurBullet = (CCBullet*)m_pArrayOfBullet->objectAtIndex(i);
pCurBullet->setPositionY(pCurBullet->getPositionY() + DEFAULT_SPEED);
if (pCurBullet->getPositionY() - pCurBullet->getContentSize().height / 2 > pDirector->getVisibleSize().height)
isOver(pCurBullet);
else
i++;
}
}
void MyBullet::hit(float dt)
{
EnemyPlane *pEnemyPlane = EnemyPlane::getSharedEnemyPlane();
for (int i = 0; i < m_pArrayOfBullet->count(); )
{
CCBullet *pCurBullet = (CCBullet*) m_pArrayOfBullet->objectAtIndex(i);
if (pEnemyPlane->hitByBullet(pCurBullet, DEFAULT_POWER))
isOver(pCurBullet);
else
i++;
}
}