2019-05-31 03:45:07 +00:00
|
|
|
#include "effect_snake.h"
|
|
|
|
#include "functions.h"
|
|
|
|
|
2019-06-04 03:57:27 +00:00
|
|
|
SnakeEffect::SnakeEffect() {
|
|
|
|
this->coords = {0, 0};
|
2019-06-11 17:48:09 +00:00
|
|
|
this->window = new Window(0, 0, LED_WIDTH, LED_HEIGHT-6);
|
2019-06-04 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 20:23:49 +00:00
|
|
|
SnakeEffect::~SnakeEffect() {
|
|
|
|
delete window;
|
|
|
|
}
|
|
|
|
|
2019-05-31 03:45:07 +00:00
|
|
|
void SnakeEffect::loop() {
|
2019-05-31 21:58:58 +00:00
|
|
|
if (run++ % EFFECT_SNAKE_SLOWDOWN == 0) { // Change the coordinates only on every n-th run.
|
2019-06-04 03:57:27 +00:00
|
|
|
if (random8(EFFECT_SNAKE_DIRECTION_CHANGE)==0 || is_turn_needed()) turn_random();
|
|
|
|
|
|
|
|
this->coords = update_position(this->coords, this->direction);
|
2019-05-31 21:58:58 +00:00
|
|
|
}
|
2019-05-31 03:45:07 +00:00
|
|
|
|
2019-06-11 17:48:09 +00:00
|
|
|
window->fadeToBlackBy(2);
|
|
|
|
CRGB color(CHSV(hue, 200, 255));
|
|
|
|
window->setPixel(this->coords.x, this->coords.y, &color);
|
2019-05-31 03:45:07 +00:00
|
|
|
hue++;
|
|
|
|
}
|
|
|
|
|
2019-06-04 03:57:27 +00:00
|
|
|
void SnakeEffect::turn_random() {
|
|
|
|
if ((random8() & 1) == 0) {
|
|
|
|
turn_right() || turn_left();
|
|
|
|
} else {
|
|
|
|
turn_left() || turn_right();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SnakeEffect::turn_left() {
|
|
|
|
if (!is_direction_okay(this->direction - 1)) return false;
|
|
|
|
this->direction--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SnakeEffect::turn_right() {
|
|
|
|
if (!is_direction_okay(this->direction + 1)) return false;
|
|
|
|
this->direction++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SnakeEffect::is_turn_needed() {
|
|
|
|
return !is_direction_okay(this->direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SnakeEffect::is_direction_okay(uint8_t dir) {
|
|
|
|
Coords c = update_position(this->coords, dir);
|
2019-06-11 17:48:09 +00:00
|
|
|
return c.x<window->width && c.y<window->height;
|
2019-06-04 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 03:45:07 +00:00
|
|
|
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::can_be_shown_with_clock() { return true; }
|