6 #include "./platforms.h"
8 #define PLAYER_WIDTH 50.0f
9 #define PLAYER_HEIGHT 50.0f
10 #define PLAYER_SPEED 500.0f
11 #define PLAYER_GRAVITY 1500.0f
19 /* static const vec_t impact_vecs[RECT_SIDE_N] = { */
21 /* { .x = 1.0f, .y = 0.0f }, */
23 /* { .x = -1.0f, .y = 0.0f }, */
25 /* { .x = 0.0f, .y = 1.0f }, */
27 /* { .x = 0.0f, .y = -1.0f } */
30 player_t *create_player(float x, float y)
32 player_t *player = malloc(sizeof(player_t));
38 player->position.x = x;
39 player->position.y = y;
40 player->velocity.x = 0.0f;
41 player->velocity.y = 0.0f;
42 player->movement.x = 0.0f;
43 player->movement.y = 0.0f;
48 void destroy_player(player_t * player)
53 int render_player(const player_t * player,
54 SDL_Renderer *renderer,
55 const camera_t *camera)
57 if (SDL_SetRenderDrawColor(renderer, 96, 255, 96, 255) < 0) {
60 rect_t player_object = {
61 .x = player->position.x,
62 .y = player->position.y,
68 return camera_fill_rect(camera, renderer, &player_object);
71 void update_player(player_t * player,
72 const platforms_t *platforms,
75 float d = (float) delta_time / 1000.0f;
77 float x = player->position.x;
78 float y = player->position.y;
80 rect_t player_object = {
81 .x = x + (player->velocity.x + player->movement.x) * d,
82 .y = y + (player->velocity.y + player->movement.y) * d,
87 int sides[4] = {0, 0, 0, 0};
89 platforms_rect_object_collide(platforms, &player_object, sides);
91 if (sides[RECT_SIDE_LEFT] || sides[RECT_SIDE_RIGHT]) {
92 player->velocity.x = 0.0f;
93 player->movement.x = 0.0f;
96 int hit_floor_ceiling = 0;
98 if (sides[RECT_SIDE_TOP] || sides[RECT_SIDE_BOTTOM]) {
99 player->velocity.y = 0.0f;
100 player->movement.y = 0.0f;
101 hit_floor_ceiling = 0;
104 player->position.x += (player->velocity.x + player->movement.x) * d;
105 player->position.y += (player->velocity.y + player->movement.y) * d;
107 if (!hit_floor_ceiling) {
108 player->velocity.y += PLAYER_GRAVITY * d;
112 void player_move_left(player_t *player)
114 player->movement.x = -PLAYER_SPEED;
115 player->movement.y = 0.0f;
118 void player_move_right(player_t *player)
120 player->movement.x = PLAYER_SPEED;
121 player->movement.y = 0.0f;
124 void player_stop(player_t *player)
126 player->movement.x = 0.0f;
127 player->movement.y = 0.0f;
130 void player_jump(player_t *player)
132 player->velocity.y = -1000.0f;
135 void player_focus_camera(player_t *player,
138 camera_translate(camera,
139 player->position.x - 800.0f * 0.5f + PLAYER_WIDTH * 0.5f,
140 player->position.y - 600.0f * 0.5f + PLAYER_HEIGHT * 0.5f);