2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:29:17 +01:00
|
|
|
#include "gsai_cohesion.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 02:31:42 +01:00
|
|
|
#include "../gsai_steering_agent.h"
|
|
|
|
#include "../gsai_target_acceleration.h"
|
|
|
|
#include "../proximities/gsai_proximity.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 02:31:42 +01:00
|
|
|
void GSAICohesion::_calculate_steering(Ref<GSAITargetAcceleration> acceleration) {
|
|
|
|
ERR_FAIL_COND(!proximity.is_valid());
|
|
|
|
ERR_FAIL_COND(!agent.is_valid());
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 02:31:42 +01:00
|
|
|
acceleration->set_zero();
|
|
|
|
_center_of_mass = Vector3();
|
|
|
|
int neighbor_count = proximity->find_neighbors(_callback);
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (neighbor_count > 0) {
|
|
|
|
_center_of_mass *= 1.0 / neighbor_count;
|
2023-01-14 02:31:42 +01:00
|
|
|
acceleration->set_linear((_center_of_mass - agent->get_position()).normalized() * agent->get_linear_acceleration_max());
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:31:42 +01:00
|
|
|
bool GSAICohesion::_report_neighbor(Ref<GSAISteeringAgent> neighbor) {
|
|
|
|
_center_of_mass += neighbor->get_position();
|
2023-01-13 21:35:07 +01:00
|
|
|
return true;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAICohesion::GSAICohesion() {
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAICohesion::~GSAICohesion() {
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 02:31:42 +01:00
|
|
|
void GSAICohesion::_bind_methods() {
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|