refactor: extreure helpers per reduir complexitat cognitiva (tidy net)

This commit is contained in:
2026-05-16 16:13:57 +02:00
parent b984e6041e
commit e1bc1b597f
31 changed files with 1145 additions and 1332 deletions
+101 -88
View File
@@ -7,7 +7,7 @@
Momia::Momia(Jd8::Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
: Sprite(gfx) {
this->dimoni = dimoni;
this->sam = sam;
this->sam_ = sam;
entitat.frames.reserve(20);
for (int row = 0; row < 4; row++) {
@@ -43,7 +43,7 @@ Momia::Momia(Jd8::Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
this->cur_frame = 0;
this->o = rand() % 4;
this->cycles_per_frame = 4;
this->cycles_per_frame_ = 4;
if (this->dimoni) {
if (x == 0) {
@@ -52,7 +52,7 @@ Momia::Momia(Jd8::Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
this->x = x;
}
if (y == 0) {
if (this->sam->y > 100) {
if (this->sam_->y > 100) {
this->y = 30;
} else {
this->y = 170;
@@ -60,7 +60,7 @@ Momia::Momia(Jd8::Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
} else {
this->y = y;
}
this->engendro = std::make_unique<Engendro>(gfx, this->x, this->y);
this->engendro_ = std::make_unique<Engendro>(gfx, this->x, this->y);
} else {
this->x = x;
this->y = y;
@@ -68,104 +68,117 @@ Momia::Momia(Jd8::Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
}
void Momia::draw() {
if (this->engendro) {
this->engendro->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);
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);
Jd8::blitCK(this->x, this->y, this->gfx_, 235, 80, 15, 15, 255);
}
}
}
}
void Momia::pickHorizontalThenVertical() {
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;
}
}
void Momia::pickVerticalThenHorizontal() {
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;
}
}
void Momia::pickDirection() {
if (!this->dimoni) {
this->o = rand() % 4;
return;
}
if (rand() % 2 == 0) {
pickHorizontalThenVertical();
} else {
pickVerticalThenHorizontal();
}
}
void Momia::stepInDirection() {
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;
default:
break;
}
}
auto Momia::collidesWithSam() const -> bool {
return this->x > (this->sam_->x - 7) && this->x < (this->sam_->x + 7) &&
this->y > (this->sam_->y - 7) && this->y < (this->sam_->y + 7);
}
void Momia::applyCollisionWithSam() {
if (this->sam_->pergami) {
this->sam_->pergami = false;
return;
}
Info::ctx.vida--;
if (Info::ctx.vida == 0) {
this->sam_->o = 5;
}
}
auto Momia::update() -> bool {
bool morta = false;
if (this->engendro) {
if (this->engendro->update()) {
this->engendro.reset();
if (this->engendro_) {
if (this->engendro_->update()) {
this->engendro_.reset();
}
return morta;
return false;
}
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;
default:
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;
}
}
const bool SAM_ALIVE = this->sam_->o < 4;
const bool MAY_STEP = this->dimoni || Info::ctx.num_piramide == 5 || Jg::getCycleCounter() % 2 == 0;
if (!SAM_ALIVE || !MAY_STEP) {
return false;
}
if ((this->x - 20) % 65 == 0 && (this->y - 30) % 35 == 0) {
pickDirection();
}
stepInDirection();
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;
}
}
return morta;
if (collidesWithSam()) {
applyCollisionWithSam();
return true;
}
return false;
}