-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.h
454 lines (361 loc) · 11.3 KB
/
player.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#pragma once
# include "camera.h"
# include "graphics.h"
# include "vector2.h"
# include "animation.h"
# include "platform.h"
# include "player_id.h"
# include "bullet.h"
# include "particle.h"
extern std::vector<Platform> platform_list;
extern std::vector<Bullet*> bullet_list;
extern Camera main_camera;
class Player {
public:
Player() {
timer_attack_cd.set_wait_time(attack_cd);
timer_attack_cd.set_one_shot(true);
timer_attack_cd.set_callback([&]() {can_attack = true; });
timer_invulnerable.set_wait_time(750);
timer_invulnerable.set_one_shot(true);
timer_invulnerable.set_callback([&]() {
is_invulnerable = false;
timer_invulnerable_blink.pause();
});
timer_invulnerable_blink.set_wait_time(75);
timer_invulnerable_blink.set_callback([&]() {is_showing_sketch_frame = !is_showing_sketch_frame; });
timer_run_effect_generation.set_wait_time(75);
timer_run_effect_generation.set_callback([&]() {
Vector2 particle_position;
IMAGE* frame = rs::atlas_run_effect.get_image(0);
particle_position.x = position.x + (size.x - frame->getwidth()) / 2;
particle_position.y = position.y + size.y - frame->getheight();
particle_list.push_back(new Particle(particle_position,&rs::atlas_run_effect,45));
});
timer_die_effect_generation.set_wait_time(35);
timer_die_effect_generation.set_callback([&]() {
Vector2 particle_position;
IMAGE* frame = rs::atlas_run_effect.get_image(0);
particle_position.x = position.x + (size.x - frame->getwidth()) / 2;
particle_position.y = position.y + size.y - frame->getheight();
particle_list.push_back(new Particle(particle_position, &rs::atlas_run_effect, 150));
});
timer_cursor_visibility.set_wait_time(2500);
timer_cursor_visibility.set_one_shot(true);
timer_cursor_visibility.set_callback([&]() {is_cursor_visibility = false; });
animation_jump_effect.set_atlas(&rs::atlas_jump_effect);
animation_jump_effect.set_interval(25);
animation_jump_effect.set_loop(false);
animation_jump_effect.set_callback([&]() {is_jump_effect_visible = false; });
animation_land_effect.set_atlas(&rs::atlas_land_effect);
animation_land_effect.set_interval(50);
animation_land_effect.set_loop(false);
animation_land_effect.set_callback([&]() {is_land_effect_visible = false; });
}
~Player() = default;
virtual void on_update(int delta) {
int direction = is_right_key_down - is_left_key_down;
if (is_attacking_ex)
current_animation = is_facing_right ? &animation_attack_ex_right : &animation_attack_ex_left;
else {
if (direction != 0) {
is_facing_right = direction > 0;
current_animation = is_facing_right ? &animation_run_right : &animation_run_left;
float distance = direction * run_velocity * delta;
on_run(distance);
}
else {
current_animation = is_facing_right ? &animation_idle_right : &animation_idle_left;
timer_run_effect_generation.pause();
}
}
if (hp <= 0)
current_animation = last_hurt_direction.x > 0 ? &animation_die_left : &animation_die_right;
current_animation->on_update(delta);
animation_jump_effect.on_update(delta);
animation_land_effect.on_update(delta);
timer_attack_cd.on_update(delta);
if (is_cursor_visibility)
timer_cursor_visibility.on_update(delta);
if (is_invulnerable) {
timer_invulnerable.on_update(delta);
timer_invulnerable_blink.on_update(delta);
}
timer_run_effect_generation.on_update(delta);
if (hp <= 0)
timer_die_effect_generation.on_update(delta);
for (auto it = particle_list.begin(); it != particle_list.end();) {
if (!(*it)->check_valid()) {
delete (*it);
it = particle_list.erase(it);
}
else it++;
}
for (Particle* particle : particle_list) {
particle->on_update(delta);
}
if (is_showing_sketch_frame && is_invulnerable)
ut::sketch_image(current_animation->get_frame(), &img_sketch);
if (check_is_fall_out())
hp = 0;
move_and_collide(delta);
}
virtual void on_run(float distance) {
if (is_attacking_ex)
return;
position.x += distance;
timer_run_effect_generation.resume();
}
virtual void on_draw(const Camera& camera) {
if (is_jump_effect_visible)
animation_jump_effect.on_draw(camera, position_jump_effect.x, position_jump_effect.y);
if (is_land_effect_visible)
animation_land_effect.on_draw(camera, position_land_effect.x, position_land_effect.y);
for (Particle* particle : particle_list) {
particle->on_draw(camera);
}
if (hp > 0 && is_showing_sketch_frame && is_invulnerable)
ut::putimage_alpha(camera, position.x, position.y, &img_sketch);
else
current_animation->on_draw(camera, (int)position.x, (int)position.y);
if (is_cursor_visibility) {
switch (id) {
case PlayerID::P1:
ut::putimage_alpha(camera,
position.x + (size.x - rs::img_1P_cursor.getwidth()) / 2,
position.y - rs::img_1P_cursor.getheight(),&rs::img_1P_cursor);
break;
case PlayerID::P2:
ut::putimage_alpha(camera,
position.x + (size.x - rs::img_2P_cursor.getwidth()) / 2,
position.y - rs::img_2P_cursor.getheight(), &rs::img_2P_cursor);
break;
}
}
if (is_debug) {
setlinecolor(RGB(255, 0, 0));
ut::draw_line(camera, position.x, position.y, position.x+size.x, position.y);
ut::draw_line(camera, position.x + size.x, position.y, position.x + size.x, position.y+size.y);
ut::draw_line(camera, position.x + size.x, position.y + size.y, position.x, position.y+size.y);
ut::draw_line(camera, position.x, position.y + size.y, position.x, position.y);
}
}
virtual void on_input(const ExMessage& msg) {
switch (msg.message) {
case WM_KEYDOWN:
switch (id) {
case PlayerID::P1:
switch (msg.vkcode) {
case 0x41:
is_left_key_down = true;
break;
case 0x44:
is_right_key_down = true;
break;
case 0x57:
on_jump();
break;
case 0x46:
if (can_attack) {
on_attack();
can_attack = false;
timer_attack_cd.restart();
}
break;
case 0x47:
if (mp >= 100) {
on_attack_ex();
mp = 0;
}
break;
}
break;
case PlayerID::P2:
switch (msg.vkcode) {
case VK_LEFT:
is_left_key_down = true;
break;
case VK_RIGHT:
is_right_key_down = true;
break;
case VK_UP:
on_jump();
break;
case VK_OEM_PERIOD:
if (can_attack) {
on_attack();
can_attack = false;
timer_attack_cd.restart();
}
break;
case VK_OEM_2:
if (mp >= 100) {
on_attack_ex();
mp = 0;
}
break;
}
}
break;
case WM_KEYUP:
switch (id) {
case PlayerID::P1:
switch (msg.vkcode) {
case 0x41:
is_left_key_down = false;
break;
case 0x44:
is_right_key_down = false;
break;
}
break;
case PlayerID::P2:
switch (msg.vkcode) {
case VK_LEFT:
is_left_key_down = false;
break;
case VK_RIGHT:
is_right_key_down = false;
break;
}
}
break;
}
}
void set_id(PlayerID id) {
this->id = id;
}
void set_position(float x, float y) {
position.x = x;
position.y = y;
}
void set_facing_right(bool is_facing_right) {
this->is_facing_right = is_facing_right;
}
virtual void on_jump() {
if (velocity.y != 0 || is_attacking_ex) return;
velocity.y += jump_velocity;
is_jump_effect_visible = true;
animation_jump_effect.reset();
IMAGE* frame = animation_jump_effect.get_frame();
position_jump_effect.x = position.x + (size.x - frame->getwidth()) / 2;
position_jump_effect.y = position.y + size.y - frame->getheight();
}
virtual void on_attack() = 0;
virtual void on_attack_ex() = 0;
const Vector2& get_position() {
return position;
}
const Vector2& get_size() {
return size;
}
void make_invulnerable() {
is_invulnerable = true;
timer_invulnerable.restart();
timer_invulnerable_blink.resume();
}
int get_hp() {
return hp;
}
int get_mp() {
return mp;
}
IMAGE* get_img_avatar() {
return img_avatar;
}
virtual void on_land() {
is_land_effect_visible = true;
animation_land_effect.reset();
IMAGE* frame = animation_land_effect.get_frame();
position_land_effect.x = position.x + (size.x - frame->getwidth()) / 2;
position_land_effect.y = position.y + size.y - frame->getheight();
}
bool check_is_fall_out() {
return position.y > getheight();
}
protected:
void move_and_collide(int delta) {
float last_frame_velocity = velocity.y;
velocity.y += gravity * delta;
position = position + velocity * delta;
if (hp <= 0) return;
if (velocity.y > 0) {
for (const Platform& platform : platform_list) {
const Platform::CollisionShape& shape = platform.collision_shape;
bool is_collide_x = (shape.left <= position.x && position.x <= shape.right ||
shape.left <= position.x + size.x && position.x + size.x <= shape.right);
bool is_collide_y = (position.y <= shape.y && shape.y <= position.y + size.y);
if (is_collide_x && is_collide_y) {
float delta_pos_y = velocity.y * delta;
float last_tick_foot_pos_y = position.y + size.y - delta_pos_y;
if (last_tick_foot_pos_y <= shape.y) {
position.y = shape.y - size.y;
velocity.y = 0;
if (last_frame_velocity != 0)
on_land();
break;
}
}
}
}
if (is_invulnerable) return;
for (Bullet* bullet : bullet_list) {
if (!bullet->get_valid() || bullet->get_collide_target() != id)
continue;
if (bullet->check_collision(position, size)) {
make_invulnerable();
bullet->set_valid(false);
bullet->on_collide();
hp -= bullet->get_damage();
last_hurt_direction = position - bullet->get_position();
if (hp <= 0) {
velocity.x = last_hurt_direction.x > 0 ? 0.35f : -0.35f;
velocity.y = -1.0f;
}
}
}
}
int mp = 0;
int hp = 100;
const float run_velocity = 0.55f;
const float gravity = 1.6e-3f;
const float jump_velocity = -0.85f;
Vector2 size;
Vector2 position;
Vector2 velocity;
Animation animation_idle_left;
Animation animation_idle_right;
Animation animation_run_left;
Animation animation_run_right;
Animation animation_attack_ex_left;
Animation animation_attack_ex_right;
Animation animation_die_left;
Animation animation_die_right;
Animation animation_jump_effect;
Animation animation_land_effect;
bool is_jump_effect_visible = false;
bool is_land_effect_visible = false;
Vector2 position_jump_effect;
Vector2 position_land_effect;
IMAGE img_sketch;
Animation* current_animation = nullptr;
bool is_left_key_down = false;
bool is_right_key_down = false;
bool is_facing_right = true;
bool is_attacking_ex = false;
bool is_invulnerable = false;
bool is_showing_sketch_frame = false;
Timer timer_invulnerable;
Timer timer_invulnerable_blink;
int attack_cd = 500;
bool can_attack = true;
Timer timer_attack_cd;
IMAGE* img_avatar = nullptr;
PlayerID id;
std::vector<Particle*> particle_list;
Timer timer_run_effect_generation;
Timer timer_die_effect_generation;
bool is_cursor_visibility = true;
Timer timer_cursor_visibility;
Vector2 last_hurt_direction;
};