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-01-15 20:15:31 +01:00
|
|
|
func setup(
|
|
|
|
max_linear_speed: float,
|
|
|
|
max_linear_accel: float,
|
|
|
|
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-01-15 20:15:31 +01:00
|
|
|
$Sprite.modulate = _color
|
|
|
|
|
|
|
|
agent.max_linear_acceleration = max_linear_accel
|
|
|
|
agent.max_linear_speed = max_linear_speed
|
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:
|
|
|
|
draw_circle(Vector2.ZERO, proximity.radius, Color(0, 1, 0, 0.1))
|
2020-01-10 20:38:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _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:
|
|
|
|
acceleration = blend.calculate_steering(acceleration)
|
2020-01-16 16:04:27 +01:00
|
|
|
_velocity += Vector2(acceleration.linear.x, acceleration.linear.y)
|
|
|
|
_velocity = _velocity.clamped(agent.max_linear_speed)
|
2020-01-10 20:38:12 +01:00
|
|
|
move_and_slide(_velocity)
|
|
|
|
|
|
|
|
|
|
|
|
func set_neighbors(neighbor: Array) -> void:
|
|
|
|
proximity.agents = neighbor
|