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;
33 RigidBodies *rigid_bodies;
38 Platforms *back_platforms;
42 Physical_world *physical_world;
45 Vec flying_camera_position;
46 float flying_camera_scale;
49 Level *create_level_from_file(const char *file_name, Broadcast *broadcast)
51 trace_assert(file_name);
53 Lt *const lt = create_lt();
58 Level *const level = PUSH_LT(lt, nth_alloc(sizeof(Level)), free);
63 LineStream *level_stream = PUSH_LT(
68 LEVEL_LINE_MAX_LENGTH),
70 if (level_stream == NULL) {
73 level->background = PUSH_LT(
75 create_background_from_line_stream(level_stream),
77 if (level->background == NULL) {
81 level->rigid_bodies = PUSH_LT(lt, create_rigid_bodies(1024), destroy_rigid_bodies);
82 if (level->rigid_bodies == NULL) {
86 level->player = PUSH_LT(
88 create_player_from_line_stream(level_stream, level->rigid_bodies, broadcast),
90 if (level->player == NULL) {
94 level->platforms = PUSH_LT(
96 create_platforms_from_line_stream(level_stream),
98 if (level->platforms == NULL) {
102 level->goals = PUSH_LT(
104 create_goals_from_line_stream(level_stream),
106 if (level->goals == NULL) {
110 level->lava = PUSH_LT(
112 create_lava_from_line_stream(level_stream),
114 if (level->lava == NULL) {
118 level->back_platforms = PUSH_LT(
120 create_platforms_from_line_stream(level_stream),
122 if (level->back_platforms == NULL) {
126 level->boxes = PUSH_LT(
128 create_boxes_from_line_stream(level_stream, level->rigid_bodies),
130 if (level->boxes == NULL) {
134 level->labels = PUSH_LT(
136 create_labels_from_line_stream(level_stream),
138 if (level->labels == NULL) {
142 level->regions = PUSH_LT(
144 create_regions_from_line_stream(level_stream, broadcast),
146 if (level->regions == NULL) {
150 level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
151 if (level->physical_world == NULL) {
155 level->flying_mode = false;
156 level->flying_camera_position = vec(0.0f, 0.0f);
157 level->flying_camera_scale = 1.0f;
161 destroy_line_stream(RELEASE_LT(lt, level_stream));
166 void destroy_level(Level *level)
169 RETURN_LT0(level->lt);
172 int level_render(const Level *level, Camera *camera)
176 if (background_render(level->background, camera) < 0) {
180 if (platforms_render(level->back_platforms, camera) < 0) {
184 if (player_render(level->player, camera) < 0) {
188 if (boxes_render(level->boxes, camera) < 0) {
192 if (lava_render(level->lava, camera) < 0) {
196 if (platforms_render(level->platforms, camera) < 0) {
200 if (goals_render(level->goals, camera) < 0) {
204 if (labels_render(level->labels, camera) < 0) {
208 if (regions_render(level->regions, camera) < 0) {
215 int level_update(Level *level, float delta_time)
218 trace_assert(delta_time > 0);
220 physical_world_apply_gravity(level->physical_world);
221 boxes_float_in_lava(level->boxes, level->lava);
222 rigid_bodies_apply_omniforce(level->rigid_bodies, vec(0.0f, LEVEL_GRAVITY));
224 boxes_update(level->boxes, delta_time);
225 player_update(level->player, delta_time);
227 rigid_bodies_collide(level->rigid_bodies, level->platforms);
228 physical_world_collide_solids(level->physical_world, level->platforms);
230 player_hide_goals(level->player, level->goals);
231 player_die_from_lava(level->player, level->lava);
232 regions_player_enter(level->regions, level->player);
233 regions_player_leave(level->regions, level->player);
235 goals_update(level->goals, delta_time);
236 lava_update(level->lava, delta_time);
237 labels_update(level->labels, delta_time);
242 int level_event(Level *level, const SDL_Event *event)
247 switch (event->type) {
249 switch (event->key.keysym.sym) {
251 player_jump(level->player);
256 case SDL_JOYBUTTONDOWN:
257 if (event->jbutton.button == 1) {
258 player_jump(level->player);
262 case SDL_MOUSEMOTION:
263 if (level->flying_mode) {
264 vec_add(&level->flying_camera_position,
265 vec((float) event->motion.xrel, (float) event->motion.yrel));
270 if (level->flying_mode) {
271 // TODO(#679): zooming in flying mode is not smooth enough
272 if (event->wheel.y > 0) {
273 level->flying_camera_scale += 0.1f;
274 } else if (event->wheel.y < 0) {
275 level->flying_camera_scale = fmaxf(0.1f, level->flying_camera_scale - 0.1f);
284 int level_input(Level *level,
285 const Uint8 *const keyboard_state,
286 SDL_Joystick *the_stick_of_joy)
289 trace_assert(keyboard_state);
290 (void) the_stick_of_joy;
292 if (keyboard_state[SDL_SCANCODE_A]) {
293 player_move_left(level->player);
294 } else if (keyboard_state[SDL_SCANCODE_D]) {
295 player_move_right(level->player);
296 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
297 player_move_left(level->player);
298 } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
299 player_move_right(level->player);
301 player_stop(level->player);
307 int level_reload_preserve_player(Level *level, const char *file_name, Broadcast *broadcast)
309 Lt * const lt = create_lt();
314 /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
316 LineStream * const level_stream = PUSH_LT(
321 LEVEL_LINE_MAX_LENGTH),
322 destroy_line_stream);
323 if (level_stream == NULL) {
327 Background * const background = create_background_from_line_stream(level_stream);
328 if (background == NULL) {
331 level->background = RESET_LT(level->lt, level->background, background);
333 Player * const skipped_player = create_player_from_line_stream(level_stream, level->rigid_bodies, broadcast);
334 if (skipped_player == NULL) {
337 destroy_player(skipped_player);
339 Platforms * const platforms = create_platforms_from_line_stream(level_stream);
340 if (platforms == NULL) {
343 level->platforms = RESET_LT(level->lt, level->platforms, platforms);
345 Goals * const goals = create_goals_from_line_stream(level_stream);
349 level->goals = RESET_LT(level->lt, level->goals, goals);
351 Lava * const lava = create_lava_from_line_stream(level_stream);
355 level->lava = RESET_LT(level->lt, level->lava, lava);
357 Platforms * const back_platforms = create_platforms_from_line_stream(level_stream);
358 if (back_platforms == NULL) {
361 level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
363 Boxes * const boxes = create_boxes_from_line_stream(level_stream, level->rigid_bodies);
367 level->boxes = RESET_LT(level->lt, level->boxes, boxes);
369 Labels * const labels = create_labels_from_line_stream(level_stream);
370 if (labels == NULL) {
373 level->labels = RESET_LT(level->lt, level->labels, labels);
375 Regions * const regions = create_regions_from_line_stream(level_stream, broadcast);
376 if (regions == NULL) {
379 level->regions = RESET_LT(level->lt, level->regions, regions);
381 physical_world_clean(level->physical_world);
386 int level_sound(Level *level, Sound_samples *sound_samples)
388 if (goals_sound(level->goals, sound_samples) < 0) {
392 if (player_sound(level->player, sound_samples) < 0) {
399 void level_toggle_debug_mode(Level *level)
401 background_toggle_debug_mode(level->background);
404 int level_enter_camera_event(Level *level, Camera *camera)
406 if (!level->flying_mode) {
407 player_focus_camera(level->player, camera);
408 camera_scale(camera, 1.0f);
410 camera_center_at(camera, level->flying_camera_position);
411 camera_scale(camera, level->flying_camera_scale);
414 goals_cue(level->goals, camera);
415 goals_checkpoint(level->goals, level->player);
416 labels_enter_camera_event(level->labels, camera);
420 struct EvalResult level_send(Level *level, Gc *gc, struct Scope *scope, struct Expr path)
426 const char *target = NULL;
427 struct Expr rest = void_expr();
428 struct EvalResult res = match_list(gc, "q*", path, &target, &rest);
433 if (strcmp(target, "goal") == 0) {
434 return goals_send(level->goals, gc, scope, rest);
435 } else if (strcmp(target, "label") == 0) {
436 return labels_send(level->labels, gc, scope, rest);
437 } else if (strcmp(target, "box") == 0) {
438 return boxes_send(level->boxes, gc, scope, rest);
439 } else if (strcmp(target, "body-push") == 0) {
440 long int id = 0, x = 0, y = 0;
441 res = match_list(gc, "ddd", rest, &id, &x, &y);
446 rigid_bodies_apply_force(level->rigid_bodies, (size_t) id, vec((float) x, (float) y));
448 return eval_success(NIL(gc));
449 } else if (strcmp(target, "body-add") == 0) {
450 long int x = 0, y = 0, w = 0, h = 0;
451 const char *color = 0;
452 res = match_list(gc, "dddds", rest, &x, &y, &w, &h, &color);
460 (long int) rigid_bodies_add(
462 rect((float)x, (float)y, (float)w, (float)h),
464 } else if (strcmp(target, "fly") == 0) {
465 level->flying_mode = !level->flying_mode;
466 SDL_SetRelativeMouseMode(level->flying_mode);
467 return eval_success(NIL(gc));
470 return unknown_target(gc, "level", target);