refactor fish
This commit is contained in:
53
src/Fish.cpp
53
src/Fish.cpp
@@ -1,16 +1,16 @@
|
||||
#include "Fish.h"
|
||||
#include "Aquarium.h"
|
||||
#include "FishAssets.h"
|
||||
#include "Random.h"
|
||||
#include "assets/FishAssets.h"
|
||||
#include <ncurses.h>
|
||||
|
||||
Fish::Fish() : Fish(getRandomFishPair()) {}
|
||||
Fish::Fish() : Fish(getRandomFishAsset()) {}
|
||||
|
||||
Fish::Fish(const FishAssetPair &pair)
|
||||
Fish::Fish(const FishAssetRef &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) {
|
||||
image(pair.asset.image), mask(pair.asset.mask) {
|
||||
y = Random::intInRange(image.size() + 6,
|
||||
Aquarium::getInstance().getHeight() - image.size());
|
||||
|
||||
@@ -19,50 +19,29 @@ Fish::Fish(const FishAssetPair &pair)
|
||||
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;
|
||||
std::unordered_map<char, char> colorMap{{'4', 'W'}};
|
||||
|
||||
// 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)];
|
||||
if (digit != '4') {
|
||||
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];
|
||||
for (char &ch : line) {
|
||||
if (auto it = colorMap.find(ch); it != colorMap.end()) {
|
||||
ch = it->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Fish::FishAssetPair Fish::getRandomFishPair() {
|
||||
if (!initialized)
|
||||
initializeFishAssets();
|
||||
|
||||
int index = Random::intInRange(0, fishPairs.size() - 1);
|
||||
|
||||
return FishAssetPair{index, &fishPairs[index].first,
|
||||
&fishPairs[index].second};
|
||||
Fish::FishAssetRef Fish::getRandomFishAsset() {
|
||||
int index =
|
||||
Random::intInRange(0, static_cast<int>(fishAssetPairs.size()) - 1);
|
||||
return FishAssetRef{index, fishAssetPairs[index]};
|
||||
}
|
||||
|
||||
void Fish::update() { x += speed; }
|
||||
|
||||
Reference in New Issue
Block a user