refactor bubble

This commit is contained in:
2025-05-22 21:47:25 -04:00
committed by laugos
parent d29689e1b2
commit 96b995bbd9
4 changed files with 26 additions and 27 deletions

View File

@@ -91,7 +91,7 @@ void Aquarium::redraw() {
auto &bubble = *it; auto &bubble = *it;
bubble->draw(); bubble->draw();
bubble->update(); bubble->update();
if (bubble->getY() < 9) if (bubble->isOutOfWater())
it = bubbles.erase(it); it = bubbles.erase(it);
else else
++it; ++it;
@@ -137,7 +137,7 @@ void Aquarium::redraw() {
applyBackBuffer(); 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)); bubbles.emplace_back(std::make_unique<Bubble>(x, y));
} }

View File

@@ -80,7 +80,7 @@ public:
[[nodiscard]] int getWidth() const { return width; } [[nodiscard]] int getWidth() const { return width; }
[[nodiscard]] int getHeight() const { return height; } [[nodiscard]] int getHeight() const { return height; }
void addFish(); void addFish();
void addBubble(float x, float y); void addBubble(size_t x, size_t y);
void addSeaweed(); void addSeaweed();
void addWaterline(); void addWaterline();
void addCastle(); void addCastle();

View File

@@ -2,25 +2,17 @@
#include "Aquarium.h" #include "Aquarium.h"
#include <ncurses.h> #include <ncurses.h>
Bubble::Bubble(float x, float y) : Entity() { Bubble::Bubble(size_t x, size_t y) : x(x), y(y) {}
this->x = x;
this->y = y; void Bubble::update() {
--y;
++lifetime;
} }
void Bubble::update() { y -= 1; } void Bubble::draw() const {
static const std::string colorString(1, BUBBLE_COLOR);
void Bubble::draw() { // Clamp frame index
lifetime++; int frameIndex = std::min(lifetime / FRAMES_PER_ANIMATION, MAX_FRAME_INDEX);
Aquarium::getInstance().drawToBackBuffer(y, x, 0, BUBBLE_FRAMES[frameIndex],
// Determine the frame based on lifetime colorString);
int frameNumber = lifetime / 9;
if (frameNumber > 2)
frameNumber = 2;
char frame = bubbleChars[frameNumber];
std::string line(1, frame);
std::string colorLine(1, 'c');
Aquarium::getInstance().drawToBackBuffer(y, x, 0, line, colorLine);
} }

View File

@@ -1,14 +1,21 @@
#pragma once #pragma once
#include "Entity.h" #include <cstddef>
#include <string>
class Bubble : public Entity { class Bubble {
private: 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; int lifetime = 0;
public: public:
Bubble(float x, float y); Bubble(size_t x, size_t y);
bool isOutOfWater() const { return y < 5; }
void update(); void update();
void draw(); void draw() const;
}; };