-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cpp
109 lines (90 loc) · 2.5 KB
/
GameManager.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "GameManager.h"
USING_NS_CC;
std::list<Bullet*> GameManager::BulletList;
cocos2d::Scene * GameManager::createScene()
{
auto Scene = Scene::createWithPhysics();
auto Layer = GameManager::create();
Layer->SetPhysicsWorld(Scene->getPhysicsWorld());
Scene->addChild(Layer);
return Scene;
}
bool GameManager::init()
{
if (!Layer::init())
{
return false;
}
VisibleSize = Director::getInstance()->getVisibleSize();
Origin = Director::getInstance()->getVisibleOrigin();
CreateCharacter();
CreateEnemies();
InitializePool(PoolSize);
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(GameManager::onContactBegin, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);
return true;
}
//Spawn Characters
void GameManager::CreateCharacter()
{
CurrentCharacter = new Character();
CurrentCharacter->SetCharacter(VisibleSize, Origin);
this->addChild(CurrentCharacter->CharacterSprite);
}
void GameManager::CreateEnemies()
{
for (int i = 1; i < 7; i++)
{
Enemy* Enemy_ = new Enemy();
Enemy_->SetCharacter();
Enemy_->CharacterSprite->setPosition(VisibleSize.width / 7 * i, VisibleSize.height / 1.2f);
this->addChild(Enemy_->CharacterSprite);
}
}
//Bullet Pool
void GameManager::InitializePool(int PoolSize)
{
for (int i = 0; i < PoolSize; i++)
{
Bullet* Bullet_ = new Bullet();
this->addChild(Bullet_->BulletSprite);
AddToPool(Bullet_);
}
}
Bullet * GameManager::AddToPool(Bullet* Bullet_)
{
Bullet_->BulletSprite->setVisible(false);
Bullet_->BulletSprite->setPosition(0, 0);
BulletList.push_back(Bullet_);
return Bullet_;
}
Bullet * GameManager::GetFromPool()
{
for each (Bullet* Bullet_ in BulletList)
{
if (Bullet_ != nullptr && Bullet_->BulletSprite->isVisible() == false)
{
return Bullet_;
}
}
}
//Collisions
bool GameManager::onContactBegin(cocos2d::PhysicsContact & contact)
{
PhysicsBody *a = contact.getShapeA()->getBody();
PhysicsBody *b = contact.getShapeB()->getBody();
if ((0 == a->getCollisionBitmask() && 1 == b->getCollisionBitmask()) || (1 == a->getCollisionBitmask() && 0 == b->getCollisionBitmask()))
{
CCLOG("Bullet hit");
a->getNode()->setVisible(false);
b->getNode()->setVisible(false);
}
if ((0 == a->getCollisionBitmask() && 2 == b->getCollisionBitmask()) || (2 == a->getCollisionBitmask() && 0 == b->getCollisionBitmask()))
{
CCLOG("Bullet hit");
a->getNode()->setVisible(false);
b->getNode()->setVisible(false);
}
return true;
}