Files
aee/source/game/momia.cpp
Sergio Valor e7aa2463b4 refactor: fase 1 — cleanup mecànic de baix risc (NULL→nullptr, typedef→using, explicit, enum class local)
- jdraw8.hpp: typedef → using (JD8_Surface, JD8_Palette)
- jdraw8.cpp: NULL → nullptr, C-casts → static_cast/reinterpret_cast, anon enum FadeType → enum class
- momia.cpp: NULL → nullptr
- bola/mapa/marcador/momia/engendro: explicit als constructors

Zero canvis de lògica ni ownership. Primera fase de la modernització RAII.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 13:37:48 +02:00

177 lines
5.1 KiB
C++

#include "game/momia.hpp"
#include <stdlib.h>
#include "core/jail/jgame.hpp"
Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
: Sprite(gfx) {
this->dimoni = dimoni;
this->sam = sam;
entitat.frames.reserve(20);
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 5; col++) {
Frame f;
f.w = 15;
f.h = 15;
if (info::ctx.num_piramide == 4) f.h -= 5;
f.x = (col * 15) + 75;
if (this->dimoni) f.x += 75;
f.y = 20 + (row * 15);
entitat.frames.push_back(f);
}
}
entitat.animacions.resize(4);
for (int i = 0; i < 4; i++) {
entitat.animacions[i].frames = {
static_cast<Uint8>(0 + i * 5),
static_cast<Uint8>(1 + i * 5),
static_cast<Uint8>(2 + i * 5),
static_cast<Uint8>(1 + i * 5),
static_cast<Uint8>(0 + i * 5),
static_cast<Uint8>(3 + i * 5),
static_cast<Uint8>(4 + i * 5),
static_cast<Uint8>(3 + i * 5),
};
}
this->cur_frame = 0;
this->o = rand() % 4;
this->cycles_per_frame = 4;
this->next = nullptr;
if (this->dimoni) {
if (x == 0) {
this->x = 150;
} else {
this->x = x;
}
if (y == 0) {
if (this->sam->y > 100) {
this->y = 30;
} else {
this->y = 170;
}
} else {
this->y = y;
}
this->engendro = std::make_unique<Engendro>(gfx, this->x, this->y);
} else {
this->x = x;
this->y = y;
}
}
void Momia::clear() {
if (this->next != nullptr) this->next->clear();
delete this->next;
}
void Momia::draw() {
if (this->engendro) {
this->engendro->draw();
} else {
Sprite::draw();
if (info::ctx.num_piramide == 4) {
if ((JG_GetCycleCounter() % 40) < 20) {
JD8_BlitCK(this->x, this->y, this->gfx, 220, 80, 15, 15, 255);
} else {
JD8_BlitCK(this->x, this->y, this->gfx, 235, 80, 15, 15, 255);
}
}
}
if (this->next != nullptr) this->next->draw();
}
bool Momia::update() {
bool morta = false;
if (this->engendro) {
if (this->engendro->update()) {
this->engendro.reset();
}
} else {
if (this->sam->o < 4 && (this->dimoni || info::ctx.num_piramide == 5 || JG_GetCycleCounter() % 2 == 0)) {
if ((this->x - 20) % 65 == 0 && (this->y - 30) % 35 == 0) {
if (this->dimoni) {
if (rand() % 2 == 0) {
if (this->x > this->sam->x) {
this->o = 3;
} else if (this->x < this->sam->x) {
this->o = 2;
} else if (this->y < this->sam->y) {
this->o = 0;
} else if (this->y > this->sam->y) {
this->o = 1;
}
} else {
if (this->y < this->sam->y) {
this->o = 0;
} else if (this->y > this->sam->y) {
this->o = 1;
} else if (this->x > this->sam->x) {
this->o = 3;
} else if (this->x < this->sam->x) {
this->o = 2;
}
}
} else {
this->o = rand() % 4;
}
}
switch (this->o) {
case 0:
if (y < 170) this->y++;
break;
case 1:
if (y > 30) this->y--;
break;
case 2:
if (x < 280) this->x++;
break;
case 3:
if (x > 20) this->x--;
break;
}
if (JG_GetCycleCounter() % this->cycles_per_frame == 0) {
this->cur_frame++;
if (this->cur_frame == entitat.animacions[this->o].frames.size()) this->cur_frame = 0;
}
if (this->x > (this->sam->x - 7) && this->x < (this->sam->x + 7) && this->y > (this->sam->y - 7) && this->y < (this->sam->y + 7)) {
morta = true;
if (this->sam->pergami) {
this->sam->pergami = false;
} else {
info::ctx.vida--;
if (info::ctx.vida == 0) this->sam->o = 5;
}
}
}
}
if (this->next != nullptr) {
if (this->next->update()) {
Momia* seguent = this->next->next;
delete this->next;
this->next = seguent;
info::ctx.momies--;
}
}
return morta;
}
void Momia::insertar(Momia* momia) {
if (this->next != nullptr) {
this->next->insertar(momia);
} else {
this->next = momia;
}
}