]> git.lizzy.rs Git - nothing.git/blob - src/game/level/lava/wavy_rect.c
5c3e14081e13cee1e5917f7ca0e10992be8747b4
[nothing.git] / src / game / level / lava / wavy_rect.c
1 #include <SDL.h>
2 #include "system/stacktrace.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include "math/pi.h"
8 #include "system/line_stream.h"
9 #include "system/log.h"
10 #include "system/lt.h"
11 #include "system/nth_alloc.h"
12 #include "wavy_rect.h"
13
14 #define WAVE_PILLAR_WIDTH 10.0f
15
16 struct Wavy_rect
17 {
18     Lt *lt;
19
20     Rect rect;
21     Color color;
22     float angle;
23 };
24
25 Wavy_rect *create_wavy_rect(Rect rect, Color color)
26 {
27     Lt *lt = create_lt();
28
29     Wavy_rect *wavy_rect = PUSH_LT(lt, nth_calloc(1, sizeof(Wavy_rect)), free);
30     if (wavy_rect == NULL) {
31         RETURN_LT(lt, NULL);
32     }
33
34     wavy_rect->rect = rect;
35     wavy_rect->color = color;
36     wavy_rect->angle = 0.0f;
37     wavy_rect->lt = lt;
38
39     return wavy_rect;
40 }
41
42 void destroy_wavy_rect(Wavy_rect *wavy_rect)
43 {
44     trace_assert(wavy_rect);
45     RETURN_LT0(wavy_rect->lt);
46 }
47
48 int wavy_rect_render(const Wavy_rect *wavy_rect,
49                      const Camera *camera)
50 {
51     trace_assert(wavy_rect);
52     trace_assert(camera);
53
54     srand(42);
55     for (float wave_scanner = 0;
56          wave_scanner < wavy_rect->rect.w;
57          wave_scanner += WAVE_PILLAR_WIDTH) {
58
59         const float s = (float) (rand() % 50) * 0.1f;
60         if (camera_fill_rect(
61                 camera,
62                 rect(
63                     wavy_rect->rect.x + wave_scanner,
64                     wavy_rect->rect.y + s * sinf(wavy_rect->angle + wave_scanner / WAVE_PILLAR_WIDTH),
65                     WAVE_PILLAR_WIDTH * 1.20f,
66                     wavy_rect->rect.h),
67                 wavy_rect->color) < 0) {
68             return -1;
69         }
70     }
71     srand((unsigned int) time(NULL));
72
73     return 0;
74 }
75
76 int wavy_rect_update(Wavy_rect *wavy_rect,
77                      float delta_time)
78 {
79     trace_assert(wavy_rect);
80     wavy_rect->angle = fmodf(wavy_rect->angle + 2.0f * delta_time, 2 * PI);
81
82     return 0;
83 }
84
85 Rect wavy_rect_hitbox(const Wavy_rect *wavy_rect)
86 {
87     return wavy_rect->rect;
88 }