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