feat: implemented avoidance and exposed speed settings to editor
This commit is contained in:
parent
fb9417a1d8
commit
2134fcae92
8 changed files with 61 additions and 14 deletions
|
|
@ -1,11 +1,13 @@
|
|||
#include "enemy_wretched.h"
|
||||
#include "scene/animation/animation_player.h"
|
||||
#include "wave_survival/macros.h"
|
||||
#include "wave_survival/npc_unit.h"
|
||||
#include "wave_survival/patrol_path.h"
|
||||
#include "wave_survival/player_body.h"
|
||||
#include "wave_survival/state_machine.h"
|
||||
|
||||
void EnemyWretched::_bind_methods() {
|
||||
BIND_PROPERTY(Variant::FLOAT, chase_speed);
|
||||
}
|
||||
|
||||
void EnemyWretched::on_child_entered(Node *node) {
|
||||
|
|
@ -16,6 +18,7 @@ void EnemyWretched::on_child_entered(Node *node) {
|
|||
this->anim = cast_to<AnimationPlayer>(node->get_node(NodePath("AnimationPlayer")));
|
||||
}
|
||||
}
|
||||
|
||||
void EnemyWretched::ready() {
|
||||
fsm->add_state(memnew(WretchedPatrolState));
|
||||
fsm->add_state(memnew(WretchedChaseState));
|
||||
|
|
@ -38,6 +41,14 @@ void EnemyWretched::_notification(int what) {
|
|||
}
|
||||
}
|
||||
|
||||
void EnemyWretched::set_chase_speed(float speed) {
|
||||
this->chase_speed = speed;
|
||||
}
|
||||
|
||||
float EnemyWretched::get_chase_speed() const {
|
||||
return this->chase_speed;
|
||||
}
|
||||
|
||||
AnimationPlayer *EnemyWretched::get_anim() const {
|
||||
return this->anim;
|
||||
}
|
||||
|
|
@ -66,6 +77,10 @@ void WretchedPatrolState::set_patrol_target(Vector3 path_point) {
|
|||
get_nav()->set_target_position(path_point + get_target()->get_unit_offset_3d());
|
||||
}
|
||||
|
||||
void WretchedPatrolState::on_velocity_calculated(Vector3 direction) {
|
||||
get_target()->set_movement_direction(Vector2{ direction.x, direction.z } / get_target()->get_movement_speed());
|
||||
}
|
||||
|
||||
void WretchedPatrolState::enter_state() {
|
||||
this->path = get_target()->get_unit()->get_patrol_path();
|
||||
|
||||
|
|
@ -76,6 +91,7 @@ void WretchedPatrolState::enter_state() {
|
|||
Vector3 const nav_target{ this->path->get_closest_point(get_target()->get_global_position(), &this->path_point) };
|
||||
set_patrol_target(nav_target);
|
||||
}
|
||||
get_nav()->connect("velocity_computed", this->mp_on_velocity_calculated);
|
||||
}
|
||||
|
||||
void WretchedPatrolState::process(double delta) {
|
||||
|
|
@ -84,11 +100,19 @@ void WretchedPatrolState::process(double delta) {
|
|||
this->path_point += 1;
|
||||
set_patrol_target(this->path->point_at(this->path_point));
|
||||
}
|
||||
Vector3 const direction{ get_target()->get_global_position().direction_to(get_nav()->get_next_path_position()) };
|
||||
get_target()->set_movement_direction(Vector2{ direction.x, direction.z }.normalized());
|
||||
Vector3 const direction{ get_target()->get_global_position().direction_to(get_nav()->get_next_path_position()).normalized() };
|
||||
if (get_nav()->get_avoidance_enabled()) {
|
||||
get_nav()->set_velocity(direction);
|
||||
} else {
|
||||
on_velocity_calculated(direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WretchedPatrolState::exit_state() {
|
||||
get_nav()->disconnect("velocity_computed", this->mp_on_velocity_calculated);
|
||||
}
|
||||
|
||||
String WretchedPatrolState::get_next_state() const {
|
||||
if (get_target()->get_unit()->is_aware_of_player()) {
|
||||
return WretchedChaseState::get_class_static();
|
||||
|
|
@ -97,9 +121,8 @@ String WretchedPatrolState::get_next_state() const {
|
|||
}
|
||||
|
||||
void WretchedChaseState::enter_state() {
|
||||
// TODO: replace this with a setting somewhere
|
||||
get_target()->set_movement_speed(get_unit()->get_patrol_speed() * 2.f);
|
||||
get_nav()->set_max_speed(get_unit()->get_patrol_speed() * 2.f);
|
||||
get_target()->set_movement_speed(get_target()->get_chase_speed());
|
||||
get_nav()->set_max_speed(get_target()->get_chase_speed());
|
||||
get_nav()->set_target_position(PlayerBody::get_singleton()->get_global_position());
|
||||
get_anim()->play("ready"); // TODO: replace this with "run"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,9 +18,12 @@ protected:
|
|||
void _notification(int what);
|
||||
|
||||
public:
|
||||
void set_chase_speed(float speed);
|
||||
float get_chase_speed() const;
|
||||
AnimationPlayer *get_anim() const;
|
||||
|
||||
private:
|
||||
float chase_speed{ 5.f };
|
||||
StateMachine *fsm{ nullptr };
|
||||
AnimationPlayer *anim{ nullptr };
|
||||
};
|
||||
|
|
@ -46,15 +49,18 @@ class WretchedPatrolState : public WretchedState {
|
|||
GDCLASS(WretchedPatrolState, WretchedState);
|
||||
static void _bind_methods() {}
|
||||
void set_patrol_target(Vector3 path_point);
|
||||
void on_velocity_calculated(Vector3 direction);
|
||||
|
||||
public:
|
||||
virtual void enter_state() override;
|
||||
virtual void process(double delta) override;
|
||||
virtual String get_next_state() const override;
|
||||
virtual void exit_state() override;
|
||||
|
||||
private:
|
||||
int path_point{ 0 };
|
||||
PatrolPath *path{ nullptr };
|
||||
Callable mp_on_velocity_calculated{ callable_mp(this, &self_type::on_velocity_calculated) };
|
||||
};
|
||||
|
||||
class WretchedChaseState : public WretchedState {
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ void EnemyBody::ready() {
|
|||
|
||||
void EnemyBody::physics_process(double delta) {
|
||||
GETSET(velocity, {
|
||||
velocity = Vector3{ this->movement_direction.x, velocity.y, this->movement_direction.y };
|
||||
velocity = Vector3{ this->movement_direction.x * this->movement_speed, velocity.y, this->movement_direction.y * this->movement_speed };
|
||||
});
|
||||
if (!get_velocity().is_zero_approx()) {
|
||||
look_at(get_global_position() + get_velocity());
|
||||
if (!this->movement_direction.is_zero_approx()) {
|
||||
look_at(get_global_position() + Vector3{ this->movement_direction.x, 0.f, this->movement_direction.y });
|
||||
}
|
||||
move_and_slide();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
void NpcUnit::_bind_methods() {
|
||||
BIND_HPROPERTY(Variant::OBJECT, patrol_path, PROPERTY_HINT_NODE_TYPE, "PatrolPath");
|
||||
BIND_PROPERTY(Variant::FLOAT, patrol_speed);
|
||||
}
|
||||
|
||||
void NpcUnit::on_npc_awareness_changed(bool value) {
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ private:
|
|||
bool aware_of_player{ false };
|
||||
Vector<EnemyBody *> npcs{};
|
||||
PatrolPath *patrol_path{ nullptr };
|
||||
float patrol_speed{ 3.f };
|
||||
float patrol_speed{ 1.f };
|
||||
};
|
||||
|
||||
#endif // !NPC_UNIT_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue