2020-01-29 17:04:04 +01:00
|
|
|
# Calculates an acceleration that attempts to move the agent towards the center
|
2020-02-11 18:33:25 +01:00
|
|
|
# of mass of the agents in the area defined by the `GSAIProximity`.
|
2020-04-03 02:31:59 +02:00
|
|
|
# @category - Group behaviors
|
2020-02-11 18:33:25 +01:00
|
|
|
class_name GSAICohesion
|
|
|
|
extends GSAIGroupBehavior
|
2020-01-09 18:24:55 +01:00
|
|
|
|
2020-01-30 19:06:35 +01:00
|
|
|
var _center_of_mass: Vector3
|
2020-01-09 18:24:55 +01:00
|
|
|
|
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
func _init(agent: GSAISteeringAgent, proximity: GSAIProximity).(agent, proximity) -> void:
|
2020-01-10 18:15:50 +01:00
|
|
|
pass
|
2020-01-09 18:24:55 +01:00
|
|
|
|
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
2020-01-09 18:24:55 +01:00
|
|
|
acceleration.set_zero()
|
2020-01-30 19:06:35 +01:00
|
|
|
_center_of_mass = Vector3.ZERO
|
2020-01-29 22:53:57 +01:00
|
|
|
var neighbor_count = proximity._find_neighbors(_callback)
|
2020-01-09 18:24:55 +01:00
|
|
|
if neighbor_count > 0:
|
2020-01-30 19:06:35 +01:00
|
|
|
_center_of_mass *= 1.0 / neighbor_count
|
2020-02-14 17:35:18 +01:00
|
|
|
acceleration.linear = (
|
|
|
|
(_center_of_mass - agent.position).normalized()
|
|
|
|
* agent.linear_acceleration_max
|
|
|
|
)
|
2020-01-09 18:24:55 +01:00
|
|
|
|
|
|
|
|
2020-01-29 05:56:10 +01:00
|
|
|
# Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position
|
|
|
|
# to the center of mass of the group.
|
2020-04-03 02:31:59 +02:00
|
|
|
# @tags - virtual
|
2020-02-11 18:33:25 +01:00
|
|
|
func _report_neighbor(neighbor: GSAISteeringAgent) -> bool:
|
2020-01-30 19:06:35 +01:00
|
|
|
_center_of_mass += neighbor.position
|
2020-01-09 18:24:55 +01:00
|
|
|
return true
|