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"
23 physical_world_t *physical_world;
25 platforms_t *platforms;
28 color_t background_color;
29 platforms_t *back_platforms;
30 background_t *background;
35 level_t *create_level_from_file(const char *file_name)
39 lt_t *const lt = create_lt();
44 level_t *const level = PUSH_LT(lt, malloc(sizeof(level_t)), free);
46 throw_error(ERROR_TYPE_LIBC);
50 FILE *level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
51 if (level_file == NULL) {
52 throw_error(ERROR_TYPE_LIBC);
57 if (fscanf(level_file, "%6s", color) == EOF) {
58 throw_error(ERROR_TYPE_LIBC);
61 level->background_color = color_from_hexstr(color);
63 level->player = PUSH_LT(lt, create_player_from_stream(level_file), destroy_player);
64 if (level->player == NULL) {
68 level->platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
69 if (level->platforms == NULL) {
73 level->goals = PUSH_LT(lt, create_goals_from_stream(level_file), destroy_goals);
74 if (level->goals == NULL) {
78 level->lava = PUSH_LT(lt, create_lava_from_stream(level_file), destroy_lava);
79 if (level->lava == NULL) {
83 level->back_platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
84 if (level->back_platforms == NULL) {
88 level->boxes = PUSH_LT(lt, create_boxes_from_stream(level_file), destroy_boxes);
89 if (level->boxes == NULL) {
93 level->labels = PUSH_LT(lt, create_labels_from_stream(level_file), destroy_labels);
94 if (level->labels == NULL) {
98 level->background = PUSH_LT(lt, create_background(level->background_color), destroy_background);
99 if (level->background == NULL) {
103 level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
104 if (level->physical_world == NULL) {
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(
112 level->physical_world) < 0) { RETURN_LT(lt, NULL); }
116 fclose(RELEASE_LT(lt, level_file));
121 void destroy_level(level_t *level)
124 RETURN_LT0(level->lt);
127 int level_render(const level_t *level, camera_t *camera)
131 player_focus_camera(level->player, camera);
133 if (camera_clear_background(camera, level->background_color) < 0) {
137 if (background_render(level->background, camera) < 0) {
141 if (platforms_render(level->back_platforms, camera) < 0) {
145 if (player_render(level->player, camera) < 0) {
149 if (boxes_render(level->boxes, camera) < 0) {
153 if (lava_render(level->lava, camera) < 0) {
157 if (platforms_render(level->platforms, camera) < 0) {
161 if (goals_render(level->goals, camera) < 0) {
165 if (labels_render(level->labels, camera) < 0) {
172 int level_update(level_t *level, float delta_time)
175 assert(delta_time > 0);
177 physical_world_apply_gravity(level->physical_world);
179 boxes_update(level->boxes, delta_time);
180 player_update(level->player, delta_time);
182 physical_world_collide_solids(level->physical_world, level->platforms);
184 player_hide_goals(level->player, level->goals);
185 player_die_from_lava(level->player, level->lava);
187 goals_update(level->goals, delta_time);
188 lava_update(level->lava, delta_time);
189 labels_update(level->labels, delta_time);
194 int level_event(level_t *level, const SDL_Event *event)
199 switch (event->type) {
201 switch (event->key.keysym.sym) {
203 player_jump(level->player);
208 case SDL_JOYBUTTONDOWN:
209 if (event->jbutton.button == 1) {
210 player_jump(level->player);
218 int level_input(level_t *level,
219 const Uint8 *const keyboard_state,
220 SDL_Joystick *the_stick_of_joy)
223 assert(keyboard_state);
224 (void) the_stick_of_joy;
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);
235 player_stop(level->player);
241 int level_reload_preserve_player(level_t *level, const char *file_name)
243 lt_t * const lt = create_lt();
248 /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
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);
259 if (fscanf(level_file, "%6s", color) == EOF) {
260 throw_error(ERROR_TYPE_LIBC);
263 level->background_color = color_from_hexstr(color);
265 player_t * const skipped_player = create_player_from_stream(level_file);
266 if (skipped_player == NULL) {
269 destroy_player(skipped_player);
271 platforms_t * const platforms = create_platforms_from_stream(level_file);
272 if (platforms == NULL) {
275 level->platforms = RESET_LT(level->lt, level->platforms, platforms);
277 goals_t * const goals = create_goals_from_stream(level_file);
281 level->goals = RESET_LT(level->lt, level->goals, goals);
283 lava_t * const lava = create_lava_from_stream(level_file);
287 level->lava = RESET_LT(level->lt, level->lava, lava);
289 platforms_t * const back_platforms = create_platforms_from_stream(level_file);
290 if (back_platforms == NULL) {
293 level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
295 boxes_t * const boxes = create_boxes_from_stream(level_file);
299 level->boxes = RESET_LT(level->lt, level->boxes, boxes);
301 labels_t * const labels = create_labels_from_stream(level_file);
302 if (labels == NULL) {
305 level->labels = RESET_LT(level->lt, level->labels, labels);
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(
313 level->physical_world) < 0) { RETURN_LT(lt, -1); }
318 int level_sound(level_t *level, sound_samples_t *sound_samples)
320 if (goals_sound(level->goals, sound_samples) < 0) {
324 if (player_sound(level->player, sound_samples) < 0) {
331 void level_toggle_debug_mode(level_t *level)
333 background_toggle_debug_mode(level->background);
336 int level_enter_camera_event(level_t *level,
337 const camera_t *camera)
339 goals_cue(level->goals, camera);
340 goals_checkpoint(level->goals, level->player);
341 labels_enter_camera_event(level->labels, camera);