2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:29:17 +01:00
|
|
|
#include "gsai_follow_path.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIPath GSAIFollowPath::get_ *path() {
|
|
|
|
return *path;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIFollowPath::set_ *path(const GSAIPath &val) {
|
|
|
|
*path = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float GSAIFollowPath::get_path_offset() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return path_offset;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIFollowPath::set_path_offset(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
path_offset = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GSAIFollowPath::get_is_arrive_enabled() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return is_arrive_enabled;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIFollowPath::set_is_arrive_enabled(const bool val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
is_arrive_enabled = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float GSAIFollowPath::get_prediction_time() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return prediction_time;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIFollowPath::set_prediction_time(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
prediction_time = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
// Produces a linear acceleration that moves the agent along the specified path.;
|
|
|
|
// @category - Individual behaviors;
|
|
|
|
// The path to follow and travel along.;
|
|
|
|
GSAIPath *path;
|
|
|
|
// The distance along the path to generate the next target position.;
|
|
|
|
float path_offset = 0.0;
|
|
|
|
// Whether to use `GSAIArrive` behavior on an open path.;
|
|
|
|
bool is_arrive_enabled = true;
|
|
|
|
// The amount of time in the future to predict the owning agent's position along;
|
|
|
|
// the path. Setting it to 0.0 will force non-predictive path following.;
|
|
|
|
float prediction_time = 0.0;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIFollowPath::_calculate_steering(const GSAITargetAcceleration &acceleration) {
|
|
|
|
Vector3 location = ;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (prediction_time == 0) {
|
|
|
|
location = agent.position;
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
else {
|
|
|
|
location = agent.position + (agent.linear_velocity * prediction_time);
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
float distance = path.calculate_distance(location);
|
|
|
|
float target_distance = distance + path_offset;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (prediction_time > 0 && path.is_open) {
|
|
|
|
if (target_distance < path.calculate_distance(agent.position)) {
|
|
|
|
target_distance = path.length;
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
Vector3 target_position = path.calculate_target_position(target_distance);
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (is_arrive_enabled && path.is_open) {
|
|
|
|
if (path_offset >= 0) {
|
|
|
|
if (target_distance > path.length - deceleration_radius) {
|
|
|
|
arrive(acceleration, target_position);
|
|
|
|
return;
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
else {
|
|
|
|
if (target_distance < deceleration_radius) {
|
|
|
|
arrive(acceleration, target_position);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
acceleration.linear = (target_position - agent.position).normalized();
|
|
|
|
acceleration.linear *= agent.linear_acceleration_max;
|
|
|
|
acceleration.angular = 0;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIFollowPath::GSAIFollowPath() {
|
|
|
|
*path;
|
|
|
|
path_offset = 0.0;
|
|
|
|
is_arrive_enabled = true;
|
|
|
|
prediction_time = 0.0;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIFollowPath::~GSAIFollowPath() {
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
static void GSAIFollowPath::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_*path"), &GSAIFollowPath::get_ * path);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_*path", "value"), &GSAIFollowPath::set_ * path);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "*path", PROPERTY_HINT_RESOURCE_TYPE, "GSAIPath"), "set_*path", "get_*path");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_path_offset"), &GSAIFollowPath::get_path_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_path_offset", "value"), &GSAIFollowPath::set_path_offset);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_offset"), "set_path_offset", "get_path_offset");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_is_arrive_enabled"), &GSAIFollowPath::get_is_arrive_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_is_arrive_enabled", "value"), &GSAIFollowPath::set_is_arrive_enabled);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_arrive_enabled"), "set_is_arrive_enabled", "get_is_arrive_enabled");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_prediction_time"), &GSAIFollowPath::get_prediction_time);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_prediction_time", "value"), &GSAIFollowPath::set_prediction_time);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "prediction_time"), "set_prediction_time", "get_prediction_time");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_calculate_steering", "acceleration"), &GSAIFollowPath::_calculate_steering);
|
|
|
|
}
|