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"
22 #include "broadcast.h"
24 #define LEVEL_LINE_MAX_LENGTH 512
30 Physical_world *physical_world;
35 Platforms *back_platforms;
36 Background *background;
42 Level *create_level_from_file(const char *file_name, Broadcast *broadcast)
44 trace_assert(file_name);
46 Lt *const lt = create_lt();
51 Level *const level = PUSH_LT(lt, nth_alloc(sizeof(Level)), free);
56 LineStream *level_stream = PUSH_LT(
61 LEVEL_LINE_MAX_LENGTH),
63 if (level_stream == NULL) {
66 level->background = PUSH_LT(
68 create_background_from_line_stream(level_stream),
70 if (level->background == NULL) {
74 level->player = PUSH_LT(
76 create_player_from_line_stream(level_stream, broadcast),
78 if (level->player == NULL) {
82 level->platforms = PUSH_LT(
84 create_platforms_from_line_stream(level_stream),
86 if (level->platforms == NULL) {
90 level->goals = PUSH_LT(
92 create_goals_from_line_stream(level_stream),
94 if (level->goals == NULL) {
98 level->lava = PUSH_LT(
100 create_lava_from_line_stream(level_stream),
102 if (level->lava == NULL) {
106 level->back_platforms = PUSH_LT(
108 create_platforms_from_line_stream(level_stream),
110 if (level->back_platforms == NULL) {
114 level->boxes = PUSH_LT(
116 create_boxes_from_line_stream(level_stream),
118 if (level->boxes == NULL) {
122 level->labels = PUSH_LT(
124 create_labels_from_line_stream(level_stream),
126 if (level->labels == NULL) {
130 level->regions = PUSH_LT(
132 create_regions_from_line_stream(level_stream, broadcast),
134 if (level->regions == NULL) {
138 level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
139 if (level->physical_world == NULL) {
142 if (physical_world_add_solid(
143 level->physical_world,
144 player_as_solid(level->player)) < 0) { RETURN_LT(lt, NULL); }
145 if (boxes_add_to_physical_world(
147 level->physical_world) < 0) { RETURN_LT(lt, NULL); }
151 destroy_line_stream(RELEASE_LT(lt, level_stream));
156 void destroy_level(Level *level)
159 RETURN_LT0(level->lt);
162 int level_render(const Level *level, Camera *camera)
166 if (background_render(level->background, camera) < 0) {
170 if (platforms_render(level->back_platforms, camera) < 0) {
174 if (player_render(level->player, camera) < 0) {
178 if (boxes_render(level->boxes, camera) < 0) {
182 if (lava_render(level->lava, camera) < 0) {
186 if (platforms_render(level->platforms, camera) < 0) {
190 if (goals_render(level->goals, camera) < 0) {
194 if (labels_render(level->labels, camera) < 0) {
198 if (regions_render(level->regions, camera) < 0) {
205 int level_update(Level *level, float delta_time)
208 trace_assert(delta_time > 0);
210 physical_world_apply_gravity(level->physical_world);
211 boxes_float_in_lava(level->boxes, level->lava);
213 boxes_update(level->boxes, delta_time);
214 player_update(level->player, delta_time);
216 physical_world_collide_solids(level->physical_world, level->platforms);
218 player_hide_goals(level->player, level->goals);
219 player_die_from_lava(level->player, level->lava);
220 regions_player_enter(level->regions, level->player);
221 regions_player_leave(level->regions, level->player);
223 goals_update(level->goals, delta_time);
224 lava_update(level->lava, delta_time);
225 labels_update(level->labels, delta_time);
230 int level_event(Level *level, const SDL_Event *event)
235 switch (event->type) {
237 switch (event->key.keysym.sym) {
239 player_jump(level->player);
244 case SDL_JOYBUTTONDOWN:
245 if (event->jbutton.button == 1) {
246 player_jump(level->player);
254 int level_input(Level *level,
255 const Uint8 *const keyboard_state,
256 SDL_Joystick *the_stick_of_joy)
259 trace_assert(keyboard_state);
260 (void) the_stick_of_joy;
262 if (keyboard_state[SDL_SCANCODE_A]) {
263 player_move_left(level->player);
264 } else if (keyboard_state[SDL_SCANCODE_D]) {
265 player_move_right(level->player);
266 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
267 player_move_left(level->player);
268 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
269 player_move_right(level->player);
271 player_stop(level->player);
277 int level_reload_preserve_player(Level *level, const char *file_name, Broadcast *broadcast)
279 Lt * const lt = create_lt();
284 /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
286 LineStream * const level_stream = PUSH_LT(
291 LEVEL_LINE_MAX_LENGTH),
292 destroy_line_stream);
293 if (level_stream == NULL) {
297 Background * const background = create_background_from_line_stream(level_stream);
298 if (background == NULL) {
301 level->background = RESET_LT(level->lt, level->background, background);
303 Player * const skipped_player = create_player_from_line_stream(level_stream, broadcast);
304 if (skipped_player == NULL) {
307 destroy_player(skipped_player);
309 Platforms * const platforms = create_platforms_from_line_stream(level_stream);
310 if (platforms == NULL) {
313 level->platforms = RESET_LT(level->lt, level->platforms, platforms);
315 Goals * const goals = create_goals_from_line_stream(level_stream);
319 level->goals = RESET_LT(level->lt, level->goals, goals);
321 Lava * const lava = create_lava_from_line_stream(level_stream);
325 level->lava = RESET_LT(level->lt, level->lava, lava);
327 Platforms * const back_platforms = create_platforms_from_line_stream(level_stream);
328 if (back_platforms == NULL) {
331 level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
333 Boxes * const boxes = create_boxes_from_line_stream(level_stream);
337 level->boxes = RESET_LT(level->lt, level->boxes, boxes);
339 Labels * const labels = create_labels_from_line_stream(level_stream);
340 if (labels == NULL) {
343 level->labels = RESET_LT(level->lt, level->labels, labels);
345 Regions * const regions = create_regions_from_line_stream(level_stream, broadcast);
346 if (regions == NULL) {
349 level->regions = RESET_LT(level->lt, level->regions, regions);
351 physical_world_clean(level->physical_world);
352 if (physical_world_add_solid(
353 level->physical_world,
354 player_as_solid(level->player)) < 0) { RETURN_LT(lt, -1); }
355 if (boxes_add_to_physical_world(
357 level->physical_world) < 0) { RETURN_LT(lt, -1); }
362 int level_sound(Level *level, Sound_samples *sound_samples)
364 if (goals_sound(level->goals, sound_samples) < 0) {
368 if (player_sound(level->player, sound_samples) < 0) {
375 void level_toggle_debug_mode(Level *level)
377 background_toggle_debug_mode(level->background);
380 int level_enter_camera_event(Level *level, Camera *camera)
382 player_focus_camera(level->player, camera);
383 goals_cue(level->goals, camera);
384 goals_checkpoint(level->goals, level->player);
385 labels_enter_camera_event(level->labels, camera);
389 Rigid_rect *level_rigid_rect(Level *level,
390 const char *rigid_rect_id)
393 trace_assert(rigid_rect_id);
395 Rigid_rect *rigid_rect = player_rigid_rect(level->player,
397 if (rigid_rect != NULL) {
401 rigid_rect = boxes_rigid_rect(level->boxes, rigid_rect_id);
402 if (rigid_rect != NULL) {
409 struct EvalResult level_send(Level *level, Gc *gc, struct Scope *scope, struct Expr path)
415 const char *target = NULL;
416 struct Expr rest = void_expr();
417 struct EvalResult res = match_list(gc, "q*", path, &target, &rest);
422 if (strcmp(target, "goal") == 0) {
423 return goals_send(level->goals, gc, scope, rest);
424 } else if (strcmp(target, "label") == 0) {
425 return labels_send(level->labels, gc, scope, rest);
426 } else if (strcmp(target, "box") == 0) {
427 return boxes_send(level->boxes, gc, scope, rest);
430 return unknown_target(gc, "level", target);