diff --git a/project/demos/group_behaviors/GroupBehaviorsDemo.tscn b/project/demos/group_behaviors/GroupBehaviorsDemo.tscn new file mode 100644 index 0000000..806d7ec --- /dev/null +++ b/project/demos/group_behaviors/GroupBehaviorsDemo.tscn @@ -0,0 +1,11 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://demos/group_behaviors/Member.tscn" type="PackedScene" id=1] +[ext_resource path="res://demos/group_behaviors/Spawner.gd" type="Script" id=2] + +[node name="GroupBehaviorsDemo" type="Node2D"] + +[node name="Spawner" type="Node2D" parent="."] +position = Vector2( 397, 207 ) +script = ExtResource( 2 ) +member = ExtResource( 1 ) diff --git a/project/demos/group_behaviors/Member.gd b/project/demos/group_behaviors/Member.gd new file mode 100644 index 0000000..9e4b8b3 --- /dev/null +++ b/project/demos/group_behaviors/Member.gd @@ -0,0 +1,51 @@ +extends KinematicBody2D + + +var agent: = GSTSteeringAgent.new() +var separation: GSTSeparation +var cohesion: GSTCohesion +var proximity: GSTRadiusProximity +var blend: = GSTBlend.new(agent) +var acceleration: = GSTTargetAcceleration.new() + +var _radius: float +var _color: = Color.red +var _velocity: = Vector2() + +onready var shape: = $CollisionShape2D + + +func _ready() -> void: + _radius = shape.shape.radius + _color = Color(rand_range(0.5, 1), rand_range(0.25, 1), rand_range(0, 1)) + agent.max_linear_acceleration = 7 + agent.max_linear_speed = 70 + + proximity = GSTRadiusProximity.new(agent, [], 140) + separation = GSTSeparation.new(agent, proximity) + separation.decay_coefficient = 2000 + cohesion = GSTCohesion.new(agent, proximity) + blend.add(separation, 1.5) + blend.add(cohesion, 0.3) + + +func _draw() -> void: + draw_circle(Vector2.ZERO, _radius, _color) + + +func _process(delta: float) -> void: + update_agent() + if blend: + acceleration = blend.calculate_steering(acceleration) + _velocity = (_velocity + Vector2(acceleration.linear.x, acceleration.linear.y)).clamped(agent.max_linear_speed) + move_and_slide(_velocity) + + +func set_neighbors(neighbor: Array) -> void: + proximity.agents = neighbor + + +func update_agent() -> void: + var current_position: = global_position + agent.position.x = current_position.x + agent.position.y = current_position.y diff --git a/project/demos/group_behaviors/Member.tscn b/project/demos/group_behaviors/Member.tscn new file mode 100644 index 0000000..c2ae670 --- /dev/null +++ b/project/demos/group_behaviors/Member.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://demos/group_behaviors/Member.gd" type="Script" id=1] + +[sub_resource type="CircleShape2D" id=1] +radius = 15.0 + +[node name="Member" type="KinematicBody2D"] +script = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) diff --git a/project/demos/group_behaviors/Spawner.gd b/project/demos/group_behaviors/Spawner.gd new file mode 100644 index 0000000..c1d8556 --- /dev/null +++ b/project/demos/group_behaviors/Spawner.gd @@ -0,0 +1,18 @@ +extends Node2D + + +export var member: PackedScene + + +func _ready() -> void: + var followers: = [] + for i in range(19): + var follower: = member.instance() + add_child(follower) + follower.position += Vector2(rand_range(-60, 60), rand_range(-60, 60)) + followers.append(follower) + var agents: = [] + for i in followers: + agents.append(i.agent) + for i in followers: + i.proximity.agents = agents diff --git a/project/src/behaviors/GSTBlend.gd b/project/src/behaviors/GSTBlend.gd index 33f0699..67f5899 100644 --- a/project/src/behaviors/GSTBlend.gd +++ b/project/src/behaviors/GSTBlend.gd @@ -9,8 +9,8 @@ class_name GSTBlend # `GSTSteeringBehavior` and a `weight` key with a value of type float. -onready var _behaviors: = [] -onready var _accel: = GSTTargetAcceleration.new() +var _behaviors: = [] +var _accel: = GSTTargetAcceleration.new() func _init(agent: GSTSteeringAgent).(agent) -> void: diff --git a/project/src/behaviors/GSTPriority.gd b/project/src/behaviors/GSTPriority.gd index 3c8dfa7..4cb84df 100644 --- a/project/src/behaviors/GSTPriority.gd +++ b/project/src/behaviors/GSTPriority.gd @@ -4,7 +4,7 @@ class_name GSTPriority # acceleration. -onready var _behaviors: = [] +var _behaviors: = [] var last_selected_index: int var threshold_for_zero: float @@ -38,7 +38,7 @@ func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration: var behavior: GSTSteeringBehavior = _behaviors[i] behavior.calculate_steering(accel) - if accel.get_squared_magnitude() > threshold_squared: + if accel.get_magnitude_squared() > threshold_squared: break else: accel.set_zero()