feat: added get_home_position and in_player_line_of_fire to PlayerState

This commit is contained in:
Sara 2024-03-23 13:12:08 +01:00
parent 22397bbf4c
commit b4d785674e
2 changed files with 17 additions and 0 deletions

View file

@ -2,6 +2,7 @@
#include "player_character.hpp" #include "player_character.hpp"
#include "tunnels_game_mode.hpp" #include "tunnels_game_mode.hpp"
#include "utils/game_root.hpp" #include "utils/game_root.hpp"
#include <cmath>
#include <godot_cpp/variant/utility_functions.hpp> #include <godot_cpp/variant/utility_functions.hpp>
namespace godot { namespace godot {
@ -35,6 +36,20 @@ PlayerCharacter *PlayerState::get_manual_character() const {
return this->player->get_character(); return this->player->get_character();
} }
Vector3 PlayerState::get_home_position() const {
Basis const player_basis = this->get_manual_character()->get_global_transform().basis;
Vector3 const player_origin = this->get_manual_character()->get_global_position();
Vector3 const offset = this->parent->get_global_position() - player_origin;
Vector3 const local = Vector3(0.f, (std::signbit(player_basis.get_column(0).dot(offset)) ? 1.f : -1.f), -1.f).normalized() * 4.f;
return player_origin + player_basis.get_column(0) * local.x + player_basis.get_column(2) * local.z;
}
bool PlayerState::in_player_line_of_fire() const {
Transform3D const player_transform = this->get_manual_character()->get_global_transform();
Vector3 const offset_dir = (this->parent->get_global_position() - player_transform.origin).normalized();
return player_transform.basis.get_column(2).dot(offset_dir) >= 9.0f;
}
#undef CLASSNAME // PlayerState #undef CLASSNAME // PlayerState
void FollowingPlayer::_bind_methods() { void FollowingPlayer::_bind_methods() {

View file

@ -18,6 +18,8 @@ public:
protected: protected:
float get_distance_from_player(bool sqr = false) const; float get_distance_from_player(bool sqr = false) const;
PlayerCharacter *get_manual_character() const; PlayerCharacter *get_manual_character() const;
Vector3 get_home_position() const;
bool in_player_line_of_fire() const;
protected: protected:
PlayerCharacter *parent{nullptr}; PlayerCharacter *parent{nullptr};
StateMachine *state_machine{nullptr}; StateMachine *state_machine{nullptr};