fissh

termios terminal aquarium. demo at ssh://fish@kloet.net
Download | Log | Files | Refs

commit 7c965fe613a12acf5efdb0b6ab51e0b8810ed906
parent e78ee156f429c2b87d03200c42517ace262ec2d0
Author: amrfti <andrew@kloet.net>
Date:   Thu, 22 May 2025 21:47:25 -0400

refactor bubble

Diffstat:
Msrc/Aquarium.cpp | 4++--
Msrc/Aquarium.h | 2+-
Msrc/Bubble.cpp | 30+++++++++++-------------------
Msrc/Bubble.h | 17++++++++++++-----
4 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/src/Aquarium.cpp b/src/Aquarium.cpp @@ -91,7 +91,7 @@ void Aquarium::redraw() { auto &bubble = *it; bubble->draw(); bubble->update(); - if (bubble->getY() < 9) + if (bubble->isOutOfWater()) it = bubbles.erase(it); else ++it; @@ -137,7 +137,7 @@ void Aquarium::redraw() { applyBackBuffer(); } -void Aquarium::addBubble(float x, float y) { +void Aquarium::addBubble(size_t x, size_t y) { bubbles.emplace_back(std::make_unique<Bubble>(x, y)); } diff --git a/src/Aquarium.h b/src/Aquarium.h @@ -80,7 +80,7 @@ public: [[nodiscard]] int getWidth() const { return width; } [[nodiscard]] int getHeight() const { return height; } void addFish(); - void addBubble(float x, float y); + void addBubble(size_t x, size_t y); void addSeaweed(); void addWaterline(); void addCastle(); diff --git a/src/Bubble.cpp b/src/Bubble.cpp @@ -2,25 +2,17 @@ #include "Aquarium.h" #include <ncurses.h> -Bubble::Bubble(float x, float y) : Entity() { - this->x = x; - this->y = y; -} - -void Bubble::update() { y -= 1; } - -void Bubble::draw() { - lifetime++; +Bubble::Bubble(size_t x, size_t y) : x(x), y(y) {} - // Determine the frame based on lifetime - int frameNumber = lifetime / 9; - if (frameNumber > 2) - frameNumber = 2; - - char frame = bubbleChars[frameNumber]; - - std::string line(1, frame); - std::string colorLine(1, 'c'); +void Bubble::update() { + --y; + ++lifetime; +} - Aquarium::getInstance().drawToBackBuffer(y, x, 0, line, colorLine); +void Bubble::draw() const { + static const std::string colorString(1, BUBBLE_COLOR); + // Clamp frame index + int frameIndex = std::min(lifetime / FRAMES_PER_ANIMATION, MAX_FRAME_INDEX); + Aquarium::getInstance().drawToBackBuffer(y, x, 0, BUBBLE_FRAMES[frameIndex], + colorString); } diff --git a/src/Bubble.h b/src/Bubble.h @@ -1,14 +1,21 @@ #pragma once -#include "Entity.h" +#include <cstddef> +#include <string> -class Bubble : public Entity { +class Bubble { private: - static constexpr char bubbleChars[3] = {'.', 'o', 'O'}; + static constexpr const char *BUBBLE_FRAMES[3] = {".", "o", "O"}; + static constexpr int FRAMES_PER_ANIMATION = 9; + static constexpr int MAX_FRAME_INDEX = 2; + static constexpr char BUBBLE_COLOR = 'c'; + size_t x, y; int lifetime = 0; public: - Bubble(float x, float y); + Bubble(size_t x, size_t y); + + bool isOutOfWater() const { return y < 5; } void update(); - void draw(); + void draw() const; };