SnakeEffect now turns into a random direction when hitting a wall. The code is now also a whole lot cleaner.

This commit is contained in:
Fabian Schlenz 2019-06-04 05:57:27 +02:00
parent 94687bab36
commit 96442b48cc
2 changed files with 44 additions and 20 deletions

View File

@ -7,11 +7,17 @@
class SnakeEffect : public Effect { class SnakeEffect : public Effect {
private: private:
Coords coords; Coords coords;
uint8_t direction; uint8_t direction = 1;
uint8_t hue; uint8_t hue = 0;
uint8_t run; uint8_t run = 0;
Window window = {0, 0, LED_WIDTH, LED_HEIGHT-6}; Window window = {0, 0, LED_WIDTH, LED_HEIGHT-6};
bool is_turn_needed();
void turn_random();
bool turn_right();
bool turn_left();
bool is_direction_okay(uint8_t direction);
public: public:
SnakeEffect();
void loop(); void loop();
boolean valid_position(Coords c); boolean valid_position(Coords c);
Coords update_position(Coords c, uint8_t direction); Coords update_position(Coords c, uint8_t direction);

View File

@ -1,22 +1,15 @@
#include "effect_snake.h" #include "effect_snake.h"
#include "functions.h" #include "functions.h"
SnakeEffect::SnakeEffect() {
this->coords = {0, 0};
}
void SnakeEffect::loop() { void SnakeEffect::loop() {
if (run++ % EFFECT_SNAKE_SLOWDOWN == 0) { // Change the coordinates only on every n-th run. if (run++ % EFFECT_SNAKE_SLOWDOWN == 0) { // Change the coordinates only on every n-th run.
uint8_t r = random8(EFFECT_SNAKE_DIRECTION_CHANGE); if (random8(EFFECT_SNAKE_DIRECTION_CHANGE)==0 || is_turn_needed()) turn_random();
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); this->coords = update_position(this->coords, this->direction);
} }
fadeToBlackBy(leds, LED_COUNT, 2); fadeToBlackBy(leds, LED_COUNT, 2);
@ -24,6 +17,35 @@ void SnakeEffect::loop() {
hue++; hue++;
} }
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);
return c.x<window.w && c.y<window.h;
}
Coords SnakeEffect::update_position(Coords original, uint8_t direction) { Coords SnakeEffect::update_position(Coords original, uint8_t direction) {
direction = direction % 4; direction = direction % 4;
if (direction == 0) original.y--; if (direction == 0) original.y--;
@ -33,8 +55,4 @@ Coords SnakeEffect::update_position(Coords original, uint8_t direction) {
return original; 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; } boolean SnakeEffect::can_be_shown_with_clock() { return true; }