2 #include "system/stacktrace.h"
6 #include "ebisp/builtins.h"
7 #include "ebisp/interpreter.h"
8 #include "game/camera.h"
9 #include "game/level.h"
10 #include "game/level/background.h"
11 #include "game/level/boxes.h"
12 #include "game/level/goals.h"
13 #include "game/level/labels.h"
14 #include "game/level/lava.h"
15 #include "game/level/physical_world.h"
16 #include "game/level/platforms.h"
17 #include "game/level/player.h"
18 #include "game/level/regions.h"
19 #include "game/level/rigid_bodies.h"
20 #include "system/line_stream.h"
21 #include "system/lt.h"
22 #include "system/lt/lt_adapters.h"
23 #include "system/nth_alloc.h"
25 #define LEVEL_LINE_MAX_LENGTH 512
26 #define LEVEL_GRAVITY 1500.0f
32 Background *background;
37 Platforms *back_platforms;
41 Physical_world *physical_world;
42 RigidBodies *rigid_bodies;
45 Vec flying_camera_position;
48 Level *create_level_from_file(const char *file_name, Broadcast *broadcast)
50 trace_assert(file_name);
52 Lt *const lt = create_lt();
57 Level *const level = PUSH_LT(lt, nth_alloc(sizeof(Level)), free);
62 LineStream *level_stream = PUSH_LT(
67 LEVEL_LINE_MAX_LENGTH),
69 if (level_stream == NULL) {
72 level->background = PUSH_LT(
74 create_background_from_line_stream(level_stream),
76 if (level->background == NULL) {
80 level->player = PUSH_LT(
82 create_player_from_line_stream(level_stream, broadcast),
84 if (level->player == NULL) {
88 level->platforms = PUSH_LT(
90 create_platforms_from_line_stream(level_stream),
92 if (level->platforms == NULL) {
96 level->goals = PUSH_LT(
98 create_goals_from_line_stream(level_stream),
100 if (level->goals == NULL) {
104 level->lava = PUSH_LT(
106 create_lava_from_line_stream(level_stream),
108 if (level->lava == NULL) {
112 level->back_platforms = PUSH_LT(
114 create_platforms_from_line_stream(level_stream),
116 if (level->back_platforms == NULL) {
120 level->boxes = PUSH_LT(
122 create_boxes_from_line_stream(level_stream),
124 if (level->boxes == NULL) {
128 level->labels = PUSH_LT(
130 create_labels_from_line_stream(level_stream),
132 if (level->labels == NULL) {
136 level->regions = PUSH_LT(
138 create_regions_from_line_stream(level_stream, broadcast),
140 if (level->regions == NULL) {
144 level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
145 if (level->physical_world == NULL) {
148 if (physical_world_add_solid(
149 level->physical_world,
150 player_as_solid(level->player)) < 0) { RETURN_LT(lt, NULL); }
151 if (boxes_add_to_physical_world(
153 level->physical_world) < 0) { RETURN_LT(lt, NULL); }
155 level->rigid_bodies = PUSH_LT(lt, create_rigid_bodies(1024), destroy_rigid_bodies);
156 if (level->rigid_bodies == NULL) {
160 level->flying_mode = false;
161 level->flying_camera_position = vec(0.0f, 0.0f);
165 destroy_line_stream(RELEASE_LT(lt, level_stream));
170 void destroy_level(Level *level)
173 RETURN_LT0(level->lt);
176 int level_render(const Level *level, Camera *camera)
180 if (background_render(level->background, camera) < 0) {
184 if (platforms_render(level->back_platforms, camera) < 0) {
188 if (player_render(level->player, camera) < 0) {
192 if (boxes_render(level->boxes, camera) < 0) {
196 if (lava_render(level->lava, camera) < 0) {
200 if (platforms_render(level->platforms, camera) < 0) {
204 if (goals_render(level->goals, camera) < 0) {
208 if (labels_render(level->labels, camera) < 0) {
212 if (regions_render(level->regions, camera) < 0) {
216 if (rigid_bodies_render(level->rigid_bodies, camera) < 0) {
223 int level_update(Level *level, float delta_time)
226 trace_assert(delta_time > 0);
228 physical_world_apply_gravity(level->physical_world);
229 boxes_float_in_lava(level->boxes, level->lava);
230 rigid_bodies_apply_omniforce(level->rigid_bodies, vec(0.0f, LEVEL_GRAVITY));
232 rigid_bodies_update(level->rigid_bodies, delta_time);
233 boxes_update(level->boxes, delta_time);
234 player_update(level->player, delta_time);
236 rigid_bodies_collide(level->rigid_bodies, level->platforms);
237 physical_world_collide_solids(level->physical_world, level->platforms);
239 player_hide_goals(level->player, level->goals);
240 player_die_from_lava(level->player, level->lava);
241 regions_player_enter(level->regions, level->player);
242 regions_player_leave(level->regions, level->player);
244 goals_update(level->goals, delta_time);
245 lava_update(level->lava, delta_time);
246 labels_update(level->labels, delta_time);
251 int level_event(Level *level, const SDL_Event *event)
256 switch (event->type) {
258 switch (event->key.keysym.sym) {
260 player_jump(level->player);
265 case SDL_JOYBUTTONDOWN:
266 if (event->jbutton.button == 1) {
267 player_jump(level->player);
271 case SDL_MOUSEMOTION:
272 if (level->flying_mode) {
273 vec_add(&level->flying_camera_position,
274 vec((float) event->motion.xrel, (float) event->motion.yrel));
282 int level_input(Level *level,
283 const Uint8 *const keyboard_state,
284 SDL_Joystick *the_stick_of_joy)
287 trace_assert(keyboard_state);
288 (void) the_stick_of_joy;
290 if (keyboard_state[SDL_SCANCODE_A]) {
291 player_move_left(level->player);
292 } else if (keyboard_state[SDL_SCANCODE_D]) {
293 player_move_right(level->player);
294 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
295 player_move_left(level->player);
296 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
297 player_move_right(level->player);
299 player_stop(level->player);
305 int level_reload_preserve_player(Level *level, const char *file_name, Broadcast *broadcast)
307 Lt * const lt = create_lt();
312 /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
314 LineStream * const level_stream = PUSH_LT(
319 LEVEL_LINE_MAX_LENGTH),
320 destroy_line_stream);
321 if (level_stream == NULL) {
325 Background * const background = create_background_from_line_stream(level_stream);
326 if (background == NULL) {
329 level->background = RESET_LT(level->lt, level->background, background);
331 Player * const skipped_player = create_player_from_line_stream(level_stream, broadcast);
332 if (skipped_player == NULL) {
335 destroy_player(skipped_player);
337 Platforms * const platforms = create_platforms_from_line_stream(level_stream);
338 if (platforms == NULL) {
341 level->platforms = RESET_LT(level->lt, level->platforms, platforms);
343 Goals * const goals = create_goals_from_line_stream(level_stream);
347 level->goals = RESET_LT(level->lt, level->goals, goals);
349 Lava * const lava = create_lava_from_line_stream(level_stream);
353 level->lava = RESET_LT(level->lt, level->lava, lava);
355 Platforms * const back_platforms = create_platforms_from_line_stream(level_stream);
356 if (back_platforms == NULL) {
359 level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
361 Boxes * const boxes = create_boxes_from_line_stream(level_stream);
365 level->boxes = RESET_LT(level->lt, level->boxes, boxes);
367 Labels * const labels = create_labels_from_line_stream(level_stream);
368 if (labels == NULL) {
371 level->labels = RESET_LT(level->lt, level->labels, labels);
373 Regions * const regions = create_regions_from_line_stream(level_stream, broadcast);
374 if (regions == NULL) {
377 level->regions = RESET_LT(level->lt, level->regions, regions);
379 physical_world_clean(level->physical_world);
380 if (physical_world_add_solid(
381 level->physical_world,
382 player_as_solid(level->player)) < 0) { RETURN_LT(lt, -1); }
383 if (boxes_add_to_physical_world(
385 level->physical_world) < 0) { RETURN_LT(lt, -1); }
390 int level_sound(Level *level, Sound_samples *sound_samples)
392 if (goals_sound(level->goals, sound_samples) < 0) {
396 if (player_sound(level->player, sound_samples) < 0) {
403 void level_toggle_debug_mode(Level *level)
405 background_toggle_debug_mode(level->background);
408 int level_enter_camera_event(Level *level, Camera *camera)
410 if (!level->flying_mode) {
411 player_focus_camera(level->player, camera);
413 camera_center_at(camera, level->flying_camera_position);
416 goals_cue(level->goals, camera);
417 goals_checkpoint(level->goals, level->player);
418 labels_enter_camera_event(level->labels, camera);
422 Rigid_rect *level_rigid_rect(Level *level,
423 const char *rigid_rect_id)
426 trace_assert(rigid_rect_id);
428 Rigid_rect *rigid_rect = player_rigid_rect(level->player,
430 if (rigid_rect != NULL) {
434 rigid_rect = boxes_rigid_rect(level->boxes, rigid_rect_id);
435 if (rigid_rect != NULL) {
442 struct EvalResult level_send(Level *level, Gc *gc, struct Scope *scope, struct Expr path)
448 const char *target = NULL;
449 struct Expr rest = void_expr();
450 struct EvalResult res = match_list(gc, "q*", path, &target, &rest);
455 if (strcmp(target, "goal") == 0) {
456 return goals_send(level->goals, gc, scope, rest);
457 } else if (strcmp(target, "label") == 0) {
458 return labels_send(level->labels, gc, scope, rest);
459 } else if (strcmp(target, "box") == 0) {
460 return boxes_send(level->boxes, gc, scope, rest);
461 } else if (strcmp(target, "body-push") == 0) {
462 long int id = 0, x = 0, y = 0;
463 res = match_list(gc, "ddd", rest, &id, &x, &y);
468 rigid_bodies_apply_force(level->rigid_bodies, (size_t) id, vec((float) x, (float) y));
470 return eval_success(NIL(gc));
471 } else if (strcmp(target, "body-add") == 0) {
472 long int x = 0, y = 0, w = 0, h = 0;
473 const char *color = 0;
474 res = match_list(gc, "dddds", rest, &x, &y, &w, &h, &color);
482 (long int) rigid_bodies_add(
484 rect((float)x, (float)y, (float)w, (float)h),
486 } else if (strcmp(target, "fly") == 0) {
487 level->flying_mode = !level->flying_mode;
488 return eval_success(NIL(gc));
491 return unknown_target(gc, "level", target);