]> git.lizzy.rs Git - nothing.git/blob - src/player.c
5945dd053b5bc5ac5511b9ff3fcfc4712cfe023f
[nothing.git] / src / player.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL2/SDL.h>
4
5 #include "./player.h"
6 #include "./platforms.h"
7
8 #define PLAYER_WIDTH 50.0f
9 #define PLAYER_HEIGHT 50.0f
10 #define PLAYER_SPEED 500.0f
11 #define PLAYER_GRAVITY 1500.0f
12
13 struct player_t {
14     vec_t position;
15     vec_t velocity;
16     vec_t movement;
17 };
18
19 /* static const vec_t impact_vecs[RECT_SIDE_N] = { */
20 /*     /\* left *\/ */
21 /*     { .x = 1.0f, .y = 0.0f }, */
22 /*     /\* right *\/ */
23 /*     { .x = -1.0f, .y = 0.0f }, */
24 /*     /\* top *\/ */
25 /*     { .x = 0.0f, .y = 1.0f }, */
26 /*     /\* bottom *\/ */
27 /*     { .x = 0.0f, .y = -1.0f } */
28 /* }; */
29
30 player_t *create_player(float x, float y)
31 {
32     player_t *player = malloc(sizeof(player_t));
33
34     if (player == NULL) {
35         return NULL;
36     }
37
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;
44
45     return player;
46 }
47
48 void destroy_player(player_t * player)
49 {
50     free(player);
51 }
52
53 int render_player(const player_t * player,
54                   SDL_Renderer *renderer,
55                   const camera_t *camera)
56 {
57     if (SDL_SetRenderDrawColor(renderer, 96, 255, 96, 255) < 0) {
58         return -1;
59     }
60     rect_t player_object = {
61         .x = player->position.x,
62         .y = player->position.y,
63         .w = PLAYER_WIDTH,
64         .h = PLAYER_HEIGHT
65     };
66
67
68     return camera_fill_rect(camera, renderer, &player_object);
69 }
70
71 void update_player(player_t * player,
72                    const platforms_t *platforms,
73                    Uint32 delta_time)
74 {
75     float d = (float) delta_time / 1000.0f;
76
77     float x = player->position.x;
78     float y = player->position.y;
79
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,
83         .w = PLAYER_WIDTH,
84         .h = PLAYER_HEIGHT
85     };
86
87     int sides[4] = {0, 0, 0, 0};
88
89     platforms_rect_object_collide(platforms, &player_object, sides);
90
91     if (sides[RECT_SIDE_LEFT] || sides[RECT_SIDE_RIGHT]) {
92         player->velocity.x = 0.0f;
93         player->movement.x = 0.0f;
94     }
95
96     int hit_floor_ceiling = 0;
97
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;
102     }
103
104     player->position.x += (player->velocity.x + player->movement.x) * d;
105     player->position.y += (player->velocity.y + player->movement.y) * d;
106
107     if (!hit_floor_ceiling) {
108         player->velocity.y += PLAYER_GRAVITY * d;
109     }
110 }
111
112 void player_move_left(player_t *player)
113 {
114     player->movement.x = -PLAYER_SPEED;
115     player->movement.y = 0.0f;
116 }
117
118 void player_move_right(player_t *player)
119 {
120     player->movement.x = PLAYER_SPEED;
121     player->movement.y = 0.0f;
122 }
123
124 void player_stop(player_t *player)
125 {
126     player->movement.x = 0.0f;
127     player->movement.y = 0.0f;
128 }
129
130 void player_jump(player_t *player)
131 {
132     player->velocity.y = -1000.0f;
133 }
134
135 void player_focus_camera(player_t *player,
136                          camera_t *camera)
137 {
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);
141 }