-
Notifications
You must be signed in to change notification settings - Fork 4
/
Controller.cpp
234 lines (193 loc) · 4.9 KB
/
Controller.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "Controller.h"
Controller::Controller()
{
_score = 0;
_balls = 0;
_highScores = new HighScores();
}
Controller::~Controller()
{
DestroyBallArray();
}
void Controller::DestroyBallArray()
{
if (_balls)
{
for(int i = 0; i < _ballCount; i++)
{
delete _balls[i];
_balls[i] = 0;
}
delete[] _balls;
_balls = 0;
}
}
void Controller::PrepareLevel(int level)
{
_level = level;
if (_level == 1)
{
_lives = 2;
_score = 0;
}
DestroyBallArray();
_speed = 5*level;
_ballCount = _speed + 10 + (int)(((double)rand()/(double)RAND_MAX)*(double)30);
if (_ballCount <= 30)
_ballCount = 30;
_balls = new Ball*[_ballCount];
int skullCount = _level/2;
if (skullCount >= (int)((double)_ballCount*(double)0.50))
skullCount = (int)((double)_ballCount*(double)0.50);
_movingBallCount = _ballCount - skullCount;
int smallBallsCount = 0;
if (_level > 2)
{
smallBallsCount = (double)(_ballCount-skullCount) * ((double)rand()/(double)RAND_MAX) * ((double)(_level-2)/(double)5);
if (smallBallsCount >= (int)((double)_ballCount*(double)0.35))
smallBallsCount = (int)((double)_ballCount*(double)0.35);
}
int i;
for (i = 0; i < skullCount; i++)
{
_balls[i] = new Ball(_width, _height, BT_SKULL, _speed);
}
for (; i < skullCount + smallBallsCount; i++)
{
_balls[i] = new Ball(_width, _height, BT_FIRE, _speed);
}
for (; i < _ballCount; i++)
{
_balls[i] = new Ball(_width, _height, BT_NORMAL, _speed);
}
}
int Controller::Lives()
{
return _lives;
}
int Controller::Score()
{
return _score;
}
int Controller::RemainingBalls()
{
return _movingBallCount;
}
int Controller::Level()
{
return _level;
}
bool Controller::Move()
{
bool inverted = false;
for (int i = 0; i < _ballCount; i++)
{
int x1, y1, x2, y2;
_balls[i]->SimulateMove(x1, y1, x2, y2);
if (x1 < 0 || x2 > _width)
{
_balls[i]->InvertX();
inverted = true;
}
if (y1 < 0 || y2 > _height)
{
_balls[i]->InvertY();
inverted = true;
}
_balls[i]->Move();
}
return inverted;
}
void Controller::ScreenSize(int width, int height)
{
_width = width;
_height = height;
}
int Controller::Hit(int x, int y, char* score)
{
score[0] = '\0';
POINTS p;
p.x = x;
p.y = y;
int ballsHit = 0;
int ballsScore = 0;
int color = -1;
bool differentColors = false;
bool skullHit = false;
for (int i = 0; i < _ballCount; i++)
{
if ((_balls[i]->Status() == BS_MOVING_LEFT || _balls[i]->Status() == BS_MOVING_RIGHT) && _balls[i]->Hittest(x,y))
{
if (_balls[i]->Color() == BC_SKULL)
skullHit = true;
else
{
if (color == -1)
color = _balls[i]->Color();
else if (color != _balls[i]->Color())
differentColors = true;
ballsHit++;
ballsScore += _balls[i]->Score();
_balls[i]->Freeze();
}
}
}
if (skullHit)
{
_lives--;
strcpy(score, "0");
_movingBallCount -= ballsHit;
if ((_movingBallCount == 0)&&(_level%2))
_lives++;
return CH_SKULL;
}
else if (ballsHit == 0)
{
_lives--;
strcpy(score, "0");
return CH_HITNONE;
}
else
{
double ballsHitProportion = (double)ballsHit/(double)_movingBallCount;
double bonusFactor = differentColors ? 1 : ballsHit;
int normalScore = (int)(ballsHitProportion*ballsHitProportion*(double)ballsScore);
char normalScoreChar[15];
itoa(normalScore, normalScoreChar, 10);
if (bonusFactor > 1)
{
itoa(bonusFactor, score, 10);
strcat(score, "x");
strcat(score, normalScoreChar);
}
else
{
strcpy(score, normalScoreChar);
}
_score += normalScore*bonusFactor;
_movingBallCount -= ballsHit;
if ((_movingBallCount == 0)&&(_level%2))
_lives++;
return CH_HIT;
}
}
Ball* Controller::BallAt(int i)
{
return _balls[i];
}
int Controller::BallCount()
{
return _ballCount;
}
bool Controller::CheckLastScore()
{
return _highScores->CheckHighScore(_score);
}
void Controller::CommitLastScore(const char* name)
{
_highScores->AddHighScore(name, _score);
}
list<HighScore*>* Controller::HighScoresList()
{
return _highScores->List();
}