-
Notifications
You must be signed in to change notification settings - Fork 0
/
BallManager.cpp
183 lines (164 loc) · 4.78 KB
/
BallManager.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#define BALL_MANAGER_CPP
#include <assert.h>
#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input / Output
#include <stdarg.h> // Header File For Variable Argument Routines
#include <math.h> // Header File For Math Operations
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include "glut.h"
#include "baseTypes.h"
#include "openglframework.h"
#include "gamedefs.h"
#include "collinfo.h"
#include "object.h"
#include "Ball.h"
#include "BallManager.h"
#include "random.h"
#include "playermanager.h"
BallManagerC* BallManagerC::sInstance=NULL;
BallManagerC *BallManagerC::CreateInstance()
{
assert(sInstance==NULL);
sInstance = new BallManagerC();
return sInstance;
}
void BallManagerC::init()
{
printf("Hey initing the ball manager!\n");
ballPtrs = (BallC**)calloc(NUM_BALLS,sizeof(BallC*));
ballPtrs[0] = new BallC(0.0f, 0.0f, 0.5f, 0.5f, 0, 1, PlayerManagerC::GetInstance()->getPlayer1Ptr());
ballPtrs[0]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[0]->enable();
ballPtrs[0]->setID(0);
ballPtrs[1] = new BallC(0.0f, 0.0f, 0.5f, 0.5f, 1, 2, PlayerManagerC::GetInstance()->getPlayer2Ptr());
ballPtrs[1]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[1]->enable();
ballPtrs[1]->setID(1);
lastBall = 1;
}
void BallManagerC::givePlayersBalls()
{
ballPtrs[lastBall] = new BallC(0.0f, 0.0f, 0.5f, 0.5f, 0, 1, PlayerManagerC::GetInstance()->getPlayer1Ptr());
ballPtrs[lastBall]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[lastBall]->enable();
ballPtrs[lastBall]->setID(0);
lastBall++;
ballPtrs[lastBall] = new BallC(0.0f, 0.0f, 0.5f, 0.5f, 1, 2, PlayerManagerC::GetInstance()->getPlayer2Ptr());
ballPtrs[lastBall]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[lastBall]->enable();
ballPtrs[lastBall]->setID(1);
}
void BallManagerC::resetBalls()
{
for (int i = 0; i < NUM_BALLS; i++) {
if (ballPtrs[i] != nullptr) {
delete ballPtrs[i];
ballPtrs[i] = nullptr;
}
}
lastBall = 0;
ballPtrs[lastBall] = new BallC(0.0f, 0.0f, 0.5f, 0.5f, 0, 1, PlayerManagerC::GetInstance()->getPlayer1Ptr());
ballPtrs[lastBall]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[lastBall]->enable();
ballPtrs[lastBall]->setID(0);
lastBall++;
ballPtrs[lastBall] = new BallC(0.0f, 0.0f, 0.5f, 0.5f, 1, 2, PlayerManagerC::GetInstance()->getPlayer2Ptr());
ballPtrs[lastBall]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[lastBall]->enable();
ballPtrs[lastBall]->setID(1);
}
void BallManagerC::updateBalls(DWORD milliseconds)
{
int i = 0;
bool player1HasBall = false;
bool player2HasBall = false;
//for(uint32_t i=0;i<NUM_BALLS;++i)
while(ballPtrs[i] != nullptr)
{
ballPtrs[i]->update(milliseconds);
if (ballPtrs[i]->isEnabled()) {
if (ballPtrs[i]->getPlayerOwner() == 1) {
player1HasBall = true;
}
if (ballPtrs[i]->getPlayerOwner() == 2) {
player2HasBall = true;
}
}
i++;
}
if (!player1HasBall) {
lastBall++;
ballPtrs[lastBall] = new BallC(0.0f, 0.0f, 0.5f, 0.5f, 0, 1, PlayerManagerC::GetInstance()->getPlayer1Ptr());
ballPtrs[lastBall]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[lastBall]->enable();
ballPtrs[lastBall]->setID(0);
}
if (!player2HasBall) {
lastBall++;
ballPtrs[lastBall] = new BallC(0.0f, 0.0f, 0.5f, 0.5f, 0, 2, PlayerManagerC::GetInstance()->getPlayer2Ptr());
ballPtrs[lastBall]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[lastBall]->enable();
ballPtrs[lastBall]->setID(0);
}
}
void BallManagerC::slowdownBalls()
{
int i = 0;
//for(uint32_t i=0;i<NUM_BALLS;++i)
while(ballPtrs[i] != nullptr)
{
ballPtrs[i]->slowdown();
i++;
}
}
BallC *BallManagerC::getBallPtr(int32_t index)
{
return ballPtrs[index];
}
void BallManagerC::renderBalls()
{
int i = 0;
//for(uint32_t i=0;i<NUM_BALLS;++i)
while(ballPtrs[i] != nullptr)
{
ballPtrs[i]->render();
i++;
}
}
void BallManagerC::shutdown()
{
int i = 0;
//for(uint32_t i=0;i<NUM_BALLS;++i)
while(ballPtrs[i] != nullptr)
{
delete ballPtrs[i];
i++;
}
free(ballPtrs);
}
void BallManagerC::addBall(float_t initPosX, float_t initPosY, float_t velX, float_t velY, int ownedBy, float velMult)
{
lastBall++;
if (lastBall >= NUM_BALLS - 1) {
return;
}
ballPtrs[lastBall] = new BallC(initPosX, initPosY, velX,velY,lastBall, ownedBy);
ballPtrs[lastBall]->setRadius(BALL_RADIUS * SCALE_SIZE);
ballPtrs[lastBall]->enable();
ballPtrs[lastBall]->enable();
ballPtrs[lastBall]->setID(lastBall);
}
void BallManagerC::multiplyBalls(int id) {
int ballCount = 0;
while(ballPtrs[ballCount] != nullptr)
{
ballCount++;
}
for(uint32_t i=0;i<ballCount;++i)
{
if (ballPtrs[i]->getPlayerOwner() == id && ballPtrs[i]->isEnabled()) {
ballPtrs[i]->splitBall();
}
}
}