REBASE AWESOME
This commit is contained in:
60
src/Fish.cpp
60
src/Fish.cpp
@@ -2,9 +2,26 @@
|
||||
#include "Aquarium.h"
|
||||
#include "Random.h"
|
||||
#include "assets/FishAssets.h"
|
||||
#include <ncurses.h>
|
||||
#include "defs.h"
|
||||
|
||||
std::unordered_map<char, char> Fish::color_map;
|
||||
std::array<std::unordered_map<char, char>, 20> Fish::color_map_cache;
|
||||
bool Fish::color_maps_initialized = false;
|
||||
|
||||
// Pre-generate 20 different color mappings at startup
|
||||
void Fish::initializeColorMaps() {
|
||||
for (auto &color_map : color_map_cache) {
|
||||
color_map.clear();
|
||||
color_map['4'] = 'W'; // White is always '4'
|
||||
|
||||
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)];
|
||||
}
|
||||
}
|
||||
}
|
||||
color_maps_initialized = true;
|
||||
}
|
||||
|
||||
Fish::Fish() : Fish(getRandomAssetIndex()) {}
|
||||
|
||||
@@ -13,28 +30,25 @@ Fish::Fish(int asset_index)
|
||||
mask(fishAssetPairs[asset_index].mask),
|
||||
speed(Random::floatInRange(0.25f, 2.25f)),
|
||||
moving_right(asset_index % 2 == 0) {
|
||||
|
||||
if (!color_maps_initialized) {
|
||||
initializeColorMaps();
|
||||
}
|
||||
|
||||
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();
|
||||
applyRandomColorMapping();
|
||||
}
|
||||
|
||||
void Fish::randomizeMask() {
|
||||
// Clear and rebuild color map
|
||||
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
|
||||
// Pick one of the pre-generated color mappings
|
||||
void Fish::applyRandomColorMapping() {
|
||||
const auto &selected_map = color_map_cache[Random::intInRange(0, 9)];
|
||||
|
||||
for (auto &line : mask) {
|
||||
for (char &ch : line) {
|
||||
if (auto it = color_map.find(ch); it != color_map.end()) {
|
||||
if (auto it = selected_map.find(ch); it != selected_map.end()) {
|
||||
ch = it->second;
|
||||
}
|
||||
}
|
||||
@@ -47,13 +61,21 @@ int Fish::getRandomAssetIndex() {
|
||||
|
||||
void Fish::update() noexcept { x += moving_right ? speed : -speed; }
|
||||
|
||||
bool Fish::isOffScreen() const noexcept {
|
||||
bool Fish::shouldBeRemoved() const noexcept {
|
||||
const auto &aquarium = Aquarium::getInstance();
|
||||
if (moving_right) {
|
||||
// Fish is off screen when its left edge is past the right border
|
||||
return x > static_cast<float>(aquarium.getWidth());
|
||||
} else {
|
||||
// Fish is off screen when its right edge is past the left border
|
||||
return (x + static_cast<float>(image[0].length())) < 0;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user