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