🌠 create a vector so we can spawn new drops dynamically :3

This commit is contained in:
Felix Pankratz 2024-12-13 19:22:44 +01:00
parent 8d51a0a036
commit f0836c66f0

View File

@ -1,6 +1,7 @@
#include "rain.h"
#include "constants.h"
#include "overlay.h"
#include <vector>
#define COLOR_HIGHLIGHT_R 128
#define COLOR_HIGHLIGHT_G 255
@ -10,19 +11,21 @@
#define COLOR_DEFAULT_G 255
#define COLOR_DEFAULT_B 0
typedef struct {
bool reached_bottom = false;
//typedef struct {
struct Raindrop {
//bool reached_bottom = false;
bool reached_bottom;
int x;
int y;
int length;
int speed;
unsigned long last_moved;
} Raindrop;
} ;
// TODO: Use a dynamic vector instead
Raindrop raindrops[PANEL_WIDTH];
bool rain_initialized = false;
uint16_t color_highlight;
uint16_t color_default;
std::vector<Raindrop> raindrops;
void setup_rain(MatrixPanel_I2S_DMA *matrix) {
color_highlight = matrix->color565(
@ -35,11 +38,15 @@ void setup_rain(MatrixPanel_I2S_DMA *matrix) {
COLOR_DEFAULT_B,
COLOR_DEFAULT_G
);
for (int i = 0; i<PANEL_WIDTH; i++) {
raindrops[i].y = 0 - random(24);
raindrops[i].length = random(20, 28);
raindrops[i].speed = random(25, 100);
raindrops[i].last_moved = millis();
for (int x = 0; x<PANEL_WIDTH; x++) {
raindrops.push_back({
false,
x,
0 - random(24),
random(20, 28),
random(25, 100),
millis()
});
}
}
@ -50,19 +57,28 @@ void draw_rain(MatrixPanel_I2S_DMA *matrix, Mode mode) {
rain_initialized = true;
}
unsigned long timestamp = millis();
for (int x = 0; x < PANEL_WIDTH; x++) {
int x = 0;
while(x < raindrops.size()) {
if ((timestamp - raindrops[x].last_moved) > raindrops[x].speed) {
// step down a pixel
raindrops[x].y++;
raindrops[x].last_moved = timestamp;
}
if (raindrops[x].y > PANEL_HEIGHT) {
raindrops[x].y = 0;
raindrops[x].reached_bottom = true;
if (raindrops[x].y > PANEL_HEIGHT + raindrops[x].length) {
raindrops.push_back({
false,
raindrops[x].x,
0 - random(8),
random(20, 28),
random(0, 76),
millis()
});
raindrops.erase(raindrops.begin() + x);
continue;
}
if (raindrops[x].y >= 0) {
//draw our pixel
matrix->drawPixel(x, raindrops[x].y, matrix->color565(128, 255, 128));
matrix->drawPixel(raindrops[x].x, raindrops[x].y, matrix->color565(128, 255, 128));
// draw our trail
for (int trail_offset = 1; trail_offset < raindrops[x].length; trail_offset++) {
int trail_y = raindrops[x].y - trail_offset;
@ -74,28 +90,25 @@ void draw_rain(MatrixPanel_I2S_DMA *matrix, Mode mode) {
);
if (trail_y < 0) {
if (not raindrops[x].reached_bottom) {
break;
}
// we need to count from the bottom up
trail_y = PANEL_HEIGHT + trail_y;
break;
}
int pixel_num = (trail_y * PANEL_WIDTH) + x;
int pixel_num = (trail_y * PANEL_WIDTH) + raindrops[x].x;
if (overlay[pixel_num / 8] & (1 << (7 - (pixel_num % 8))) and mode != Stealth) {
if (mode == HighVis) {
matrix->drawPixel(x, trail_y, color_highlight);
matrix->drawPixel(raindrops[x].x, trail_y, color_highlight);
} else {
matrix->drawPixel(x, trail_y, matrix->color565(
matrix->drawPixel(raindrops[x].x, trail_y, matrix->color565(
COLOR_HIGHLIGHT_R * brightness,
COLOR_HIGHLIGHT_G * brightness,
COLOR_HIGHLIGHT_B * brightness
));
}
} else {
matrix->drawPixel(x, trail_y, fade_color);
matrix->drawPixel(raindrops[x].x, trail_y, fade_color);
}
}
}
x++;
}
}