Skip to content

Commit

Permalink
FaceTowards and FaceAway Commands
Browse files Browse the repository at this point in the history
```js
@raw 2050, "FaceTowards", eventAIsVar,eventA, eventBisVar, eventB
```
```js
@raw 2050, "FaceAway", eventAIsVar,eventA, eventBisVar, eventB
```
  • Loading branch information
jetrotal committed Oct 8, 2023
1 parent 6452470 commit feeed6f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 48 deletions.
90 changes: 52 additions & 38 deletions src/game_character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
13 changes: 6 additions & 7 deletions src/game_character.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -743,8 +742,8 @@ class Game_Character {
*/
void SetAnimationType(AnimType anim_type);

int DistanceXfromPlayer() const;
int DistanceYfromPlayer() const;
int Game_Character::DistanceXfromCharacter(const Game_Character* target) const;
int Game_Character::DistanceYfromCharacter(const Game_Character* target) const;

/**
* Tests if the character is currently on the tile at x/y or moving
Expand Down
6 changes: 3 additions & 3 deletions src/game_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit feeed6f

Please sign in to comment.