-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.c
178 lines (132 loc) · 3.78 KB
/
bullet.c
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
#ifndef ____blt__
#define ____blt__
#include "rock.c"
#include "LTexture.c"
/***********************************************************************/
struct Bullet{
//Maximum axis velocity of the bullet
static const double BULLET_VEL = 10.0;
double BULLET_PROPULSION;
LTexture* bullet_texture = NULL;
//Initializes the variables
Bullet(double, double, double, double);
//sets window and renderer
void set_texture(LTexture* text);
//Moves the bullet, return -1 if good, else is the rock hit
int move(std::vector<Rock*>& rocks, int, int);
//Shows the bullet on the screen
void render();
//checks for collisions with rocks or enemies
int collide(std::vector<Rock*>& rocks);
//The X and Y offsets of the bullet
double mPosX, mPosY;
//The velocity of the bullet
double mVelX, mVelY;
//degrees of the bullet
double degrees;
double img_radius;
};
/***********************************************************************/
/***********************************************************************/
Bullet::Bullet(double x, double y, double degree, double prop)
{
img_radius = 45;
degrees = degree;
if(prop * 3 > 0.8) BULLET_PROPULSION = prop * 3;
else BULLET_PROPULSION = 0.8;
// SHORTCODE: BULLET OFFSETS
mPosX = x + 35;
mPosY = y + 25;
if(degrees >= 0 && degrees < 45)
{
mPosX -= 6;
mPosY -= 4;
}
if(degrees > 44 && degrees < 120)
{
mPosX += 3;
mPosY -= 7;
}
if(degrees > 119 && degrees < 179)
{
mPosX += 5;
mPosY -= 3;
}
if(degrees > 180 && degrees < 180)
{
//mPosX += 5;
}
if(degrees > 270 && degrees < 320)
{
mPosY += 3;
}
//Initialize the velocity
mVelX = cos((degrees) /(180.0 / M_PI)) * 10.0;
mVelY = sin((degrees) /(180.0 / M_PI)) * 10.0;
mPosX += mVelX * 5;
mPosY += mVelY * 5;
}
void Bullet::set_texture(LTexture* text)
{
bullet_texture = text;
}
int Bullet::move(std::vector<Rock*>& rocks, int screen_w, int screen_h)
{
//Move the bullet left or right
double xVar = mVelX * BULLET_PROPULSION;
double yVar = mVelY * BULLET_PROPULSION;
mPosX += xVar; mPosY += yVar;
if(mPosX < -5) {
return -2;
}
if(( mPosX > screen_w )) {
return -2;
}
if( mPosY < -5 ){
return -2;
}
if ( mPosY > screen_h ){
return -2;
}
int col = collide(rocks);
return col;
}
void Bullet::render()
{
bullet_texture->render( mPosX, mPosY, NULL, degrees, NULL, SDL_FLIP_NONE );
}
int Bullet::collide(std::vector<Rock*>& rocks)
{
//if int is -1 after loop, no collision
int no_collision = -1;
for(int i = 0; i < (int)rocks.size(); i++)
{
if(rocks[i]->size != 11)
{
if(mPosX <= rocks[i]->mPosX + rocks[i]->size*2.8
&& mPosX + bullet_texture->mWidth >= rocks[i]->mPosX)
{
if(mPosY <= rocks[i]->mPosY + rocks[i]->size*2.8
&& mPosY + bullet_texture->mHeight >= rocks[i]->mPosY)
{
return i;
}
}
}
else
{
if(mPosX <= rocks[i]->mPosX + rocks[i]->size*2
&& mPosX + bullet_texture->mWidth >= rocks[i]->mPosX)
{
if(mPosY <= rocks[i]->mPosY + rocks[i]->size*2
&& mPosY + bullet_texture->mHeight >= rocks[i]->mPosY)
{
return i;
}
}
}
}
return no_collision;
}
/***********************************************************************/
#endif