]> git.lizzy.rs Git - nothing.git/blob - src/game/level/phantom_platforms.c
877349f78949b912c181910f074bbd59d2e27786
[nothing.git] / src / game / level / phantom_platforms.c
1 #include "phantom_platforms.h"
2
3 Phantom_Platforms create_phantom_platforms(RectLayer *rect_layer)
4 {
5     Phantom_Platforms pp;
6
7     pp.size = rect_layer->rects.count;
8     pp.rects = malloc(sizeof(pp.rects[0]) * pp.size);
9     memcpy(pp.rects, rect_layer->rects.data, sizeof(pp.rects[0]) * pp.size);
10
11     pp.colors = malloc(sizeof(pp.colors[0]) * pp.size);
12     memcpy(pp.colors, rect_layer->colors.data, sizeof(pp.colors[0]) * pp.size);
13
14     pp.hiding = calloc(1, sizeof(pp.hiding[0]) * pp.size);
15
16     return pp;
17 }
18
19 void destroy_phantom_platforms(Phantom_Platforms pp)
20 {
21     free(pp.rects);
22     free(pp.colors);
23     free(pp.hiding);
24 }
25
26 void phantom_platforms_render(const Phantom_Platforms *pp, const Camera *camera)
27 {
28     trace_assert(pp);
29     trace_assert(camera);
30
31     for (size_t i = 0; i < pp->size; ++i) {
32         camera_fill_rect(camera, pp->rects[i], pp->colors[i]);
33     }
34 }
35
36 #define HIDING_SPEED 4.0f
37
38 // TODO(#1247): phantom_platforms_update is O(N) even when nothing is animated
39 void phantom_platforms_update(Phantom_Platforms *pp, float dt)
40 {
41     trace_assert(pp);
42
43     for (size_t i = 0; i < pp->size; ++i) {
44         if (pp->hiding[i]) {
45             if (pp->colors[i].a > 0.0f) {
46                 pp->colors[i].a =
47                     fmaxf(0.0f, pp->colors[i].a - HIDING_SPEED * dt);
48             } else {
49                 pp->hiding[i] = 0;
50             }
51         }
52     }
53 }
54
55 // TODO: phantom_platforms_hide_at is O(N)
56 void phantom_platforms_hide_at(Phantom_Platforms *pp, Vec2f position)
57 {
58     trace_assert(pp);
59
60     for (size_t i = 0; i < pp->size; ++i) {
61         if (rect_contains_point(pp->rects[i], position)) {
62             pp->hiding[i] = 1;
63         }
64     }
65 }