refactor fish

This commit is contained in:
2025-05-22 21:32:08 -04:00
committed by laugos
parent 27c81ebef1
commit d29689e1b2
3 changed files with 76 additions and 100 deletions

View File

@@ -1,16 +1,16 @@
#include "Fish.h" #include "Fish.h"
#include "Aquarium.h" #include "Aquarium.h"
#include "FishAssets.h"
#include "Random.h" #include "Random.h"
#include "assets/FishAssets.h"
#include <ncurses.h> #include <ncurses.h>
Fish::Fish() : Fish(getRandomFishPair()) {} Fish::Fish() : Fish(getRandomFishAsset()) {}
Fish::Fish(const FishAssetPair &pair) Fish::Fish(const FishAssetRef &pair)
: Entity(), : Entity(),
speed((pair.index % 2 == 0) ? Random::floatInRange(0.25, 2.25) speed((pair.index % 2 == 0) ? Random::floatInRange(0.25, 2.25)
: -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, y = Random::intInRange(image.size() + 6,
Aquarium::getInstance().getHeight() - image.size()); Aquarium::getInstance().getHeight() - image.size());
@@ -19,50 +19,29 @@ Fish::Fish(const FishAssetPair &pair)
randomizeMask(); 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() { void Fish::randomizeMask() {
// Create a mapping of digit to color std::unordered_map<char, char> colorMap{{'4', 'W'}};
std::unordered_map<char, char> colorMap;
mask = refMask;
// For each digit 1-9, assign a random color
for (char digit = '1'; digit <= '9'; ++digit) { for (char digit = '1'; digit <= '9'; ++digit) {
colorMap[digit] = if (digit != '4') {
availableColors[Random::intInRange(0, availableColors.size() - 1)]; 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 &line : mask) {
for (auto &ch : line) { for (char &ch : line) {
if (ch >= '1' && ch <= '9') { if (auto it = colorMap.find(ch); it != colorMap.end()) {
ch = colorMap[ch]; ch = it->second;
} }
} }
} }
} }
Fish::FishAssetPair Fish::getRandomFishPair() { Fish::FishAssetRef Fish::getRandomFishAsset() {
if (!initialized) int index =
initializeFishAssets(); Random::intInRange(0, static_cast<int>(fishAssetPairs.size()) - 1);
return FishAssetRef{index, fishAssetPairs[index]};
int index = Random::intInRange(0, fishPairs.size() - 1);
return FishAssetPair{index, &fishPairs[index].first,
&fishPairs[index].second};
} }
void Fish::update() { x += speed; } void Fish::update() { x += speed; }

View File

@@ -1,31 +1,26 @@
#pragma once #pragma once
#include "Entity.h" #include "Entity.h"
#include "assets/FishAssets.h"
#include <array> #include <array>
class Fish : public Entity { class Fish : public Entity {
private: private:
struct FishAssetPair { struct FishAssetRef {
int index; int index;
const std::vector<std::string> *image; FishAsset &asset;
const std::vector<std::string> *mask;
}; };
Fish(const FishAssetPair &pair); Fish(const FishAssetRef &ref);
static std::vector<
std::pair<std::vector<std::string>, std::vector<std::string>>>
fishPairs;
static bool initialized; static bool initialized;
const std::vector<std::string> &image; const std::vector<std::string> &image;
const std::vector<std::string> &refMask; std::vector<std::string> &mask;
std::vector<std::string> mask;
static constexpr std::array<char, 12> availableColors = { static constexpr std::array<char, 12> availableColors = {
'c', 'C', 'r', 'R', 'y', 'Y', 'b', 'B', 'g', 'G', 'm', 'M'}; 'c', 'C', 'r', 'R', 'y', 'Y', 'b', 'B', 'g', 'G', 'm', 'M'};
const float speed; const float speed;
static FishAssetPair getRandomFishPair(); static FishAssetRef getRandomFishAsset();
static void initializeFishAssets(); static void initializeFishAssets();
void randomizeMask(); void randomizeMask();

View File

@@ -3,10 +3,12 @@
#include <string> #include <string>
#include <vector> #include <vector>
using FishAssetPairRaw = struct FishAsset {
std::pair<std::vector<std::string>, std::vector<std::string>>; std::vector<std::string> image;
std::vector<std::string> mask;
};
inline std::vector<FishAssetPairRaw> fishAssetPairs = { inline std::vector<FishAsset> fishAssetPairs = {
{ {
{ {
R"(???\)", R"(???\)",
@@ -81,7 +83,7 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
}, },
{ {
{ {
R"(???????,--,_???)", R"(???????,--,_)",
R"(__????_\.---'-.)", R"(__????_\.---'-.)",
R"(\ '.-" // o\)", R"(\ '.-" // o\)",
R"(/_.'-._ \\ /)", R"(/_.'-._ \\ /)",
@@ -97,18 +99,18 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
}, },
{ {
{ {
R"(????_,--,???????)", R"(????_,--,)",
R"(?.-'---./_????__)", R"(?.-'---./_????__)",
R"(/o \\ "-.' /)", R"(/o \\ "-.' /)",
R"(\ // _.-'._\)", R"(\ // _.-'._\)",
R"(?`"\)--"`???????)" R"(?`"\)--"`)"
}, },
{ {
R"( 22222 )", R"( 22222)",
R"( 112111121 66)", R"( 112111121 66)",
R"(14 77 1116 6)", R"(14 77 1116 6)",
R"(1 77 1111666)", R"(1 77 1111666)",
R"( 11331111 )" R"( 11331111)"
} }
}, },
{ {
@@ -137,101 +139,101 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
}, },
{ {
{ {
R"(????????_.-`\??????)", R"(????????_.-`\)",
R"(?????-:`_..,_\?????)", R"(?????-:`_..,_\)",
R"(('-..:-` , '-.,?)", R"(('-..:-` , '-.,)",
R"(?} _ ;':( o :)", R"(?} _ ;':( o :)",
R"((.-`/'-.,__'` _.-`?)", R"((.-`/'-.,__'` _.-`)",
R"(???`'-.,/??//`?????)" R"(???`'-.,/??//`)"
}, },
{ {
R"( 22222 )", R"( 22222)",
R"( 222111112 )", R"( 222111112)",
R"(66661111 7 1111 )", R"(66661111 7 1111)",
R"( 6 1 7777 4 1)", R"( 6 1 7777 4 1)",
R"(6666211111177 1111 )", R"(6666211111177 1111)",
R"( 222222 333 )" R"( 222222 333)"
} }
}, },
{ {
{ {
R"(??????/`-._????????)", R"(??????/`-._)",
R"(?????/_,.._`:-?????)", R"(?????/_,.._`:-)",
R"(?,.-' , `-:..-'))", R"(?,.-' , `-:..-'))",
R"(: o ):'; _ {?)", R"(: o ):'; _ {)",
R"(?`-._ `'__,.-'\`-.))", R"(?`-._ `'__,.-'\`-.))",
R"(?????`\\??\,.-'`???)" R"(?????`\\??\,.-'`)"
}, },
{ {
R"( 22222 )", R"( 22222)",
R"( 211111222 )", R"( 211111222)",
R"( 1111 7 11116666)", R"( 1111 7 11116666)",
R"(1 4 7777 1 6 )", R"(1 4 7777 1 6)",
R"( 1111 7711111126666)", R"( 1111 7711111126666)",
R"( 333 222222 )" R"( 333 222222)"
} }
}, },
{ {
{ {
R"(????????/\??????)", R"(????????/\)",
R"(????????\.\_????)", R"(????????\.\_)",
R"(\'-,.:-` '-,?)", R"(\'-,.:-` '-,)",
R"( ) _ (>( o <)", R"( ) _ (>( o <)",
R"(/.-`?':._ _.-`?)", R"(/.-`?':._ _.-`)",
R"(??????;/?``?????)", R"(??????;/?``)",
}, },
{ {
R"( 22 )", R"( 22)",
R"( 2121 )", R"( 2121)",
R"(66661111 111 )", R"(66661111 111)",
R"( 6 1 777 4 1)", R"( 6 1 777 4 1)",
R"(6666 1111 1111 )", R"(6666 1111 1111)",
R"( 22 33 )", R"( 22 33)",
} }
}, },
{ {
{ {
R"(??????/\????????)", R"(??????/\)",
R"(????_/./????????)", R"(????_/./)",
R"(?,-' `-:.,-'/)", R"(?,-' `-:.,-'/)",
R"(> o )<) _ ( )", R"(> o )<) _ ()",
R"(?`-._ _.:'?`-.\)", R"(?`-._ _.:'?`-.\)",
R"(?????``?\;??????)", R"(?????``?\;)",
}, },
{ {
R"( 22 )", R"( 22)",
R"( 1212 )", R"( 1212)",
R"( 111 11116666)", R"( 111 11116666)",
R"(1 4 777 1 6 )", R"(1 4 777 1 6)",
R"( 1111 1111 6666)", R"( 1111 1111 6666)",
R"( 33 22 )", R"( 33 22)",
} }
}, },
{ {
{ {
R"(_?????????_.*"\??????)", R"(_?????????_.*"\)",
R"(\'-._..-*` `'*-.??)", R"(\'-._..-*` `'*-.)",
R"(?) , (( o >)", R"(?) , (( o >)",
R"(/.`"*--.__)_.`_.-*`??)" R"(/.`"*--.__)_.`_.-*`)"
}, },
{ {
R"(6 11222 )", R"(6 11222)",
R"(6661111111 11111 )", R"(6661111111 11111)",
R"( 6 3 77 4 1)", R"( 6 3 77 4 1)",
R"(6661111111311311111 )", R"(6661111111311311111)",
} }
}, },
{ {
{ {
R"(??????/"*._?????????_)", R"(??????/"*._?????????_)",
R"(??.-*'` `*-.._.-'/)", R"(??.-*'` `*-.._.-'/)",
R"(< o )) , ( )", R"(< o )) , ()",
R"(??`*-._`._(__.--*"`.\)", R"(??`*-._`._(__.--*"`.\)",
}, },
{ {
R"( 22211 6)", R"( 22211 6)",
R"( 11111 1111111666)", R"( 11111 1111111666)",
R"(1 4 77 3 6 )", R"(1 4 77 3 6)",
R"( 1111131131111111666)", R"( 1111131131111111666)",
}, },
} }