]> git.lizzy.rs Git - nothing.git/blob - src/game/level.c
Remove TODO(#345)
[nothing.git] / src / game / level.c
1 #include <SDL2/SDL.h>
2 #include <assert.h>
3
4 #include "color.h"
5 #include "game/camera.h"
6 #include "game/level.h"
7 #include "game/level/background.h"
8 #include "game/level/boxes.h"
9 #include "game/level/goals.h"
10 #include "game/level/labels.h"
11 #include "game/level/lava.h"
12 #include "game/level/physical_world.h"
13 #include "game/level/platforms.h"
14 #include "game/level/player.h"
15 #include "system/error.h"
16 #include "system/lt.h"
17 #include "system/lt/lt_adapters.h"
18
19 struct Level
20 {
21     Lt *lt;
22
23     Physical_world *physical_world;
24     Player *player;
25     Platforms *platforms;
26     Goals *goals;
27     Lava *lava;
28     Color background_color;
29     Platforms *back_platforms;
30     Background *background;
31     Boxes *boxes;
32     Labels *labels;
33 };
34
35 Level *create_level_from_file(const char *file_name)
36 {
37     assert(file_name);
38
39     Lt *const lt = create_lt();
40     if (lt == NULL) {
41         return NULL;
42     }
43
44     Level *const level = PUSH_LT(lt, malloc(sizeof(Level)), free);
45     if (level == NULL) {
46         throw_error(ERROR_TYPE_LIBC);
47         RETURN_LT(lt, NULL);
48     }
49
50     FILE *level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
51     if (level_file == NULL) {
52         throw_error(ERROR_TYPE_LIBC);
53         RETURN_LT(lt, NULL);
54     }
55
56     char color[7];
57     if (fscanf(level_file, "%6s", color) == EOF) {
58         throw_error(ERROR_TYPE_LIBC);
59         RETURN_LT(lt, NULL);
60     }
61     level->background_color = color_from_hexstr(color);
62
63     level->player = PUSH_LT(lt, create_player_from_stream(level_file), destroy_player);
64     if (level->player == NULL) {
65         RETURN_LT(lt, NULL);
66     }
67
68     level->platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
69     if (level->platforms == NULL) {
70         RETURN_LT(lt, NULL);
71     }
72
73     level->goals = PUSH_LT(lt, create_goals_from_stream(level_file), destroy_goals);
74     if (level->goals == NULL) {
75         RETURN_LT(lt, NULL);
76     }
77
78     level->lava = PUSH_LT(lt, create_lava_from_stream(level_file), destroy_lava);
79     if (level->lava == NULL) {
80         RETURN_LT(lt, NULL);
81     }
82
83     level->back_platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
84     if (level->back_platforms == NULL) {
85         RETURN_LT(lt, NULL);
86     }
87
88     level->boxes = PUSH_LT(lt, create_boxes_from_stream(level_file), destroy_boxes);
89     if (level->boxes == NULL) {
90         RETURN_LT(lt, NULL);
91     }
92
93     level->labels = PUSH_LT(lt, create_labels_from_stream(level_file), destroy_labels);
94     if (level->labels == NULL) {
95         RETURN_LT(lt, NULL);
96     }
97
98     level->background = PUSH_LT(lt, create_background(level->background_color), destroy_background);
99     if (level->background == NULL) {
100         RETURN_LT(lt, NULL);
101     }
102
103     level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
104     if (level->physical_world == NULL) {
105         RETURN_LT(lt, NULL);
106     }
107     if (physical_world_add_solid(
108             level->physical_world,
109             player_as_solid(level->player)) < 0) { RETURN_LT(lt, NULL); }
110     if (boxes_add_to_physical_world(
111             level->boxes,
112             level->physical_world) < 0) { RETURN_LT(lt, NULL); }
113
114     level->lt = lt;
115
116     fclose(RELEASE_LT(lt, level_file));
117
118     return level;
119 }
120
121 void destroy_level(Level *level)
122 {
123     assert(level);
124     RETURN_LT0(level->lt);
125 }
126
127 int level_render(const Level *level, Camera *camera)
128 {
129     assert(level);
130
131     player_focus_camera(level->player, camera);
132
133     if (camera_clear_background(camera, level->background_color) < 0) {
134         return -1;
135     }
136
137     if (background_render(level->background, camera) < 0) {
138         return -1;
139     }
140
141     if (platforms_render(level->back_platforms, camera) < 0) {
142         return -1;
143     }
144
145     if (player_render(level->player, camera) < 0) {
146         return -1;
147     }
148
149     if (boxes_render(level->boxes, camera) < 0) {
150         return -1;
151     }
152
153     if (lava_render(level->lava, camera) < 0) {
154         return -1;
155     }
156
157     if (platforms_render(level->platforms, camera) < 0) {
158         return -1;
159     }
160
161     if (goals_render(level->goals, camera) < 0) {
162         return -1;
163     }
164
165     if (labels_render(level->labels, camera) < 0) {
166         return -1;
167     }
168
169     return 0;
170 }
171
172 int level_update(Level *level, float delta_time)
173 {
174     assert(level);
175     assert(delta_time > 0);
176
177     physical_world_apply_gravity(level->physical_world);
178     boxes_float_in_lava(level->boxes, level->lava);
179
180     boxes_update(level->boxes, delta_time);
181     player_update(level->player, delta_time);
182
183     physical_world_collide_solids(level->physical_world, level->platforms);
184
185     player_hide_goals(level->player, level->goals);
186     player_die_from_lava(level->player, level->lava);
187
188     goals_update(level->goals, delta_time);
189     lava_update(level->lava, delta_time);
190     labels_update(level->labels, delta_time);
191
192     return 0;
193 }
194
195 int level_event(Level *level, const SDL_Event *event)
196 {
197     assert(level);
198     assert(event);
199
200     switch (event->type) {
201     case SDL_KEYDOWN:
202         switch (event->key.keysym.sym) {
203         case SDLK_SPACE:
204             player_jump(level->player);
205             break;
206         }
207         break;
208
209     case SDL_JOYBUTTONDOWN:
210         if (event->jbutton.button == 1) {
211             player_jump(level->player);
212         }
213         break;
214     }
215
216     return 0;
217 }
218
219 int level_input(Level *level,
220                 const Uint8 *const keyboard_state,
221                 SDL_Joystick *the_stick_of_joy)
222 {
223     assert(level);
224     assert(keyboard_state);
225     (void) the_stick_of_joy;
226
227     if (keyboard_state[SDL_SCANCODE_A]) {
228         player_move_left(level->player);
229     } else if (keyboard_state[SDL_SCANCODE_D]) {
230         player_move_right(level->player);
231     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
232         player_move_left(level->player);
233     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
234         player_move_right(level->player);
235     } else {
236         player_stop(level->player);
237     }
238
239     return 0;
240 }
241
242 int level_reload_preserve_player(Level *level, const char *file_name)
243 {
244     Lt * const lt = create_lt();
245     if (lt == NULL) {
246         return -1;
247     }
248
249     /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
250
251
252
253     FILE * const level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
254     if (level_file == NULL) {
255         throw_error(ERROR_TYPE_LIBC);
256         RETURN_LT(lt, -1);
257     }
258
259     char color[7];
260     if (fscanf(level_file, "%6s", color) == EOF) {
261         throw_error(ERROR_TYPE_LIBC);
262         RETURN_LT(lt, -1);
263     }
264     level->background_color = color_from_hexstr(color);
265
266     Player * const skipped_player = create_player_from_stream(level_file);
267     if (skipped_player == NULL) {
268         RETURN_LT(lt, -1);
269     }
270     destroy_player(skipped_player);
271
272     Platforms * const platforms = create_platforms_from_stream(level_file);
273     if (platforms == NULL) {
274         RETURN_LT(lt, -1);
275     }
276     level->platforms = RESET_LT(level->lt, level->platforms, platforms);
277
278     Goals * const goals = create_goals_from_stream(level_file);
279     if (goals == NULL) {
280         RETURN_LT(lt, -1);
281     }
282     level->goals = RESET_LT(level->lt, level->goals, goals);
283
284     Lava * const lava = create_lava_from_stream(level_file);
285     if (lava == NULL) {
286         RETURN_LT(lt, -1);
287     }
288     level->lava = RESET_LT(level->lt, level->lava, lava);
289
290     Platforms * const back_platforms = create_platforms_from_stream(level_file);
291     if (back_platforms == NULL) {
292         RETURN_LT(lt, -1);
293     }
294     level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
295
296     Boxes * const boxes = create_boxes_from_stream(level_file);
297     if (boxes == NULL) {
298         RETURN_LT(lt, -1);
299     }
300     level->boxes = RESET_LT(level->lt, level->boxes, boxes);
301
302     Labels * const labels = create_labels_from_stream(level_file);
303     if (labels == NULL) {
304         RETURN_LT(lt, -1);
305     }
306     level->labels = RESET_LT(level->lt, level->labels, labels);
307
308     physical_world_clean(level->physical_world);
309     if (physical_world_add_solid(
310             level->physical_world,
311             player_as_solid(level->player)) < 0) { RETURN_LT(lt, -1); }
312     if (boxes_add_to_physical_world(
313             level->boxes,
314             level->physical_world) < 0) { RETURN_LT(lt, -1); }
315
316     RETURN_LT(lt, 0);
317 }
318
319 int level_sound(Level *level, Sound_samples *sound_samples)
320 {
321     if (goals_sound(level->goals, sound_samples) < 0) {
322         return -1;
323     }
324
325     if (player_sound(level->player, sound_samples) < 0) {
326         return -1;
327     }
328
329     return 0;
330 }
331
332 void level_toggle_debug_mode(Level *level)
333 {
334     background_toggle_debug_mode(level->background);
335 }
336
337 int level_enter_camera_event(Level *level,
338                              const Camera *camera)
339 {
340     goals_cue(level->goals, camera);
341     goals_checkpoint(level->goals, level->player);
342     labels_enter_camera_event(level->labels, camera);
343     return 0;
344 }
345
346 Rigid_rect *level_rigid_rect(Level *level,
347                              const char *rigid_rect_id)
348 {
349     assert(level);
350     assert(rigid_rect_id);
351
352     Rigid_rect *rigid_rect = player_rigid_rect(level->player,
353                                                rigid_rect_id);
354     if (rigid_rect != NULL) {
355         return rigid_rect;
356     }
357
358     rigid_rect = boxes_rigid_rect(level->boxes, rigid_rect_id);
359     if (rigid_rect != NULL) {
360         return rigid_rect;
361     }
362
363     return NULL;
364 }