6 #include "game/level/player/dying_rect.h"
7 #include "game/level/player/rigid_rect.h"
9 #include "math/point.h"
10 #include "platforms.h"
12 #include "system/error.h"
13 #include "system/lt.h"
15 #define PLAYER_WIDTH 25.0f
16 #define PLAYER_HEIGHT 25.0f
17 #define PLAYER_SPEED 500.0f
18 #define PLAYER_JUMP 550.0f
19 #define PLAYER_DEATH_DURATION 0.75f
21 typedef enum player_state_t {
22 PLAYER_STATE_ALIVE = 0,
30 rigid_rect_t *alive_body;
31 dying_rect_t *dying_body;
36 /* TODO(#110): introduce checkpoints */
40 player_t *create_player(float x, float y, color_t color)
42 lt_t *lt = create_lt();
48 player_t *player = PUSH_LT(lt, malloc(sizeof(player_t)), free);
50 throw_error(ERROR_TYPE_LIBC);
54 player->state = PLAYER_STATE_ALIVE;
56 player->alive_body = PUSH_LT(
59 rect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT),
62 if (player->alive_body == NULL) {
66 player->dying_body = PUSH_LT(
69 rect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT),
71 PLAYER_DEATH_DURATION),
73 if (player->dying_body == NULL) {
78 player->jump_count = 0;
79 player->color = color;
80 player->checkpoint = vec(x, y);
85 player_t *create_player_from_stream(FILE *stream)
87 float x = 0.0f, y = 0.0f;
90 if (fscanf(stream, "%f%f%6s", &x, &y, color) == EOF) {
91 throw_error(ERROR_TYPE_LIBC);
95 return create_player(x, y, color_from_hexstr(color));
98 void destroy_player(player_t * player)
100 RETURN_LT0(player->lt);
103 int player_render(const player_t * player,
104 SDL_Renderer *renderer,
105 const camera_t *camera)
111 switch (player->state) {
112 case PLAYER_STATE_ALIVE:
113 return rigid_rect_render(player->alive_body, renderer, camera);
115 case PLAYER_STATE_DYING:
116 return dying_rect_render(player->dying_body, renderer, camera);
124 void player_update(player_t *player,
125 const platforms_t *platforms,
131 switch (player->state) {
132 case PLAYER_STATE_ALIVE: {
133 rigid_rect_update(player->alive_body, platforms, delta_time);
135 if (rigid_rect_touches_ground(player->alive_body)) {
136 player->jump_count = 0;
139 const rect_t hitbox = rigid_rect_hitbox(player->alive_body);
141 if (hitbox.y > 1000.0f) {
146 case PLAYER_STATE_DYING: {
147 dying_rect_update(player->dying_body, delta_time);
149 if (dying_rect_is_dead(player->dying_body)) {
150 player->alive_body = RESET_LT(
156 vec(PLAYER_WIDTH, PLAYER_HEIGHT)),
158 player->state = PLAYER_STATE_ALIVE;
166 void player_move_left(player_t *player)
169 rigid_rect_move(player->alive_body, vec(-PLAYER_SPEED, 0.0f));
172 void player_move_right(player_t *player)
176 rigid_rect_move(player->alive_body, vec(PLAYER_SPEED, 0.0f));
179 void player_stop(player_t *player)
183 rigid_rect_move(player->alive_body, vec(0.0f, 0.0f));
186 void player_jump(player_t *player)
189 if (player->jump_count < 2) {
190 rigid_rect_jump(player->alive_body, PLAYER_JUMP);
191 player->jump_count++;
195 void player_die(player_t *player)
199 if (player->state == PLAYER_STATE_ALIVE) {
200 player->dying_body = RESET_LT(
204 rigid_rect_hitbox(player->alive_body),
206 PLAYER_DEATH_DURATION));
207 player->state = PLAYER_STATE_DYING;
211 void player_focus_camera(player_t *player,
217 const rect_t player_hitbox = rigid_rect_hitbox(player->alive_body);
222 vec(player_hitbox.x, player_hitbox.y),
223 vec(0.0f, -player_hitbox.h * 0.5f)));
226 void player_hide_goals(const player_t *player,
231 goals_hide(goals, rigid_rect_hitbox(player->alive_body));
234 void player_die_from_lava(player_t *player,
237 if (lava_overlaps_rect(lava, rigid_rect_hitbox(player->alive_body))) {
242 void player_checkpoint(player_t *player, vec_t checkpoint)
244 player->checkpoint = checkpoint;