51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
#include "Waterline.h"
|
|
#include "Aquarium.h"
|
|
#include "Random.h"
|
|
#include "defs.h"
|
|
#include <algorithm>
|
|
|
|
Waterline::Waterline() : x(0), y(WATERLINE_Y) {
|
|
shape = {
|
|
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", "^^^^ ^^^ ^^^ ^^^ ^^^^ ",
|
|
"^^^^ ^^^^ ^^^ ^^ ", "^^ ^^^^ ^^^ ^^^^^^ "};
|
|
|
|
const size_t width = Aquarium::getInstance().getWidth();
|
|
for (auto &line : shape) {
|
|
const std::string original = line;
|
|
while (line.size() < width) {
|
|
line += original;
|
|
}
|
|
colorLines.emplace_back(line.size(), WATERLINE_COLOR);
|
|
}
|
|
}
|
|
|
|
void Waterline::draw() const {
|
|
for (size_t i = 0; i < shape.size(); ++i) {
|
|
Aquarium::getInstance().drawToBackBuffer(y + static_cast<int>(i), x, 0,
|
|
shape[i], colorLines[i]);
|
|
}
|
|
}
|
|
|
|
void Waterline::update() {
|
|
// Skip the first line (index 0) as it's static
|
|
for (size_t i = 1; i < shape.size(); ++i) {
|
|
// Probability increases with depth (higher index = more movement)
|
|
float movementChance =
|
|
static_cast<float>(i) / static_cast<float>(shape.size());
|
|
float threshold = movementChance / WAVE_MOVE_CHANCE;
|
|
|
|
if (Random::floatInRange(0.0f, 1.0f) < threshold) {
|
|
int direction = Random::intInRange(0, 1) == 0 ? -1 : 1;
|
|
shiftString(shape[i], direction);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Waterline::shiftString(std::string &str, int direction) {
|
|
if (direction > 0) {
|
|
std::rotate(str.rbegin(), str.rbegin() + 1, str.rend());
|
|
} else {
|
|
std::rotate(str.begin(), str.begin() + 1, str.end());
|
|
}
|
|
}
|