add ship, improve inheritance

This commit is contained in:
2025-05-23 11:48:30 -04:00
parent bd90e222ee
commit b4e08ff28d
10 changed files with 207 additions and 64 deletions

View File

@@ -14,12 +14,9 @@ Fish::Fish(int asset_index)
speed(Random::floatInRange(0.25f, 2.25f)),
moving_right(asset_index % 2 == 0) {
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();
}
@@ -27,7 +24,6 @@ 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') {
@@ -35,7 +31,6 @@ void Fish::randomizeMask() {
0, static_cast<int>(AVAILABLE_COLORS.size()) - 1)];
}
}
// Apply color mapping to mask
for (auto &line : mask) {
for (char &ch : line) {
@@ -52,50 +47,13 @@ int Fish::getRandomAssetIndex() {
void Fish::update() noexcept { x += moving_right ? speed : -speed; }
void Fish::draw(int layer) const {
auto &aquarium = Aquarium::getInstance();
// Pre-allocate strings to avoid repeated allocations
std::string current_segment;
std::string current_colors;
current_segment.reserve(32); // Reserve reasonable capacity
current_colors.reserve(32);
const int base_x = static_cast<int>(x);
const int base_y = static_cast<int>(y);
for (size_t i = 0; i < image.size(); ++i) {
const std::string &row = image[i];
const std::string &mask_row = (i < mask.size()) ? mask[i] : "";
int cursor_x = base_x;
current_segment.clear();
current_colors.clear();
for (size_t j = 0; j < row.size(); ++j) {
const char ch = row[j];
if (ch == '?') {
// Flush current segment if not empty
if (!current_segment.empty()) {
aquarium.drawToBackBuffer(base_y + static_cast<int>(i), cursor_x,
layer, current_segment, current_colors);
cursor_x += static_cast<int>(current_segment.size());
current_segment.clear();
current_colors.clear();
}
++cursor_x; // Skip transparent character
continue;
}
current_segment.push_back(ch);
current_colors.push_back((j < mask_row.size()) ? mask_row[j] : 'k');
}
// Flush remaining segment
if (!current_segment.empty()) {
aquarium.drawToBackBuffer(base_y + static_cast<int>(i), cursor_x, layer,
current_segment, current_colors);
}
bool Fish::isOffScreen() 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;
}
}