refactor waterline

This commit is contained in:
2025-05-22 21:55:44 -04:00
committed by laugos
parent 96b995bbd9
commit 86b6811c44
2 changed files with 21 additions and 10 deletions

View File

@@ -3,9 +3,8 @@
#include "Random.h"
#include "defs.h"
#include <algorithm>
#include <ncurses.h>
Waterline::Waterline() : x(0), y(5) {
Waterline::Waterline() : x(0), y(WATERLINE_Y) {
shape = {
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", "^^^^ ^^^ ^^^ ^^^ ^^^^ ",
"^^^^ ^^^^ ^^^ ^^ ", "^^ ^^^^ ^^^ ^^^^^^ "};
@@ -16,21 +15,26 @@ Waterline::Waterline() : x(0), y(5) {
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(i + y, x, 0, shape[i],
std::string(shape[i].size(), 'c'));
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 line index (later lines = higher chance)
float chance = static_cast<float>(i) / shape.size();
if (Random::floatInRange(0.0f, 1.0f) < chance * (1.0f / WAVE_MOVE_CHANCE)) {
// 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);
}
@@ -38,7 +42,7 @@ void Waterline::update() {
}
void Waterline::shiftString(std::string &str, int direction) {
if (direction == 1) {
if (direction > 0) {
std::rotate(str.rbegin(), str.rbegin() + 1, str.rend());
} else {
std::rotate(str.begin(), str.begin() + 1, str.end());

View File

@@ -1,11 +1,18 @@
#pragma once
#include "Entity.h"
#include <string>
#include <vector>
class Waterline {
private:
static constexpr int WATERLINE_Y = 5;
static constexpr char WATERLINE_COLOR = 'c';
size_t x, y;
std::vector<std::string> shape;
void shiftString(std::string &, int direction);
std::vector<std::string> colorLines;
void shiftString(std::string &str, int direction);
void initializeShape();
public:
Waterline();