]> git.lizzy.rs Git - nothing.git/blob - src/game/level.c
(#265) color -> c
[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_t
20 {
21     lt_t *lt;
22
23     physical_world_t *physical_world;
24     player_t *player;
25     platforms_t *platforms;
26     goals_t *goals;
27     lava_t *lava;
28     color_t background_color;
29     platforms_t *back_platforms;
30     background_t *background;
31     boxes_t *boxes;
32     labels_t *labels;
33 };
34
35 level_t *create_level_from_file(const char *file_name)
36 {
37     assert(file_name);
38
39     lt_t *const lt = create_lt();
40     if (lt == NULL) {
41         return NULL;
42     }
43
44     level_t *const level = PUSH_LT(lt, malloc(sizeof(level_t)), 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_t *level)
122 {
123     assert(level);
124     RETURN_LT0(level->lt);
125 }
126
127 int level_render(const level_t *level, camera_t *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_t *level, float delta_time)
173 {
174     assert(level);
175     assert(delta_time > 0);
176
177     physical_world_apply_gravity(level->physical_world);
178
179     boxes_update(level->boxes, delta_time);
180     player_update(level->player, delta_time);
181
182     physical_world_collide_solids(level->physical_world, level->platforms);
183
184     player_hide_goals(level->player, level->goals);
185     player_die_from_lava(level->player, level->lava);
186
187     goals_update(level->goals, delta_time);
188     lava_update(level->lava, delta_time);
189     labels_update(level->labels, delta_time);
190
191     return 0;
192 }
193
194 int level_event(level_t *level, const SDL_Event *event)
195 {
196     assert(level);
197     assert(event);
198
199     switch (event->type) {
200     case SDL_KEYDOWN:
201         switch (event->key.keysym.sym) {
202         case SDLK_SPACE:
203             player_jump(level->player);
204             break;
205         }
206         break;
207
208     case SDL_JOYBUTTONDOWN:
209         if (event->jbutton.button == 1) {
210             player_jump(level->player);
211         }
212         break;
213     }
214
215     return 0;
216 }
217
218 int level_input(level_t *level,
219                 const Uint8 *const keyboard_state,
220                 SDL_Joystick *the_stick_of_joy)
221 {
222     assert(level);
223     assert(keyboard_state);
224     (void) the_stick_of_joy;
225
226     if (keyboard_state[SDL_SCANCODE_A]) {
227         player_move_left(level->player);
228     } else if (keyboard_state[SDL_SCANCODE_D]) {
229         player_move_right(level->player);
230     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
231         player_move_left(level->player);
232     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
233         player_move_right(level->player);
234     } else {
235         player_stop(level->player);
236     }
237
238     return 0;
239 }
240
241 int level_reload_preserve_player(level_t *level, const char *file_name)
242 {
243     lt_t * const lt = create_lt();
244     if (lt == NULL) {
245         return -1;
246     }
247
248     /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
249
250
251
252     FILE * const level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
253     if (level_file == NULL) {
254         throw_error(ERROR_TYPE_LIBC);
255         RETURN_LT(lt, -1);
256     }
257
258     char color[7];
259     if (fscanf(level_file, "%6s", color) == EOF) {
260         throw_error(ERROR_TYPE_LIBC);
261         RETURN_LT(lt, -1);
262     }
263     level->background_color = color_from_hexstr(color);
264
265     player_t * const skipped_player = create_player_from_stream(level_file);
266     if (skipped_player == NULL) {
267         RETURN_LT(lt, -1);
268     }
269     destroy_player(skipped_player);
270
271     platforms_t * const platforms = create_platforms_from_stream(level_file);
272     if (platforms == NULL) {
273         RETURN_LT(lt, -1);
274     }
275     level->platforms = RESET_LT(level->lt, level->platforms, platforms);
276
277     goals_t * const goals = create_goals_from_stream(level_file);
278     if (goals == NULL) {
279         RETURN_LT(lt, -1);
280     }
281     level->goals = RESET_LT(level->lt, level->goals, goals);
282
283     lava_t * const lava = create_lava_from_stream(level_file);
284     if (lava == NULL) {
285         RETURN_LT(lt, -1);
286     }
287     level->lava = RESET_LT(level->lt, level->lava, lava);
288
289     platforms_t * const back_platforms = create_platforms_from_stream(level_file);
290     if (back_platforms == NULL) {
291         RETURN_LT(lt, -1);
292     }
293     level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
294
295     boxes_t * const boxes = create_boxes_from_stream(level_file);
296     if (boxes == NULL) {
297         RETURN_LT(lt, -1);
298     }
299     level->boxes = RESET_LT(level->lt, level->boxes, boxes);
300
301     labels_t * const labels = create_labels_from_stream(level_file);
302     if (labels == NULL) {
303         RETURN_LT(lt, -1);
304     }
305     level->labels = RESET_LT(level->lt, level->labels, labels);
306
307     physical_world_clean(level->physical_world);
308     if (physical_world_add_solid(
309             level->physical_world,
310             player_as_solid(level->player)) < 0) { RETURN_LT(lt, -1); }
311     if (boxes_add_to_physical_world(
312             level->boxes,
313             level->physical_world) < 0) { RETURN_LT(lt, -1); }
314
315     RETURN_LT(lt, 0);
316 }
317
318 int level_sound(level_t *level, sound_samples_t *sound_samples)
319 {
320     if (goals_sound(level->goals, sound_samples) < 0) {
321         return -1;
322     }
323
324     if (player_sound(level->player, sound_samples) < 0) {
325         return -1;
326     }
327
328     return 0;
329 }
330
331 void level_toggle_debug_mode(level_t *level)
332 {
333     background_toggle_debug_mode(level->background);
334 }
335
336 int level_enter_camera_event(level_t *level,
337                              const camera_t *camera)
338 {
339     goals_cue(level->goals, camera);
340     goals_checkpoint(level->goals, level->player);
341     labels_enter_camera_event(level->labels, camera);
342     return 0;
343 }