refactor(anim): direccions cardinals (north/south/east) en YAML i codi
This commit is contained in:
1 parent
1d43a77800
commit
9dc7ff0da3
9 files changed
+189
-240
No files matched your search
@@ -73,9 +73,9 @@ namespace {
|
||||
} // namespace
|
||||
|
||||
Mummy::Mummy(const fkyaml::node& yaml)
|
||||
: anim_walk_down_(buildAnimation(yaml, "walk_down")),
|
||||
anim_walk_up_(buildAnimation(yaml, "walk_up")),
|
||||
anim_walk_lateral_(buildAnimation(yaml, "walk")) {
|
||||
: anim_walk_south_(buildAnimation(yaml, "walk_south")),
|
||||
anim_walk_north_(buildAnimation(yaml, "walk_north")),
|
||||
anim_walk_east_(buildAnimation(yaml, "walk_east")) {
|
||||
name_ = yaml.contains("name") ? yaml["name"].get_value<std::string>() : std::string("mummy");
|
||||
collides_ = true;
|
||||
interactable_ = false;
|
||||
@@ -87,21 +87,22 @@ Mummy::Mummy(const fkyaml::node& yaml)
|
||||
if (s.contains("damage")) { setDamage(s["damage"].get_value<int>()); }
|
||||
}
|
||||
|
||||
// Direcció inicial llegida del bloc `ai:`. Per defecte dreta, per
|
||||
// coherència amb el que fa RollingRock. Valors acceptats: right, left,
|
||||
// up, down. Qualsevol altre s'interpreta com a "sense direcció" (la
|
||||
// momia no es mou fins que step() no triï una direcció).
|
||||
std::string initial = "right";
|
||||
// Direcció inicial llegida del bloc `ai:`. Per defecte est, per
|
||||
// coherència amb el que fa RollingRock. Valors acceptats:
|
||||
// east, west, north, south. Qualsevol altre s'interpreta com a
|
||||
// "sense direcció" (la momia no es mou fins que step() no triï una
|
||||
// direcció).
|
||||
std::string initial = "east";
|
||||
if (yaml.contains("ai") && yaml["ai"].contains("initial_direction")) {
|
||||
initial = yaml["ai"]["initial_direction"].get_value<std::string>();
|
||||
}
|
||||
if (initial == "right") {
|
||||
if (initial == "east") {
|
||||
changeDirection(1.0F, 0.0F);
|
||||
} else if (initial == "left") {
|
||||
} else if (initial == "west") {
|
||||
changeDirection(-1.0F, 0.0F);
|
||||
} else if (initial == "up") {
|
||||
} else if (initial == "north") {
|
||||
changeDirection(0.0F, -1.0F);
|
||||
} else if (initial == "down") {
|
||||
} else if (initial == "south") {
|
||||
changeDirection(0.0F, 1.0F);
|
||||
}
|
||||
|
||||
@@ -125,18 +126,18 @@ Mummy::Mummy(const fkyaml::node& yaml)
|
||||
void Mummy::changeDirection(float dx, float dy) {
|
||||
setDirection(dx, dy);
|
||||
// Prevalença lateral: si hi ha component horitzontal s'usa l'anim
|
||||
// lateral + flip segons el signe. Només direccions purament verticals
|
||||
// disparen walk_up/walk_down. Quan la direcció queda (0,0) (cul-de-sac
|
||||
// amb NONE) no toquem l'animació: la momia es quedarà al frame actual
|
||||
// fins que step() li torni a assignar direcció.
|
||||
// est + flip segons el signe. Només direccions purament verticals
|
||||
// disparen walk_north/walk_south. Quan la direcció queda (0,0)
|
||||
// (cul-de-sac amb NONE) no toquem l'animació: la momia es quedarà al
|
||||
// frame actual fins que step() li torni a assignar direcció.
|
||||
if (dx != 0.0F) {
|
||||
anim_ = anim_walk_lateral_;
|
||||
anim_ = anim_walk_east_;
|
||||
flip_ = (dx < 0.0F) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
} else if (dy < 0.0F) {
|
||||
anim_ = anim_walk_up_;
|
||||
anim_ = anim_walk_north_;
|
||||
flip_ = SDL_FLIP_NONE;
|
||||
} else if (dy > 0.0F) {
|
||||
anim_ = anim_walk_down_;
|
||||
anim_ = anim_walk_south_;
|
||||
flip_ = SDL_FLIP_NONE;
|
||||
}
|
||||
if (anim_.has_value()) { anim_->reset(); }
|
||||
@@ -234,10 +235,10 @@ auto Mummy::detectLaneCrossing(float dx, float dy) const -> std::optional<float>
|
||||
}
|
||||
|
||||
auto Mummy::currentDirection() const -> Direction {
|
||||
if (dir_x_ > 0.0F) { return Direction::RIGHT; }
|
||||
if (dir_x_ < 0.0F) { return Direction::LEFT; }
|
||||
if (dir_y_ > 0.0F) { return Direction::DOWN; }
|
||||
if (dir_y_ < 0.0F) { return Direction::UP; }
|
||||
if (dir_x_ > 0.0F) { return Direction::EAST; }
|
||||
if (dir_x_ < 0.0F) { return Direction::WEST; }
|
||||
if (dir_y_ > 0.0F) { return Direction::SOUTH; }
|
||||
if (dir_y_ < 0.0F) { return Direction::NORTH; }
|
||||
return Direction::NONE;
|
||||
}
|
||||
|
||||
@@ -268,10 +269,10 @@ auto Mummy::collectDirectionPools(float step) const -> DirectionPools {
|
||||
Direction dir;
|
||||
};
|
||||
const std::array<Candidate, 4> ALL{{
|
||||
{.dx = step, .dy = 0.0F, .dir = Direction::RIGHT},
|
||||
{.dx = -step, .dy = 0.0F, .dir = Direction::LEFT},
|
||||
{.dx = 0.0F, .dy = -step, .dir = Direction::UP},
|
||||
{.dx = 0.0F, .dy = step, .dir = Direction::DOWN},
|
||||
{.dx = step, .dy = 0.0F, .dir = Direction::EAST},
|
||||
{.dx = -step, .dy = 0.0F, .dir = Direction::WEST},
|
||||
{.dx = 0.0F, .dy = -step, .dir = Direction::NORTH},
|
||||
{.dx = 0.0F, .dy = step, .dir = Direction::SOUTH},
|
||||
}};
|
||||
DirectionPools pools;
|
||||
for (const auto& c : ALL) {
|
||||
@@ -293,16 +294,16 @@ auto Mummy::collectDirectionPools(float step) const -> DirectionPools {
|
||||
|
||||
void Mummy::applyChoice(Direction dir) {
|
||||
switch (dir) {
|
||||
case Direction::UP:
|
||||
case Direction::NORTH:
|
||||
changeDirection(0.0F, -1.0F);
|
||||
break;
|
||||
case Direction::DOWN:
|
||||
case Direction::SOUTH:
|
||||
changeDirection(0.0F, 1.0F);
|
||||
break;
|
||||
case Direction::LEFT:
|
||||
case Direction::WEST:
|
||||
changeDirection(-1.0F, 0.0F);
|
||||
break;
|
||||
case Direction::RIGHT:
|
||||
case Direction::EAST:
|
||||
changeDirection(1.0F, 0.0F);
|
||||
break;
|
||||
case Direction::NONE:
|
||||
@@ -347,16 +348,16 @@ auto Mummy::chooseHurtDirection(Direction preferred) const -> Direction {
|
||||
float dx = 0.0F;
|
||||
float dy = 0.0F;
|
||||
switch (d) {
|
||||
case Direction::UP:
|
||||
case Direction::NORTH:
|
||||
dy = -STEP;
|
||||
break;
|
||||
case Direction::DOWN:
|
||||
case Direction::SOUTH:
|
||||
dy = STEP;
|
||||
break;
|
||||
case Direction::LEFT:
|
||||
case Direction::WEST:
|
||||
dx = -STEP;
|
||||
break;
|
||||
case Direction::RIGHT:
|
||||
case Direction::EAST:
|
||||
dx = STEP;
|
||||
break;
|
||||
case Direction::NONE:
|
||||
@@ -373,21 +374,21 @@ auto Mummy::chooseHurtDirection(Direction preferred) const -> Direction {
|
||||
Direction left = Direction::NONE;
|
||||
Direction right = Direction::NONE;
|
||||
switch (preferred) {
|
||||
case Direction::UP:
|
||||
left = Direction::LEFT;
|
||||
right = Direction::RIGHT;
|
||||
case Direction::NORTH:
|
||||
left = Direction::WEST;
|
||||
right = Direction::EAST;
|
||||
break;
|
||||
case Direction::DOWN:
|
||||
left = Direction::RIGHT;
|
||||
right = Direction::LEFT;
|
||||
case Direction::SOUTH:
|
||||
left = Direction::EAST;
|
||||
right = Direction::WEST;
|
||||
break;
|
||||
case Direction::LEFT:
|
||||
left = Direction::DOWN;
|
||||
right = Direction::UP;
|
||||
case Direction::WEST:
|
||||
left = Direction::SOUTH;
|
||||
right = Direction::NORTH;
|
||||
break;
|
||||
case Direction::RIGHT:
|
||||
left = Direction::UP;
|
||||
right = Direction::DOWN;
|
||||
case Direction::EAST:
|
||||
left = Direction::NORTH;
|
||||
right = Direction::SOUTH;
|
||||
break;
|
||||
case Direction::NONE:
|
||||
return Direction::NONE;
|
||||
|
||||
Reference in new issue
Block a user