#include "system/stacktrace.h"
+#include "broadcast.h"
+#include "ebisp/builtins.h"
+#include "ebisp/interpreter.h"
#include "game/level/boxes.h"
#include "game/level/player.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"
-
-#define BOXES_CAPACITY 100
+#define BOXES_CAPACITY 1000
struct Boxes
{
log_fail("Could not read amount of boxes\n");
RETURN_LT(lt, NULL);
}
+ log_info("Boxes count: %d\n", boxes->count);
- boxes->body_ids = PUSH_LT(lt, nth_alloc(sizeof(RigidBodyId) * boxes->count), free);
+ trace_assert(boxes->count < BOXES_CAPACITY);
+
+ boxes->body_ids = PUSH_LT(lt, nth_alloc(sizeof(RigidBodyId) * BOXES_CAPACITY), free);
if (boxes->body_ids == NULL) {
RETURN_LT(lt, NULL);
}
}
}
-static
int boxes_add_box(Boxes *boxes, Rect rect, Color color)
{
trace_assert(boxes);
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));
}
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;
+}