-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
318 lines (277 loc) · 6.54 KB
/
Player.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
#include "GameObj.h"
#include "Global.h"
#include "Bullet.h"
#include "Player.h"
#include <cmath>
#include <fstream>
using namespace std;
static int cn = 0;
vector<string> keys;
string prevAct;
string prevKeyPress;
SDL_Renderer* gameRenderer;
const double GRAVITY = 9.81;
Player::Player(){}
Player::Player(float x, float y, int obj_code, string playername){
PlayerSheetTexture = NULL;
name = playername;
health = 100;
isCollidingBelow = false;
collisionBox.x = x;
collisionBox.y = y;
collisionBox.w = 40.0;
collisionBox.h = 60.0;
code = obj_code;
xPrevPos = x;
yPrevPos = y;
maxVel = 4.0;
jumpVel = 4.0;
tempJvel = 0.0;
jumpCount = 0;
xDelPos =0.0;
yDelPos =0.0;
camera = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
initializeClips();
renderingClip.x = run[0].x;
renderingClip.y = run[0].y;
renderingClip.w = run[0].w;
renderingClip.h = run[0].h;
flipType = SDL_FLIP_NONE;
}
void Player::initializeClips(){
ifstream in;
string act;
SDL_Rect box;
in.open("PlayerClips.txt");
while(!in.eof()){
in>>act;
in>> box.x >> box.y >> box.w >> box.h;
run.push_back(box);
}
}
void Player::renderHealthBar(){
float percentHealth = health/100.0;
int barWidth = 300;
int barHeight = 15;
SDL_Rect emptyHealth, fillHealth;
SDL_SetRenderDrawColor( gameRenderer, 50, 205, 50, 255 );
fillHealth.x = 20;
fillHealth.y = 20;
fillHealth.w = barWidth*percentHealth;
fillHealth.h = barHeight;
SDL_RenderFillRect(gameRenderer, &fillHealth);
SDL_SetRenderDrawColor(gameRenderer, 255, 0, 0, 255);
emptyHealth.x = fillHealth.x+fillHealth.w;
emptyHealth.y = fillHealth.y;
emptyHealth.w = barWidth*(1.0-percentHealth);
emptyHealth.h = barHeight;
SDL_RenderFillRect(gameRenderer, &emptyHealth);
}
void Player::render(float x, float y, SDL_Rect* clip, SDL_RendererFlip flipType){
SDL_Rect spriteRect = {(int)x, (int)y, 0, 0};
spriteRect.w = clip->w;
spriteRect.h = clip->h;
if(flipType == SDL_FLIP_NONE){
spriteRect.x = x-20;
}
else{
spriteRect.x = x-20;
}
// SDL_RenderDrawRect(gameRenderer, &collisionBox);
SDL_RenderCopyEx(gameRenderer, PlayerSheetTexture, clip, &spriteRect, 0, NULL, flipType);
renderHealthBar();
}
void Player::handleMovement(SDL_Event e){
switch(e.key.keysym.sym){
case SDLK_LEFT:
xDelPos -= (maxVel*TIME_STEP)*SCALE;
flipType = SDL_FLIP_HORIZONTAL;
prevKeyPress = "LD";
keys.push_back(prevKeyPress);
break;
case SDLK_RIGHT:
xDelPos += (maxVel*TIME_STEP)*SCALE;
flipType = SDL_FLIP_NONE;
prevKeyPress = "RD";
keys.push_back(prevKeyPress);
break;
case SDLK_UP:
if(jumpCount<2){
tempJvel = jumpVel;
jumpCount++;
}
cn=0;
prevKeyPress = "UD";
keys.push_back(prevKeyPress);
break;
case SDLK_DOWN:
prevKeyPress = "DD";
keys.push_back(prevKeyPress);
break;
case SDLK_x:
// animate("blink");
prevKeyPress = "XD";
keys.push_back(prevKeyPress);
// BYPASS = true;
break;
case SDLK_z:
Bullet* bullet = new Bullet(collisionBox.x+collisionBox.w, collisionBox.y+10, "pbullet", flipType);
object.push_back(bullet);
BYPASS = true;
prevKeyPress = "ZD";
prevAct = "shoot";
keys.push_back(prevKeyPress);
xDelPos = 0;
cn=0;
break;
}
}
void Player::handleMovement(SDL_Event e, int button_released){
switch(e.key.keysym.sym){
case SDLK_LEFT:
// xDelPos += (maxVel*TIME_STEP)*SCALE;
xDelPos = 0;
break;
case SDLK_RIGHT:
// xDelPos -= (maxVel*TIME_STEP)*SCALE;
xDelPos = 0;
break;
case SDLK_UP:
break;
case SDLK_DOWN:
break;
case SDLK_x:
if(flipType == SDL_FLIP_HORIZONTAL){
updatePos(collisionBox.x-200, collisionBox.y);
}
else{
updatePos(collisionBox.x+200, collisionBox.y);
}
break;
case SDLK_z:
break;
}
}
void Player::playerHitStatic(GameObj object){
if(yPrevPos + collisionBox.h <= (object).collisionBox.y){
collisionBox.y = (object).collisionBox.y - collisionBox.h;
yDelPos = 0;
tempJvel = 0;
jumpCount = 0;
isCollidingBelow = true;
}
else{
if(xPrevPos + collisionBox.w <= (object).collisionBox.x){
collisionBox.x = (object).collisionBox.x - collisionBox.w;
}
else{
if(xPrevPos >= (object).collisionBox.x + (object).collisionBox.w){
collisionBox.x = (object).collisionBox.x + (object).collisionBox.w;
}
else{
if(yPrevPos >= (object).collisionBox.y + (object).collisionBox.h){
collisionBox.y = (object).collisionBox.y + (object).collisionBox.h;
tempJvel = 0;
}
}
}
}
}
//===========================OVERRIDEN updatePos()==================================//
void Player::updatePos(int x, int y){
collisionBox.x = x;
collisionBox.y = y;
}
//====================================END===========================================//
//========================UPDATE POSITION GENERAL===================================//
void Player::updatePos(){
static string prev;
xPrevPos = collisionBox.x;
yPrevPos = collisionBox.y;
yDelPos = ((-1*tempJvel*TIME_STEP + (0.5)*GRAVITY*TIME_STEP*TIME_STEP)*SCALE);
tempJvel = tempJvel - (GRAVITY*TIME_STEP);
collisionBox.x += xDelPos;
collisionBox.y += yDelPos;
if(collisionBox.x+collisionBox.w > LEVEL_WIDTH){
collisionBox.x = LEVEL_WIDTH-collisionBox.w;
}
else if(collisionBox.x < 0){
collisionBox.x = 0;
}
//*********SETTING ANIMATION ACTIONS********//
if(BYPASS){
animate(prevAct);
}
else if(abs(xDelPos)>=EPSILON && abs(tempJvel)<=0.85){
animate("run");
}
else{
if(abs(xDelPos)<=EPSILON && abs(tempJvel)<=0.85){
animate("stand");
}
else{
animate("jump");
}
}
isCollidingBelow = false;
}
//===================================END===================================================//
void Player::bulletHitPlayer(GameObj obj){
health -= 1;
prevAct = "damage";
cn=0;
BYPASS = true;
}
void Player::animate(string act){
// static int i=0;
if(act == "run"){
renderingClip = run[cn/5];
cn++;
if(cn==20){
cn=0;
}
prevAct = "run";
}
else if(act == "shoot"){
renderingClip = run[14+cn/4];
cn++;
if(cn>=8){
cn=0;
BYPASS = false;
}
prevAct = "shoot";
}
else if(act == "stand"){
renderingClip = run[5];
prevAct = "stand";
}
else if(act == "damage"){
renderingClip = run[13+cn/10];
cn++;
if(cn==10){
cn=0;
BYPASS = false;
}
prevAct = "damage";
}
// else if(act == "blink"){
// renderingClip = run[16+cn/1];
// cn++;
// if(cn==2){
// cn=0;
// BYPASS = false;
// }
// prevAct = "blink";
// }
else if(act == "jump"){
if(tempJvel>0 && abs(xDelPos)>=EPSILON){
renderingClip = run[6];
}
else if(tempJvel>0 && abs(xDelPos)<=EPSILON){
renderingClip = run[7];
}
else if(tempJvel < 0){
renderingClip = run[9];
}
}
}