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);
31 Lava *lava = PUSH_LT(lt, nth_calloc(1, sizeof(Lava)), free);
37 line_stream_next(line_stream),
39 &lava->rects_count) < 0) {
40 log_fail("Could not read amount of lavas\n");
44 lava->rects = PUSH_LT(lt, nth_calloc(1, sizeof(Wavy_rect*) * lava->rects_count), free);
45 if (lava->rects == NULL) {
49 for (size_t i = 0; i < lava->rects_count; ++i) {
50 lava->rects[i] = PUSH_LT(lt, create_wavy_rect_from_line_stream(line_stream), destroy_wavy_rect);
51 if (lava->rects[i] == NULL) {
61 void destroy_lava(Lava *lava)
67 /* TODO(#449): lava does not render its id in debug mode */
68 int lava_render(const Lava *lava,
74 for (size_t i = 0; i < lava->rects_count; ++i) {
75 if (wavy_rect_render(lava->rects[i], camera) < 0) {
83 int lava_update(Lava *lava, float delta_time)
87 for (size_t i = 0; i < lava->rects_count; ++i) {
88 if (wavy_rect_update(lava->rects[i], delta_time) < 0) {
96 bool lava_overlaps_rect(const Lava *lava,
101 for (size_t i = 0; i < lava->rects_count; ++i) {
102 if (rects_overlap(wavy_rect_hitbox(lava->rects[i]), rect)) {
110 void lava_float_rigid_body(Lava *lava, RigidBodies *rigid_bodies, RigidBodyId id)
114 const Rect object_hitbox = rigid_bodies_hitbox(rigid_bodies, id);
115 for (size_t i = 0; i < lava->rects_count; ++i) {
116 const Rect lava_hitbox = wavy_rect_hitbox(lava->rects[i]);
117 if (rects_overlap(object_hitbox, lava_hitbox)) {
118 const Rect overlap_area = rects_overlap_area(object_hitbox, lava_hitbox);
119 const float k = overlap_area.w * overlap_area.h / (object_hitbox.w * object_hitbox.h);
120 rigid_bodies_apply_force(
123 vec(0.0f, -k * LAVA_BOINGNESS));
124 rigid_bodies_damper(rigid_bodies, id, vec(0.0f, -0.9f));