godot-steering-ai-framework/project/demos/GroupBehaviors/Member.gd
Răzvan C. Rădulescu 1baed58659 Review smart agents
I made minimal changes, mostly cosmetic like so:

- rename KinematicMovementType to MovementType since
  GSTKinematicBody2DAgent.KinematicMovementType.COLLIDE for example is
  really more than a mouthful with repeated Kinematic in the name
- add optional movement_type parameter to the constructor, otherwise
  we'd be forced to construct the object and then specify as an
  aditional step the type of movement if we want something else than the
  default
- rewrote the constructor to yield on ready and removed _on_body_ready
- renamed _apply_steering to apply_steering as this is a public method
- renamed _on_SceneTree_frame to _on_SceneTree_physics_frame
2020-02-07 12:29:45 +02:00

54 lines
1.4 KiB
GDScript

extends KinematicBody2D
var separation: GSTSeparation
var cohesion: GSTCohesion
var proximity: GSTRadiusProximity
var agent := GSTKinematicBody2DAgent.new(self)
var blend := GSTBlend.new(agent)
var acceleration := GSTTargetAcceleration.new()
var draw_proximity := false
var _color := Color.red
var _velocity := Vector2()
onready var collision_shape := $CollisionShape2D
func setup(
linear_speed_max: float,
linear_accel_max: float,
proximity_radius: float,
separation_decay_coefficient: float,
cohesion_strength: float,
separation_strength: float
) -> void:
_color = Color(rand_range(0.5, 1), rand_range(0.25, 1), rand_range(0, 1))
collision_shape.inner_color = _color
agent.linear_acceleration_max = linear_accel_max
agent.linear_speed_max = linear_speed_max
agent.linear_drag_percentage = 0.1
proximity = GSTRadiusProximity.new(agent, [], proximity_radius)
separation = GSTSeparation.new(agent, proximity)
separation.decay_coefficient = separation_decay_coefficient
cohesion = GSTCohesion.new(agent, proximity)
blend.add(separation, separation_strength)
blend.add(cohesion, cohesion_strength)
func _draw() -> void:
if draw_proximity:
draw_circle(Vector2.ZERO, proximity.radius, Color(0.4, 1.0, 0.89, 0.3))
func _physics_process(delta: float) -> void:
if blend:
blend.calculate_steering(acceleration)
agent.apply_steering(acceleration, delta)
func set_neighbors(neighbor: Array) -> void:
proximity.agents = neighbor