-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
126 lines (108 loc) · 4.89 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
#include "entities.h"
void SetupPlayer(Player &player) {
player.e.is_alive = true;
player.stats.lives = 3;
player.stats.fire_rate = .4f;
player.stats.fire_rate_timer = 0;
player.stats.xp_pickup_range = 50;
player.stats.required_xp = 5;
player.rotation = 0;
player.e.rec = {(GLOBALS::SCREEN.x / 2), (GLOBALS::SCREEN.y / 2),
SPRITE_SIZE_SCALED, SPRITE_SIZE_SCALED};
player.e.sprite.src = {56, 0, SPRITE_SIZE, SPRITE_SIZE};
player.e.sprite.dest = {player.e.rec.x + SPRITE_OFFSET,
player.e.rec.y + SPRITE_OFFSET,
player.e.sprite.src.width * SPRITE_SCALE_MULTI,
player.e.sprite.src.height * SPRITE_SCALE_MULTI};
player.e.sprite.center = {
(player.e.sprite.src.width * SPRITE_SCALE_MULTI) / 2,
(player.e.sprite.src.height * SPRITE_SCALE_MULTI) / 2};
player.engine_s.src = {72, 0, SPRITE_SIZE, SPRITE_SIZE * 2};
player.engine_s.dest = {player.e.sprite.dest.x, player.e.sprite.dest.y,
player.e.sprite.dest.width,
player.e.sprite.dest.height * 2};
player.engine_s.center = {player.e.sprite.center.x, player.e.sprite.center.y};
player.engine_anim.frames_counter = 0;
player.engine_anim.current_frame = 0;
player.engine_anim.num_frames = 4;
player.engine_anim.is_active = false;
player.e.color = C_PLAYER;
player.e.speed = 110;
};
void ResetPlayer_OnDie(Player &player) {
player.e.rec = {GLOBALS::SCREEN.x / 2, GLOBALS::SCREEN.y / 2,
SPRITE_SIZE_SCALED, SPRITE_SIZE_SCALED};
player.e.sprite.dest = {player.e.rec.x + SPRITE_OFFSET,
player.e.rec.y + SPRITE_OFFSET,
player.e.sprite.src.width * SPRITE_SCALE_MULTI,
player.e.sprite.src.height * SPRITE_SCALE_MULTI};
player.engine_s.dest = {player.e.sprite.dest.x, player.e.sprite.dest.y,
player.e.sprite.dest.width,
player.e.sprite.dest.height * 2};
};
void ResetPlayer_OnGameOver(Player &player) {
player.e.is_alive = true;
player.stats.lives = 3;
player.e.rec = {GLOBALS::SCREEN.x / 2, GLOBALS::SCREEN.y / 2,
SPRITE_SIZE_SCALED, SPRITE_SIZE_SCALED};
player.e.sprite.dest = {player.e.rec.x + SPRITE_OFFSET,
player.e.rec.y + SPRITE_OFFSET,
player.e.sprite.src.width * SPRITE_SCALE_MULTI,
player.e.sprite.src.height * SPRITE_SCALE_MULTI};
player.engine_s.dest = {player.e.sprite.dest.x, player.e.sprite.dest.y,
player.e.sprite.dest.width,
player.e.sprite.dest.height * 2};
};
void UpdatePlayer(Player &player, Sprite &bg) {
player.stats.fire_rate_timer -= GetFrameTime();
//------Player movement------//
player.engine_anim.is_active = true;
if (IsKeyDown(KEY_W)) {
player.e.rec.y -= player.e.speed * GetFrameTime();
player.e.sprite.dest.y -= player.e.speed * GetFrameTime();
bg.dest.y += player.e.speed * GetFrameTime() * .1f;
}
if (IsKeyDown(KEY_S)) {
player.e.rec.y += player.e.speed * GetFrameTime();
player.e.sprite.dest.y += player.e.speed * GetFrameTime();
bg.dest.y -= player.e.speed * GetFrameTime() * .1f;
}
if (IsKeyDown(KEY_A)) {
player.e.rec.x -= player.e.speed * GetFrameTime();
player.e.sprite.dest.x -= player.e.speed * GetFrameTime();
bg.dest.x += player.e.speed * GetFrameTime() * .1f;
}
if (IsKeyDown(KEY_D)) {
player.e.rec.x += player.e.speed * GetFrameTime();
player.e.sprite.dest.x += player.e.speed * GetFrameTime();
bg.dest.x -= player.e.speed * GetFrameTime() * .1f;
}
float dx = player.e.rec.x - (GetMouseX() - SPRITE_OFFSET);
float dy = player.e.rec.y - (GetMouseY() - SPRITE_OFFSET);
float final_rotation = ((atan2(dy, dx)) * 180 / PI) - 90;
player.rotation = final_rotation;
player.engine_s.dest.x = player.e.sprite.dest.x;
player.engine_s.dest.y = player.e.sprite.dest.y;
UpdateAnimation(player.engine_anim, true);
player.engine_s.src.x =
(float)(72 + (SPRITE_SIZE * player.engine_anim.current_frame));
};
void RenderPlayer(const Player &player, const Texture2D &game_atlas) {
#ifdef DEBUGGING
DrawRectangleLinesEx(player.e.rec, 1, player.e.color);
DrawCircleLines(player.e.rec.x, player.e.rec.y, 10, player.e.color);
#endif
if (player.engine_anim.is_active) {
DrawTexturePro(game_atlas, player.engine_s.src, player.engine_s.dest,
player.engine_s.center, player.rotation, WHITE);
}
DrawTexturePro(game_atlas, player.e.sprite.src, player.e.sprite.dest,
player.e.sprite.center, player.rotation, WHITE);
};
void IncreaseReqXP(Player &player) { player.stats.required_xp *= 1.2f; }
void Upgrade_PlayerFirerate(Player &player, float multiplier) {
player.stats.fire_rate /= multiplier;
}
void Upgrade_PlayerSpeed(Player &player, float multiplier) {
player.e.speed *= multiplier;
}