mirror of
https://github.com/Relintai/godot-steering-ai-framework.git
synced 2024-11-10 00:52:10 +01:00
Implement demo for separation and cohesion
This commit is contained in:
parent
a01f5d5b2e
commit
b0442982d0
11
project/demos/group_behaviors/GroupBehaviorsDemo.tscn
Normal file
11
project/demos/group_behaviors/GroupBehaviorsDemo.tscn
Normal file
@ -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 )
|
51
project/demos/group_behaviors/Member.gd
Normal file
51
project/demos/group_behaviors/Member.gd
Normal file
@ -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
|
12
project/demos/group_behaviors/Member.tscn
Normal file
12
project/demos/group_behaviors/Member.tscn
Normal file
@ -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 )
|
18
project/demos/group_behaviors/Spawner.gd
Normal file
18
project/demos/group_behaviors/Spawner.gd
Normal file
@ -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
|
@ -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:
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user