feat: added PlayerState base class and FollowingPlayer state

This commit is contained in:
Sara 2024-03-22 00:16:19 +01:00
parent 5ab4540cc5
commit 7532a68b91
2 changed files with 75 additions and 0 deletions

43
src/player_states.cpp Normal file
View file

@ -0,0 +1,43 @@
#include "player_states.hpp"
#include "player_character.hpp"
#include "tunnels_game_mode.hpp"
#include "utils/game_root.hpp"
#include <godot_cpp/variant/utility_functions.hpp>
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<PlayerCharacter>(target);
if(!this->target)
UtilityFunctions::push_error("Attempt to apply player state to non-PlayerCharacter node of type '", target->get_class(), "'");
Ref<TunnelsGameMode> 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
}

32
src/player_states.hpp Normal file
View file

@ -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