refactor waterline
This commit is contained in:
@@ -3,9 +3,8 @@
|
|||||||
#include "Random.h"
|
#include "Random.h"
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ncurses.h>
|
|
||||||
|
|
||||||
Waterline::Waterline() : x(0), y(5) {
|
Waterline::Waterline() : x(0), y(WATERLINE_Y) {
|
||||||
shape = {
|
shape = {
|
||||||
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", "^^^^ ^^^ ^^^ ^^^ ^^^^ ",
|
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", "^^^^ ^^^ ^^^ ^^^ ^^^^ ",
|
||||||
"^^^^ ^^^^ ^^^ ^^ ", "^^ ^^^^ ^^^ ^^^^^^ "};
|
"^^^^ ^^^^ ^^^ ^^ ", "^^ ^^^^ ^^^ ^^^^^^ "};
|
||||||
@@ -16,21 +15,26 @@ Waterline::Waterline() : x(0), y(5) {
|
|||||||
while (line.size() < width) {
|
while (line.size() < width) {
|
||||||
line += original;
|
line += original;
|
||||||
}
|
}
|
||||||
|
colorLines.emplace_back(line.size(), WATERLINE_COLOR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Waterline::draw() const {
|
void Waterline::draw() const {
|
||||||
for (size_t i = 0; i < shape.size(); ++i) {
|
for (size_t i = 0; i < shape.size(); ++i) {
|
||||||
Aquarium::getInstance().drawToBackBuffer(i + y, x, 0, shape[i],
|
Aquarium::getInstance().drawToBackBuffer(y + static_cast<int>(i), x, 0,
|
||||||
std::string(shape[i].size(), 'c'));
|
shape[i], colorLines[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Waterline::update() {
|
void Waterline::update() {
|
||||||
|
// Skip the first line (index 0) as it's static
|
||||||
for (size_t i = 1; i < shape.size(); ++i) {
|
for (size_t i = 1; i < shape.size(); ++i) {
|
||||||
// Probability increases with line index (later lines = higher chance)
|
// Probability increases with depth (higher index = more movement)
|
||||||
float chance = static_cast<float>(i) / shape.size();
|
float movementChance =
|
||||||
if (Random::floatInRange(0.0f, 1.0f) < chance * (1.0f / WAVE_MOVE_CHANCE)) {
|
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;
|
int direction = Random::intInRange(0, 1) == 0 ? -1 : 1;
|
||||||
shiftString(shape[i], direction);
|
shiftString(shape[i], direction);
|
||||||
}
|
}
|
||||||
@@ -38,7 +42,7 @@ void Waterline::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Waterline::shiftString(std::string &str, int direction) {
|
void Waterline::shiftString(std::string &str, int direction) {
|
||||||
if (direction == 1) {
|
if (direction > 0) {
|
||||||
std::rotate(str.rbegin(), str.rbegin() + 1, str.rend());
|
std::rotate(str.rbegin(), str.rbegin() + 1, str.rend());
|
||||||
} else {
|
} else {
|
||||||
std::rotate(str.begin(), str.begin() + 1, str.end());
|
std::rotate(str.begin(), str.begin() + 1, str.end());
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Entity.h"
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class Waterline {
|
class Waterline {
|
||||||
private:
|
private:
|
||||||
|
static constexpr int WATERLINE_Y = 5;
|
||||||
|
static constexpr char WATERLINE_COLOR = 'c';
|
||||||
|
|
||||||
size_t x, y;
|
size_t x, y;
|
||||||
std::vector<std::string> shape;
|
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:
|
public:
|
||||||
Waterline();
|
Waterline();
|
||||||
|
|||||||
Reference in New Issue
Block a user