From 86b6811c4410a94a0771cea0e75e0c34ec758247 Mon Sep 17 00:00:00 2001 From: Andrew Kloet Date: Thu, 22 May 2025 21:55:44 -0400 Subject: [PATCH] refactor waterline --- src/Waterline.cpp | 20 ++++++++++++-------- src/Waterline.h | 11 +++++++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Waterline.cpp b/src/Waterline.cpp index f0648af..0fae247 100644 --- a/src/Waterline.cpp +++ b/src/Waterline.cpp @@ -3,9 +3,8 @@ #include "Random.h" #include "defs.h" #include -#include -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(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(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(i) / static_cast(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()); diff --git a/src/Waterline.h b/src/Waterline.h index c05d9c5..fff1493 100644 --- a/src/Waterline.h +++ b/src/Waterline.h @@ -1,11 +1,18 @@ #pragma once -#include "Entity.h" +#include +#include class Waterline { private: + static constexpr int WATERLINE_Y = 5; + static constexpr char WATERLINE_COLOR = 'c'; + size_t x, y; std::vector shape; - void shiftString(std::string &, int direction); + std::vector colorLines; + + void shiftString(std::string &str, int direction); + void initializeShape(); public: Waterline();