diff --git a/modules/wave_survival/enemies/enemy_wretched.cpp b/modules/wave_survival/enemies/enemy_wretched.cpp index 91174e33..040234f9 100644 --- a/modules/wave_survival/enemies/enemy_wretched.cpp +++ b/modules/wave_survival/enemies/enemy_wretched.cpp @@ -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(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" } diff --git a/modules/wave_survival/enemies/enemy_wretched.h b/modules/wave_survival/enemies/enemy_wretched.h index 62bb4749..b96cbfbb 100644 --- a/modules/wave_survival/enemies/enemy_wretched.h +++ b/modules/wave_survival/enemies/enemy_wretched.h @@ -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 { diff --git a/modules/wave_survival/enemy_body.cpp b/modules/wave_survival/enemy_body.cpp index 669c4b21..b1005b5b 100644 --- a/modules/wave_survival/enemy_body.cpp +++ b/modules/wave_survival/enemy_body.cpp @@ -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(); } diff --git a/modules/wave_survival/npc_unit.cpp b/modules/wave_survival/npc_unit.cpp index 6d0a6d61..7111be73 100644 --- a/modules/wave_survival/npc_unit.cpp +++ b/modules/wave_survival/npc_unit.cpp @@ -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) { diff --git a/modules/wave_survival/npc_unit.h b/modules/wave_survival/npc_unit.h index 9d489b85..6f649912 100644 --- a/modules/wave_survival/npc_unit.h +++ b/modules/wave_survival/npc_unit.h @@ -27,7 +27,7 @@ private: bool aware_of_player{ false }; Vector npcs{}; PatrolPath *patrol_path{ nullptr }; - float patrol_speed{ 3.f }; + float patrol_speed{ 1.f }; }; #endif // !NPC_UNIT_H diff --git a/project/assets/models/enemies/model_scenes/wretched.tscn b/project/assets/models/enemies/model_scenes/wretched.tscn index 70ef0b58..ecd3a187 100644 --- a/project/assets/models/enemies/model_scenes/wretched.tscn +++ b/project/assets/models/enemies/model_scenes/wretched.tscn @@ -12,7 +12,7 @@ size = Vector3(0.37524414, 0.64245605, 0.38867188) size = Vector3(0.24902344, 0.42858887, 0.2619629) [sub_resource type="BoxShape3D" id="BoxShape3D_yvqru"] -size = Vector3(0.86776733, 0.107666016, 0.11816406) +size = Vector3(0.35500336, 0.107666016, 0.11816406) [node name="wretched" instance=ExtResource("1_w0sx6")] diff --git a/project/objects/enemies/enemy_wretched.tscn b/project/objects/enemies/enemy_wretched.tscn index 34587353..e9c700b5 100644 --- a/project/objects/enemies/enemy_wretched.tscn +++ b/project/objects/enemies/enemy_wretched.tscn @@ -12,6 +12,7 @@ func _on_health_status_death() -> void: [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_ng1ul"] [node name="EnemyWretched" type="EnemyWretched"] +chase_speed = 2.0 script = SubResource("GDScript_qot2n") [node name="wretched" parent="." instance=ExtResource("1_qot2n")] @@ -25,6 +26,9 @@ health = NodePath("../../../../../HealthStatus") [node name="Hitbox" parent="wretched/Character/Skeleton3D/HeadAttach" index="0" node_paths=PackedStringArray("health")] health = NodePath("../../../../../HealthStatus") +[node name="CollisionShape3D" parent="wretched/Character/Skeleton3D/BoneAttachment3D/DamageBox" index="0"] +transform = Transform3D(1, -9.834766e-07, -2.454035e-06, 1.4305115e-06, 0.9999989, -8.145726e-06, 1.8983264e-06, 7.3344854e-06, 1, -0.20908, -5.9604645e-07, -4.7683716e-07) + [node name="CollisionShape3D" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) shape = SubResource("CapsuleShape3D_ng1ul") @@ -35,6 +39,10 @@ unique_name_in_owner = true [node name="NavigationAgent3D" type="NavigationAgent3D" parent="."] unique_name_in_owner = true path_desired_distance = 0.25 +avoidance_enabled = true +neighbor_distance = 10.0 +time_horizon_agents = 0.7 +debug_enabled = true [node name="PlayerDetector" type="PlayerDetector" parent="."] unique_name_in_owner = true diff --git a/project/objects/units/unit_4_wretched.tscn b/project/objects/units/unit_4_wretched.tscn index bc1e47b7..fc9b8f8c 100644 --- a/project/objects/units/unit_4_wretched.tscn +++ b/project/objects/units/unit_4_wretched.tscn @@ -5,13 +5,22 @@ [node name="NpcUnit" type="NpcUnit"] [node name="EnemyWretched" parent="." instance=ExtResource("1_l77gx")] -transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, -0.9145346, 0.023195954, -0.63123465) +transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, -1.1853347, 0.023195954, -0.8597343) + +[node name="EnemyWretched5" parent="." instance=ExtResource("1_l77gx")] +transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, 0.3304333, 0.023195954, 1.0620495) + +[node name="EnemyWretched6" parent="." instance=ExtResource("1_l77gx")] +transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, 0.026455224, 0.023195954, -1.7789332) + +[node name="EnemyWretched7" parent="." instance=ExtResource("1_l77gx")] +transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, 0.054681122, 0.023195954, -0.4100113) [node name="EnemyWretched2" parent="." instance=ExtResource("1_l77gx")] -transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, 1.0534863, 0.023196908, -0.6773462) +transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, 1.3425274, 0.023196908, -0.9988682) [node name="EnemyWretched3" parent="." instance=ExtResource("1_l77gx")] -transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, -0.5066114, 0.023196908, 0.93247986) +transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, -1.06229, 0.023196908, 0.46605587) [node name="EnemyWretched4" parent="." instance=ExtResource("1_l77gx")] -transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, 1.3384295, 0.023197861, 0.74458694) +transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, 1.1567543, 0.023197861, 0.23159564)