Compare commits

..

3 commits

13 changed files with 71 additions and 42 deletions

Binary file not shown.

Binary file not shown.

View file

@ -46,24 +46,32 @@ NavigationAgent3D *WretchedState::get_nav() const {
return this->nav;
}
void WretchedPatrolState::set_patrol_target(Vector3 path_point) {
get_nav()->set_target_position(path_point + get_target()->get_unit_offset_3d());
}
void WretchedPatrolState::enter_state() {
this->path = get_target()->get_unit()->get_patrol_path();
float const max_speed{ get_unit()->get_patrol_speed() };
get_target()->set_movement_speed(max_speed);
get_nav()->set_max_speed(max_speed);
if (this->path) {
Vector3 const nav_target{ this->path->get_closest_point(get_target()->get_global_position(), &this->path_point) };
get_nav()->set_target_position(nav_target);
set_patrol_target(nav_target);
}
}
void WretchedPatrolState::process(double delta) {
if (this->path) {
if (get_nav()->is_navigation_finished()) {
this->path_point += 1;
get_nav()->set_target_position(this->path->point_at(this->path_point));
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());
}
}
String WretchedPatrolState::get_next_state() const {
if (get_target()->get_unit()->is_aware_of_player()) {

View file

@ -36,6 +36,7 @@ private:
class WretchedPatrolState : public WretchedState {
GDCLASS(WretchedPatrolState, WretchedState);
static void _bind_methods() {}
void set_patrol_target(Vector3 path_point);
public:
virtual void enter_state() override;

View file

@ -59,3 +59,15 @@ void EnemyBody::set_movement_speed(float value) {
float EnemyBody::get_movement_speed() const {
return this->movement_speed;
}
void EnemyBody::set_unit_offset(Vector2 offset) {
this->unit_offset = offset;
}
Vector2 EnemyBody::get_unit_offset() const {
return this->unit_offset;
}
Vector3 EnemyBody::get_unit_offset_3d() const {
return { this->unit_offset.x, 0, this->unit_offset.y };
}

View file

@ -23,6 +23,9 @@ public:
NavigationAgent3D *get_nav() const;
void set_movement_speed(float value);
float get_movement_speed() const;
void set_unit_offset(Vector2 offset);
Vector2 get_unit_offset() const;
Vector3 get_unit_offset_3d() const;
private:
float movement_speed{ 1.f };
@ -31,6 +34,7 @@ private:
StateMachine *fsm{ nullptr };
NpcUnit *unit{ nullptr };
NavigationAgent3D *nav{ nullptr };
Vector2 unit_offset{ 0, 0 };
};
#endif // !ENEMY_BODY_H

View file

@ -18,10 +18,13 @@ void NpcUnit::child_added(Node *node) {
if (EnemyBody * npc{ cast_to<EnemyBody>(node) }) {
this->npcs.push_back(npc);
npc->set_unit(this);
PlayerDetector *detector{ cast_to<PlayerDetector>(npc->get_node(NodePath("%PlayerDetector"))) };
Vector3 const offset{ npc->get_global_position() - get_global_position() };
npc->set_unit_offset({ offset.x, offset.z });
if (PlayerDetector * detector{ cast_to<PlayerDetector>(npc->get_node(NodePath("%PlayerDetector"))) }) {
detector->connect(PlayerDetector::sig_awareness_changed, callable_mp(this, &self_type::on_npc_awareness_changed));
}
}
}
void NpcUnit::enter_tree() {
this->connect("child_entered_tree", callable_mp(this, &self_type::child_added));

View file

@ -1,13 +1,13 @@
#ifndef NPC_UNIT_H
#define NPC_UNIT_H
#include "scene/main/node.h"
#include "scene/3d/node_3d.h"
class StateMachine;
class EnemyBody;
class PatrolPath;
class NpcUnit : public Node {
GDCLASS(NpcUnit, Node);
class NpcUnit : public Node3D {
GDCLASS(NpcUnit, Node3D);
static void _bind_methods();
void on_npc_awareness_changed(bool seen);
void child_added(Node *node);

View file

@ -56,8 +56,4 @@ void StateMachine::add_state(State *state) {
if (this->current_state == nullptr) {
this->switch_to_state(state);
}
print_line("states:");
for (KeyValue<StringName, State *> kv : this->states) {
print_line(vformat("\t\t| %s %s", kv.key, kv.value));
}
}

File diff suppressed because one or more lines are too long

View file

@ -18,7 +18,6 @@ unique_name_in_owner = true
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
unique_name_in_owner = true
path_desired_distance = 0.25
debug_enabled = true
[node name="PlayerDetector" type="PlayerDetector" parent="."]
unique_name_in_owner = true

View file

@ -0,0 +1,17 @@
[gd_scene load_steps=2 format=3 uid="uid://5hg5eirw7v8n"]
[ext_resource type="PackedScene" uid="uid://dqlqgk1veyos8" path="res://objects/enemies/enemy_wretched.tscn" id="1_l77gx"]
[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)
[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)
[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)
[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)