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
|
|
|
|
|
|
|
|
|
|
|
var center_of_mass: Vector3
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
|
|
|
acceleration.set_zero()
|
|
|
|
center_of_mass = Vector3.ZERO
|
2020-01-10 18:15:50 +01:00
|
|
|
var neighbor_count = proximity.find_neighbors(_callback)
|
2020-01-09 18:24:55 +01:00
|
|
|
if neighbor_count > 0:
|
|
|
|
center_of_mass *= 1.0 / neighbor_count
|
2020-01-22 17:55:49 +01:00
|
|
|
acceleration.linear = (center_of_mass - agent.position).normalized() * agent.linear_acceleration_max
|
2020-01-09 18:24:55 +01:00
|
|
|
return acceleration
|
|
|
|
|
|
|
|
|
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-01-10 18:15:50 +01:00
|
|
|
func report_neighbor(neighbor: GSTSteeringAgent) -> bool:
|
2020-01-09 18:24:55 +01:00
|
|
|
center_of_mass += neighbor.position
|
|
|
|
return true
|