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