-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlingshot.h
135 lines (107 loc) · 2.8 KB
/
Slingshot.h
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
#pragma once
#include "Application.h"
#include "Module.h"
#include "Prop.h"
#include "ModulePhysics.h"
#include "ModuleTextures.h"
#include "ModuleAudio.h"
#include "Animation.h"
class ModulePhysics;
class SDL_Texture;
enum SlingPlace {
SLEFT, SRIGHT
};
class Slingshot : public Prop {
public:
Slingshot(PropType type, SlingPlace sPlace) : Prop(type) {
place = sPlace;
switch (sPlace)
{
case SlingPlace::SLEFT:
pBody2 = App->physics->CreateChain(0, 0, pLeft, 6);
pBody = App->physics->CreateRectangleSensor(78, 356, 0, 34);
pBody->body->SetTransform(pBody->body->GetPosition(), -0.6);
break;
case SlingPlace::SRIGHT:
pBody2 = App->physics->CreateChain(0, 0, pRight, 6);
pBody = App->physics->CreateRectangleSensor(160, 356, 0, 34);
pBody->body->SetTransform({ pBody->body->GetPosition() }, 0.6);
break;
}
force = 1.0f;
pBody->body->SetType(b2BodyType::b2_staticBody);
pBody2->body->SetType(b2BodyType::b2_staticBody);
pBody->prop = this;
pBody->listener = (Module*)App->pManager;
slingshotTexture = App->textures->Load("pinball/Textures/Assets_Map.png");
bumperSfx = App->audio->LoadFx("pinball/Sounds/bumpers.wav");
}
void Blit() {
SDL_Rect left;
SDL_Rect right;
switch (place)
{
case SlingPlace::SLEFT:
left = { 0, 59, 23, 34 };
App->renderer->Blit(slingshotTexture, METERS_TO_PIXELS(pBody->body->GetPosition().x), METERS_TO_PIXELS(pBody->body->GetPosition().y), &left);
break;
case SlingPlace::SRIGHT:
right = { 25, 59, 23, 34 };
App->renderer->Blit(slingshotTexture, METERS_TO_PIXELS(pBody->body->GetPosition().x), METERS_TO_PIXELS(pBody->body->GetPosition().y), &right);
break;
}
}
void PlaySFX() {
App->audio->PlayFx(bumperSfx);
}
void OnCollision(PhysBody* bodyB) {
PlaySFX();
Blit();
if (bodyB->prop != NULL) {
if (bodyB->prop->type == PropType::BALL) {
switch (place)
{
case SlingPlace::SLEFT:
//bodyB->body->SetLinearVelocity(b2Vec2{ 0,0 });
bodyB->body->ApplyLinearImpulse({ (float32)(force * 0.866), (float32)(-force * 0.8) }, bodyB->body->GetPosition(), true);
break;
case SlingPlace::SRIGHT:
//bodyB->body->SetLinearVelocity(b2Vec2{ 0,0 });
bodyB->body->ApplyLinearImpulse({ (float32)(force * (-0.866)), (float32)(-force * 0.8) }, bodyB->body->GetPosition(), true);
break;
}
}
}
}
bool Update() {
return true;
}
bool PostUpdate() {
return true;
}
bool CleanUp() {
App->textures->Unload(slingshotTexture);
delete pBody;
pBody = nullptr;
delete pBody2;
pBody2 = nullptr;
return true;
}
public:
float32 force;
private:
int pLeft[6] = {
66, 360,
66, 345,
86, 370 };
int pRight[6] = {
152, 370,
172, 345,
172, 360 };
PhysBody* pBody;
PhysBody* pBody2;
// SFX
int bumperSfx;
SlingPlace place;
SDL_Texture* slingshotTexture;
};