diff --git a/src/Seaweed.cpp b/src/Seaweed.cpp index 5a71115..f20a82a 100644 --- a/src/Seaweed.cpp +++ b/src/Seaweed.cpp @@ -4,38 +4,37 @@ #include "defs.h" #include -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(x) + (use_left ? 0 : 1); + const int drawY = y - static_cast(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 index 5f62e00..074721d 100644 --- a/src/Seaweed.h +++ b/src/Seaweed.h @@ -1,18 +1,24 @@ #pragma once -#include "Entity.h" +#include -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; };