From b58c8afd8382b1cef2ac42345edd07493ec028ab Mon Sep 17 00:00:00 2001 From: Mauro Junior <45118493+jetrotal@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:20:41 -0300 Subject: [PATCH] FaceTowards and FaceAway Commands ```js @raw 2050, "FaceTowards", eventAIsVar,eventA, eventBisVar, eventB ``` ```js @raw 2050, "FaceAway", eventAIsVar,eventA, eventBisVar, eventB ``` --- src/game_character.cpp | 90 +++++++++++++++++++++++----------------- src/game_character.h | 13 +++--- src/game_event.cpp | 6 +-- src/game_interpreter.cpp | 13 ++++++ 4 files changed, 74 insertions(+), 48 deletions(-) diff --git a/src/game_character.cpp b/src/game_character.cpp index 2942b9295f7..3fc497e46b7 100644 --- a/src/game_character.cpp +++ b/src/game_character.cpp @@ -290,10 +290,10 @@ void Game_Character::UpdateMoveRoute(int32_t& current_index, const lcf::rpg::Mov TurnRandom(); break; case Code::move_towards_hero: - TurnTowardHero(); + TurnTowardCharacter(CharPlayer); break; case Code::move_away_from_hero: - TurnAwayFromHero(); + TurnAwayFromCharacter(CharPlayer); break; case Code::move_forward: break; @@ -347,10 +347,10 @@ void Game_Character::UpdateMoveRoute(int32_t& current_index, const lcf::rpg::Mov TurnRandom(); break; case Code::face_hero: - TurnTowardHero(); + TurnTowardCharacter(CharPlayer); break; case Code::face_away_from_hero: - TurnAwayFromHero(); + TurnAwayFromCharacter(CharPlayer); break; default: break; @@ -528,20 +528,21 @@ void Game_Character::Turn90DegreeLeftOrRight() { } } -int Game_Character::GetDirectionToHero() { - int sx = DistanceXfromPlayer(); - int sy = DistanceYfromPlayer(); +int Game_Character::GetDirectionToCharacter(const Game_Character* target) { + int sx = DistanceXfromCharacter(target); + int sy = DistanceYfromCharacter(target); - if ( std::abs(sx) > std::abs(sy) ) { + if (std::abs(sx) > std::abs(sy)) { return (sx > 0) ? Left : Right; - } else { + } + else { return (sy > 0) ? Up : Down; } } -int Game_Character::GetDirectionAwayHero() { - int sx = DistanceXfromPlayer(); - int sy = DistanceYfromPlayer(); +int Game_Character::GetDirectionAwayCharacter(const Game_Character* target) { + int sx = DistanceXfromCharacter(target); + int sy = DistanceYfromCharacter(target); if ( std::abs(sx) > std::abs(sy) ) { return (sx > 0) ? Right : Left; @@ -550,12 +551,18 @@ int Game_Character::GetDirectionAwayHero() { } } -void Game_Character::TurnTowardHero() { - SetDirection(GetDirectionToHero()); +void Game_Character::TurnTowardCharacter(int targetID) { + Game_Character* target = GetCharacter(targetID, targetID); + if (target) { + SetDirection(GetDirectionToCharacter(target)); + } } -void Game_Character::TurnAwayFromHero() { - SetDirection(GetDirectionAwayHero()); +void Game_Character::TurnAwayFromCharacter(int targetID) { + Game_Character* target = GetCharacter(targetID, targetID); + if (target) { + SetDirection(GetDirectionAwayCharacter(target)); + } } void Game_Character::TurnRandom() { @@ -591,10 +598,10 @@ bool Game_Character::BeginMoveRouteJump(int32_t& current_index, const lcf::rpg:: TurnRandom(); break; case Code::move_towards_hero: - TurnTowardHero(); + TurnTowardCharacter(CharPlayer); break; case Code::move_away_from_hero: - TurnAwayFromHero(); + TurnAwayFromCharacter(CharPlayer); break; case Code::move_forward: break; @@ -635,10 +642,10 @@ bool Game_Character::BeginMoveRouteJump(int32_t& current_index, const lcf::rpg:: TurnRandom(); break; case Code::face_hero: - TurnTowardHero(); + TurnTowardCharacter(CharPlayer); break; case Code::face_away_from_hero: - TurnAwayFromHero(); + TurnAwayFromCharacter(CharPlayer); break; default: break; @@ -724,32 +731,39 @@ bool Game_Character::Jump(int x, int y) { return true; } -int Game_Character::DistanceXfromPlayer() const { - int sx = GetX() - Main_Data::game_player->GetX(); - if (Game_Map::LoopHorizontal()) { - if (std::abs(sx) > Game_Map::GetTilesX() / 2) { - if (sx > 0) - sx -= Game_Map::GetTilesX(); - else - sx += Game_Map::GetTilesX(); +int Game_Character::DistanceXfromCharacter(const Game_Character* target) const { + if (target) { + int sx = GetX() - target->GetX(); + if (Game_Map::LoopHorizontal()) { + if (std::abs(sx) > Game_Map::GetTilesX() / 2) { + if (sx > 0) + sx -= Game_Map::GetTilesX(); + else + sx += Game_Map::GetTilesX(); + } } + return sx; } - return sx; + return 0; // Return a default value or handle the case where target is nullptr. } -int Game_Character::DistanceYfromPlayer() const { - int sy = GetY() - Main_Data::game_player->GetY(); - if (Game_Map::LoopVertical()) { - if (std::abs(sy) > Game_Map::GetTilesY() / 2) { - if (sy > 0) - sy -= Game_Map::GetTilesY(); - else - sy += Game_Map::GetTilesY(); +int Game_Character::DistanceYfromCharacter(const Game_Character* target) const { + if (target) { + int sy = GetY() - target->GetY(); + if (Game_Map::LoopVertical()) { + if (std::abs(sy) > Game_Map::GetTilesY() / 2) { + if (sy > 0) + sy -= Game_Map::GetTilesY(); + else + sy += Game_Map::GetTilesY(); + } } + return sy; } - return sy; + return 0; // Return a default value or handle the case where target is nullptr. } + void Game_Character::ForceMoveRoute(const lcf::rpg::MoveRoute& new_route, int frequency) { if (!IsMoveRouteOverwritten()) { diff --git a/src/game_character.h b/src/game_character.h index 938bbe5ec27..ec23b508d0c 100644 --- a/src/game_character.h +++ b/src/game_character.h @@ -607,10 +607,10 @@ class Game_Character { void Turn90DegreeLeftOrRight(); /** @return the direction we would need to face the hero. */ - int GetDirectionToHero(); + int GetDirectionToCharacter(const Game_Character* target); /** @return the direction we would need to face away from hero. */ - int GetDirectionAwayHero(); + int GetDirectionAwayCharacter(const Game_Character* target); /** * @param dir input direction @@ -641,13 +641,12 @@ class Game_Character { /** * Character looks towards the hero. */ - void TurnTowardHero(); + void TurnTowardCharacter(int targetID); /** * Character looks away from the hero. */ - void TurnAwayFromHero(); - + void TurnAwayFromCharacter(int targetID); /** * Character waits for 20 frames more. */ @@ -743,8 +742,8 @@ class Game_Character { */ void SetAnimationType(AnimType anim_type); - int DistanceXfromPlayer() const; - int DistanceYfromPlayer() const; + int DistanceXfromCharacter(const Game_Character* target) const; + int DistanceYfromCharacter(const Game_Character* target) const; /** * Tests if the character is currently on the tile at x/y or moving diff --git a/src/game_event.cpp b/src/game_event.cpp index 83d0be0c4bd..318fa243399 100644 --- a/src/game_event.cpp +++ b/src/game_event.cpp @@ -349,7 +349,7 @@ bool Game_Event::ScheduleForegroundExecution(bool by_decision_key, bool face_pla } if (face_player && !(IsFacingLocked() || IsSpinning())) { - SetFacing(GetDirectionToHero()); + SetFacing(GetDirectionToCharacter(GetCharacter(CharPlayer, CharPlayer))); } data()->waiting_execution = true; @@ -570,8 +570,8 @@ void Game_Event::MoveTypeTowardsOrAwayPlayer(bool towards) { dir = Rand::GetRandomNumber(0, 3); } else { dir = towards - ? GetDirectionToHero() - : GetDirectionAwayHero(); + ? GetDirectionToCharacter(GetCharacter(CharPlayer, CharPlayer)) + : GetDirectionAwayCharacter(GetCharacter(CharPlayer, CharPlayer)); } } diff --git a/src/game_interpreter.cpp b/src/game_interpreter.cpp index 6fab24e2d5f..fbc0cd67305 100644 --- a/src/game_interpreter.cpp +++ b/src/game_interpreter.cpp @@ -4678,6 +4678,19 @@ bool Game_Interpreter::CommandCallMovement(lcf::rpg::EventCommand const& com) { event->SetY(target->GetY()); } + if (moveCommand == "FaceTowards"){ + if(!event->IsMoving()) { + event->TurnTowardCharacter(outputParam); + event->UpdateFacing(); + } +} + if (moveCommand == "FaceAway"){ + if (!event->IsMoving()) { + event->TurnAwayFromCharacter(outputParam); + event->UpdateFacing(); + } + } + if (moveCommand == "SetFacingLocked")event->SetFacingLocked(outputParam); if (moveCommand == "SetLayer")event->SetLayer(outputParam); if (moveCommand == "SetFlying")event->SetFlying(outputParam); //FIXME: I wish any event could imitate an airship, lacks more work.