commit b6a82e69043c927059c8367b8d0221fabd812534
parent 2f83d76ccae9a70d9c20518c2d5d1ba92d3f92c9
Author: amrfti <andrew@kloet.net>
Date: Fri, 23 May 2025 10:58:06 -0400
decouple seaweed from entity
Diffstat:
2 files changed, 35 insertions(+), 30 deletions(-)
diff --git a/src/Seaweed.cpp b/src/Seaweed.cpp
@@ -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);
}
}
diff --git a/src/Seaweed.h b/src/Seaweed.h
@@ -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;
};