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 solid_ref_t boxes_as_solid(boxes_t *boxes)
68 .solid = (void*) boxes
74 int boxes_render(boxes_t *boxes, camera_t *camera)
79 for (size_t i = 0; i < boxes->count; ++i) {
80 if (rigid_rect_render(boxes->bodies[i], camera) < 0) {
88 int boxes_update(boxes_t *boxes,
94 for (size_t i = 0; i < boxes->count; ++i) {
95 if (rigid_rect_update(boxes->bodies[i], delta_time) < 0) {
100 for (size_t i = 0; i < boxes->count; ++i) {
101 for (size_t j = 0; j < boxes->count; ++j) {
103 rigid_rect_collide_with_rect(
105 rigid_rect_hitbox(boxes->bodies[j]));
113 void boxes_collide_with_platforms(boxes_t *boxes,
114 platforms_t *platforms)
119 for (size_t i = 0; i < boxes->count; ++i) {
120 rigid_rect_collide_with_solid(boxes->bodies[i], platforms_as_solid(platforms));
124 void boxes_collide_with_player(boxes_t *boxes,
130 for (size_t i = 0; i < boxes->count; ++i) {
131 player_impact_rigid_rect(player, boxes->bodies[i]);
135 void boxes_rect_object_collide(const boxes_t *boxes,
137 int sides[RECT_SIDE_N])
141 memset(sides, 0, sizeof(int) * RECT_SIDE_N);
143 for (size_t i = 0; i < boxes->count; ++i) {
144 const rect_t hitbox = rigid_rect_hitbox(boxes->bodies[i]);
145 rect_object_impact(&object, &hitbox, sides);