2020-01-29 17:04:04 +01:00
|
|
|
# Calculates an acceleration that attempts to move the agent towards the center
|
|
|
|
# of mass of the agents in the area defined by the `GSTProximity`.
|
2020-01-29 05:56:10 +01:00
|
|
|
class_name GSTCohesion
|
|
|
|
extends GSTGroupBehavior
|
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-01-10 18:15:50 +01:00
|
|
|
func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) -> void:
|
|
|
|
pass
|
2020-01-09 18:24:55 +01:00
|
|
|
|
|
|
|
|
2020-02-06 20:46:21 +01:00
|
|
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> 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
|
|
|
|
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-02-02 18:48:58 +01:00
|
|
|
# tags: virtual
|
2020-01-29 22:53:57 +01:00
|
|
|
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
|
2020-01-30 19:06:35 +01:00
|
|
|
_center_of_mass += neighbor.position
|
2020-01-09 18:24:55 +01:00
|
|
|
return true
|