Fish.cpp (1866B)
1 #include "Fish.h" 2 #include "../assets/FishAssets.h" 3 #include "../core/Aquarium.h" 4 #include "../utils/Random.h" 5 #include "../utils/defs.h" 6 7 std::unordered_map<char, char> Fish::color_map; 8 9 Fish::Fish() : Fish(getRandomAssetIndex()) {} 10 11 Fish::Fish(int asset_index) 12 : Entity(asset_index % 2 == 0), 13 image(getFishAssetPairs()[asset_index].image), 14 mask(getFishAssetPairs()[asset_index].mask), 15 speed(Random::floatInRange(0.25f, 2.25f)) { 16 17 const auto &aquarium = Aquarium::getInstance(); 18 y = Random::intInRange(static_cast<int>(image.size()) + 6, 19 aquarium.getHeight() - static_cast<int>(image.size())); 20 x = moving_right ? -this->getWidth() 21 : static_cast<float>(aquarium.getWidth()); 22 randomizeMask(); 23 } 24 25 void Fish::randomizeMask() { 26 // Clear and rebuild color map with random colors each time 27 color_map.clear(); 28 color_map['4'] = 'W'; // White is always '4' for eyes 29 30 // Assign random colors to digits 1-3, 5-9 31 for (char digit = '1'; digit <= '9'; ++digit) { 32 if (digit != '4') { 33 color_map[digit] = AVAILABLE_COLORS[Random::intInRange( 34 0, static_cast<int>(AVAILABLE_COLORS.size()) - 1)]; 35 } 36 } 37 38 // Apply color mapping to mask 39 for (auto &line : mask) { 40 for (char &ch : line) { 41 if (auto it = color_map.find(ch); it != color_map.end()) { 42 ch = it->second; 43 } 44 } 45 } 46 } 47 48 int Fish::getRandomAssetIndex() { 49 return Random::intInRange(0, 50 static_cast<int>(getFishAssetPairs().size()) - 1); 51 } 52 53 void Fish::update() noexcept { x += moving_right ? speed : -speed; } 54 55 std::unique_ptr<Entity> Fish::createReplacement() const { 56 return std::make_unique<Fish>(); 57 } 58 59 int Fish::getPreferredLayer() const noexcept { return 10; } 60 61 bool Fish::shouldSpawnBubble() const { 62 return Random::floatInRange(0, 1) < BUBBLE_SPAWN_CHANCE; 63 }