decouple seaweed from entity

This commit is contained in:
2025-05-23 10:58:06 -04:00
parent 4530b45cca
commit dc560109b6
2 changed files with 35 additions and 30 deletions

View File

@@ -4,38 +4,37 @@
#include "defs.h"
#include <ncurses.h>
Seaweed::Seaweed() : Entity() {
speed = Random::floatInRange(0.1f, 0.3f);
height = Random::intInRange(SEAWEED_MIN_HEIGHT, SEAWEED_MAX_HEIGHT);
x = Random::intInRange(0, Aquarium::getInstance().getWidth());
y = Aquarium::getInstance().getHeight() - 1;
lifetime = Random::intInRange(SEAWEED_MIN_LIFETIME, SEAWEED_MAX_LIFETIME);
Seaweed::Seaweed()
: x(Random::intInRange(0, Aquarium::getInstance().getWidth())),
y(Aquarium::getInstance().getHeight()),
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)) {
}
void Seaweed::update() {
void Seaweed::update() noexcept {
frame += speed;
if (frame >= 1.0f) {
std::swap(pattern[0], pattern[1]);
pattern_flipped = !pattern_flipped;
frame -= 1.0f;
}
--lifetime;
}
void Seaweed::draw() {
std::string line;
std::string colorLine;
void Seaweed::draw() const {
auto &aquarium = Aquarium::getInstance();
for (int i = 0; i < height; ++i) {
line.clear();
char ch = (pattern[i % 2] == '(') ? '(' : ')';
std::string line(1, '\0');
std::string colorLine(1, 'g');
// Adjust x and y based on the pattern
int drawX = (ch == '(') ? x : x + 1;
int drawY = y - i;
for (size_t i = 0; i < height; ++i) {
// Determine character and position for this segment
const bool use_left = (i % 2 == 0) ^ pattern_flipped;
const char ch = use_left ? PATTERN_LEFT : PATTERN_RIGHT;
const int drawX = static_cast<int>(x) + (use_left ? 0 : 1);
const int drawY = y - static_cast<int>(i);
line.push_back(ch);
colorLine.push_back('g');
Aquarium::getInstance().drawToBackBuffer(drawY, drawX, 0, line, colorLine);
line[0] = ch;
aquarium.drawToBackBuffer(drawY, drawX, 0, line, colorLine);
}
}

View File

@@ -1,18 +1,24 @@
#pragma once
#include "Entity.h"
#include <cstddef>
class Seaweed : public Entity {
class Seaweed {
private:
char pattern[2] = {'(', ')'};
const int y;
static constexpr char PATTERN_LEFT = '(';
static constexpr char PATTERN_RIGHT = ')';
float speed, frame = 0;
int lifetime;
int height;
const size_t x;
const size_t height;
const float speed;
float frame = 0.0f;
size_t lifetime;
bool pattern_flipped = false;
public:
Seaweed();
int getLifetime() { return lifetime; };
void update();
void draw();
size_t getLifetime() const noexcept { return lifetime; }
void update() noexcept;
void draw() const;
};