#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"
-/* TODO(#631): Boxes entity should be implemented in terms of RigidBodies instead of Rigid_rect */
+#define BOXES_CAPACITY 1000
struct Boxes
{
Lt *lt;
+ RigidBodies *rigid_bodies;
+ RigidBodyId *body_ids;
size_t count;
- Rigid_rect **bodies;
- size_t capacity;
};
-Boxes *create_boxes_from_line_stream(LineStream *line_stream)
+Boxes *create_boxes_from_line_stream(LineStream *line_stream, RigidBodies *rigid_bodies)
{
trace_assert(line_stream);
if (boxes == NULL) {
RETURN_LT(lt, NULL);
}
+ boxes->lt = lt;
+
+ boxes->rigid_bodies = rigid_bodies;
if (sscanf(
line_stream_next(line_stream),
log_fail("Could not read amount of boxes\n");
RETURN_LT(lt, NULL);
}
- boxes->capacity = boxes->count;
+ log_info("Boxes count: %d\n", boxes->count);
+
+ trace_assert(boxes->count < BOXES_CAPACITY);
- boxes->bodies = PUSH_LT(lt, nth_alloc(sizeof(Rigid_rect*) * boxes->count), free);
- if (boxes->bodies == NULL) {
+ boxes->body_ids = PUSH_LT(lt, nth_alloc(sizeof(RigidBodyId) * BOXES_CAPACITY), free);
+ if (boxes->body_ids == NULL) {
RETURN_LT(lt, NULL);
}
for (size_t i = 0; i < boxes->count; ++i) {
- boxes->bodies[i] = PUSH_LT(
- lt,
- create_rigid_rect_from_line_stream(line_stream),
- destroy_rigid_rect);
- if (boxes->bodies[i] == NULL) {
- RETURN_LT(lt, NULL);
- }
+ boxes->body_ids[i] = rigid_bodies_add_from_line_stream(boxes->rigid_bodies, line_stream);
}
- boxes->lt = lt;
-
return boxes;
}
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);
}
trace_assert(camera);
for (size_t i = 0; i < boxes->count; ++i) {
- if (rigid_rect_render(boxes->bodies[i], camera) < 0) {
+ if (rigid_bodies_render(boxes->rigid_bodies, boxes->body_ids[i], camera) < 0) {
return -1;
}
}
trace_assert(delta_time);
for (size_t i = 0; i < boxes->count; ++i) {
- if (rigid_rect_update(boxes->bodies[i], delta_time) < 0) {
- return -1;
- }
- }
-
- return 0;
-}
-
-int boxes_add_to_physical_world(const Boxes *boxes,
- Physical_world *physical_world)
-{
- trace_assert(boxes);
- trace_assert(physical_world);
-
- for (size_t i = 0; i < boxes->count; ++i) {
- if (physical_world_add_solid(
- physical_world,
- rigid_rect_as_solid(boxes->bodies[i])) < 0) {
+ if (rigid_bodies_update(boxes->rigid_bodies, boxes->body_ids[i], delta_time) < 0) {
return -1;
}
}
trace_assert(lava);
for (size_t i = 0; i < boxes->count; ++i) {
- lava_float_rigid_rect(lava, boxes->bodies[i]);
+ lava_float_rigid_body(lava, boxes->rigid_bodies, boxes->body_ids[i]);
}
}
-Rigid_rect *boxes_rigid_rect(Boxes *boxes, const char *id)
+int boxes_add_box(Boxes *boxes, Rect rect, Color color)
{
trace_assert(boxes);
- trace_assert(id);
+ trace_assert(boxes->count < BOXES_CAPACITY);
- for (size_t i = 0; i < boxes->count; ++i) {
- if (rigid_rect_has_id(boxes->bodies[i], id)) {
- return boxes->bodies[i];
- }
- }
-
- return 0;
-}
-
-static
-int boxes_add_box(Boxes *boxes, Rect rect, Color color, const char *id)
-{
- trace_assert(boxes);
- trace_assert(id);
-
- /* TODO(#628): boxes_add_box is unable to register a new box in the physical world */
-
- if (boxes->count >= boxes->capacity) {
- const size_t new_capacity = boxes->capacity * 2;
-
- Rigid_rect **const new_bodies = nth_realloc(
- boxes->bodies,
- sizeof(Rigid_rect*) * new_capacity);
-
- if (new_bodies == NULL) {
- return -1;
- }
-
- boxes->capacity = new_capacity;
- boxes->bodies = REPLACE_LT(
- boxes->lt,
- boxes->bodies,
- new_bodies);
- }
-
- boxes->bodies[boxes->count++] = create_rigid_rect(rect, color, id);
+ boxes->body_ids[boxes->count++] = rigid_bodies_add(boxes->rigid_bodies, rect, color);
return 0;
}
return res;
}
- if (string_p(target)) {
- const char *box_id = target.atom->str;
-
- for (size_t i = 0; i < boxes->count; ++i) {
- if (rigid_rect_has_id(boxes->bodies[i], box_id)) {
- return rigid_rect_send(boxes->bodies[i], gc, scope, rest);
- }
- }
-
- return unknown_target(gc, "box", box_id);
- } else if (symbol_p(target)) {
+ if (symbol_p(target)) {
const char *action = target.atom->str;
if (strcmp(action, "new") == 0) {
+ struct Expr optional_args = void_expr();
long int x, y, w, h;
- res = match_list(gc, "dddd", rest, &x, &y, &w, &h);
+ res = match_list(gc, "dddd*", rest, &x, &y, &w, &h, &optional_args);
if (res.is_error) {
return res;
}
- /* TODO(#629): the color and id of added box is hardcoded */
- boxes_add_box(boxes, rect((float) x, (float) y, (float) w, (float) h), hexstr("a02c2c"), "khooy");
+ 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;
+}