3 #include "game/level/boxes.h"
4 #include "game/level/player.h"
5 #include "game/level/player/rigid_rect.h"
7 #include "system/error.h"
13 rigid_rect_t **bodies;
16 boxes_t *create_boxes_from_stream(FILE *stream)
20 lt_t *lt = create_lt();
26 boxes_t *boxes = PUSH_LT(lt, malloc(sizeof(boxes_t)), free);
28 throw_error(ERROR_TYPE_LIBC);
32 if (fscanf(stream, "%lu", &boxes->count) == EOF) {
33 throw_error(ERROR_TYPE_LIBC);
37 boxes->bodies = PUSH_LT(lt, malloc(sizeof(rigid_rect_t*) * boxes->count), free);
38 if (boxes->bodies == NULL) {
39 throw_error(ERROR_TYPE_LIBC);
43 for (size_t i = 0; i < boxes->count; ++i) {
44 boxes->bodies[i] = PUSH_LT(
46 create_rigid_rect_from_stream(stream),
48 if (boxes->bodies[i] == NULL) {
58 void destroy_boxes(boxes_t *boxes)
61 RETURN_LT0(boxes->lt);
64 int boxes_render(boxes_t *boxes, camera_t *camera)
69 for (size_t i = 0; i < boxes->count; ++i) {
70 if (rigid_rect_render(boxes->bodies[i], camera) < 0) {
78 int boxes_update(boxes_t *boxes,
84 for (size_t i = 0; i < boxes->count; ++i) {
85 if (rigid_rect_update(boxes->bodies[i], delta_time) < 0) {
90 for (size_t i = 0; i < boxes->count; ++i) {
91 for (size_t j = 0; j < boxes->count; ++j) {
93 rigid_rect_collide_with_rect(
95 rigid_rect_hitbox(boxes->bodies[j]));
103 void boxes_collide_with_platforms(boxes_t *boxes,
104 const platforms_t *platforms)
109 for (size_t i = 0; i < boxes->count; ++i) {
110 rigid_rect_collide_with_platforms(boxes->bodies[i], platforms);
114 void boxes_collide_with_player(boxes_t *boxes,
120 for (size_t i = 0; i < boxes->count; ++i) {
121 player_impact_rigid_rect(player, boxes->bodies[i]);
125 void boxes_rect_object_collide(const boxes_t *boxes,
127 int sides[RECT_SIDE_N])
131 memset(sides, 0, sizeof(int) * RECT_SIDE_N);
133 for (size_t i = 0; i < boxes->count; ++i) {
134 const rect_t hitbox = rigid_rect_hitbox(boxes->bodies[i]);
135 rect_object_impact(&object, &hitbox, sides);