Skip to content

Commit feeed6f

Browse files
committed
FaceTowards and FaceAway Commands
```js @raw 2050, "FaceTowards", eventAIsVar,eventA, eventBisVar, eventB ``` ```js @raw 2050, "FaceAway", eventAIsVar,eventA, eventBisVar, eventB ```
1 parent 6452470 commit feeed6f

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

src/game_character.cpp

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ void Game_Character::UpdateMoveRoute(int32_t& current_index, const lcf::rpg::Mov
290290
TurnRandom();
291291
break;
292292
case Code::move_towards_hero:
293-
TurnTowardHero();
293+
TurnTowardCharacter(CharPlayer);
294294
break;
295295
case Code::move_away_from_hero:
296-
TurnAwayFromHero();
296+
TurnAwayFromCharacter(CharPlayer);
297297
break;
298298
case Code::move_forward:
299299
break;
@@ -347,10 +347,10 @@ void Game_Character::UpdateMoveRoute(int32_t& current_index, const lcf::rpg::Mov
347347
TurnRandom();
348348
break;
349349
case Code::face_hero:
350-
TurnTowardHero();
350+
TurnTowardCharacter(CharPlayer);
351351
break;
352352
case Code::face_away_from_hero:
353-
TurnAwayFromHero();
353+
TurnAwayFromCharacter(CharPlayer);
354354
break;
355355
default:
356356
break;
@@ -528,20 +528,21 @@ void Game_Character::Turn90DegreeLeftOrRight() {
528528
}
529529
}
530530

531-
int Game_Character::GetDirectionToHero() {
532-
int sx = DistanceXfromPlayer();
533-
int sy = DistanceYfromPlayer();
531+
int Game_Character::GetDirectionToCharacter(const Game_Character* target) {
532+
int sx = DistanceXfromCharacter(target);
533+
int sy = DistanceYfromCharacter(target);
534534

535-
if ( std::abs(sx) > std::abs(sy) ) {
535+
if (std::abs(sx) > std::abs(sy)) {
536536
return (sx > 0) ? Left : Right;
537-
} else {
537+
}
538+
else {
538539
return (sy > 0) ? Up : Down;
539540
}
540541
}
541542

542-
int Game_Character::GetDirectionAwayHero() {
543-
int sx = DistanceXfromPlayer();
544-
int sy = DistanceYfromPlayer();
543+
int Game_Character::GetDirectionAwayCharacter(const Game_Character* target) {
544+
int sx = DistanceXfromCharacter(target);
545+
int sy = DistanceYfromCharacter(target);
545546

546547
if ( std::abs(sx) > std::abs(sy) ) {
547548
return (sx > 0) ? Right : Left;
@@ -550,12 +551,18 @@ int Game_Character::GetDirectionAwayHero() {
550551
}
551552
}
552553

553-
void Game_Character::TurnTowardHero() {
554-
SetDirection(GetDirectionToHero());
554+
void Game_Character::TurnTowardCharacter(int targetID) {
555+
Game_Character* target = GetCharacter(targetID, targetID);
556+
if (target) {
557+
SetDirection(GetDirectionToCharacter(target));
558+
}
555559
}
556560

557-
void Game_Character::TurnAwayFromHero() {
558-
SetDirection(GetDirectionAwayHero());
561+
void Game_Character::TurnAwayFromCharacter(int targetID) {
562+
Game_Character* target = GetCharacter(targetID, targetID);
563+
if (target) {
564+
SetDirection(GetDirectionAwayCharacter(target));
565+
}
559566
}
560567

561568
void Game_Character::TurnRandom() {
@@ -591,10 +598,10 @@ bool Game_Character::BeginMoveRouteJump(int32_t& current_index, const lcf::rpg::
591598
TurnRandom();
592599
break;
593600
case Code::move_towards_hero:
594-
TurnTowardHero();
601+
TurnTowardCharacter(CharPlayer);
595602
break;
596603
case Code::move_away_from_hero:
597-
TurnAwayFromHero();
604+
TurnAwayFromCharacter(CharPlayer);
598605
break;
599606
case Code::move_forward:
600607
break;
@@ -635,10 +642,10 @@ bool Game_Character::BeginMoveRouteJump(int32_t& current_index, const lcf::rpg::
635642
TurnRandom();
636643
break;
637644
case Code::face_hero:
638-
TurnTowardHero();
645+
TurnTowardCharacter(CharPlayer);
639646
break;
640647
case Code::face_away_from_hero:
641-
TurnAwayFromHero();
648+
TurnAwayFromCharacter(CharPlayer);
642649
break;
643650
default:
644651
break;
@@ -724,32 +731,39 @@ bool Game_Character::Jump(int x, int y) {
724731
return true;
725732
}
726733

727-
int Game_Character::DistanceXfromPlayer() const {
728-
int sx = GetX() - Main_Data::game_player->GetX();
729-
if (Game_Map::LoopHorizontal()) {
730-
if (std::abs(sx) > Game_Map::GetTilesX() / 2) {
731-
if (sx > 0)
732-
sx -= Game_Map::GetTilesX();
733-
else
734-
sx += Game_Map::GetTilesX();
734+
int Game_Character::DistanceXfromCharacter(const Game_Character* target) const {
735+
if (target) {
736+
int sx = GetX() - target->GetX();
737+
if (Game_Map::LoopHorizontal()) {
738+
if (std::abs(sx) > Game_Map::GetTilesX() / 2) {
739+
if (sx > 0)
740+
sx -= Game_Map::GetTilesX();
741+
else
742+
sx += Game_Map::GetTilesX();
743+
}
735744
}
745+
return sx;
736746
}
737-
return sx;
747+
return 0; // Return a default value or handle the case where target is nullptr.
738748
}
739749

740-
int Game_Character::DistanceYfromPlayer() const {
741-
int sy = GetY() - Main_Data::game_player->GetY();
742-
if (Game_Map::LoopVertical()) {
743-
if (std::abs(sy) > Game_Map::GetTilesY() / 2) {
744-
if (sy > 0)
745-
sy -= Game_Map::GetTilesY();
746-
else
747-
sy += Game_Map::GetTilesY();
750+
int Game_Character::DistanceYfromCharacter(const Game_Character* target) const {
751+
if (target) {
752+
int sy = GetY() - target->GetY();
753+
if (Game_Map::LoopVertical()) {
754+
if (std::abs(sy) > Game_Map::GetTilesY() / 2) {
755+
if (sy > 0)
756+
sy -= Game_Map::GetTilesY();
757+
else
758+
sy += Game_Map::GetTilesY();
759+
}
748760
}
761+
return sy;
749762
}
750-
return sy;
763+
return 0; // Return a default value or handle the case where target is nullptr.
751764
}
752765

766+
753767
void Game_Character::ForceMoveRoute(const lcf::rpg::MoveRoute& new_route,
754768
int frequency) {
755769
if (!IsMoveRouteOverwritten()) {

src/game_character.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,10 @@ class Game_Character {
607607
void Turn90DegreeLeftOrRight();
608608

609609
/** @return the direction we would need to face the hero. */
610-
int GetDirectionToHero();
610+
int GetDirectionToCharacter(const Game_Character* target);
611611

612612
/** @return the direction we would need to face away from hero. */
613-
int GetDirectionAwayHero();
613+
int GetDirectionAwayCharacter(const Game_Character* target);
614614

615615
/**
616616
* @param dir input direction
@@ -641,13 +641,12 @@ class Game_Character {
641641
/**
642642
* Character looks towards the hero.
643643
*/
644-
void TurnTowardHero();
644+
void TurnTowardCharacter(int targetID);
645645

646646
/**
647647
* Character looks away from the hero.
648648
*/
649-
void TurnAwayFromHero();
650-
649+
void TurnAwayFromCharacter(int targetID);
651650
/**
652651
* Character waits for 20 frames more.
653652
*/
@@ -743,8 +742,8 @@ class Game_Character {
743742
*/
744743
void SetAnimationType(AnimType anim_type);
745744

746-
int DistanceXfromPlayer() const;
747-
int DistanceYfromPlayer() const;
745+
int Game_Character::DistanceXfromCharacter(const Game_Character* target) const;
746+
int Game_Character::DistanceYfromCharacter(const Game_Character* target) const;
748747

749748
/**
750749
* Tests if the character is currently on the tile at x/y or moving

src/game_event.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ bool Game_Event::ScheduleForegroundExecution(bool by_decision_key, bool face_pla
349349
}
350350

351351
if (face_player && !(IsFacingLocked() || IsSpinning())) {
352-
SetFacing(GetDirectionToHero());
352+
SetFacing(GetDirectionToCharacter(GetCharacter(CharPlayer, CharPlayer)));
353353
}
354354

355355
data()->waiting_execution = true;
@@ -570,8 +570,8 @@ void Game_Event::MoveTypeTowardsOrAwayPlayer(bool towards) {
570570
dir = Rand::GetRandomNumber(0, 3);
571571
} else {
572572
dir = towards
573-
? GetDirectionToHero()
574-
: GetDirectionAwayHero();
573+
? GetDirectionToCharacter(GetCharacter(CharPlayer, CharPlayer))
574+
: GetDirectionAwayCharacter(GetCharacter(CharPlayer, CharPlayer));
575575
}
576576
}
577577

src/game_interpreter.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4678,6 +4678,19 @@ bool Game_Interpreter::CommandCallMovement(lcf::rpg::EventCommand const& com) {
46784678
event->SetY(target->GetY());
46794679
}
46804680

4681+
if (moveCommand == "FaceTowards"){
4682+
if(!event->IsMoving()) {
4683+
event->TurnTowardCharacter(outputParam);
4684+
event->UpdateFacing();
4685+
}
4686+
}
4687+
if (moveCommand == "FaceAway"){
4688+
if (!event->IsMoving()) {
4689+
event->TurnAwayFromCharacter(outputParam);
4690+
event->UpdateFacing();
4691+
}
4692+
}
4693+
46814694
if (moveCommand == "SetFacingLocked")event->SetFacingLocked(outputParam);
46824695
if (moveCommand == "SetLayer")event->SetLayer(outputParam);
46834696
if (moveCommand == "SetFlying")event->SetFlying(outputParam); //FIXME: I wish any event could imitate an airship, lacks more work.

0 commit comments

Comments
 (0)