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 "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) {
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; }

View File

@@ -1,31 +1,26 @@
#pragma once
#include "Entity.h"
#include "assets/FishAssets.h"
#include <array>
class Fish : public Entity {
private:
struct FishAssetPair {
struct FishAssetRef {
int index;
const std::vector<std::string> *image;
const std::vector<std::string> *mask;
FishAsset &asset;
};
Fish(const FishAssetPair &pair);
static std::vector<
std::pair<std::vector<std::string>, std::vector<std::string>>>
fishPairs;
Fish(const FishAssetRef &ref);
static bool initialized;
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 = {
'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();

View File

@@ -3,10 +3,12 @@
#include <string>
#include <vector>
using FishAssetPairRaw =
std::pair<std::vector<std::string>, std::vector<std::string>>;
struct FishAsset {
std::vector<std::string> image;
std::vector<std::string> mask;
};
inline std::vector<FishAssetPairRaw> fishAssetPairs = {
inline std::vector<FishAsset> fishAssetPairs = {
{
{
R"(???\)",
@@ -81,7 +83,7 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
},
{
{
R"(???????,--,_???)",
R"(???????,--,_)",
R"(__????_\.---'-.)",
R"(\ '.-" // o\)",
R"(/_.'-._ \\ /)",
@@ -97,11 +99,11 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
},
{
{
R"(????_,--,???????)",
R"(????_,--,)",
R"(?.-'---./_????__)",
R"(/o \\ "-.' /)",
R"(\ // _.-'._\)",
R"(?`"\)--"`???????)"
R"(?`"\)--"`)"
},
{
R"( 22222)",
@@ -137,12 +139,12 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
},
{
{
R"(????????_.-`\??????)",
R"(?????-:`_..,_\?????)",
R"(('-..:-` , '-.,?)",
R"(????????_.-`\)",
R"(?????-:`_..,_\)",
R"(('-..:-` , '-.,)",
R"(?} _ ;':( o :)",
R"((.-`/'-.,__'` _.-`?)",
R"(???`'-.,/??//`?????)"
R"((.-`/'-.,__'` _.-`)",
R"(???`'-.,/??//`)"
},
{
R"( 22222)",
@@ -155,12 +157,12 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
},
{
{
R"(??????/`-._????????)",
R"(?????/_,.._`:-?????)",
R"(??????/`-._)",
R"(?????/_,.._`:-)",
R"(?,.-' , `-:..-'))",
R"(: o ):'; _ {?)",
R"(: o ):'; _ {)",
R"(?`-._ `'__,.-'\`-.))",
R"(?????`\\??\,.-'`???)"
R"(?????`\\??\,.-'`)"
},
{
R"( 22222)",
@@ -173,12 +175,12 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
},
{
{
R"(????????/\??????)",
R"(????????\.\_????)",
R"(\'-,.:-` '-,?)",
R"(????????/\)",
R"(????????\.\_)",
R"(\'-,.:-` '-,)",
R"( ) _ (>( o <)",
R"(/.-`?':._ _.-`?)",
R"(??????;/?``?????)",
R"(/.-`?':._ _.-`)",
R"(??????;/?``)",
},
{
R"( 22)",
@@ -191,12 +193,12 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
},
{
{
R"(??????/\????????)",
R"(????_/./????????)",
R"(??????/\)",
R"(????_/./)",
R"(?,-' `-:.,-'/)",
R"(> o )<) _ ()",
R"(?`-._ _.:'?`-.\)",
R"(?????``?\;??????)",
R"(?????``?\;)",
},
{
R"( 22)",
@@ -209,10 +211,10 @@ inline std::vector<FishAssetPairRaw> fishAssetPairs = {
},
{
{
R"(_?????????_.*"\??????)",
R"(\'-._..-*` `'*-.??)",
R"(_?????????_.*"\)",
R"(\'-._..-*` `'*-.)",
R"(?) , (( o >)",
R"(/.`"*--.__)_.`_.-*`??)"
R"(/.`"*--.__)_.`_.-*`)"
},
{
R"(6 11222)",