2 #include "system/stacktrace.h"
6 #include "game/level/lava/wavy_rect.h"
10 #include "system/line_stream.h"
11 #include "system/nth_alloc.h"
12 #include "system/log.h"
14 #define LAVA_BOINGNESS 2500.0f
22 Lava *create_lava_from_line_stream(LineStream *line_stream)
24 trace_assert(line_stream);
28 Lava *lava = PUSH_LT(lt, nth_calloc(1, sizeof(Lava)), free);
34 line_stream_next(line_stream),
36 &lava->rects_count) < 0) {
37 log_fail("Could not read amount of lavas\n");
41 lava->rects = PUSH_LT(lt, nth_calloc(1, sizeof(Wavy_rect*) * lava->rects_count), free);
42 if (lava->rects == NULL) {
46 for (size_t i = 0; i < lava->rects_count; ++i) {
47 lava->rects[i] = PUSH_LT(lt, create_wavy_rect_from_line_stream(line_stream), destroy_wavy_rect);
48 if (lava->rects[i] == NULL) {
58 void destroy_lava(Lava *lava)
64 /* TODO(#449): lava does not render its id in debug mode */
65 int lava_render(const Lava *lava,
71 for (size_t i = 0; i < lava->rects_count; ++i) {
72 if (wavy_rect_render(lava->rects[i], camera) < 0) {
80 int lava_update(Lava *lava, float delta_time)
84 for (size_t i = 0; i < lava->rects_count; ++i) {
85 if (wavy_rect_update(lava->rects[i], delta_time) < 0) {
93 bool lava_overlaps_rect(const Lava *lava,
98 for (size_t i = 0; i < lava->rects_count; ++i) {
99 if (rects_overlap(wavy_rect_hitbox(lava->rects[i]), rect)) {
107 void lava_float_rigid_body(Lava *lava, RigidBodies *rigid_bodies, RigidBodyId id)
111 const Rect object_hitbox = rigid_bodies_hitbox(rigid_bodies, id);
112 for (size_t i = 0; i < lava->rects_count; ++i) {
113 const Rect lava_hitbox = wavy_rect_hitbox(lava->rects[i]);
114 if (rects_overlap(object_hitbox, lava_hitbox)) {
115 const Rect overlap_area = rects_overlap_area(object_hitbox, lava_hitbox);
116 const float k = overlap_area.w * overlap_area.h / (object_hitbox.w * object_hitbox.h);
117 rigid_bodies_apply_force(
120 vec(0.0f, -k * LAVA_BOINGNESS));
121 rigid_bodies_damper(rigid_bodies, id, vec(0.0f, -0.9f));