Added effect "snake".

This commit is contained in:
2019-05-31 05:45:07 +02:00
parent 36663cf067
commit a2fe1461ad
5 changed files with 73 additions and 0 deletions

40
src/effect_snake.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "effect_snake.h"
#include "functions.h"
void SnakeEffect::loop() {
if (run++ % EFFECT_SNAKE_SLOWDOWN) return;
uint8_t r = random8(EFFECT_SNAKE_DIRECTION_CHANGE);
if (r==0 && valid_position(update_position(this->coords, direction+1))) {
direction++;
} else if (r==1 && valid_position(update_position(this->coords, direction-1))) {
direction--;
} else if (!valid_position(update_position(this->coords, direction))) {
if (valid_position(update_position(this->coords, direction+1))) {
direction++;
} else if (valid_position(update_position(this->coords, direction-1))) {
direction--;
}
}
this->coords = update_position(this->coords, direction);
fadeToBlackBy(leds, LED_COUNT, 2);
setPixel(window, this->coords.x, this->coords.y, CHSV(hue, 200, 255));
hue++;
}
Coords SnakeEffect::update_position(Coords original, uint8_t direction) {
direction = direction % 4;
if (direction == 0) original.y--;
else if (direction == 1) original.x++;
else if (direction == 2) original.y++;
else if (direction == 3) original.x--;
return original;
}
boolean SnakeEffect::valid_position(Coords c) {
return c.x<window.w && c.y<window.h;
}
boolean SnakeEffect::can_be_shown_with_clock() { return true; }

View File

@ -14,6 +14,7 @@
#include "effect_twirl.h"
#include "effect_cycle.h"
#include "effect_confetti.h"
#include "effect_snake.h"
SimpleList<EffectEntry>* effects;
SimpleList<Effect*>* cycle_effects;
@ -33,6 +34,7 @@ MatrixEffect effect_matrix;
CycleEffect effect_cycle;
TwirlEffect effect_twirl;
ConfettiEffect effect_confetti;
SnakeEffect effect_snake;
Effect* current_effect;
@ -54,12 +56,14 @@ void setup_effects() {
effects->add((EffectEntry){"twirl", (Effect *)&effect_twirl});
effects->add((EffectEntry){"heart", (Effect*)&effect_anim_heart});
effects->add((EffectEntry){"confetti", (Effect *)&effect_confetti});
effects->add((EffectEntry){"snake", (Effect *)&effect_snake});
cycle_effects->add(&effect_sinematrix3);
cycle_effects->add(&effect_single_dynamic);
cycle_effects->add(&effect_multi_dynamic);
cycle_effects->add(&effect_matrix);
cycle_effects->add(&effect_confetti);
cycle_effects->add(&effect_snake);
current_effect = &effect_cycle;
}