#include <assert.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 "system/lt.h"
#include "system/error.h"
+#include "system/line_stream.h"
+#include "system/lt.h"
+#include "system/nth_alloc.h"
-struct boxes_t
+struct Boxes
{
- lt_t *lt;
+ Lt *lt;
size_t count;
- rigid_rect_t **bodies;
+ Rigid_rect **bodies;
};
-boxes_t *create_boxes_from_stream(FILE *stream)
+Boxes *create_boxes_from_line_stream(LineStream *line_stream)
{
- assert(stream);
+ assert(line_stream);
- lt_t *lt = create_lt();
+ Lt *lt = create_lt();
if (lt == NULL) {
return NULL;
}
- boxes_t *boxes = PUSH_LT(lt, malloc(sizeof(boxes_t)), free);
+ Boxes *boxes = PUSH_LT(lt, nth_alloc(sizeof(Boxes)), free);
if (boxes == NULL) {
throw_error(ERROR_TYPE_LIBC);
RETURN_LT(lt, NULL);
}
- if (fscanf(stream, "%lu", &boxes->count) == EOF) {
+ if (sscanf(
+ line_stream_next(line_stream),
+ "%lu",
+ &boxes->count) == EOF) {
throw_error(ERROR_TYPE_LIBC);
RETURN_LT(lt, NULL);
}
- boxes->bodies = PUSH_LT(lt, malloc(sizeof(rigid_rect_t*) * boxes->count), free);
+ boxes->bodies = PUSH_LT(lt, nth_alloc(sizeof(Rigid_rect*) * boxes->count), free);
if (boxes->bodies == NULL) {
throw_error(ERROR_TYPE_LIBC);
RETURN_LT(lt, NULL);
for (size_t i = 0; i < boxes->count; ++i) {
boxes->bodies[i] = PUSH_LT(
lt,
- create_rigid_rect_from_stream(stream),
+ create_rigid_rect_from_line_stream(line_stream),
destroy_rigid_rect);
if (boxes->bodies[i] == NULL) {
RETURN_LT(lt, NULL);
return boxes;
}
-void destroy_boxes(boxes_t *boxes)
+void destroy_boxes(Boxes *boxes)
{
assert(boxes);
RETURN_LT0(boxes->lt);
}
-solid_ref_t boxes_as_solid(boxes_t *boxes)
-{
- solid_ref_t ref = {
- .tag = SOLID_BOXES,
- .ptr = (void*) boxes
- };
-
- return ref;
-}
-
-int boxes_render(boxes_t *boxes, camera_t *camera)
+int boxes_render(Boxes *boxes, Camera *camera)
{
assert(boxes);
assert(camera);
return 0;
}
-int boxes_update(boxes_t *boxes,
+int boxes_update(Boxes *boxes,
float delta_time)
{
assert(boxes);
}
}
- for (size_t i = 0; i < boxes->count; ++i) {
- for (size_t j = 0; j < boxes->count; ++j) {
- if (i != j) {
- rigid_rect_collide_with_solid(
- boxes->bodies[i],
- rigid_rect_as_solid(boxes->bodies[j]));
- }
- }
- }
-
return 0;
}
-void boxes_collide_with_solid(boxes_t *boxes,
- solid_ref_t solid)
+int boxes_add_to_physical_world(const Boxes *boxes,
+ Physical_world *physical_world)
{
assert(boxes);
+ assert(physical_world);
for (size_t i = 0; i < boxes->count; ++i) {
- rigid_rect_collide_with_solid(boxes->bodies[i], solid);
+ if (physical_world_add_solid(
+ physical_world,
+ rigid_rect_as_solid(boxes->bodies[i])) < 0) {
+ return -1;
+ }
}
+
+ return 0;
}
-void boxes_take_impact_from_player(boxes_t *boxes,
- player_t *player)
+void boxes_float_in_lava(Boxes *boxes, Lava *lava)
{
assert(boxes);
- assert(player);
+ assert(lava);
for (size_t i = 0; i < boxes->count; ++i) {
- player_impact_rigid_rect(player, boxes->bodies[i]);
+ lava_float_rigid_rect(lava, boxes->bodies[i]);
}
}
-void boxes_rect_object_collide(const boxes_t *boxes,
- rect_t object,
- int sides[RECT_SIDE_N])
+Rigid_rect *boxes_rigid_rect(Boxes *boxes, const char *id)
{
assert(boxes);
-
- memset(sides, 0, sizeof(int) * RECT_SIDE_N);
+ assert(id);
for (size_t i = 0; i < boxes->count; ++i) {
- const rect_t hitbox = rigid_rect_hitbox(boxes->bodies[i]);
- rect_object_impact(&object, &hitbox, sides);
+ if (rigid_rect_has_id(boxes->bodies[i], id)) {
+ return boxes->bodies[i];
+ }
}
+
+ return 0;
}