initial commit
This commit is contained in:
105
src/Fish.cpp
Normal file
105
src/Fish.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "Fish.h"
|
||||
#include "Aquarium.h"
|
||||
#include "FishAssets.h"
|
||||
#include "Random.h"
|
||||
#include <ncurses.h>
|
||||
|
||||
Fish::Fish() : Fish(getRandomFishPair()) {}
|
||||
|
||||
Fish::Fish(const FishAssetPair &pair)
|
||||
: Entity(),
|
||||
speed((pair.index % 2 == 0) ? Random::floatInRange(0.25, 2.25)
|
||||
: -Random::floatInRange(0.25, 2.25)),
|
||||
image(*pair.image), refMask(*pair.mask) {
|
||||
y = Random::intInRange(image.size() + 6,
|
||||
Aquarium::getInstance().getHeight() - image.size());
|
||||
|
||||
x = (speed < 0) ? Aquarium::getInstance().getWidth() : -20;
|
||||
|
||||
randomizeMask();
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::vector<std::string>, std::vector<std::string>>>
|
||||
Fish::fishPairs;
|
||||
|
||||
bool Fish::initialized = false;
|
||||
|
||||
void Fish::initializeFishAssets() {
|
||||
if (initialized)
|
||||
return;
|
||||
fishPairs = fishAssetPairs;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
void Fish::randomizeMask() {
|
||||
// Create a mapping of digit to color
|
||||
std::unordered_map<char, char> colorMap;
|
||||
mask = refMask;
|
||||
|
||||
// For each digit 1-9, assign a random color
|
||||
for (char digit = '1'; digit <= '9'; ++digit) {
|
||||
colorMap[digit] =
|
||||
availableColors[Random::intInRange(0, availableColors.size() - 1)];
|
||||
}
|
||||
|
||||
// Special case: '4' always maps to 'W'
|
||||
colorMap['4'] = 'W';
|
||||
|
||||
// Apply the color mapping to each character in the mask
|
||||
for (auto &line : mask) {
|
||||
for (auto &ch : line) {
|
||||
if (ch >= '1' && ch <= '9') {
|
||||
ch = colorMap[ch];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Fish::FishAssetPair Fish::getRandomFishPair() {
|
||||
if (!initialized)
|
||||
initializeFishAssets();
|
||||
|
||||
int index = Random::intInRange(0, fishPairs.size() - 1);
|
||||
|
||||
return FishAssetPair{index, &fishPairs[index].first,
|
||||
&fishPairs[index].second};
|
||||
}
|
||||
|
||||
void Fish::update() { x += speed; }
|
||||
|
||||
void Fish::draw(int layer) {
|
||||
Aquarium &aq = Aquarium::getInstance();
|
||||
|
||||
for (size_t i = 0; i < image.size(); ++i) {
|
||||
const std::string &row = image[i];
|
||||
const std::string &maskRow = (i < mask.size()) ? mask[i] : "";
|
||||
|
||||
int baseY = y + static_cast<int>(i);
|
||||
int cursorX = static_cast<int>(x);
|
||||
std::string currentSegment;
|
||||
std::string currentColors;
|
||||
|
||||
for (size_t j = 0; j < row.size(); ++j) {
|
||||
char ch = row[j];
|
||||
|
||||
if (ch == '?') {
|
||||
if (!currentSegment.empty()) {
|
||||
aq.drawToBackBuffer(baseY, cursorX, layer, currentSegment,
|
||||
currentColors);
|
||||
cursorX += currentSegment.size();
|
||||
currentSegment.clear();
|
||||
currentColors.clear();
|
||||
}
|
||||
cursorX += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
currentSegment.push_back(ch);
|
||||
currentColors.push_back((j < maskRow.size()) ? maskRow[j] : 'k');
|
||||
}
|
||||
|
||||
if (!currentSegment.empty()) {
|
||||
aq.drawToBackBuffer(baseY, cursorX, layer, currentSegment, currentColors);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user