-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.cpp
345 lines (286 loc) · 9.33 KB
/
Ball.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "Ball.h"
#include "Globals.h"
#include "Application.h"
#include "ModuleRender.h"
#include "ModuleScene.h"
#include "ModuleInput.h"
#include "ModuleTextures.h"
#include "ModuleAudio.h"
#include "ModulePhysics.h"
#include "ModuleScene.h"
#include "Module.h"
Ball::Ball(Application* app) : Entity(EntityType::BALL, app)
{
name.Create("Ball");
this->app = app;
}
Ball::~Ball()
{}
// Load assets
bool Ball::Start()
{
LOG("Loading player");
p.x = 515;
p.y = 726;
ballBody = app->physics->CreateCircle(p.x, p.y, 15);
ballBody->body->SetType(b2_dynamicBody);
ballBody->ctype = ColliderType::BALL;
//app->scene_intro->boxes.add(ballBody);
ballBody->listener = this;
bounce = false;
bounceDir = { 0.0f, 0.0f };
intensity = 100;
separed = false;
jointCreated = false;
scorex10finished = 0;
delayNewBall = -1;
delaySaverLeft = -1;
delaySaverRight = -1;
return true;
}
// Unload assets
bool Ball::CleanUp()
{
LOG("Unloading player");
delete ballBody;
ballBody = nullptr;
return true;
}
// Update: draw background
bool Ball::Update()
{
//Need to Apply Restitution Coefficient !!!
if (bounce) {
bounceDir.Normalize();
ballBody->body->ApplyForce(intensity*bounceDir, ballBody->body->GetWorldCenter(), true);
bounce = false;
}
//Update player position in pixels
position.x = METERS_TO_PIXELS(ballBody->body->GetTransform().p.x - 15);
position.y = METERS_TO_PIXELS(ballBody->body->GetTransform().p.y - 15);
//Spring force
int springForce = app->scene_intro->springForce;
if (app->input->GetKey(SDL_SCANCODE_DOWN) == KEY_REPEAT)
{
app->scene_intro->springForce = springForce == 420 ? 420 : springForce += 4.0;
}
else if (app->input->GetKey(SDL_SCANCODE_DOWN) == KEY_UP && app->scene_intro->sensorSpring_Sensed && springForce > 60)
{
app->audio->PlayFx(app->scene_intro->sfx_spring);
ballBody->body->ApplyForceToCenter(b2Vec2(0, -springForce), true);
app->scene_intro->springForce = 0;
app->scene_intro->sensorSpring_Sensed = false;
LOG("RELEASE BALL");
}
else
app->scene_intro->springForce = 0;
// Gravity
if (app->input->GetKey(SDL_SCANCODE_W) == KEY_REPEAT) {
app->physics->gravity+=0.5;
app->physics->world->SetGravity(b2Vec2(GRAVITY_X, app->physics->gravity));
}
if (app->input->GetKey(SDL_SCANCODE_S) == KEY_REPEAT) {
app->physics->gravity -= 0.5;
app->physics->world->SetGravity(b2Vec2(GRAVITY_X, app->physics->gravity));
}
// Bounce
if (app->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) {
intensity += 10;
}
if (app->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) {
intensity -= 10;
}
//Right hole combo
if (app->scene_intro->sensorX10_Sensed)
{
ballBody->body->ApplyForce(b2Vec2(100, -100), ballBody->body->GetWorldCenter(), true);
scorex10finished++;
}
if (scorex10finished > 300)
{
ballBody->body->ApplyForce(b2Vec2(-500, 300), ballBody->body->GetWorldCenter(), true);
scorex10finished = 0;
app->scene_intro->sensorX10_Sensed = false;
}
//Delay New Ball
if (delayNewBall > 0)
delayNewBall--;
else if (delayNewBall == 0)
{
ballBody->body->SetTransform(PIXEL_TO_METERS(p), 0);
app->scene_intro->ballsCounter--;
delayNewBall--;
if (app->scene_intro->ballsCounter > 0)
app->audio->PlayFx(app->scene_intro->sfx_new_ball);
}
//Delay Saver Left
if (delaySaverLeft > 0)
delaySaverLeft--;
else if (delaySaverLeft == 0)
{
ballBody->body->ApplyForceToCenter(b2Vec2(0, -420), true);
app->audio->PlayFx(app->scene_intro->sfx_spring);
delete app->scene_intro->saverLeft;
app->scene_intro->saverLeft = nullptr;
delaySaverLeft = -255;
}
//Delay Saver Right
if (delaySaverRight > 0)
delaySaverRight--;
else if (delaySaverRight == 0)
{
ballBody->body->ApplyForceToCenter(b2Vec2(0, -348), true);
app->audio->PlayFx(app->scene_intro->sfx_spring);
delete app->scene_intro->saverRight;
app->scene_intro->saverRight = nullptr;
delaySaverRight = -255;
}
return true;
}
bool Ball::PostUpdate()
{
SDL_Rect rectBall = { 229, 106, 31, 31 };
app->renderer->Blit(app->scene_intro->assets, position.x, position.y, &rectBall);
SDL_Rect rectTop = { 0, 0, 115, 335 };
app->renderer->Blit(app->scene_intro->top, 434, 122, &rectTop);
//Left saver
SDL_Rect leftSaver;
if (delaySaverLeft == -255)
leftSaver = { 0, 0, 52, 20 };
else
leftSaver = { 0, 20, 52, 20 };
app->renderer->Blit(app->scene_intro->savers, 9, 630, &leftSaver);
//Right Savers
SDL_Rect rightSaver;
if (delaySaverRight == -255)
rightSaver = { 52, 0, 52, 20 };
else
rightSaver = { 52, 20, 52, 20 };
app->renderer->Blit(app->scene_intro->savers, 450, 630, &rightSaver);
//Lower part where game info is
SDL_Rect rectLower = { 0, 0, 462, 207 };
app->renderer->Blit(app->scene_intro->lower, 24, 730, &rectLower);
//Time bar
SDL_Rect bar = { 71, 865, app->scene_intro->time * 0.066666f, 60 };
app->renderer->DrawQuad(bar, 119 + (3600 - app->scene_intro->time) * 0.027777f, 40 + app->scene_intro->time * 0.045f, app->scene_intro->time * 0.066666f, 255, true);
SDL_Rect timerRect = { 0, 0, 282, 65 };
app->renderer->Blit(app->scene_intro->timebar, 50, 860, &timerRect);
return true;
}
void Ball::OnCollision(PhysBody* bodyA, PhysBody* bodyB)
{
switch (bodyB->ctype)
{
case ColliderType::WALL:
LOG("Collision WALL");
break;
case ColliderType::BLUE_25:
LOG("Collision BLUE_25");
app->scene_intro->sensorBlue_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_bouncer_circle);
app->scene_intro->currentScore += app->scene_intro->AddScore(25);
bounceDir = { ballBody->body->GetWorldCenter() - app->scene_intro->blue->body->GetWorldCenter() };
bounce = true;
break;
case ColliderType::YELLOW_50:
LOG("Collision YELLOW_50");
app->scene_intro->sensorYellow_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_bouncer_circle);
app->scene_intro->currentScore += app->scene_intro->AddScore(50);
bounceDir = { ballBody->body->GetWorldCenter() - app->scene_intro->yellow->body->GetWorldCenter() };
bounce = true;
break;
case ColliderType::RED_100:
LOG("Collision RED_100");
app->scene_intro->sensorRed_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_bouncer_circle);
app->scene_intro->currentScore += app->scene_intro->AddScore(100);
bounceDir = { ballBody->body->GetWorldCenter() - app->scene_intro->red->body->GetWorldCenter() };
bounce = true;
break;
case ColliderType::SENSOR_TRI_LEFT:
LOG("Collision TRIANGLE_LEFT");
app->scene_intro->sensorTriLeft_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_bouncer_tri);
app->scene_intro->currentScore += app->scene_intro->AddScore(10);
bounceDir = { 1.0f, -0.5f };
bounce = true;
break;
case ColliderType::SENSOR_TRI_RIGHT:
LOG("Collision TRIANGLE_RIGHT");
app->scene_intro->sensorTriRight_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_bouncer_tri);
app->scene_intro->currentScore += app->scene_intro->AddScore(10);
bounceDir = { -1.0f, -0.5f };
bounce = true;
break;
// SENSORS ===================================================
case ColliderType::SENSOR_SPRING:
LOG("Collision SPRING_SENSOR");
app->scene_intro->sensorSpring_Sensed = true;
break;
case ColliderType::SENSOR_X10:
LOG("Collision SCOREX10");
app->scene_intro->sensorX10_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_intro);
app->scene_intro->currentScore += app->scene_intro->AddScore(15000);
break;
case ColliderType::SENSOR_DEATH:
LOG("Collision DEATH");
if (!app->scene_intro->ray_on && delayNewBall == -1)
{
app->audio->PlayFx(app->scene_intro->sfx_death);
delayNewBall = 100;
app->scene_intro->sensorDeath_Sensed = true;
app->scene_intro->scoreMultiplier = 1;
}
break;
case ColliderType::SENSOR_COMBO_A1:
app->scene_intro->sensorComboA1_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_comboA, 0);
app->scene_intro->currentScore += app->scene_intro->AddScore(200);
break;
case ColliderType::SENSOR_COMBO_A2:
app->scene_intro->sensorComboA2_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_comboA, 0);
app->scene_intro->currentScore += app->scene_intro->AddScore(200);
break;
case ColliderType::SENSOR_COMBO_A3:
app->scene_intro->sensorComboA3_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_comboA, 0);
app->scene_intro->currentScore += app->scene_intro->AddScore(200);
break;
case ColliderType::SENSOR_COMBO_B1:
app->scene_intro->sensorComboB1_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_trigger, 0);
app->scene_intro->currentScore += app->scene_intro->AddScore(300);
break;
case ColliderType::SENSOR_COMBO_B2:
app->scene_intro->sensorComboB2_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_trigger, 0);
app->scene_intro->currentScore += app->scene_intro->AddScore(300);
break;
case ColliderType::SENSOR_COMBO_B3:
app->scene_intro->sensorComboB3_Sensed = true;
app->audio->PlayFx(app->scene_intro->sfx_trigger, 0);
app->scene_intro->currentScore += app->scene_intro->AddScore(300);
break;
case ColliderType::SENSOR_TIME:
app->scene_intro->sensorTime_Sensed = true;
app->scene_intro->scoreMultiplier = 0;
app->scene_intro->time += app->scene_intro->timeScore*0.06;
app->scene_intro->timeScore = 0;
break;
case ColliderType::SAVER_LEFT:
delaySaverLeft = 100;
app->scene_intro->saverLeftSensed = true;
break;
case ColliderType::SAVER_RIGHT:
delaySaverRight = 100;
app->scene_intro->saverRightSensed = true;
break;
case ColliderType::UNKNOWN:
LOG("Collision UNKNOWN");
break;
}
}