2020-01-10 20:38:12 +01:00
|
|
|
extends KinematicBody2D
|
|
|
|
|
|
|
|
|
|
|
|
var separation: GSTSeparation
|
|
|
|
var cohesion: GSTCohesion
|
|
|
|
var proximity: GSTRadiusProximity
|
2020-01-16 16:04:27 +01:00
|
|
|
var agent := GSTSteeringAgent.new()
|
2020-01-16 09:44:44 +01:00
|
|
|
var blend := GSTBlend.new(agent)
|
|
|
|
var acceleration := GSTTargetAcceleration.new()
|
|
|
|
var draw_proximity := false
|
2020-01-10 20:38:12 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var _color := Color.red
|
|
|
|
var _velocity := Vector2()
|
2020-01-10 20:38:12 +01:00
|
|
|
|
2020-02-03 17:07:43 +01:00
|
|
|
onready var collision_shape := $CollisionShape2D
|
|
|
|
|
2020-01-10 20:38:12 +01:00
|
|
|
|
2020-01-15 20:15:31 +01:00
|
|
|
func setup(
|
2020-01-22 17:55:49 +01:00
|
|
|
linear_speed_max: float,
|
|
|
|
linear_accel_max: float,
|
2020-01-15 20:15:31 +01:00
|
|
|
proximity_radius: float,
|
|
|
|
separation_decay_coefficient: float,
|
|
|
|
cohesion_strength: float,
|
|
|
|
separation_strength: float
|
|
|
|
) -> void:
|
2020-01-10 20:38:12 +01:00
|
|
|
_color = Color(rand_range(0.5, 1), rand_range(0.25, 1), rand_range(0, 1))
|
2020-02-03 17:07:43 +01:00
|
|
|
collision_shape.inner_color = _color
|
2020-01-15 20:15:31 +01:00
|
|
|
|
2020-01-22 17:55:49 +01:00
|
|
|
agent.linear_acceleration_max = linear_accel_max
|
|
|
|
agent.linear_speed_max = linear_speed_max
|
2020-01-10 20:38:12 +01:00
|
|
|
|
2020-01-15 20:15:31 +01:00
|
|
|
proximity = GSTRadiusProximity.new(agent, [], proximity_radius)
|
2020-01-10 20:38:12 +01:00
|
|
|
separation = GSTSeparation.new(agent, proximity)
|
2020-01-15 20:15:31 +01:00
|
|
|
separation.decay_coefficient = separation_decay_coefficient
|
2020-01-10 20:38:12 +01:00
|
|
|
cohesion = GSTCohesion.new(agent, proximity)
|
2020-01-15 20:15:31 +01:00
|
|
|
blend.add(separation, separation_strength)
|
|
|
|
blend.add(cohesion, cohesion_strength)
|
2020-01-10 20:38:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _draw() -> void:
|
2020-01-15 20:15:31 +01:00
|
|
|
if draw_proximity:
|
2020-02-03 17:07:43 +01:00
|
|
|
draw_circle(Vector2.ZERO, proximity.radius, Color(0.4, 1.0, 0.89, 0.3))
|
2020-01-10 20:38:12 +01:00
|
|
|
|
|
|
|
|
2020-01-16 18:31:02 +01:00
|
|
|
func _physics_process(delta: float) -> void:
|
2020-01-16 16:04:27 +01:00
|
|
|
agent.position.x = global_position.x
|
|
|
|
agent.position.y = global_position.y
|
2020-01-10 20:38:12 +01:00
|
|
|
if blend:
|
2020-02-06 20:46:21 +01:00
|
|
|
blend.calculate_steering(acceleration)
|
2020-01-16 16:04:27 +01:00
|
|
|
_velocity += Vector2(acceleration.linear.x, acceleration.linear.y)
|
2020-01-16 18:31:02 +01:00
|
|
|
_velocity = _velocity.linear_interpolate(Vector2.ZERO, 0.1)
|
2020-01-22 17:55:49 +01:00
|
|
|
_velocity = _velocity.clamped(agent.linear_speed_max)
|
2020-01-10 20:38:12 +01:00
|
|
|
move_and_slide(_velocity)
|
|
|
|
|
|
|
|
|
|
|
|
func set_neighbors(neighbor: Array) -> void:
|
|
|
|
proximity.agents = neighbor
|