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 *physical_world;
28 Color background_color;
29 Platforms *back_platforms;
30 Background *background;
35 Level *create_level_from_file(const char *file_name)
39 Lt *const lt = create_lt();
44 Level *const level = PUSH_LT(lt, malloc(sizeof(Level)), 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 *level)
124 RETURN_LT0(level->lt);
127 int level_render(const Level *level, Camera *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 *level, float delta_time)
175 assert(delta_time > 0);
177 physical_world_apply_gravity(level->physical_world);
178 boxes_float_in_lava(level->boxes, level->lava);
180 boxes_update(level->boxes, delta_time);
181 player_update(level->player, delta_time);
183 physical_world_collide_solids(level->physical_world, level->platforms);
185 player_hide_goals(level->player, level->goals);
186 player_die_from_lava(level->player, level->lava);
188 goals_update(level->goals, delta_time);
189 lava_update(level->lava, delta_time);
190 labels_update(level->labels, delta_time);
195 int level_event(Level *level, const SDL_Event *event)
200 switch (event->type) {
202 switch (event->key.keysym.sym) {
204 player_jump(level->player);
209 case SDL_JOYBUTTONDOWN:
210 if (event->jbutton.button == 1) {
211 player_jump(level->player);
219 int level_input(Level *level,
220 const Uint8 *const keyboard_state,
221 SDL_Joystick *the_stick_of_joy)
224 assert(keyboard_state);
225 (void) the_stick_of_joy;
227 if (keyboard_state[SDL_SCANCODE_A]) {
228 player_move_left(level->player);
229 } else if (keyboard_state[SDL_SCANCODE_D]) {
230 player_move_right(level->player);
231 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
232 player_move_left(level->player);
233 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
234 player_move_right(level->player);
236 player_stop(level->player);
242 int level_reload_preserve_player(Level *level, const char *file_name)
244 Lt * const lt = create_lt();
249 /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
253 FILE * const level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
254 if (level_file == NULL) {
255 throw_error(ERROR_TYPE_LIBC);
260 if (fscanf(level_file, "%6s", color) == EOF) {
261 throw_error(ERROR_TYPE_LIBC);
264 level->background_color = color_from_hexstr(color);
266 Player * const skipped_player = create_player_from_stream(level_file);
267 if (skipped_player == NULL) {
270 destroy_player(skipped_player);
272 Platforms * const platforms = create_platforms_from_stream(level_file);
273 if (platforms == NULL) {
276 level->platforms = RESET_LT(level->lt, level->platforms, platforms);
278 Goals * const goals = create_goals_from_stream(level_file);
282 level->goals = RESET_LT(level->lt, level->goals, goals);
284 Lava * const lava = create_lava_from_stream(level_file);
288 level->lava = RESET_LT(level->lt, level->lava, lava);
290 Platforms * const back_platforms = create_platforms_from_stream(level_file);
291 if (back_platforms == NULL) {
294 level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
296 Boxes * const boxes = create_boxes_from_stream(level_file);
300 level->boxes = RESET_LT(level->lt, level->boxes, boxes);
302 Labels * const labels = create_labels_from_stream(level_file);
303 if (labels == NULL) {
306 level->labels = RESET_LT(level->lt, level->labels, labels);
308 physical_world_clean(level->physical_world);
309 if (physical_world_add_solid(
310 level->physical_world,
311 player_as_solid(level->player)) < 0) { RETURN_LT(lt, -1); }
312 if (boxes_add_to_physical_world(
314 level->physical_world) < 0) { RETURN_LT(lt, -1); }
319 int level_sound(Level *level, Sound_samples *sound_samples)
321 if (goals_sound(level->goals, sound_samples) < 0) {
325 if (player_sound(level->player, sound_samples) < 0) {
332 void level_toggle_debug_mode(Level *level)
334 background_toggle_debug_mode(level->background);
337 int level_enter_camera_event(Level *level,
338 const Camera *camera)
340 goals_cue(level->goals, camera);
341 goals_checkpoint(level->goals, level->player);
342 labels_enter_camera_event(level->labels, camera);
346 Rigid_rect *level_rigid_rect(Level *level,
347 const char *rigid_rect_id)
350 assert(rigid_rect_id);
352 Rigid_rect *rigid_rect = player_rigid_rect(level->player,
354 if (rigid_rect != NULL) {
358 rigid_rect = boxes_rigid_rect(level->boxes, rigid_rect_id);
359 if (rigid_rect != NULL) {