diff --git a/src/Fish.cpp b/src/Fish.cpp index d6cbfb9..46142dc 100644 --- a/src/Fish.cpp +++ b/src/Fish.cpp @@ -1,16 +1,16 @@ #include "Fish.h" #include "Aquarium.h" -#include "FishAssets.h" #include "Random.h" +#include "assets/FishAssets.h" #include -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::vector>> - 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 colorMap; - mask = refMask; + std::unordered_map 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(fishAssetPairs.size()) - 1); + return FishAssetRef{index, fishAssetPairs[index]}; } void Fish::update() { x += speed; } diff --git a/src/Fish.h b/src/Fish.h index 95c626f..6e29207 100644 --- a/src/Fish.h +++ b/src/Fish.h @@ -1,31 +1,26 @@ #pragma once #include "Entity.h" +#include "assets/FishAssets.h" #include class Fish : public Entity { private: - struct FishAssetPair { + struct FishAssetRef { int index; - const std::vector *image; - const std::vector *mask; + FishAsset &asset; }; - Fish(const FishAssetPair &pair); - - static std::vector< - std::pair, std::vector>> - fishPairs; + Fish(const FishAssetRef &ref); static bool initialized; const std::vector ℑ - const std::vector &refMask; - std::vector mask; + std::vector &mask; static constexpr std::array availableColors = { 'c', 'C', 'r', 'R', 'y', 'Y', 'b', 'B', 'g', 'G', 'm', 'M'}; const float speed; - static FishAssetPair getRandomFishPair(); + static FishAssetRef getRandomFishAsset(); static void initializeFishAssets(); void randomizeMask(); diff --git a/src/FishAssets.h b/src/assets/FishAssets.h similarity index 63% rename from src/FishAssets.h rename to src/assets/FishAssets.h index f694b7f..d94e32f 100644 --- a/src/FishAssets.h +++ b/src/assets/FishAssets.h @@ -3,10 +3,12 @@ #include #include -using FishAssetPairRaw = - std::pair, std::vector>; +struct FishAsset { + std::vector image; + std::vector mask; +}; -inline std::vector fishAssetPairs = { +inline std::vector fishAssetPairs = { { { R"(???\)", @@ -81,7 +83,7 @@ inline std::vector fishAssetPairs = { }, { { - R"(???????,--,_???)", + R"(???????,--,_)", R"(__????_\.---'-.)", R"(\ '.-" // o\)", R"(/_.'-._ \\ /)", @@ -97,18 +99,18 @@ inline std::vector fishAssetPairs = { }, { { - R"(????_,--,???????)", + R"(????_,--,)", R"(?.-'---./_????__)", R"(/o \\ "-.' /)", R"(\ // _.-'._\)", - R"(?`"\)--"`???????)" + R"(?`"\)--"`)" }, { - R"( 22222 )", + R"( 22222)", R"( 112111121 66)", R"(14 77 1116 6)", R"(1 77 1111666)", - R"( 11331111 )" + R"( 11331111)" } }, { @@ -137,101 +139,101 @@ inline std::vector fishAssetPairs = { }, { { - R"(????????_.-`\??????)", - R"(?????-:`_..,_\?????)", - R"(('-..:-` , '-.,?)", + R"(????????_.-`\)", + R"(?????-:`_..,_\)", + R"(('-..:-` , '-.,)", R"(?} _ ;':( o :)", - R"((.-`/'-.,__'` _.-`?)", - R"(???`'-.,/??//`?????)" + R"((.-`/'-.,__'` _.-`)", + R"(???`'-.,/??//`)" }, { - R"( 22222 )", - R"( 222111112 )", - R"(66661111 7 1111 )", + R"( 22222)", + R"( 222111112)", + R"(66661111 7 1111)", R"( 6 1 7777 4 1)", - R"(6666211111177 1111 )", - R"( 222222 333 )" + R"(6666211111177 1111)", + R"( 222222 333)" } }, { { - R"(??????/`-._????????)", - R"(?????/_,.._`:-?????)", + R"(??????/`-._)", + R"(?????/_,.._`:-)", R"(?,.-' , `-:..-'))", - R"(: o ):'; _ {?)", + R"(: o ):'; _ {)", R"(?`-._ `'__,.-'\`-.))", - R"(?????`\\??\,.-'`???)" + R"(?????`\\??\,.-'`)" }, { - R"( 22222 )", - R"( 211111222 )", + R"( 22222)", + R"( 211111222)", R"( 1111 7 11116666)", - R"(1 4 7777 1 6 )", + R"(1 4 7777 1 6)", R"( 1111 7711111126666)", - R"( 333 222222 )" + R"( 333 222222)" } }, { { - R"(????????/\??????)", - R"(????????\.\_????)", - R"(\'-,.:-` '-,?)", + R"(????????/\)", + R"(????????\.\_)", + R"(\'-,.:-` '-,)", R"( ) _ (>( o <)", - R"(/.-`?':._ _.-`?)", - R"(??????;/?``?????)", + R"(/.-`?':._ _.-`)", + R"(??????;/?``)", }, { - R"( 22 )", - R"( 2121 )", - R"(66661111 111 )", + R"( 22)", + R"( 2121)", + R"(66661111 111)", R"( 6 1 777 4 1)", - R"(6666 1111 1111 )", - R"( 22 33 )", + R"(6666 1111 1111)", + R"( 22 33)", } }, { { - R"(??????/\????????)", - R"(????_/./????????)", + R"(??????/\)", + R"(????_/./)", R"(?,-' `-:.,-'/)", - R"(> o )<) _ ( )", + R"(> o )<) _ ()", R"(?`-._ _.:'?`-.\)", - R"(?????``?\;??????)", + R"(?????``?\;)", }, { - R"( 22 )", - R"( 1212 )", + R"( 22)", + R"( 1212)", R"( 111 11116666)", - R"(1 4 777 1 6 )", + R"(1 4 777 1 6)", R"( 1111 1111 6666)", - R"( 33 22 )", + R"( 33 22)", } }, { { - R"(_?????????_.*"\??????)", - R"(\'-._..-*` `'*-.??)", + R"(_?????????_.*"\)", + R"(\'-._..-*` `'*-.)", R"(?) , (( o >)", - R"(/.`"*--.__)_.`_.-*`??)" + R"(/.`"*--.__)_.`_.-*`)" }, { - R"(6 11222 )", - R"(6661111111 11111 )", + R"(6 11222)", + R"(6661111111 11111)", R"( 6 3 77 4 1)", - R"(6661111111311311111 )", + R"(6661111111311311111)", } }, { { R"(??????/"*._?????????_)", R"(??.-*'` `*-.._.-'/)", - R"(< o )) , ( )", + R"(< o )) , ()", R"(??`*-._`._(__.--*"`.\)", }, { R"( 22211 6)", R"( 11111 1111111666)", - R"(1 4 77 3 6 )", + R"(1 4 77 3 6)", R"( 1111131131111111666)", }, }