]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level.c
(#232) Activate goals checkpoints in level_enter_camera_event
[nothing.git] / src / game / level.c
index 398a03b673b554b8ef52ffd02d2fd03e2ddc19cb..adcdbe80a6fd5a306f17ad2b28c017f800f72570 100644 (file)
@@ -44,11 +44,6 @@ level_t *create_level_from_file(const char *file_name)
         RETURN_LT(lt, NULL);
     }
 
-    level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
-    if (level->physical_world == NULL) {
-        RETURN_LT(lt, NULL);
-    }
-
     FILE *level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
     if (level_file == NULL) {
         throw_error(ERROR_TYPE_LIBC);
@@ -66,19 +61,11 @@ level_t *create_level_from_file(const char *file_name)
     if (level->player == NULL) {
         RETURN_LT(lt, NULL);
     }
-    if (physical_world_add_solid(level->physical_world,
-                                 player_as_solid(level->player)) < 0) {
-        RETURN_LT(lt, NULL);
-    }
 
     level->platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
     if (level->platforms == NULL) {
         RETURN_LT(lt, NULL);
     }
-    if (physical_world_add_solid(level->physical_world,
-                                 platforms_as_solid(level->platforms)) < 0) {
-        RETURN_LT(lt, NULL);
-    }
 
     level->goals = PUSH_LT(lt, create_goals_from_stream(level_file), destroy_goals);
     if (level->goals == NULL) {
@@ -99,16 +86,23 @@ level_t *create_level_from_file(const char *file_name)
     if (level->boxes == NULL) {
         RETURN_LT(lt, NULL);
     }
-    if (boxes_add_to_physical_world(level->boxes,
-                                    level->physical_world) < 0) {
-        RETURN_LT(lt, NULL);
-    }
 
     level->background = PUSH_LT(lt, create_background(level->background_color), destroy_background);
     if (level->background == NULL) {
         RETURN_LT(lt, NULL);
     }
 
+    level->physical_world = PUSH_LT(lt, create_physical_world(), destroy_physical_world);
+    if (level->physical_world == NULL) {
+        RETURN_LT(lt, NULL);
+    }
+    if (physical_world_add_solid(
+            level->physical_world,
+            player_as_solid(level->player)) < 0) { RETURN_LT(lt, NULL); }
+    if (boxes_add_to_physical_world(
+            level->boxes,
+            level->physical_world) < 0) { RETURN_LT(lt, NULL); }
+
     level->lt = lt;
 
     fclose(RELEASE_LT(lt, level_file));
@@ -160,11 +154,6 @@ int level_render(const level_t *level, camera_t *camera)
         return -1;
     }
 
-    /* TODO(#157): goals_cue is not supposed to be invoked in level_render
-     *
-     * But I simply couldn't find a better place for it.
-     */
-    goals_cue(level->goals, camera);
 
     return 0;
 }
@@ -179,30 +168,12 @@ int level_update(level_t *level, float delta_time)
     boxes_update(level->boxes, delta_time);
     player_update(level->player, delta_time);
 
-    physical_world_collide_solids(level->physical_world);
-    /* TODO(#202): it is diffcult to introduce more kinds of object into the physics engine */
-    /*
-    boxes_collide_with_solid(level->boxes, platforms_as_solid(level->platforms));
-    player_collide_with_solid(level->player, platforms_as_solid(level->platforms));
-
-    boxes_collide_with_lava(level->boxes, level->lava);
-    boxes_collide_with_solid(level->boxes, boxes_as_solid(level->boxes));
-    boxes_collide_with_solid(level->boxes, player_as_solid(level->player));
-
-    player_collide_with_solid(level->player, boxes_as_solid(level->boxes));
-
-    boxes_collide_with_solid(level->boxes, platforms_as_solid(level->platforms));
-    player_collide_with_solid(level->player, platforms_as_solid(level->platforms));
-
-    boxes_collide_with_solid(level->boxes, boxes_as_solid(level->boxes));
-    player_collide_with_solid(level->player, boxes_as_solid(level->boxes));
-    */
+    physical_world_collide_solids(level->physical_world, level->platforms);
 
     player_hide_goals(level->player, level->goals);
     player_die_from_lava(level->player, level->lava);
 
     goals_update(level->goals, delta_time);
-    goals_checkpoint(level->goals, level->player);
     lava_update(level->lava, delta_time);
 
     return 0;
@@ -264,7 +235,7 @@ int level_reload_preserve_player(level_t *level, const char *file_name)
 
     /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
 
-    physical_world_clean(level->physical_world);
+
 
     FILE * const level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose_lt);
     if (level_file == NULL) {
@@ -284,20 +255,12 @@ int level_reload_preserve_player(level_t *level, const char *file_name)
         RETURN_LT(lt, -1);
     }
     destroy_player(skipped_player);
-    if (physical_world_add_solid(level->physical_world,
-                                 player_as_solid(level->player)) < 0) {
-        RETURN_LT(lt, -1);
-    }
 
     platforms_t * const platforms = create_platforms_from_stream(level_file);
     if (platforms == NULL) {
         RETURN_LT(lt, -1);
     }
     level->platforms = RESET_LT(level->lt, level->platforms, platforms);
-    if (physical_world_add_solid(level->physical_world,
-                                 platforms_as_solid(level->platforms)) < 0) {
-        RETURN_LT(lt, -1);
-    }
 
     goals_t * const goals = create_goals_from_stream(level_file);
     if (goals == NULL) {
@@ -322,10 +285,14 @@ int level_reload_preserve_player(level_t *level, const char *file_name)
         RETURN_LT(lt, -1);
     }
     level->boxes = RESET_LT(level->lt, level->boxes, boxes);
-    if (boxes_add_to_physical_world(level->boxes,
-                                    level->physical_world) < 0) {
-        RETURN_LT(lt, -1);
-    }
+
+    physical_world_clean(level->physical_world);
+    if (physical_world_add_solid(
+            level->physical_world,
+            player_as_solid(level->player)) < 0) { RETURN_LT(lt, -1); }
+    if (boxes_add_to_physical_world(
+            level->boxes,
+            level->physical_world) < 0) { RETURN_LT(lt, -1); }
 
     RETURN_LT(lt, 0);
 }
@@ -347,3 +314,11 @@ void level_toggle_debug_mode(level_t *level)
 {
     background_toggle_debug_mode(level->background);
 }
+
+int level_enter_camera_event(level_t *level,
+                             const camera_t *camera)
+{
+    goals_cue(level->goals, camera);
+    goals_checkpoint(level->goals, level->player);
+    return 0;
+}