2 #include "system/stacktrace.h"
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"
20 #include "ebisp/interpreter.h"
21 #include "ebisp/builtins.h"
23 #define LEVEL_LINE_MAX_LENGTH 512
29 Physical_world *physical_world;
34 Platforms *back_platforms;
35 Background *background;
41 Level *create_level_from_file(const char *file_name, Game *game)
43 trace_assert(file_name);
45 Lt *const lt = create_lt();
50 Level *const level = PUSH_LT(lt, nth_alloc(sizeof(Level)), free);
55 LineStream *level_stream = PUSH_LT(
60 LEVEL_LINE_MAX_LENGTH),
62 if (level_stream == NULL) {
65 level->background = PUSH_LT(
67 create_background_from_line_stream(level_stream),
69 if (level->background == NULL) {
73 level->player = PUSH_LT(
75 create_player_from_line_stream(level_stream, game),
77 if (level->player == NULL) {
81 level->platforms = PUSH_LT(
83 create_platforms_from_line_stream(level_stream),
85 if (level->platforms == NULL) {
89 level->goals = PUSH_LT(
91 create_goals_from_line_stream(level_stream),
93 if (level->goals == NULL) {
97 level->lava = PUSH_LT(
99 create_lava_from_line_stream(level_stream),
101 if (level->lava == NULL) {
105 level->back_platforms = PUSH_LT(
107 create_platforms_from_line_stream(level_stream),
109 if (level->back_platforms == NULL) {
113 level->boxes = PUSH_LT(
115 create_boxes_from_line_stream(level_stream),
117 if (level->boxes == NULL) {
121 level->labels = PUSH_LT(
123 create_labels_from_line_stream(level_stream),
125 if (level->labels == NULL) {
129 level->regions = PUSH_LT(
131 create_regions_from_line_stream(level_stream, game),
133 if (level->regions == NULL) {
137 level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
138 if (level->physical_world == NULL) {
141 if (physical_world_add_solid(
142 level->physical_world,
143 player_as_solid(level->player)) < 0) { RETURN_LT(lt, NULL); }
144 if (boxes_add_to_physical_world(
146 level->physical_world) < 0) { RETURN_LT(lt, NULL); }
150 destroy_line_stream(RELEASE_LT(lt, level_stream));
155 void destroy_level(Level *level)
158 RETURN_LT0(level->lt);
161 int level_render(const Level *level, Camera *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 trace_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 trace_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, Game *game)
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, game);
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, game);
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, Camera *camera)
381 player_focus_camera(level->player, 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 trace_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 void level_hide_label(Level *level, const char *label_id)
421 trace_assert(label_id);
423 labels_hide(level->labels, label_id);
426 static struct EvalResult
427 unknown_object(Gc *gc, const char *source, const char *target)
430 list(gc, "qqq", "unknown-object", source, target));
433 struct EvalResult level_send(Level *level, Gc *gc, struct Scope *scope, struct Expr path)
439 const char *target = NULL;
440 struct EvalResult res = match_list(gc, "q*", path, &target, NULL);
445 return unknown_object(gc, "level", target);