]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/boxes.c
(#802) TODO
[nothing.git] / src / game / level / boxes.c
index 5cbd3491407bc1fb6726a2f1b09125300d1f3027..462475e9fb169996797fd6ec30107bddeac63eab 100644 (file)
@@ -1,21 +1,18 @@
 #include "system/stacktrace.h"
 
+#include "broadcast.h"
+#include "ebisp/builtins.h"
+#include "ebisp/interpreter.h"
 #include "game/level/boxes.h"
-#include "game/level/physical_world.h"
 #include "game/level/player.h"
-#include "game/level/player/rigid_rect.h"
+#include "game/level/rigid_bodies.h"
+#include "math/rand.h"
 #include "system/line_stream.h"
+#include "system/log.h"
 #include "system/lt.h"
 #include "system/nth_alloc.h"
-#include "system/log.h"
-#include "ebisp/interpreter.h"
-#include "ebisp/builtins.h"
-#include "broadcast.h"
-#include "game/level/rigid_bodies.h"
 
-/* TODO(#631): Boxes entity should be implemented in terms of RigidBodies instead of Rigid_rect */
-
-#define BOXES_CAPACITY 100
+#define BOXES_CAPACITY 1000
 
 struct Boxes
 {
@@ -50,8 +47,11 @@ Boxes *create_boxes_from_line_stream(LineStream *line_stream, RigidBodies *rigid
         log_fail("Could not read amount of boxes\n");
         RETURN_LT(lt, NULL);
     }
+    log_info("Boxes count: %d\n", boxes->count);
+
+    trace_assert(boxes->count < BOXES_CAPACITY);
 
-    boxes->body_ids = PUSH_LT(lt, nth_alloc(sizeof(RigidBodyId) * boxes->count), free);
+    boxes->body_ids = PUSH_LT(lt, nth_alloc(sizeof(RigidBodyId) * BOXES_CAPACITY), free);
     if (boxes->body_ids == NULL) {
         RETURN_LT(lt, NULL);
     }
@@ -66,6 +66,11 @@ Boxes *create_boxes_from_line_stream(LineStream *line_stream, RigidBodies *rigid
 void destroy_boxes(Boxes *boxes)
 {
     trace_assert(boxes);
+
+    for (size_t i = 0; i < boxes->count; ++i) {
+        rigid_bodies_remove(boxes->rigid_bodies, boxes->body_ids[i]);
+    }
+
     RETURN_LT0(boxes->lt);
 }
 
@@ -108,7 +113,6 @@ void boxes_float_in_lava(Boxes *boxes, Lava *lava)
     }
 }
 
-static
 int boxes_add_box(Boxes *boxes, Rect rect, Color color)
 {
     trace_assert(boxes);
@@ -137,14 +141,21 @@ boxes_send(Boxes *boxes, Gc *gc, struct Scope *scope, struct Expr path)
         const char *action = target.atom->str;
 
         if (strcmp(action, "new") == 0) {
-            const char *color = NULL;
+            struct Expr optional_args = void_expr();
             long int x, y, w, h;
-            res = match_list(gc, "dddds", rest, &x, &y, &w, &h, &color);
+            res = match_list(gc, "dddd*", rest, &x, &y, &w, &h, &optional_args);
             if (res.is_error) {
                 return res;
             }
 
-            boxes_add_box(boxes, rect((float) x, (float) y, (float) w, (float) h), hexstr(color));
+            Color color = rgba(rand_float(1.0f), rand_float(1.0f), rand_float(1.0f), 1.0f);
+            if (!nil_p(optional_args)) {
+                const char *color_hex = NULL;
+                res = match_list(gc, "s*", optional_args, &color_hex, NULL);
+                color = hexstr(color_hex);
+            }
+
+            boxes_add_box(boxes, rect((float) x, (float) y, (float) w, (float) h), color);
 
             return eval_success(NIL(gc));
         }
@@ -154,3 +165,25 @@ boxes_send(Boxes *boxes, Gc *gc, struct Scope *scope, struct Expr path)
 
     return wrong_argument_type(gc, "string-or-symbol-p", target);
 }
+
+
+int boxes_delete_at(Boxes *boxes, Vec position)
+{
+    trace_assert(boxes);
+
+    for (size_t i = 0; i < boxes->count; ++i) {
+        const Rect hitbox = rigid_bodies_hitbox(
+            boxes->rigid_bodies,
+            boxes->body_ids[i]);
+        if (rect_contains_point(hitbox, position)) {
+            rigid_bodies_remove(boxes->rigid_bodies, boxes->body_ids[i]);
+            for (size_t j = i; j < boxes->count - 1; ++j) {
+                boxes->body_ids[j] = boxes->body_ids[j + 1];
+            }
+            boxes->count--;
+            return 0;
+        }
+    }
+
+    return 0;
+}