-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.cpp
More file actions
139 lines (115 loc) · 2.45 KB
/
Bullet.cpp
File metadata and controls
139 lines (115 loc) · 2.45 KB
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
#include"Bullet.h"
Bullet::Bullet(int index,BulletInfo info)
{
this->index = index;
this->visible = false;
this->restart = true;
this->info = info;
this->reflected = 0;
this->dirX = info.dirX[index];
this->dirY = info.dirY[index];
this->cosRad = acos(dirX * 0.0f + dirY * 1.0f);
this->cosDeg = cosRad * 180.0f / M_PI;
float crossZ;
crossZ = dirX * 1.0f - dirY * 0.0f;
if (crossZ > 0.0f)
{
this->cosDeg *= -1.0f;
this->cosRad *= -1.0f;
}
this->texture = info.texture;
SDL_QueryTexture(this->texture, nullptr, nullptr, &texWidth, &texHeight);
texWidth *= info.texScale;
texHeight *= info.texScale;
this->pos->x = FLT_MAX;
this->pos->y = FLT_MAX;
collisionBox.calcCollisionBox(pos->x, pos->y, pos->x + texWidth, pos->y + texHeight, cosRad);
}
bool Bullet::isRestart()
{
return restart;
}
void Bullet::shoot(int index,float x,float y)
{
visible = true;
restart = false;
pos->x = x;
pos->y = y;
reflected = 0;
dirX = info.dirX[index];
dirY = info.dirY[index];
this->cosRad = acos(dirX * 0.0f + dirY * 1.0f);
this->cosDeg = cosRad * 180.0f / M_PI;
float crossZ;
crossZ = dirX * 1.0f - dirY * 0.0f;
if (crossZ > 0.0f)
{
this->cosDeg *= -1.0f;
this->cosRad *= -1.0f;
}
}
void Bullet::initFrameSettings()
{
}
void Bullet::Update()
{
if (!restart)
{
setPos(pos->x + dirX * info.speed, pos->y + dirY * info.speed);
collisionBox.calcCollisionBox(pos->x, pos->y, pos->x + texWidth, pos->y + texHeight, cosRad);
}
}
void Bullet::drawObjects(SDL_Renderer* gRenderer)
{
if (isVisible())
{
renderQuad = { (int)pos->x, (int)pos->y,static_cast<int>(texWidth), static_cast<int>(texHeight) };
//Set clip rendering dimensions
if (clip != nullptr)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopyEx(gRenderer, texture, clip, &renderQuad, cosDeg , center, flip);
}
}
void Bullet::restartCheck()
{
if (pos->x < 0.0 || pos->x > SCREEN_WIDTH)
{
if (reflected >= info.reflectCount)
{
Restart();
}
else
{
reflected++;
dirX *= -1.0f;
cosDeg *= -1.0f;
cosRad *= -1.0f;
}
}
else if(pos->y < 0.0 || pos->y > SCREEN_HEIGHT)
{
if (reflected >= info.reflectCount)
{
Restart();
}
else
{
reflected++;
dirY *= -1.0f;
cosDeg *= -1.0f;
cosRad *= -1.0f;
}
}
}
void Bullet::Restart()
{
visible = false;
restart = true;
pos->x = FLT_MAX;
pos->y = FLT_MAX;
collisionBox.calcCollisionBox(pos->x, pos->y, pos->x + texWidth, pos->y + texHeight, cosRad);
}