61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
#include "Fish.h"
|
|
#include "Aquarium.h"
|
|
#include "Random.h"
|
|
#include "assets/FishAssets.h"
|
|
#include "defs.h"
|
|
|
|
std::unordered_map<char, char> Fish::color_map;
|
|
|
|
Fish::Fish() : Fish(getRandomAssetIndex()) {}
|
|
|
|
Fish::Fish(int asset_index)
|
|
: Entity(asset_index % 2 == 0), image(fishAssetPairs[asset_index].image),
|
|
mask(fishAssetPairs[asset_index].mask),
|
|
speed(Random::floatInRange(0.25f, 2.25f)) {
|
|
|
|
const auto &aquarium = Aquarium::getInstance();
|
|
y = Random::intInRange(static_cast<int>(image.size()) + 6,
|
|
aquarium.getHeight() - static_cast<int>(image.size()));
|
|
x = moving_right ? -20.0f : static_cast<float>(aquarium.getWidth());
|
|
randomizeMask();
|
|
}
|
|
|
|
void Fish::randomizeMask() {
|
|
// Clear and rebuild color map with fresh random colors each time
|
|
color_map.clear();
|
|
color_map['4'] = 'W'; // White is always '4'
|
|
|
|
// Assign random colors to digits 1-3, 5-9
|
|
for (char digit = '1'; digit <= '9'; ++digit) {
|
|
if (digit != '4') {
|
|
color_map[digit] = AVAILABLE_COLORS[Random::intInRange(
|
|
0, static_cast<int>(AVAILABLE_COLORS.size()) - 1)];
|
|
}
|
|
}
|
|
|
|
// Apply color mapping to mask
|
|
for (auto &line : mask) {
|
|
for (char &ch : line) {
|
|
if (auto it = color_map.find(ch); it != color_map.end()) {
|
|
ch = it->second;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int Fish::getRandomAssetIndex() {
|
|
return Random::intInRange(0, static_cast<int>(fishAssetPairs.size()) - 1);
|
|
}
|
|
|
|
void Fish::update() noexcept { x += moving_right ? speed : -speed; }
|
|
|
|
std::unique_ptr<Entity> Fish::createReplacement() const {
|
|
return std::make_unique<Fish>();
|
|
}
|
|
|
|
int Fish::getPreferredLayer() const noexcept { return 10; }
|
|
|
|
bool Fish::shouldSpawnBubble() const {
|
|
return Random::floatInRange(0, 1) < BUBBLE_SPAWN_CHANCE;
|
|
}
|