2020-01-09 18:24:55 +01:00
|
|
|
class_name GSTCohesion
|
2020-01-16 23:14:50 +01:00
|
|
|
extends GSTGroupBehavior
|
2020-01-09 18:24:55 +01:00
|
|
|
# Group behavior that produces linear acceleration that attempts to move the agent towards the
|
2020-01-27 19:24:05 +01:00
|
|
|
# center of mass of the agents in the area defined by the Proximity.
|
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-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
|