diff --git a/src/player_states.cpp b/src/player_states.cpp new file mode 100644 index 0000000..1ec935f --- /dev/null +++ b/src/player_states.cpp @@ -0,0 +1,43 @@ +#include "player_states.hpp" +#include "player_character.hpp" +#include "tunnels_game_mode.hpp" +#include "utils/game_root.hpp" +#include + +namespace godot { +void PlayerState::_bind_methods() { +#define CLASSNAME PlayerState +} + +void PlayerState::init(Node *target, StateMachine *machine) { + this->state_machine = machine; + this->target = Object::cast_to(target); + if(!this->target) + UtilityFunctions::push_error("Attempt to apply player state to non-PlayerCharacter node of type '", target->get_class(), "'"); + Ref game_mode = GameRoot::get_singleton()->get_game_mode(); + this->player = game_mode->get_player_instance(); + if(!this->player) + UtilityFunctions::push_error("Failed to find valid TunnelsPlayer instance in game mode"); +} + +StringName PlayerState::get_next() const { + return this->get_class(); +} + +#undef CLASSNAME // PlayerState + +void FollowingPlayer::_bind_methods() { +#define CLASSNAME FollowingPlayer +} + +void FollowingPlayer::_process(double delta_time) { + UtilityFunctions::print(this->get_path(), " _process"); + Vector3 const target_position = this->player->get_character()->get_global_position(); + if(target_position.distance_squared_to(this->target->get_global_position()) < 5.f * 5.f) + this->target->move(this->player->get_character()->get_velocity().normalized()); + else + this->target->move_to(target_position, 5.f); +} + +#undef CLASSNAME // FollowingPlayer +} diff --git a/src/player_states.hpp b/src/player_states.hpp new file mode 100644 index 0000000..aac2aaa --- /dev/null +++ b/src/player_states.hpp @@ -0,0 +1,32 @@ +#ifndef PLAYER_STATES_HPP +#define PLAYER_STATES_HPP + +#include "state.hpp" + +namespace godot { +class PlayerCharacter; +class StateMachine; +class TunnelsPlayer; + +class PlayerState : public Node, + public IState { + GDCLASS(PlayerState, Node); + static void _bind_methods(); +public: + virtual void init(Node *target, StateMachine *machine) override; + virtual StringName get_next() const override; +protected: + PlayerCharacter *target{nullptr}; + StateMachine *state_machine{nullptr}; + TunnelsPlayer *player{nullptr}; +}; + +class FollowingPlayer : public PlayerState { + GDCLASS(FollowingPlayer, PlayerState); + static void _bind_methods(); +public: + virtual void _process(double delta_time) override; +}; +} + +#endif // !PLAYER_STATES_HPP