]> git.lizzy.rs Git - nothing.git/blob - src/game/level/phantom_platforms.c
Introduce Phantom Platforms
[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(Phantom_Platforms *pp, 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 2.0f
37
38 void phantom_platforms_update(Phantom_Platforms *pp, float dt)
39 {
40     trace_assert(pp);
41
42     for (size_t i = 0; i < pp->size; ++i) {
43         if (pp->hiding[i]) {
44             if (pp->colors[i].a > 0.0f) {
45                 pp->colors[i].a =
46                     fmaxf(0.0f, pp->colors[i].a - HIDING_SPEED * dt);
47             } else {
48                 pp->hiding[i] = 0;
49             }
50         }
51     }
52 }
53
54 void phantom_platforms_hide_at(Phantom_Platforms *pp, Vec2f position)
55 {
56     trace_assert(pp);
57
58     for (size_t i = 0; i < pp->size; ++i) {
59         if (rect_contains_point(pp->rects[i], position)) {
60             pp->hiding[i] = 1;
61         }
62     }
63 }