refactor: fase 4 — llista enllaçada de Momia a std::vector<unique_ptr>
Eliminada completament la recursivitat per next-pointer: - Momia::next, clear(), insertar() desapareixen - update()/draw() no recursen: operen només sobre la instància pròpia - ModuleGame::momies: Momia* (head de llista) → std::vector<std::unique_ptr<Momia>> - Destructor simplificat (vector s'autodestrueix) - Draw: range-for sobre el vector - Update: std::erase_if + decrement sincronitzat de info::ctx.momies - Cheat "alone": momies.clear() - iniciarMomies i nova_momia: emplace_back(std::make_unique<Momia>(...)) Zero new/delete manuals al cicle de vida de les momies. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "game/modulegame.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/jail/jdraw8.hpp"
|
||||
#include "core/jail/jgame.hpp"
|
||||
@@ -20,11 +22,6 @@ ModuleGame::ModuleGame() {
|
||||
}
|
||||
|
||||
ModuleGame::~ModuleGame() {
|
||||
if (this->momies != nullptr) {
|
||||
this->momies->clear();
|
||||
delete this->momies;
|
||||
}
|
||||
|
||||
JD8_FreeSurface(this->gfx);
|
||||
}
|
||||
|
||||
@@ -115,7 +112,7 @@ void ModuleGame::Draw() {
|
||||
this->mapa->draw();
|
||||
this->marcador->draw();
|
||||
this->sam->draw();
|
||||
if (this->momies != nullptr) this->momies->draw();
|
||||
for (auto& m : this->momies) m->draw();
|
||||
if (this->bola) this->bola->draw();
|
||||
}
|
||||
|
||||
@@ -124,32 +121,19 @@ void ModuleGame::Update() {
|
||||
JI_Update();
|
||||
|
||||
this->final_ = this->sam->update();
|
||||
if (this->momies != nullptr && this->momies->update()) {
|
||||
Momia* seguent = this->momies->next;
|
||||
delete this->momies;
|
||||
this->momies = seguent;
|
||||
info::ctx.momies--;
|
||||
}
|
||||
const auto erased = std::erase_if(this->momies, [](auto& m) { return m->update(); });
|
||||
info::ctx.momies -= static_cast<int>(erased);
|
||||
if (this->bola) this->bola->update();
|
||||
this->mapa->update();
|
||||
if (this->mapa->novaMomia()) {
|
||||
if (this->momies != nullptr) {
|
||||
this->momies->insertar(new Momia(this->gfx, true, 0, 0, this->sam.get()));
|
||||
info::ctx.momies++;
|
||||
} else {
|
||||
this->momies = new Momia(this->gfx, true, 0, 0, this->sam.get());
|
||||
info::ctx.momies++;
|
||||
}
|
||||
this->momies.emplace_back(std::make_unique<Momia>(this->gfx, true, 0, 0, this->sam.get()));
|
||||
info::ctx.momies++;
|
||||
}
|
||||
|
||||
if (JI_CheatActivated("reviu")) info::ctx.vida = 5;
|
||||
if (JI_CheatActivated("alone")) {
|
||||
if (this->momies != nullptr) {
|
||||
this->momies->clear();
|
||||
delete this->momies;
|
||||
this->momies = nullptr;
|
||||
info::ctx.momies = 0;
|
||||
}
|
||||
this->momies.clear();
|
||||
info::ctx.momies = 0;
|
||||
}
|
||||
if (JI_CheatActivated("obert")) {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
@@ -179,11 +163,7 @@ void ModuleGame::iniciarMomies() {
|
||||
int y = 170;
|
||||
bool dimonis = info::ctx.num_piramide == 6;
|
||||
for (int i = 0; i < info::ctx.momies; i++) {
|
||||
if (this->momies == nullptr) {
|
||||
this->momies = new Momia(this->gfx, dimonis, x, y, this->sam.get());
|
||||
} else {
|
||||
this->momies->insertar(new Momia(this->gfx, dimonis, x, y, this->sam.get()));
|
||||
}
|
||||
this->momies.emplace_back(std::make_unique<Momia>(this->gfx, dimonis, x, y, this->sam.get()));
|
||||
x += 65;
|
||||
if (x == 345) {
|
||||
x = 20;
|
||||
|
||||
Reference in New Issue
Block a user