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;
41 player_t *create_player(float x, float y, color_t color)
43 lt_t *lt = create_lt();
49 player_t *player = PUSH_LT(lt, malloc(sizeof(player_t)), free);
51 throw_error(ERROR_TYPE_LIBC);
55 player->state = PLAYER_STATE_ALIVE;
57 player->alive_body = PUSH_LT(
60 rect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT),
63 if (player->alive_body == NULL) {
67 player->dying_body = PUSH_LT(
70 rect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT),
72 PLAYER_DEATH_DURATION),
74 if (player->dying_body == NULL) {
79 player->jump_count = 0;
80 player->color = color;
81 player->checkpoint = vec(x, y);
82 player->play_die_cue = 0;
87 player_t *create_player_from_stream(FILE *stream)
89 float x = 0.0f, y = 0.0f;
92 if (fscanf(stream, "%f%f%6s", &x, &y, color) == EOF) {
93 throw_error(ERROR_TYPE_LIBC);
97 return create_player(x, y, color_from_hexstr(color));
100 void destroy_player(player_t * player)
102 RETURN_LT0(player->lt);
105 solid_ref_t player_as_solid(player_t *player)
109 .ptr = (void*) player
115 int player_render(const player_t * player,
116 const camera_t *camera)
121 switch (player->state) {
122 case PLAYER_STATE_ALIVE:
123 return rigid_rect_render(player->alive_body, camera);
125 case PLAYER_STATE_DYING:
126 return dying_rect_render(player->dying_body, camera);
134 void player_update(player_t *player,
139 switch (player->state) {
140 case PLAYER_STATE_ALIVE: {
141 rigid_rect_update(player->alive_body, delta_time);
143 const rect_t hitbox = rigid_rect_hitbox(player->alive_body);
145 if (hitbox.y > 1000.0f) {
150 case PLAYER_STATE_DYING: {
151 dying_rect_update(player->dying_body, delta_time);
153 if (dying_rect_is_dead(player->dying_body)) {
154 player->alive_body = RESET_LT(
160 vec(PLAYER_WIDTH, PLAYER_HEIGHT)),
162 player->state = PLAYER_STATE_ALIVE;
170 void player_collide_with_solid(player_t *player, solid_ref_t solid)
172 if (player->state == PLAYER_STATE_ALIVE) {
173 rigid_rect_collide_with_solid(player->alive_body, solid);
175 if (rigid_rect_touches_ground(player->alive_body)) {
176 player->jump_count = 0;
181 void player_impact_rigid_rect(player_t * player,
182 rigid_rect_t *rigid_rect)
184 if (player->state == PLAYER_STATE_ALIVE) {
185 rigid_rect_impact_rigid_rect(player->alive_body, rigid_rect);
189 void player_move_left(player_t *player)
192 rigid_rect_move(player->alive_body, vec(-PLAYER_SPEED, 0.0f));
195 void player_move_right(player_t *player)
199 rigid_rect_move(player->alive_body, vec(PLAYER_SPEED, 0.0f));
202 void player_stop(player_t *player)
206 rigid_rect_move(player->alive_body, vec(0.0f, 0.0f));
209 void player_jump(player_t *player)
212 if (player->jump_count < 2) {
213 rigid_rect_jump(player->alive_body, PLAYER_JUMP);
214 player->jump_count++;
218 void player_die(player_t *player)
222 if (player->state == PLAYER_STATE_ALIVE) {
223 player->play_die_cue = 1;
224 player->dying_body = RESET_LT(
228 rigid_rect_hitbox(player->alive_body),
230 PLAYER_DEATH_DURATION));
231 player->state = PLAYER_STATE_DYING;
235 void player_focus_camera(player_t *player,
241 const rect_t player_hitbox = rigid_rect_hitbox(player->alive_body);
246 vec(player_hitbox.x, player_hitbox.y),
247 vec(0.0f, -player_hitbox.h * 0.5f)));
250 void player_hide_goals(const player_t *player,
255 goals_hide(goals, rigid_rect_hitbox(player->alive_body));
258 void player_die_from_lava(player_t *player,
261 if (lava_overlaps_rect(lava, rigid_rect_hitbox(player->alive_body))) {
266 void player_checkpoint(player_t *player, vec_t checkpoint)
268 player->checkpoint = checkpoint;
271 int player_sound(player_t *player,
272 sound_samples_t *sound_samples)
274 if (player->play_die_cue) {
275 player->play_die_cue = 0;
277 if (sound_samples_play_sound(sound_samples, 0, 0) < 0) {
285 void player_rect_object_collide(player_t *player,
287 int sides[RECT_SIDE_N])
289 if (player->state == PLAYER_STATE_ALIVE) {
290 rigid_rect_touches_rect_sides(player->alive_body, object, sides);