89 lines
2.2 KiB
C++
89 lines
2.2 KiB
C++
#include "corner.h"
|
|
#include "prototypes.h"
|
|
#include "tools.h"
|
|
|
|
Corner::Corner(Edge* new_e1, Edge* new_e2): e1(new_e1), e2(new_e2) {
|
|
|
|
}
|
|
|
|
void Corner::draw() {
|
|
for(uint16_t led: this->_leds) {
|
|
leds[led] = this->color;
|
|
}
|
|
}
|
|
|
|
void Corner::_infect(std::vector<Corner*>* neighbours) {
|
|
for(Corner* c : *neighbours) {
|
|
if (c==nullptr) continue;
|
|
c->blend_to(this->target_color, this->effect_id, effect_speed);
|
|
}
|
|
}
|
|
|
|
void Corner::step() {
|
|
if (this->color_blend_state < 1024) {
|
|
this->color = blend(this->source_color, this->target_color, color_blend_state>>2);
|
|
this->color_blend_state = (this->color_blend_state + effect_speed > 1024) ? 1024 : this->color_blend_state + effect_speed;
|
|
if (this->color_blend_state==1024) {
|
|
this->color = this->target_color;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Corner::infect(uint16_t infect_short_level, uint16_t infect_long_level) {
|
|
if (reached_level(infect_short_level)) {
|
|
_infect(&_short_neighbours);
|
|
}
|
|
if (reached_level(infect_long_level)) {
|
|
_infect(&_long_neighbours);
|
|
}
|
|
}
|
|
|
|
bool Corner::reached_level(uint16_t level) {
|
|
if (color_blend_state >= 1024) return false;
|
|
uint16_t old_cbs = color_blend_state >= effect_speed ? color_blend_state - effect_speed : 0;
|
|
return (old_cbs < level && color_blend_state >= level);
|
|
}
|
|
|
|
void Corner::blend_to(CRGB target, uint16_t eid, uint8_t effect_speed) {
|
|
//LOGln("blendTo called. Corner: %p, target: %d,%d,%d, eid: %d, 'old' effect_id: %d, speed: %d", this, target.r, target.g, target.b, eid, effect_id, effect_speed);
|
|
if (eid==0) {
|
|
this->effect_id = random16();
|
|
//LOGln("Set effect_id to %d", this->effect_id);
|
|
} else {
|
|
if (this->effect_id == eid) {
|
|
//LOGln("'Old' effect. Doing nothing.");
|
|
return;
|
|
}
|
|
this->effect_id = eid;
|
|
}
|
|
|
|
if (effect_speed==0) {
|
|
// Effect speed 2 - 7.
|
|
// Speed 7 @ 50fps => 3 seconds
|
|
effect_speed = random8(6) + 2;
|
|
}
|
|
this->effect_speed = effect_speed;
|
|
|
|
this->source_color = this->color;
|
|
this->target_color = target;
|
|
this->color_blend_state = 0;
|
|
}
|
|
|
|
void Corner::add_led(uint16_t led) {
|
|
_leds.push_back(led);
|
|
}
|
|
|
|
void Corner::merge_leds(Corner* c) {
|
|
for(uint16_t led : c->_leds) {
|
|
_leds.push_back(led);
|
|
}
|
|
}
|
|
|
|
bool Corner::is_finished() {
|
|
return color_blend_state < 1024;
|
|
}
|
|
|
|
void Corner::set_color(CRGB color) {
|
|
this->color = color;
|
|
this->color_blend_state = 1024;
|
|
} |