]> git.lizzy.rs Git - nothing.git/blob - src/game/level.c
Move fclose_lt to a new header for LT adapters
[nothing.git] / src / game / level.c
1 #include <SDL2/SDL.h>
2 #include <assert.h>
3
4 #include "game/camera.h"
5 #include "game/level.h"
6 #include "game/level/background.h"
7 #include "game/level/boxes.h"
8 #include "game/level/goals.h"
9 #include "game/level/lava.h"
10 #include "game/level/platforms.h"
11 #include "game/level/player.h"
12 #include "system/error.h"
13 #include "system/lt.h"
14 #include "system/lt/lt_adapters.h"
15
16 #define LEVEL_GRAVITY 1500.0f
17
18 struct level_t
19 {
20     lt_t *lt;
21     player_t *player;
22     platforms_t *platforms;
23     goals_t *goals;
24     lava_t *lava;
25     color_t background_color;
26     platforms_t *back_platforms;
27     background_t *background;
28     boxes_t *boxes;
29 };
30
31 level_t *create_level_from_file(const char *file_name)
32 {
33     assert(file_name);
34
35     lt_t *const lt = create_lt();
36     if (lt == NULL) {
37         return NULL;
38     }
39
40     level_t *const level = PUSH_LT(lt, malloc(sizeof(level_t)), free);
41     if (level == NULL) {
42         throw_error(ERROR_TYPE_LIBC);
43         RETURN_LT(lt, NULL);
44     }
45
46     FILE *level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
47     if (level_file == NULL) {
48         throw_error(ERROR_TYPE_LIBC);
49         RETURN_LT(lt, NULL);
50     }
51
52     char color[7];
53     if (fscanf(level_file, "%6s", color) == EOF) {
54         throw_error(ERROR_TYPE_LIBC);
55         RETURN_LT(lt, NULL);
56     }
57     level->background_color = color_from_hexstr(color);
58
59     level->player = PUSH_LT(lt, create_player_from_stream(level_file), destroy_player);
60     if (level->player == NULL) {
61         RETURN_LT(lt, NULL);
62     }
63
64     level->platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
65     if (level->platforms == NULL) {
66         RETURN_LT(lt, NULL);
67     }
68
69     level->goals = PUSH_LT(lt, create_goals_from_stream(level_file), destroy_goals);
70     if (level->goals == NULL) {
71         RETURN_LT(lt, NULL);
72     }
73
74     level->lava = PUSH_LT(lt, create_lava_from_stream(level_file), destroy_lava);
75     if (level->lava == NULL) {
76         RETURN_LT(lt, NULL);
77     }
78
79     level->back_platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
80     if (level->back_platforms == NULL) {
81         RETURN_LT(lt, NULL);
82     }
83
84     level->boxes = PUSH_LT(lt, create_boxes_from_stream(level_file), destroy_boxes);
85     if (level->boxes == NULL) {
86         RETURN_LT(lt, NULL);
87     }
88
89     level->background = PUSH_LT(lt, create_background(level->background_color), destroy_background);
90     if (level->background == NULL) {
91         RETURN_LT(lt, NULL);
92     }
93
94     level->lt = lt;
95
96     fclose(RELEASE_LT(lt, level_file));
97
98     return level;
99 }
100
101 void destroy_level(level_t *level)
102 {
103     assert(level);
104     RETURN_LT0(level->lt);
105 }
106
107 int level_render(const level_t *level, camera_t *camera)
108 {
109     assert(level);
110
111     player_focus_camera(level->player, camera);
112
113     if (camera_clear_background(camera, level->background_color) < 0) {
114         return -1;
115     }
116
117     if (background_render(level->background, camera) < 0) {
118         return -1;
119     }
120
121     if (platforms_render(level->back_platforms, camera) < 0) {
122         return -1;
123     }
124
125     if (player_render(level->player, camera) < 0) {
126         return -1;
127     }
128
129     if (boxes_render(level->boxes, camera) < 0) {
130         return -1;
131     }
132
133     if (lava_render(level->lava, camera) < 0) {
134         return -1;
135     }
136
137     if (platforms_render(level->platforms, camera) < 0) {
138         return -1;
139     }
140
141     if (goals_render(level->goals, camera) < 0) {
142         return -1;
143     }
144
145     /* TODO(#157): goals_cue is not supposed to be invoked in level_render
146      *
147      * But I simply couldn't find a better place for it.
148      */
149     goals_cue(level->goals, camera);
150
151     return 0;
152 }
153
154 int level_update(level_t *level, float delta_time)
155 {
156     assert(level);
157     assert(delta_time > 0);
158
159     player_apply_force(level->player, vec(0.0f, LEVEL_GRAVITY));
160     boxes_apply_force(level->boxes, vec(0.0f, LEVEL_GRAVITY));
161
162     boxes_update(level->boxes, delta_time);
163     player_update(level->player, delta_time);
164
165     /* TODO(#202): it is diffcult to introduce more kinds of object into the physics engine */
166     boxes_collide_with_solid(level->boxes, platforms_as_solid(level->platforms));
167     player_collide_with_solid(level->player, platforms_as_solid(level->platforms));
168
169     boxes_collide_with_lava(level->boxes, level->lava);
170     boxes_collide_with_solid(level->boxes, boxes_as_solid(level->boxes));
171     boxes_collide_with_solid(level->boxes, player_as_solid(level->player));
172
173     player_collide_with_solid(level->player, boxes_as_solid(level->boxes));
174
175     boxes_collide_with_solid(level->boxes, platforms_as_solid(level->platforms));
176     player_collide_with_solid(level->player, platforms_as_solid(level->platforms));
177
178     boxes_collide_with_solid(level->boxes, boxes_as_solid(level->boxes));
179     player_collide_with_solid(level->player, boxes_as_solid(level->boxes));
180
181     player_hide_goals(level->player, level->goals);
182     player_die_from_lava(level->player, level->lava);
183
184     goals_update(level->goals, delta_time);
185     goals_checkpoint(level->goals, level->player);
186     lava_update(level->lava, delta_time);
187
188     return 0;
189 }
190
191 int level_event(level_t *level, const SDL_Event *event)
192 {
193     assert(level);
194     assert(event);
195
196     switch (event->type) {
197     case SDL_KEYDOWN:
198         switch (event->key.keysym.sym) {
199         case SDLK_SPACE:
200             player_jump(level->player);
201             break;
202         }
203         break;
204
205     case SDL_JOYBUTTONDOWN:
206         if (event->jbutton.button == 1) {
207             player_jump(level->player);
208         }
209         break;
210     }
211
212     return 0;
213 }
214
215 int level_input(level_t *level,
216                 const Uint8 *const keyboard_state,
217                 SDL_Joystick *the_stick_of_joy)
218 {
219     assert(level);
220     assert(keyboard_state);
221     (void) the_stick_of_joy;
222
223     if (keyboard_state[SDL_SCANCODE_A]) {
224         player_move_left(level->player);
225     } else if (keyboard_state[SDL_SCANCODE_D]) {
226         player_move_right(level->player);
227     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
228         player_move_left(level->player);
229     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
230         player_move_right(level->player);
231     } else {
232         player_stop(level->player);
233     }
234
235     return 0;
236 }
237
238 int level_reload_preserve_player(level_t *level, const char *file_name)
239 {
240     lt_t * const lt = create_lt();
241     if (lt == NULL) {
242         return -1;
243     }
244
245     /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
246
247     FILE * const level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
248     if (level_file == NULL) {
249         throw_error(ERROR_TYPE_LIBC);
250         RETURN_LT(lt, -1);
251     }
252
253     char color[7];
254     if (fscanf(level_file, "%6s", color) == EOF) {
255         throw_error(ERROR_TYPE_LIBC);
256         RETURN_LT(lt, -1);
257     }
258     level->background_color = color_from_hexstr(color);
259
260     player_t * const skipped_player = create_player_from_stream(level_file);
261     if (skipped_player == NULL) {
262         RETURN_LT(lt, -1);
263     }
264     destroy_player(skipped_player);
265
266     platforms_t * const platforms = create_platforms_from_stream(level_file);
267     if (platforms == NULL) {
268         RETURN_LT(lt, -1);
269     }
270     level->platforms = RESET_LT(level->lt, level->platforms, platforms);
271
272     goals_t * const goals = create_goals_from_stream(level_file);
273     if (goals == NULL) {
274         RETURN_LT(lt, -1);
275     }
276     level->goals = RESET_LT(level->lt, level->goals, goals);
277
278     lava_t * const lava = create_lava_from_stream(level_file);
279     if (lava == NULL) {
280         RETURN_LT(lt, -1);
281     }
282     level->lava = RESET_LT(level->lt, level->lava, lava);
283
284     platforms_t * const back_platforms = create_platforms_from_stream(level_file);
285     if (back_platforms == NULL) {
286         RETURN_LT(lt, -1);
287     }
288     level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
289
290     boxes_t * const boxes = create_boxes_from_stream(level_file);
291     if (level->boxes == NULL) {
292         RETURN_LT(lt, -1);
293     }
294     level->boxes = RESET_LT(level->lt, level->boxes, boxes);
295
296     RETURN_LT(lt, 0);
297 }
298
299 int level_sound(level_t *level, sound_samples_t *sound_samples)
300 {
301     if (goals_sound(level->goals, sound_samples) < 0) {
302         return -1;
303     }
304
305     if (player_sound(level->player, sound_samples) < 0) {
306         return -1;
307     }
308
309     return 0;
310 }
311
312 void level_toggle_debug_mode(level_t *level)
313 {
314     background_toggle_debug_mode(level->background);
315 }