]> git.lizzy.rs Git - nothing.git/blob - src/game/level/boxes.c
(#553) Remove labels->enabled
[nothing.git] / src / game / level / boxes.c
1 #include <assert.h>
2
3 #include "game/level/boxes.h"
4 #include "game/level/physical_world.h"
5 #include "game/level/player.h"
6 #include "game/level/player/rigid_rect.h"
7 #include "system/line_stream.h"
8 #include "system/lt.h"
9 #include "system/nth_alloc.h"
10 #include "system/log.h"
11
12 struct Boxes
13 {
14     Lt *lt;
15     size_t count;
16     Rigid_rect **bodies;
17 };
18
19 Boxes *create_boxes_from_line_stream(LineStream *line_stream)
20 {
21     assert(line_stream);
22
23     Lt *lt = create_lt();
24
25     if (lt == NULL) {
26         return NULL;
27     }
28
29     Boxes *boxes = PUSH_LT(lt, nth_alloc(sizeof(Boxes)), free);
30     if (boxes == NULL) {
31         RETURN_LT(lt, NULL);
32     }
33
34     if (sscanf(
35             line_stream_next(line_stream),
36             "%lu",
37             &boxes->count) == EOF) {
38         log_fail("Could not read amount of boxes\n");
39         RETURN_LT(lt, NULL);
40     }
41
42     boxes->bodies = PUSH_LT(lt, nth_alloc(sizeof(Rigid_rect*) * boxes->count), free);
43     if (boxes->bodies == NULL) {
44         RETURN_LT(lt, NULL);
45     }
46
47     for (size_t i = 0; i < boxes->count; ++i) {
48         boxes->bodies[i] = PUSH_LT(
49             lt,
50             create_rigid_rect_from_line_stream(line_stream),
51             destroy_rigid_rect);
52         if (boxes->bodies[i] == NULL) {
53             RETURN_LT(lt, NULL);
54         }
55     }
56
57     boxes->lt = lt;
58
59     return boxes;
60 }
61
62 void destroy_boxes(Boxes *boxes)
63 {
64     assert(boxes);
65     RETURN_LT0(boxes->lt);
66 }
67
68 int boxes_render(Boxes *boxes, Camera *camera)
69 {
70     assert(boxes);
71     assert(camera);
72
73     for (size_t i = 0; i < boxes->count; ++i) {
74         if (rigid_rect_render(boxes->bodies[i], camera) < 0) {
75             return -1;
76         }
77     }
78
79     return 0;
80 }
81
82 int boxes_update(Boxes *boxes,
83                  float delta_time)
84 {
85     assert(boxes);
86     assert(delta_time);
87
88     for (size_t i = 0; i < boxes->count; ++i) {
89         if (rigid_rect_update(boxes->bodies[i], delta_time) < 0) {
90             return -1;
91         }
92     }
93
94     return 0;
95 }
96
97 int boxes_add_to_physical_world(const Boxes *boxes,
98                                 Physical_world *physical_world)
99 {
100     assert(boxes);
101     assert(physical_world);
102
103     for (size_t i = 0; i < boxes->count; ++i) {
104         if (physical_world_add_solid(
105                 physical_world,
106                 rigid_rect_as_solid(boxes->bodies[i])) < 0) {
107             return -1;
108         }
109     }
110
111     return 0;
112 }
113
114 void boxes_float_in_lava(Boxes *boxes, Lava *lava)
115 {
116     assert(boxes);
117     assert(lava);
118
119     for (size_t i = 0; i < boxes->count; ++i) {
120         lava_float_rigid_rect(lava, boxes->bodies[i]);
121     }
122 }
123
124 Rigid_rect *boxes_rigid_rect(Boxes *boxes, const char *id)
125 {
126     assert(boxes);
127     assert(id);
128
129     for (size_t i = 0; i < boxes->count; ++i) {
130         if (rigid_rect_has_id(boxes->bodies[i], id)) {
131             return boxes->bodies[i];
132         }
133     }
134
135     return 0;
136 }