forked from lechienn/BTL-LTNC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.h
205 lines (175 loc) · 4.44 KB
/
GameObject.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
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
#include "library.h"
#include "skill.h"
#include "TextureManager.h"
class GameObject
{
private:
int xPos;
int yPos;
int HP;
int angle = 45;
int power = 0;
bool direction;
int frame = 0;
SDL_Texture *objectTexture;
SDL_Rect srcRect[6];
SDL_Rect destRect;
SDL_Renderer *renderer;
public:
Skill skill;
GameObject(const char *textureSheet, SDL_Renderer *ren, bool direc, const int &x, const int &y, const int &hpBarX, const int &hpBarY);
~GameObject();
SDL_Rect getRect() { return destRect; }
int getAngle() { return angle; }
int getPower() { return power; }
bool getDirection() { return direction; }
int getHP() { return HP; }
void setHP(int HP);
// bool getTriggerSkill2(){return skill.triggerIncreaseDamage;}
void setPower(int power);
void setFrame(int frame);
void handleEvent(SDL_Event &event, bool &isFire, bool &player1Turn);
void update();
void render();
void renderAngle();
};
GameObject::GameObject(const char *textureSheet, SDL_Renderer *ren, bool direc, const int &x, const int &y, const int &hpBarX, const int &hpBarY)
{
renderer = ren;
objectTexture = TextureManager::LoadTexture(textureSheet, ren);
xPos = x;
yPos = y;
direction = direc;
HP = 100;
skill.init(ren, hpBarX, hpBarY);
srcRect[0].h = 200;
srcRect[0].w = 150;
srcRect[0].x = 0;
srcRect[0].y = 0;
srcRect[1].h = 200;
srcRect[1].w = 150;
srcRect[1].x = 150;
srcRect[1].y = 0;
srcRect[2].h = 200;
srcRect[2].w = 150;
srcRect[2].x = 300;
srcRect[2].y = 0;
srcRect[3].h = 200;
srcRect[3].w = 150;
srcRect[3].x = 450;
srcRect[3].y = 0;
srcRect[4].h = 200;
srcRect[4].w = 150;
srcRect[4].x = 600;
srcRect[4].y = 0;
srcRect[5].h = 200;
srcRect[5].w = 150;
srcRect[5].x = 750;
srcRect[5].y = 0;
}
GameObject::~GameObject() {}
void GameObject::handleEvent(SDL_Event &event, bool &isFire, bool &player1Turn)
{
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
HP = 0;
case SDLK_LEFT:
xPos--;
// direction = false;
break;
case SDLK_RIGHT:
xPos++;
// direction = true;
break;
case SDLK_UP:
if (angle < 90)
angle++;
break;
case SDLK_DOWN:
if (angle > 0)
angle--;
break;
case SDLK_1:
if (skill.doubleShoot)
{
skill.triggerDoubleShoot = true;
skill.doubleShoot = false;
}
break;
case SDLK_2:
if (skill.increaseDamage)
{
skill.triggerIncreaseDamage = true;
skill.increaseDamage = false;
}
break;
case SDLK_3:
if (skill.heal)
{
HP >= 80 ? HP = 100 : HP += 20;
skill.heal = false;
player1Turn = !player1Turn;
}
break;
case SDLK_SPACE:
power++;
frame = 2;
break;
}
}
else if (event.type == SDL_KEYUP)
{
switch (event.key.keysym.sym)
{
case SDLK_SPACE:
isFire = !isFire;
frame = 3;
break;
}
}
}
void GameObject::update()
{
destRect.x = xPos;
destRect.y = yPos;
destRect.h = heightCharacter;
destRect.w = widthCharacter;
}
void GameObject::render()
{
skill.render();
SDL_RenderCopy(renderer, objectTexture, &srcRect[frame], &destRect);
}
// void GameObject::setAngle(int angle){
// this->angle = angle;
// }
void GameObject::setPower(int power)
{
this->power = power;
}
void GameObject::setHP(int HP)
{
if (HP < 0)
this->HP = 0;
else
this->HP = HP;
}
void GameObject::renderAngle()
{
float x = (float)xPos + (float)widthCharacter / 2;
float y = (float)yPos + (float)heightCharacter / 2;
float xBegin, xEnd, yBegin, yEnd;
float rad = direction ? (((float)angle / 180) * M_PI) : (M_PI - ((float)angle / 180) * M_PI);
xBegin = x + cos(rad) * 90;
yBegin = y - sin(rad) * 90;
xEnd = x + cos(rad) * 180;
yEnd = y - sin(rad) * 180;
SDL_RenderDrawLine(renderer, (int)xBegin, (int)yBegin, (int)xEnd, (int)yEnd);
}
void GameObject::setFrame(int frame)
{
this->frame = frame;
}