feat(mummy): estat HURT amb fugida en línia recta i recuperació al timer
This commit is contained in:
1 parent
9ad21eb5bb
commit
5db3bb0bd0
3 files changed
+187
No files matched your search
@@ -104,6 +104,22 @@ Mummy::Mummy(const fkyaml::node& yaml)
|
||||
} else if (initial == "down") {
|
||||
changeDirection(0.0F, 1.0F);
|
||||
}
|
||||
|
||||
// Configuració de l'estat HURT: opcional. Si el bloc no apareix, la
|
||||
// mòmia no entrarà mai a HURT efectiu (defensiu); el WhipAttack la
|
||||
// pot ignorar amb `isWhippable() == false` si volem que no reacció.
|
||||
if (yaml.contains("hurt")) {
|
||||
const auto& h = yaml["hurt"];
|
||||
if (h.contains("duration_ms")) {
|
||||
hurt_config_.duration_s = static_cast<float>(h["duration_ms"].get_value<int>()) / 1000.0F;
|
||||
}
|
||||
if (h.contains("speed")) {
|
||||
hurt_config_.speed_px_per_s = h["speed"].get_value<float>();
|
||||
}
|
||||
if (h.contains("whippable")) {
|
||||
whippable_ = h["whippable"].get_value<bool>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Mummy::changeDirection(float dx, float dy) {
|
||||
@@ -305,4 +321,118 @@ void Mummy::onPlayerContact(Player* player) {
|
||||
}
|
||||
}
|
||||
|
||||
void Mummy::enterHurt(Direction flee_dir) {
|
||||
state_ = State::HURT;
|
||||
hurt_remaining_s_ = hurt_config_.duration_s;
|
||||
hurt_speed_ = (hurt_config_.speed_px_per_s > 0.0F) ? hurt_config_.speed_px_per_s : speed_;
|
||||
|
||||
const Direction CHOSEN = chooseHurtDirection(flee_dir);
|
||||
if (CHOSEN == Direction::NONE) {
|
||||
// Cap direcció lliure: la mòmia queda quieta però en HURT durant
|
||||
// tot el timer. Posem dir a 0 perquè `hurtStep` no es mou.
|
||||
setDirection(0.0F, 0.0F);
|
||||
} else {
|
||||
applyChoice(CHOSEN);
|
||||
}
|
||||
}
|
||||
|
||||
auto Mummy::chooseHurtDirection(Direction preferred) const -> Direction {
|
||||
// Avenç ~1 frame a 60 FPS amb la velocitat de HURT. Tan curt com
|
||||
// perquè un canvi sub-tile no decideixe; suficient perquè un mur
|
||||
// adjacent (o sarc tocant la caja) es detecte.
|
||||
const float TEST_SPEED = (hurt_config_.speed_px_per_s > 0.0F) ? hurt_config_.speed_px_per_s : speed_;
|
||||
const float STEP = TEST_SPEED * (1.0F / 60.0F);
|
||||
|
||||
auto is_open = [this, STEP](Direction d) -> bool {
|
||||
float dx = 0.0F;
|
||||
float dy = 0.0F;
|
||||
switch (d) {
|
||||
case Direction::UP:
|
||||
dy = -STEP;
|
||||
break;
|
||||
case Direction::DOWN:
|
||||
dy = STEP;
|
||||
break;
|
||||
case Direction::LEFT:
|
||||
dx = -STEP;
|
||||
break;
|
||||
case Direction::RIGHT:
|
||||
dx = STEP;
|
||||
break;
|
||||
case Direction::NONE:
|
||||
return false;
|
||||
}
|
||||
const auto BOX = candidateBox(dx, dy);
|
||||
return Map::insidePlayfield(BOX) && !Map::collides(BOX);
|
||||
};
|
||||
|
||||
if (preferred != Direction::NONE && is_open(preferred)) { return preferred; }
|
||||
|
||||
// Perpendiculars segons la direcció preferida (ordre arbitrari però
|
||||
// consistent: "esquerra del whip" primer).
|
||||
Direction left = Direction::NONE;
|
||||
Direction right = Direction::NONE;
|
||||
switch (preferred) {
|
||||
case Direction::UP:
|
||||
left = Direction::LEFT;
|
||||
right = Direction::RIGHT;
|
||||
break;
|
||||
case Direction::DOWN:
|
||||
left = Direction::RIGHT;
|
||||
right = Direction::LEFT;
|
||||
break;
|
||||
case Direction::LEFT:
|
||||
left = Direction::DOWN;
|
||||
right = Direction::UP;
|
||||
break;
|
||||
case Direction::RIGHT:
|
||||
left = Direction::UP;
|
||||
right = Direction::DOWN;
|
||||
break;
|
||||
case Direction::NONE:
|
||||
return Direction::NONE;
|
||||
}
|
||||
if (is_open(left)) { return left; }
|
||||
if (is_open(right)) { return right; }
|
||||
|
||||
return Direction::NONE;
|
||||
}
|
||||
|
||||
void Mummy::hurtStep(float delta_time) {
|
||||
// Si està quieta (totes les direccions bloquejades a `enterHurt`), no
|
||||
// avança ni surt: l'únic camí de sortida és el timer.
|
||||
if (dir_x_ == 0.0F && dir_y_ == 0.0F) { return; }
|
||||
|
||||
const float DX = dir_x_ * hurt_speed_ * delta_time;
|
||||
const float DY = dir_y_ * hurt_speed_ * delta_time;
|
||||
const auto CANDIDATE = candidateBox(DX, DY);
|
||||
if (Map::insidePlayfield(CANDIDATE) && !Map::collides(CANDIDATE)) {
|
||||
pixel_rect_.x += DX;
|
||||
pixel_rect_.y += DY;
|
||||
return;
|
||||
}
|
||||
|
||||
// Topa amb mur o sarc: surt del HURT immediatament. La IA normal
|
||||
// re-decideix al pròxim step (no fem U-turn ni re-orientació aquí).
|
||||
state_ = State::ROAMING;
|
||||
hurt_remaining_s_ = 0.0F;
|
||||
}
|
||||
|
||||
void Mummy::update(float delta_time) {
|
||||
if (state_ == State::HURT) {
|
||||
// Avancem l'animació inline (no podem cridar `Entity::update` ni
|
||||
// `DynamicHazard::update`: el primer salta una capa virtual i el
|
||||
// segon dispararia `step()` amb la lògica d'IA). Volem només el
|
||||
// tick d'animació + el moviment recta del HURT.
|
||||
if (anim_.has_value()) { anim_->tick(delta_time); }
|
||||
hurtStep(delta_time);
|
||||
hurt_remaining_s_ -= delta_time;
|
||||
if (hurt_remaining_s_ <= 0.0F && state_ == State::HURT) {
|
||||
state_ = State::ROAMING;
|
||||
}
|
||||
return;
|
||||
}
|
||||
DynamicHazard::update(delta_time);
|
||||
}
|
||||
|
||||
REGISTER_ENTITY("mummy", Mummy)
|
||||
Reference in new issue
Block a user