feat: simplified following behaviour of characters

This commit is contained in:
Sara 2024-03-23 22:19:25 +01:00
parent d78ce22e8b
commit 173feaef28
4 changed files with 5 additions and 60 deletions

View file

@ -22,6 +22,7 @@ public:
virtual void _physics_process(double delta_time) override;
void move(Vector3 world_vector);
void aim(Vector3 at);
void aim_direction(Vector3 direction);
void move_to(Vector3 to, float target_distance = 0.5f);
void shoot_at(Vector3 at);
void set_firing(bool firing);
@ -37,6 +38,7 @@ public:
void set_weapon_muzzle(Node3D *node);
void set_velocity_target(Vector3 value);
Vector3 get_velocity_target() const;
protected:
void process_ai(double delta_time);

View file

@ -25,13 +25,6 @@ StringName PlayerState::get_next() const {
return this->get_class();
}
float PlayerState::get_distance_from_player(bool sqr) const {
Vector3 const target_position = this->get_manual_character()->get_global_position();
return sqr
? target_position.distance_squared_to(this->parent->get_global_position())
: target_position.distance_to(this->parent->get_global_position());
}
PlayerCharacter *PlayerState::get_manual_character() const {
return this->player->get_character();
}
@ -40,16 +33,10 @@ 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;
Vector3 const local = Vector3(0.f, (std::signbit(player_basis.get_column(0).dot(offset)) ? 1.f : -1.f), -1.f).normalized() * 3.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
void FollowingPlayer::_bind_methods() {
@ -57,40 +44,9 @@ void FollowingPlayer::_bind_methods() {
}
void FollowingPlayer::_process(double delta_time) {
this->parent->move(this->get_manual_character()->get_velocity_target().normalized());
}
StringName FollowingPlayer::get_next() const {
if(this->get_distance_from_player(true) > std::pow(4.f, 2.f))
return "Regroup";
return this->get_class();
this->parent->move_to(this->get_home_position(), 1.f);
this->parent->aim_direction(this->parent->get_velocity_target());
}
#undef CLASSNAME // FollowingPlayer
void Regroup::_bind_methods() {
#define CLASSNAME Regroup
}
void Regroup::_process(double delta_time) {
Basis const player_basis = this->get_manual_character()->get_global_transform().basis;
Vector3 const parent_position = this->parent->get_global_position();
Vector3 const player_position = this->get_manual_character()->get_global_position();
Vector3 const left = player_basis.get_column(2);
Vector3 const offset = left.dot(parent_position - player_position) * left;
this->target = player_position - player_basis.get_column(2) + offset;
this->parent->move_to(this->target, 1.f);
}
void Regroup::_exit_tree() {
this->parent->move_to(this->parent->get_global_position(), 0.5f);
}
StringName Regroup::get_next() const {
if(this->target.distance_squared_to(this->parent->get_global_position()) < std::pow(1.f, 2.f))
return "FollowingPlayer";
return this->get_class();
}
#undef CLASSNAME // Regroup
}

View file

@ -31,18 +31,6 @@ class FollowingPlayer : public PlayerState {
static void _bind_methods();
public:
virtual void _process(double delta_time) override;
virtual StringName get_next() const override;
};
class Regroup : public PlayerState {
GDCLASS(Regroup, PlayerState);
static void _bind_methods();
public:
virtual void _process(double delta_time) override;
virtual void _exit_tree() override;
virtual StringName get_next() const override;
private:
Vector3 target{0.f, 0.f, 0.f};
};
}

View file

@ -58,7 +58,6 @@ void initialize_gdextension_types(ModuleInitializationLevel p_level)
ClassDB::register_class<StateMachine>();
ClassDB::register_class<PlayerState>();
ClassDB::register_class<FollowingPlayer>();
ClassDB::register_class<Regroup>();
}
extern "C"