Refactor OverworldEntity to use uint16_t for map_id and improve code organization

This commit is contained in:
scawful
2024-08-22 10:49:12 -04:00
parent 4099d8731d
commit 270bef0973
7 changed files with 50 additions and 54 deletions

View File

@@ -45,14 +45,14 @@ class OverworldEntity {
int game_x_;
int game_y_;
int entity_id_;
int map_id_;
uint16_t map_id_;
auto set_x(int x) { x_ = x; }
auto set_y(int y) { y_ = y; }
OverworldEntity() = default;
virtual void UpdateMapProperties(short map_id) = 0;
virtual void UpdateMapProperties(uint16_t map_id) = 0;
};
static const std::string TileTypeNames[] = {

View File

@@ -468,8 +468,8 @@ absl::Status Overworld::LoadItems() {
(y * 16) + (sy * 512), false);
auto size = all_items_.size();
all_items_[size - 1].game_x = (uint8_t)x;
all_items_[size - 1].game_y = (uint8_t)y;
all_items_[size - 1].game_x_ = (uint8_t)x;
all_items_[size - 1].game_y_ = (uint8_t)y;
addr += 3;
}
}
@@ -1335,7 +1335,7 @@ bool compareItemsArrays(std::vector<OverworldItem> itemArray1,
// Check all sprite in 2nd array if one match
if (itemArray1[i].x_ == itemArray2[j].x_ &&
itemArray1[i].y_ == itemArray2[j].y_ &&
itemArray1[i].id == itemArray2[j].id) {
itemArray1[i].id_ == itemArray2[j].id_) {
match = true;
break;
}
@@ -1357,11 +1357,11 @@ absl::Status Overworld::SaveItems() {
for (int i = 0; i < 128; i++) {
room_items[i] = std::vector<OverworldItem>();
for (const OverworldItem &item : all_items_) {
if (item.room_map_id == i) {
if (item.room_map_id_ == i) {
room_items[i].emplace_back(item);
if (item.id == 0x86) {
if (item.id_ == 0x86) {
RETURN_IF_ERROR(rom()->WriteWord(
0x16DC5 + (i * 2), (item.game_x + (item.game_y * 64)) * 2));
0x16DC5 + (i * 2), (item.game_x_ + (item.game_y_ * 64)) * 2));
}
}
}
@@ -1398,11 +1398,11 @@ absl::Status Overworld::SaveItems() {
item_pointers[i] = data_pos;
for (const OverworldItem &item : room_items[i]) {
short map_pos =
static_cast<short>(((item.game_y << 6) + item.game_x) << 1);
static_cast<short>(((item.game_y_ << 6) + item.game_x_) << 1);
uint32_t data = static_cast<uint8_t>(map_pos & 0xFF) |
static_cast<uint8_t>(map_pos >> 8) |
static_cast<uint8_t>(item.id);
static_cast<uint8_t>(item.id_);
RETURN_IF_ERROR(rom()->WriteLong(data_pos, data));
data_pos += 3;
}

View File

@@ -79,21 +79,21 @@ constexpr int overworldItemsEndData = 0xDC89C; // 0DC89E
class OverworldItem : public OverworldEntity {
public:
bool bg2 = false;
uint8_t game_x;
uint8_t game_y;
uint8_t id;
uint16_t room_map_id;
bool bg2_ = false;
uint8_t id_;
uint8_t game_x_;
uint8_t game_y_;
uint16_t room_map_id_;
int unique_id = 0;
bool deleted = false;
OverworldItem() = default;
OverworldItem(uint8_t id, uint16_t room_map_id, int x, int y, bool bg2) {
this->id = id;
this->id_ = id;
this->x_ = x;
this->y_ = y;
this->bg2 = bg2;
this->room_map_id = room_map_id;
this->bg2_ = bg2;
this->room_map_id_ = room_map_id;
this->map_id_ = room_map_id;
this->entity_id_ = id;
this->type_ = kItem;
@@ -101,32 +101,28 @@ class OverworldItem : public OverworldEntity {
int map_x = room_map_id - ((room_map_id / 8) * 8);
int map_y = room_map_id / 8;
this->game_x = static_cast<uint8_t>(std::abs(x - (map_x * 512)) / 16);
this->game_y = static_cast<uint8_t>(std::abs(y - (map_y * 512)) / 16);
// this->unique_id = ROM.unique_item_id++;
game_x_ = static_cast<uint8_t>(std::abs(x - (map_x * 512)) / 16);
game_y_ = static_cast<uint8_t>(std::abs(y - (map_y * 512)) / 16);
}
void UpdateMapProperties(int16_t room_map_id) override {
this->room_map_id = static_cast<uint16_t>(room_map_id);
void UpdateMapProperties(uint16_t room_map_id) override {
room_map_id_ = room_map_id;
if (room_map_id >= 64) {
room_map_id -= 64;
if (room_map_id_ >= 64) {
room_map_id_ -= 64;
}
int map_x = room_map_id - ((room_map_id / 8) * 8);
int map_y = room_map_id / 8;
int map_x = room_map_id_ - ((room_map_id_ / 8) * 8);
int map_y = room_map_id_ / 8;
this->game_x =
static_cast<uint8_t>(std::abs(this->x_ - (map_x * 512)) / 16);
this->game_y =
static_cast<uint8_t>(std::abs(this->y_ - (map_y * 512)) / 16);
game_x_ = static_cast<uint8_t>(std::abs(x_ - (map_x * 512)) / 16);
game_y_ = static_cast<uint8_t>(std::abs(y_ - (map_y * 512)) / 16);
std::cout << "Item: " << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(this->id) << " MapId: " << std::hex
<< std::setw(2) << std::setfill('0')
<< static_cast<int>(this->room_map_id)
<< " X: " << static_cast<int>(this->game_x)
<< " Y: " << static_cast<int>(this->game_y) << std::endl;
<< static_cast<int>(id_) << " MapId: " << std::hex << std::setw(2)
<< std::setfill('0') << static_cast<int>(room_map_id_)
<< " X: " << static_cast<int>(game_x_)
<< " Y: " << static_cast<int>(game_y_) << std::endl;
}
};
@@ -191,7 +187,6 @@ class OverworldExit : public OverworldEntity {
entrance_id_(0),
area_x_(0),
area_y_(0),
is_hole_(false),
room_id_(room_id),
y_scroll_(y_scroll),
x_scroll_(x_scroll),
@@ -203,12 +198,13 @@ class OverworldExit : public OverworldEntity {
scroll_mod_x_(scroll_mod_x),
door_type_1_(door_type_1),
door_type_2_(door_type_2),
is_hole_(false),
deleted_(deleted) {
// Initialize entity variables
this->x_ = player_x;
this->y_ = player_y;
this->map_id_ = map_id;
this->type_ = kExit;
x_ = player_x;
y_ = player_y;
map_id_ = map_id;
type_ = kExit;
int mapX = (map_id_ - ((map_id_ / 8) * 8));
int mapY = (map_id_ / 8);
@@ -242,7 +238,7 @@ class OverworldExit : public OverworldEntity {
}
// Overworld overworld
void UpdateMapProperties(short map_id) override {
void UpdateMapProperties(uint16_t map_id) override {
map_id_ = map_id;
int large = 256;
@@ -365,7 +361,7 @@ class OverworldEntrance : public OverworldEntity {
is_hole_);
}
void UpdateMapProperties(short map_id) override {
void UpdateMapProperties(uint16_t map_id) override {
map_id_ = map_id;
if (map_id_ >= 64) {

View File

@@ -6,7 +6,7 @@ namespace yaze {
namespace app {
namespace zelda3 {
void Sprite::UpdateMapProperties(short map_id) {
void Sprite::UpdateMapProperties(uint16_t map_id) {
map_x_ = x_;
map_y_ = y_;
name_ = kSpriteDefaultNames[id_];

View File

@@ -328,7 +328,7 @@ class Sprite : public OverworldEntity {
bool mirror_x = false, bool mirror_y = false,
int sizex = 2, int sizey = 2);
void UpdateMapProperties(short map_id) override;
void UpdateMapProperties(uint16_t map_id) override;
// New methods
void UpdateCoordinates(int map_x, int map_y);