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),
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 player_focus_camera(level->player, camera);
165 if (background_render(level->background, camera) < 0) {
169 if (platforms_render(level->back_platforms, camera) < 0) {
173 if (player_render(level->player, camera) < 0) {
177 if (boxes_render(level->boxes, camera) < 0) {
181 if (lava_render(level->lava, camera) < 0) {
185 if (platforms_render(level->platforms, camera) < 0) {
189 if (goals_render(level->goals, camera) < 0) {
193 if (labels_render(level->labels, camera) < 0) {
197 if (regions_render(level->regions, camera) < 0) {
204 int level_update(Level *level, float delta_time)
207 assert(delta_time > 0);
209 physical_world_apply_gravity(level->physical_world);
210 boxes_float_in_lava(level->boxes, level->lava);
212 boxes_update(level->boxes, delta_time);
213 player_update(level->player, delta_time);
215 physical_world_collide_solids(level->physical_world, level->platforms);
217 player_hide_goals(level->player, level->goals);
218 player_die_from_lava(level->player, level->lava);
219 regions_player_enter(level->regions, level->player);
220 regions_player_leave(level->regions, level->player);
222 goals_update(level->goals, delta_time);
223 lava_update(level->lava, delta_time);
224 labels_update(level->labels, delta_time);
229 int level_event(Level *level, const SDL_Event *event)
234 switch (event->type) {
236 switch (event->key.keysym.sym) {
238 player_jump(level->player);
243 case SDL_JOYBUTTONDOWN:
244 if (event->jbutton.button == 1) {
245 player_jump(level->player);
253 int level_input(Level *level,
254 const Uint8 *const keyboard_state,
255 SDL_Joystick *the_stick_of_joy)
258 assert(keyboard_state);
259 (void) the_stick_of_joy;
261 if (keyboard_state[SDL_SCANCODE_A]) {
262 player_move_left(level->player);
263 } else if (keyboard_state[SDL_SCANCODE_D]) {
264 player_move_right(level->player);
265 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
266 player_move_left(level->player);
267 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
268 player_move_right(level->player);
270 player_stop(level->player);
276 int level_reload_preserve_player(Level *level, const char *file_name)
278 Lt * const lt = create_lt();
283 /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
285 LineStream * const level_stream = PUSH_LT(
290 LEVEL_LINE_MAX_LENGTH),
291 destroy_line_stream);
292 if (level_stream == NULL) {
296 Background * const background = create_background_from_line_stream(level_stream);
297 if (background == NULL) {
300 level->background = RESET_LT(level->lt, level->background, background);
302 Player * const skipped_player = create_player_from_line_stream(level_stream);
303 if (skipped_player == NULL) {
306 destroy_player(skipped_player);
308 Platforms * const platforms = create_platforms_from_line_stream(level_stream);
309 if (platforms == NULL) {
312 level->platforms = RESET_LT(level->lt, level->platforms, platforms);
314 Goals * const goals = create_goals_from_line_stream(level_stream);
318 level->goals = RESET_LT(level->lt, level->goals, goals);
320 Lava * const lava = create_lava_from_line_stream(level_stream);
324 level->lava = RESET_LT(level->lt, level->lava, lava);
326 Platforms * const back_platforms = create_platforms_from_line_stream(level_stream);
327 if (back_platforms == NULL) {
330 level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
332 Boxes * const boxes = create_boxes_from_line_stream(level_stream);
336 level->boxes = RESET_LT(level->lt, level->boxes, boxes);
338 Labels * const labels = create_labels_from_line_stream(level_stream);
339 if (labels == NULL) {
342 level->labels = RESET_LT(level->lt, level->labels, labels);
344 Regions * const regions = create_regions_from_line_stream(level_stream, level);
345 if (regions == NULL) {
348 level->regions = RESET_LT(level->lt, level->regions, regions);
350 physical_world_clean(level->physical_world);
351 if (physical_world_add_solid(
352 level->physical_world,
353 player_as_solid(level->player)) < 0) { RETURN_LT(lt, -1); }
354 if (boxes_add_to_physical_world(
356 level->physical_world) < 0) { RETURN_LT(lt, -1); }
361 int level_sound(Level *level, Sound_samples *sound_samples)
363 if (goals_sound(level->goals, sound_samples) < 0) {
367 if (player_sound(level->player, sound_samples) < 0) {
374 void level_toggle_debug_mode(Level *level)
376 background_toggle_debug_mode(level->background);
379 int level_enter_camera_event(Level *level,
380 const Camera *camera)
382 goals_cue(level->goals, camera);
383 goals_checkpoint(level->goals, level->player);
384 labels_enter_camera_event(level->labels, camera);
388 Rigid_rect *level_rigid_rect(Level *level,
389 const char *rigid_rect_id)
392 assert(rigid_rect_id);
394 Rigid_rect *rigid_rect = player_rigid_rect(level->player,
396 if (rigid_rect != NULL) {
400 rigid_rect = boxes_rigid_rect(level->boxes, rigid_rect_id);
401 if (rigid_rect != NULL) {
408 void level_hide_goal(Level *level, const char *goal_id)
410 goals_hide(level->goals, goal_id);
413 void level_show_goal(Level *level, const char *goal_id)
415 goals_show(level->goals, goal_id);
418 long int level_player_jump_count(Level *level)
421 return player_jump_count(level->player);
424 void level_hide_label(Level *level, const char *label_id)
429 /* TODO: level_hide_label is not implemented */