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 */
42 player_t *create_player(float x, float y, color_t color)
44 lt_t *lt = create_lt();
50 player_t *player = PUSH_LT(lt, malloc(sizeof(player_t)), free);
52 throw_error(ERROR_TYPE_LIBC);
56 player->state = PLAYER_STATE_ALIVE;
58 player->alive_body = PUSH_LT(
61 rect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT),
64 if (player->alive_body == NULL) {
68 player->dying_body = PUSH_LT(
71 rect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT),
73 PLAYER_DEATH_DURATION),
75 if (player->dying_body == NULL) {
80 player->jump_count = 0;
81 player->color = color;
82 player->checkpoint = vec(x, y);
83 player->play_die_cue = 0;
88 player_t *create_player_from_stream(FILE *stream)
90 float x = 0.0f, y = 0.0f;
93 if (fscanf(stream, "%f%f%6s", &x, &y, color) == EOF) {
94 throw_error(ERROR_TYPE_LIBC);
98 return create_player(x, y, color_from_hexstr(color));
101 void destroy_player(player_t * player)
103 RETURN_LT0(player->lt);
106 int player_render(const player_t * player,
107 const camera_t *camera)
112 switch (player->state) {
113 case PLAYER_STATE_ALIVE:
114 return rigid_rect_render(player->alive_body, camera);
116 case PLAYER_STATE_DYING:
117 return dying_rect_render(player->dying_body, camera);
125 void player_update(player_t *player,
130 switch (player->state) {
131 case PLAYER_STATE_ALIVE: {
132 rigid_rect_update(player->alive_body, delta_time);
134 const rect_t hitbox = rigid_rect_hitbox(player->alive_body);
136 if (hitbox.y > 1000.0f) {
141 case PLAYER_STATE_DYING: {
142 dying_rect_update(player->dying_body, delta_time);
144 if (dying_rect_is_dead(player->dying_body)) {
145 player->alive_body = RESET_LT(
151 vec(PLAYER_WIDTH, PLAYER_HEIGHT)),
153 player->state = PLAYER_STATE_ALIVE;
161 void player_collide_with_solid(player_t *player, solid_ref_t solid)
163 if (player->state == PLAYER_STATE_ALIVE) {
164 rigid_rect_collide_with_solid(player->alive_body, solid);
166 if (rigid_rect_touches_ground(player->alive_body)) {
167 player->jump_count = 0;
172 void player_collide_with_platforms(player_t * player,
173 const platforms_t *platforms)
175 if (player->state == PLAYER_STATE_ALIVE) {
176 rigid_rect_collide_with_platforms(player->alive_body, platforms);
178 if (rigid_rect_touches_ground(player->alive_body)) {
179 player->jump_count = 0;
184 void player_collide_with_boxes(player_t * player,
185 const boxes_t * boxes)
187 if (player->state == PLAYER_STATE_ALIVE) {
188 rigid_rect_collide_with_boxes(player->alive_body, boxes);
190 if (rigid_rect_touches_ground(player->alive_body)) {
191 player->jump_count = 0;
196 void player_impact_rigid_rect(player_t * player,
197 rigid_rect_t *rigid_rect)
199 if (player->state == PLAYER_STATE_ALIVE) {
200 rigid_rect_impact_rigid_rect(player->alive_body, rigid_rect);
204 void player_move_left(player_t *player)
207 rigid_rect_move(player->alive_body, vec(-PLAYER_SPEED, 0.0f));
210 void player_move_right(player_t *player)
214 rigid_rect_move(player->alive_body, vec(PLAYER_SPEED, 0.0f));
217 void player_stop(player_t *player)
221 rigid_rect_move(player->alive_body, vec(0.0f, 0.0f));
224 void player_jump(player_t *player)
227 if (player->jump_count < 2) {
228 rigid_rect_jump(player->alive_body, PLAYER_JUMP);
229 player->jump_count++;
233 void player_die(player_t *player)
237 if (player->state == PLAYER_STATE_ALIVE) {
238 player->play_die_cue = 1;
239 player->dying_body = RESET_LT(
243 rigid_rect_hitbox(player->alive_body),
245 PLAYER_DEATH_DURATION));
246 player->state = PLAYER_STATE_DYING;
250 void player_focus_camera(player_t *player,
256 const rect_t player_hitbox = rigid_rect_hitbox(player->alive_body);
261 vec(player_hitbox.x, player_hitbox.y),
262 vec(0.0f, -player_hitbox.h * 0.5f)));
265 void player_hide_goals(const player_t *player,
270 goals_hide(goals, rigid_rect_hitbox(player->alive_body));
273 void player_die_from_lava(player_t *player,
276 if (lava_overlaps_rect(lava, rigid_rect_hitbox(player->alive_body))) {
281 void player_checkpoint(player_t *player, vec_t checkpoint)
283 player->checkpoint = checkpoint;
286 int player_sound(player_t *player,
287 sound_samples_t *sound_samples)
289 if (player->play_die_cue) {
290 player->play_die_cue = 0;
292 if (sound_samples_play_sound(sound_samples, 0, 0) < 0) {
300 rect_t player_hitbox(const player_t *player)
302 if (player->state == PLAYER_STATE_ALIVE) {
303 return rigid_rect_hitbox(player->alive_body);
305 return rect(0.0f, 0.0f, 0.0f, 0.0f);