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