64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
#include "Seaweed.h"
|
|
#include "../core/Aquarium.h"
|
|
#include "../utils/Random.h"
|
|
#include "../utils/defs.h"
|
|
|
|
Seaweed::Seaweed()
|
|
: Entity(),
|
|
height(Random::intInRange(SEAWEED_MIN_HEIGHT, SEAWEED_MAX_HEIGHT)),
|
|
speed(Random::floatInRange(0.1f, 0.3f)),
|
|
lifetime(Random::intInRange(SEAWEED_MIN_LIFETIME, SEAWEED_MAX_LIFETIME)) {
|
|
|
|
x = Random::intInRange(0, Aquarium::getInstance().getWidth());
|
|
y = Aquarium::getInstance().getHeight() - height;
|
|
|
|
current_image.resize(height);
|
|
current_mask.resize(height);
|
|
generateCurrentFrame();
|
|
}
|
|
|
|
void Seaweed::update() noexcept {
|
|
frame += speed;
|
|
if (frame >= 1.0f) {
|
|
pattern_flipped = !pattern_flipped;
|
|
frame -= 1.0f;
|
|
frame_dirty = true; // mark frame as needing regeneration
|
|
}
|
|
--lifetime;
|
|
}
|
|
|
|
const std::vector<std::string> &Seaweed::getImage() const {
|
|
if (frame_dirty) {
|
|
generateCurrentFrame();
|
|
frame_dirty = false;
|
|
}
|
|
return current_image;
|
|
}
|
|
|
|
const std::vector<std::string> &Seaweed::getMask() const {
|
|
if (frame_dirty) {
|
|
generateCurrentFrame();
|
|
frame_dirty = false;
|
|
}
|
|
return current_mask;
|
|
}
|
|
|
|
void Seaweed::generateCurrentFrame() const {
|
|
for (size_t i = 0; i < height; ++i) {
|
|
const bool use_left = (i % 2 == 0) ^ pattern_flipped;
|
|
const char ch = use_left ? PATTERN_LEFT : PATTERN_RIGHT;
|
|
const int offset = use_left ? 0 : 1;
|
|
|
|
current_image[i] = std::string(offset, ' ') + ch;
|
|
current_mask[i] = std::string(current_image[i].size(), 'g');
|
|
}
|
|
}
|
|
|
|
bool Seaweed::shouldBeRemoved() const noexcept { return lifetime == 0; }
|
|
|
|
std::unique_ptr<Entity> Seaweed::createReplacement() const {
|
|
return std::make_unique<Seaweed>();
|
|
}
|
|
|
|
int Seaweed::getPreferredLayer() const noexcept { return 1; }
|