add ship, improve inheritance
This commit is contained in:
59
src/Entity.cpp
Normal file
59
src/Entity.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "Entity.h"
|
||||
#include "Aquarium.h"
|
||||
|
||||
void Entity::draw(int layer) const {
|
||||
auto &aquarium = Aquarium::getInstance();
|
||||
|
||||
const auto &image = getImage();
|
||||
const auto &mask = getMask();
|
||||
const char default_color = getDefaultColor();
|
||||
|
||||
std::string current_segment;
|
||||
std::string current_colors;
|
||||
current_segment.reserve(32);
|
||||
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);
|
||||
|
||||
// Use mask color if available, otherwise use default color for spaces
|
||||
char color = default_color;
|
||||
if (j < mask_row.size()) {
|
||||
color = (mask_row[j] == ' ') ? default_color : mask_row[j];
|
||||
}
|
||||
current_colors.push_back(color);
|
||||
}
|
||||
|
||||
// Flush remaining segment
|
||||
if (!current_segment.empty()) {
|
||||
aquarium.drawToBackBuffer(base_y + static_cast<int>(i), cursor_x, layer,
|
||||
current_segment, current_colors);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user