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 "game/level/regions.h"
16 #include "system/line_stream.h"
17 #include "system/lt.h"
18 #include "system/lt/lt_adapters.h"
19 #include "system/nth_alloc.h"
21 #define LEVEL_LINE_MAX_LENGTH 512
27 Physical_world *physical_world;
32 Platforms *back_platforms;
33 Background *background;
39 Level *create_level_from_file(const char *file_name)
43 Lt *const lt = create_lt();
48 Level *const level = PUSH_LT(lt, nth_alloc(sizeof(Level)), free);
53 LineStream *level_stream = PUSH_LT(
58 LEVEL_LINE_MAX_LENGTH),
60 if (level_stream == NULL) {
63 level->background = PUSH_LT(
65 create_background_from_line_stream(level_stream),
67 if (level->background == NULL) {
71 level->player = PUSH_LT(
73 create_player_from_line_stream(level_stream, level),
75 if (level->player == NULL) {
79 level->platforms = PUSH_LT(
81 create_platforms_from_line_stream(level_stream),
83 if (level->platforms == NULL) {
87 level->goals = PUSH_LT(
89 create_goals_from_line_stream(level_stream),
91 if (level->goals == NULL) {
95 level->lava = PUSH_LT(
97 create_lava_from_line_stream(level_stream),
99 if (level->lava == NULL) {
103 level->back_platforms = PUSH_LT(
105 create_platforms_from_line_stream(level_stream),
107 if (level->back_platforms == NULL) {
111 level->boxes = PUSH_LT(
113 create_boxes_from_line_stream(level_stream),
115 if (level->boxes == NULL) {
119 level->labels = PUSH_LT(
121 create_labels_from_line_stream(level_stream),
123 if (level->labels == NULL) {
127 level->regions = PUSH_LT(
129 create_regions_from_line_stream(level_stream, level),
131 if (level->regions == NULL) {
135 level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
136 if (level->physical_world == NULL) {
139 if (physical_world_add_solid(
140 level->physical_world,
141 player_as_solid(level->player)) < 0) { RETURN_LT(lt, NULL); }
142 if (boxes_add_to_physical_world(
144 level->physical_world) < 0) { RETURN_LT(lt, NULL); }
148 destroy_line_stream(RELEASE_LT(lt, level_stream));
153 void destroy_level(Level *level)
156 RETURN_LT0(level->lt);
159 int level_render(const Level *level, Camera *camera)
163 if (background_render(level->background, camera) < 0) {
167 if (platforms_render(level->back_platforms, camera) < 0) {
171 if (player_render(level->player, camera) < 0) {
175 if (boxes_render(level->boxes, camera) < 0) {
179 if (lava_render(level->lava, camera) < 0) {
183 if (platforms_render(level->platforms, camera) < 0) {
187 if (goals_render(level->goals, camera) < 0) {
191 if (labels_render(level->labels, camera) < 0) {
195 if (regions_render(level->regions, camera) < 0) {
202 int level_update(Level *level, float delta_time)
205 assert(delta_time > 0);
207 physical_world_apply_gravity(level->physical_world);
208 boxes_float_in_lava(level->boxes, level->lava);
210 boxes_update(level->boxes, delta_time);
211 player_update(level->player, delta_time);
213 physical_world_collide_solids(level->physical_world, level->platforms);
215 player_hide_goals(level->player, level->goals);
216 player_die_from_lava(level->player, level->lava);
217 regions_player_enter(level->regions, level->player);
218 regions_player_leave(level->regions, level->player);
220 goals_update(level->goals, delta_time);
221 lava_update(level->lava, delta_time);
222 labels_update(level->labels, delta_time);
227 int level_event(Level *level, const SDL_Event *event)
232 switch (event->type) {
234 switch (event->key.keysym.sym) {
236 player_jump(level->player);
241 case SDL_JOYBUTTONDOWN:
242 if (event->jbutton.button == 1) {
243 player_jump(level->player);
251 int level_input(Level *level,
252 const Uint8 *const keyboard_state,
253 SDL_Joystick *the_stick_of_joy)
256 assert(keyboard_state);
257 (void) the_stick_of_joy;
259 if (keyboard_state[SDL_SCANCODE_A]) {
260 player_move_left(level->player);
261 } else if (keyboard_state[SDL_SCANCODE_D]) {
262 player_move_right(level->player);
263 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
264 player_move_left(level->player);
265 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
266 player_move_right(level->player);
268 player_stop(level->player);
274 int level_reload_preserve_player(Level *level, const char *file_name)
276 Lt * const lt = create_lt();
281 /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
283 LineStream * const level_stream = PUSH_LT(
288 LEVEL_LINE_MAX_LENGTH),
289 destroy_line_stream);
290 if (level_stream == NULL) {
294 Background * const background = create_background_from_line_stream(level_stream);
295 if (background == NULL) {
298 level->background = RESET_LT(level->lt, level->background, background);
300 Player * const skipped_player = create_player_from_line_stream(level_stream, level);
301 if (skipped_player == NULL) {
304 destroy_player(skipped_player);
306 Platforms * const platforms = create_platforms_from_line_stream(level_stream);
307 if (platforms == NULL) {
310 level->platforms = RESET_LT(level->lt, level->platforms, platforms);
312 Goals * const goals = create_goals_from_line_stream(level_stream);
316 level->goals = RESET_LT(level->lt, level->goals, goals);
318 Lava * const lava = create_lava_from_line_stream(level_stream);
322 level->lava = RESET_LT(level->lt, level->lava, lava);
324 Platforms * const back_platforms = create_platforms_from_line_stream(level_stream);
325 if (back_platforms == NULL) {
328 level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
330 Boxes * const boxes = create_boxes_from_line_stream(level_stream);
334 level->boxes = RESET_LT(level->lt, level->boxes, boxes);
336 Labels * const labels = create_labels_from_line_stream(level_stream);
337 if (labels == NULL) {
340 level->labels = RESET_LT(level->lt, level->labels, labels);
342 Regions * const regions = create_regions_from_line_stream(level_stream, level);
343 if (regions == NULL) {
346 level->regions = RESET_LT(level->lt, level->regions, regions);
348 physical_world_clean(level->physical_world);
349 if (physical_world_add_solid(
350 level->physical_world,
351 player_as_solid(level->player)) < 0) { RETURN_LT(lt, -1); }
352 if (boxes_add_to_physical_world(
354 level->physical_world) < 0) { RETURN_LT(lt, -1); }
359 int level_sound(Level *level, Sound_samples *sound_samples)
361 if (goals_sound(level->goals, sound_samples) < 0) {
365 if (player_sound(level->player, sound_samples) < 0) {
372 void level_toggle_debug_mode(Level *level)
374 background_toggle_debug_mode(level->background);
377 int level_enter_camera_event(Level *level, Camera *camera)
379 player_focus_camera(level->player, camera);
380 goals_cue(level->goals, camera);
381 goals_checkpoint(level->goals, level->player);
382 labels_enter_camera_event(level->labels, camera);
386 Rigid_rect *level_rigid_rect(Level *level,
387 const char *rigid_rect_id)
390 assert(rigid_rect_id);
392 Rigid_rect *rigid_rect = player_rigid_rect(level->player,
394 if (rigid_rect != NULL) {
398 rigid_rect = boxes_rigid_rect(level->boxes, rigid_rect_id);
399 if (rigid_rect != NULL) {
406 void level_hide_goal(Level *level, const char *goal_id)
408 goals_hide(level->goals, goal_id);
411 void level_show_goal(Level *level, const char *goal_id)
413 goals_show(level->goals, goal_id);
416 void level_hide_label(Level *level, const char *label_id)
421 labels_hide(level->labels, label_id);