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

View File

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