Format the code using gdformat

gdformat follows the official style guide, and handles line length and wrapping
lines for us.
This commit is contained in:
Nathan Lovato 2020-02-14 10:35:18 -06:00
parent 2940442b95
commit 68b85bb234
64 changed files with 337 additions and 423 deletions

View File

@ -1,22 +1,16 @@
extends Node extends Node
export (float, 0, 2000, 40) var linear_speed_max := 800.0 setget set_linear_speed_max
export(float, 0, 2000, 40) var linear_speed_max := 800.0 setget set_linear_speed_max export (float, 0, 200, 2.0) var linear_acceleration_max := 80.0 setget set_linear_acceleration_max
export(float, 0, 200, 2.0) var linear_acceleration_max := 80.0 setget set_linear_acceleration_max export (float, 0, 100, 0.1) var arrival_tolerance := 25.0 setget set_arrival_tolerance
export(float, 0, 100, 0.1) var arrival_tolerance := 25.0 setget set_arrival_tolerance export (float, 0, 500, 10) var deceleration_radius := 125.0 setget set_deceleration_radius
export(float, 0, 500, 10) var deceleration_radius := 125.0 setget set_deceleration_radius
onready var arriver := $Arriver onready var arriver := $Arriver
onready var target_drawer := $TargetDrawer onready var target_drawer := $TargetDrawer
func _ready() -> void: func _ready() -> void:
arriver.setup( arriver.setup(linear_speed_max, linear_acceleration_max, arrival_tolerance, deceleration_radius)
linear_speed_max,
linear_acceleration_max,
arrival_tolerance,
deceleration_radius
)
func _unhandled_input(event: InputEvent) -> void: func _unhandled_input(event: InputEvent) -> void:
@ -29,7 +23,7 @@ func set_arrival_tolerance(value: float) -> void:
arrival_tolerance = value arrival_tolerance = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.arrive.arrival_tolerance = value arriver.arrive.arrival_tolerance = value
@ -37,7 +31,7 @@ func set_deceleration_radius(value: float) -> void:
deceleration_radius = value deceleration_radius = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.arrive.deceleration_radius = value arriver.arrive.deceleration_radius = value
@ -45,7 +39,7 @@ func set_linear_speed_max(value: float) -> void:
linear_speed_max = value linear_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.agent.linear_speed_max = value arriver.agent.linear_speed_max = value
@ -53,5 +47,5 @@ func set_linear_acceleration_max(value: float) -> void:
linear_acceleration_max = value linear_acceleration_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.agent.linear_acceleration_max = value arriver.agent.linear_acceleration_max = value

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
var agent := GSAIKinematicBody2DAgent.new(self) var agent := GSAIKinematicBody2DAgent.new(self)
var target := GSAIAgentLocation.new() var target := GSAIAgentLocation.new()
var arrive := GSAIArrive.new(agent, target) var arrive := GSAIArrive.new(agent, target)

View File

@ -1,6 +1,5 @@
extends Node2D extends Node2D
const COLORS := { const COLORS := {
deceleration_radius = Color(1.0, 0.419, 0.592, 0.5), deceleration_radius = Color(1.0, 0.419, 0.592, 0.5),
arrival_tolerance = Color(0.278, 0.231, 0.47, 0.3) arrival_tolerance = Color(0.278, 0.231, 0.47, 0.3)

View File

@ -1,6 +1,5 @@
extends Camera extends Camera
var target: Spatial var target: Spatial
onready var ray := $RayCast onready var ray := $RayCast

View File

@ -1,14 +1,13 @@
extends Node extends Node
export (float, 0, 50, 0.1) var linear_speed_max := 10.0 setget set_linear_speed_max
export(float, 0, 50, 0.1) var linear_speed_max := 10.0 setget set_linear_speed_max export (float, 0, 50, 0.1) var linear_acceleration_max := 1.0 setget set_linear_acceleration_max
export(float, 0, 50, 0.1) var linear_acceleration_max := 1.0 setget set_linear_acceleration_max export (float, 0, 50, 0.1) var arrival_tolerance := 0.5 setget set_arrival_tolerance
export(float, 0, 50, 0.1) var arrival_tolerance := 0.5 setget set_arrival_tolerance export (float, 0, 50, 0.1) var deceleration_radius := 5.0 setget set_deceleration_radius
export(float, 0, 50, 0.1) var deceleration_radius := 5.0 setget set_deceleration_radius export (int, 0, 359, 2) var angular_speed_max := 270 setget set_angular_speed_max
export(int, 0, 359, 2) var angular_speed_max := 270 setget set_angular_speed_max export (int, 0, 359, 2) var angular_accel_max := 45 setget set_angular_accel_max
export(int, 0, 359, 2) var angular_accel_max := 45 setget set_angular_accel_max export (int, 0, 180, 2) var align_tolerance := 5 setget set_align_tolerance
export(int, 0, 180, 2) var align_tolerance := 5 setget set_align_tolerance export (int, 0, 359, 2) var angular_deceleration_radius := 45 setget set_angular_deceleration_radius
export(int, 0, 359, 2) var angular_deceleration_radius := 45 setget set_angular_deceleration_radius
onready var target := $MouseTarget onready var target := $MouseTarget
onready var arriver := $Arriver onready var arriver := $Arriver
@ -33,7 +32,7 @@ func set_align_tolerance(value: int) -> void:
align_tolerance = value align_tolerance = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.face.alignment_tolerance = deg2rad(value) arriver.face.alignment_tolerance = deg2rad(value)
@ -41,7 +40,7 @@ func set_angular_deceleration_radius(value: int) -> void:
deceleration_radius = value deceleration_radius = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.face.deceleration_radius = deg2rad(value) arriver.face.deceleration_radius = deg2rad(value)
@ -49,7 +48,7 @@ func set_angular_accel_max(value: int) -> void:
angular_accel_max = value angular_accel_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.agent.angular_acceleration_max = deg2rad(value) arriver.agent.angular_acceleration_max = deg2rad(value)
@ -57,7 +56,7 @@ func set_angular_speed_max(value: int) -> void:
angular_speed_max = value angular_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.agent.angular_speed_max = deg2rad(value) arriver.agent.angular_speed_max = deg2rad(value)
@ -65,7 +64,7 @@ func set_arrival_tolerance(value: float) -> void:
arrival_tolerance = value arrival_tolerance = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.arrive.arrival_tolerance = value arriver.arrive.arrival_tolerance = value
@ -73,7 +72,7 @@ func set_deceleration_radius(value: float) -> void:
deceleration_radius = value deceleration_radius = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.arrive.deceleration_radius = value arriver.arrive.deceleration_radius = value
@ -81,7 +80,7 @@ func set_linear_speed_max(value: float) -> void:
linear_speed_max = value linear_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.agent.linear_speed_max = value arriver.agent.linear_speed_max = value
@ -89,5 +88,5 @@ func set_linear_acceleration_max(value: float) -> void:
linear_acceleration_max = value linear_acceleration_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
arriver.agent.linear_acceleration_max = value arriver.agent.linear_acceleration_max = value

View File

@ -1,6 +1,5 @@
extends KinematicBody extends KinematicBody
var target_node: Spatial var target_node: Spatial
onready var agent := GSAIKinematicBody3DAgent.new(self) onready var agent := GSAIKinematicBody3DAgent.new(self)
@ -19,29 +18,29 @@ func _physics_process(delta: float) -> void:
func setup( func setup(
align_tolerance: float, align_tolerance: float,
angular_deceleration_radius: float, angular_deceleration_radius: float,
angular_accel_max: float, angular_accel_max: float,
angular_speed_max: float, angular_speed_max: float,
deceleration_radius: float, deceleration_radius: float,
arrival_tolerance: float, arrival_tolerance: float,
linear_acceleration_max: float, linear_acceleration_max: float,
linear_speed_max: float, linear_speed_max: float,
_target: Spatial _target: Spatial
) -> void: ) -> void:
agent.linear_speed_max = linear_speed_max agent.linear_speed_max = linear_speed_max
agent.linear_acceleration_max = linear_acceleration_max agent.linear_acceleration_max = linear_acceleration_max
agent.linear_drag_percentage = 0.05 agent.linear_drag_percentage = 0.05
agent.angular_acceleration_max = angular_accel_max agent.angular_acceleration_max = angular_accel_max
agent.angular_speed_max = angular_speed_max agent.angular_speed_max = angular_speed_max
agent.angular_drag_percentage = 0.1 agent.angular_drag_percentage = 0.1
arrive.arrival_tolerance = arrival_tolerance arrive.arrival_tolerance = arrival_tolerance
arrive.deceleration_radius = deceleration_radius arrive.deceleration_radius = deceleration_radius
face.alignment_tolerance = align_tolerance face.alignment_tolerance = align_tolerance
face.deceleration_radius = angular_deceleration_radius face.deceleration_radius = angular_deceleration_radius
target_node = _target target_node = _target
self.target.position = target_node.transform.origin self.target.position = target_node.transform.origin
blend.add(arrive, 1) blend.add(arrive, 1)

View File

@ -1,9 +1,8 @@
extends Node extends Node
export (float, 0, 2000, 40) var linear_speed_max := 350.0 setget set_linear_speed_max
export(float, 0, 2000, 40) var linear_speed_max := 350.0 setget set_linear_speed_max export (float, 0, 100, 2) var linear_acceleration_max := 40.0 setget set_linear_accel_max
export(float, 0, 100, 2) var linear_acceleration_max := 40.0 setget set_linear_accel_max export (float, 0, 500, 10) var proximity_radius := 140.0 setget set_proximity_radius
export(float, 0, 500, 10) var proximity_radius := 140.0 setget set_proximity_radius
export var draw_proximity := true setget set_draw_proximity export var draw_proximity := true setget set_draw_proximity
onready var spawner := $Spawner onready var spawner := $Spawner
@ -13,7 +12,7 @@ func set_linear_speed_max(value: float) -> void:
linear_speed_max = value linear_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_linear_speed_max(value) spawner.set_linear_speed_max(value)
@ -21,7 +20,7 @@ func set_linear_accel_max(value: float) -> void:
linear_acceleration_max = value linear_acceleration_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_linear_accel_max(value) spawner.set_linear_accel_max(value)
@ -29,7 +28,7 @@ func set_proximity_radius(value: float) -> void:
proximity_radius = value proximity_radius = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_proximity_radius(value) spawner.set_proximity_radius(value)
@ -37,5 +36,5 @@ func set_draw_proximity(value: bool) -> void:
draw_proximity = value draw_proximity = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_draw_proximity(value) spawner.set_draw_proximity(value)

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
var draw_proximity: bool var draw_proximity: bool
var _boundary_right: float var _boundary_right: float
@ -27,39 +26,39 @@ func _draw() -> void:
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
target.position.x = agent.position.x + _direction.x*_radius target.position.x = agent.position.x + _direction.x * _radius
target.position.y = agent.position.y + _direction.y*_radius target.position.y = agent.position.y + _direction.y * _radius
priority.calculate_steering(_accel) priority.calculate_steering(_accel)
agent._apply_steering(_accel, delta) agent._apply_steering(_accel, delta)
func setup( func setup(
linear_speed_max: float, linear_speed_max: float,
linear_accel_max: float, linear_accel_max: float,
proximity_radius: float, proximity_radius: float,
boundary_right: float, boundary_right: float,
boundary_bottom: float, boundary_bottom: float,
_draw_proximity: bool, _draw_proximity: bool,
rng: RandomNumberGenerator rng: RandomNumberGenerator
) -> void: ) -> void:
rng.randomize() rng.randomize()
_direction = Vector2(rng.randf_range(-1, 1), rng.randf_range(-1, 1)).normalized() _direction = Vector2(rng.randf_range(-1, 1), rng.randf_range(-1, 1)).normalized()
agent.linear_speed_max = linear_speed_max agent.linear_speed_max = linear_speed_max
agent.linear_acceleration_max = linear_accel_max agent.linear_acceleration_max = linear_accel_max
proximity.radius = proximity_radius proximity.radius = proximity_radius
_boundary_bottom = boundary_bottom _boundary_bottom = boundary_bottom
_boundary_right = boundary_right _boundary_right = boundary_right
_radius = collision.shape.radius _radius = collision.shape.radius
agent.bounding_radius = _radius agent.bounding_radius = _radius
agent.linear_drag_percentage = _drag agent.linear_drag_percentage = _drag
self.draw_proximity = _draw_proximity self.draw_proximity = _draw_proximity
priority.add(avoid) priority.add(avoid)
priority.add(seek) priority.add(seek)
@ -75,15 +74,18 @@ func set_random_nonoverlapping_position(others: Array, distance_from_boundary_mi
while tries_max > 0: while tries_max > 0:
tries_max -= 1 tries_max -= 1
global_position.x = rng.randf_range( global_position.x = rng.randf_range(
distance_from_boundary_min, _boundary_right-distance_from_boundary_min distance_from_boundary_min, _boundary_right - distance_from_boundary_min
) )
global_position.y = rng.randf_range( global_position.y = rng.randf_range(
distance_from_boundary_min, _boundary_bottom-distance_from_boundary_min distance_from_boundary_min, _boundary_bottom - distance_from_boundary_min
) )
var done := true var done := true
for i in range(others.size()): for i in range(others.size()):
var other: Node2D = others[i] var other: Node2D = others[i]
if other.global_position.distance_to(position) <= _radius*2 + distance_from_boundary_min: if (
other.global_position.distance_to(position)
<= _radius * 2 + distance_from_boundary_min
):
done = false done = false
if done: if done:
break break

View File

@ -1,6 +1,5 @@
extends Node2D extends Node2D
export var avoider_template: PackedScene export var avoider_template: PackedScene
export var inner_color := Color() export var inner_color := Color()
export var outer_color := Color() export var outer_color := Color()
@ -11,22 +10,22 @@ var boundaries: Vector2
func _ready() -> void: func _ready() -> void:
boundaries = Vector2( boundaries = Vector2(
ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/height"]
ProjectSettings["display/window/size/height"]) )
var rng: = RandomNumberGenerator.new() var rng := RandomNumberGenerator.new()
var avoiders := [] var avoiders := []
var avoider_agents := [] var avoider_agents := []
for i in range(agent_count): for i in range(agent_count):
var avoider := avoider_template.instance() var avoider := avoider_template.instance()
add_child(avoider) add_child(avoider)
avoider.setup( avoider.setup(
owner.linear_speed_max, owner.linear_speed_max,
owner.linear_acceleration_max, owner.linear_acceleration_max,
owner.proximity_radius, owner.proximity_radius,
boundaries.x, boundaries.x,
boundaries.y, boundaries.y,
true if i == 0 and owner.draw_proximity else false, true if i == 0 and owner.draw_proximity else false,
rng rng
) )
avoider_agents.append(avoider.agent) avoider_agents.append(avoider.agent)
avoider.set_random_nonoverlapping_position(avoiders, 16) avoider.set_random_nonoverlapping_position(avoiders, 16)

View File

@ -1,7 +1,6 @@
class_name DemoPickerUI class_name DemoPickerUI
extends Control extends Control
# warning-ignore:unused_signal # warning-ignore:unused_signal
signal demo_requested signal demo_requested
@ -23,7 +22,7 @@ func _ready() -> void:
func set_demo_path(value: String) -> void: func set_demo_path(value: String) -> void:
demo_path = value demo_path = value
func _on_ItemList_item_activated(_index: int) -> void: func _on_ItemList_item_activated(_index: int) -> void:
emit_signal("demo_requested") emit_signal("demo_requested")

View File

@ -1,6 +1,5 @@
extends Node extends Node
onready var demo_picker: DemoPickerUI = $DemoPickerUI onready var demo_picker: DemoPickerUI = $DemoPickerUI
onready var demo_player := $DemoPlayer onready var demo_player := $DemoPlayer
onready var button_go_back: Button = $ButtonGoBack onready var button_go_back: Button = $ButtonGoBack

View File

@ -1,11 +1,10 @@
extends Node extends Node
export (int, 0, 359, 2) var angular_speed_max := 120 setget set_angular_speed_max
export(int, 0, 359, 2) var angular_speed_max := 120 setget set_angular_speed_max export (int, 0, 359, 2) var angular_accel_max := 10 setget set_angular_accel_max
export(int, 0, 359, 2) var angular_accel_max := 10 setget set_angular_accel_max export (int, 0, 180, 2) var align_tolerance := 5 setget set_align_tolerance
export(int, 0, 180, 2) var align_tolerance := 5 setget set_align_tolerance export (int, 0, 359, 2) var deceleration_radius := 45 setget set_deceleration_radius
export(int, 0, 359, 2) var deceleration_radius := 45 setget set_deceleration_radius export (float, 0, 1000, 40) var player_speed := 600.0 setget set_player_speed
export(float, 0, 1000, 40) var player_speed := 600.0 setget set_player_speed
onready var player := $Player onready var player := $Player
onready var turret := $Turret onready var turret := $Turret
@ -26,7 +25,7 @@ func set_align_tolerance(value: int) -> void:
align_tolerance = value align_tolerance = value
if not is_inside_tree(): if not is_inside_tree():
return return
turret.face.alignment_tolerance = deg2rad(value) turret.face.alignment_tolerance = deg2rad(value)
@ -34,7 +33,7 @@ func set_deceleration_radius(value: int) -> void:
deceleration_radius = value deceleration_radius = value
if not is_inside_tree(): if not is_inside_tree():
return return
turret.face.deceleration_radius = deg2rad(value) turret.face.deceleration_radius = deg2rad(value)
@ -42,7 +41,7 @@ func set_angular_accel_max(value: int) -> void:
angular_accel_max = value angular_accel_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
turret.agent.angular_acceleration_max = deg2rad(value) turret.agent.angular_acceleration_max = deg2rad(value)
@ -50,7 +49,7 @@ func set_angular_speed_max(value: int) -> void:
angular_speed_max = value angular_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
turret.agent.angular_speed_max = deg2rad(value) turret.agent.angular_speed_max = deg2rad(value)
@ -58,5 +57,5 @@ func set_player_speed(value: float) -> void:
player_speed = value player_speed = value
if not is_inside_tree(): if not is_inside_tree():
return return
player.speed = player_speed player.speed = player_speed

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
var speed: float var speed: float
onready var agent := GSAIAgentLocation.new() onready var agent := GSAIAgentLocation.new()
@ -14,5 +13,6 @@ func _physics_process(_delta: float) -> void:
func _get_movement() -> Vector2: func _get_movement() -> Vector2:
return Vector2( return Vector2(
Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"), Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"),
Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up")) Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up")
)

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
var face: GSAIFace var face: GSAIFace
var agent := GSAIKinematicBody2DAgent.new(self) var agent := GSAIKinematicBody2DAgent.new(self)
@ -14,7 +13,7 @@ onready var collision_shape := $CollisionShape2D
func _ready() -> void: func _ready() -> void:
var radius = collision_shape.shape.radius var radius = collision_shape.shape.radius
_cannon = Rect2(Vector2(-5, 0), Vector2(10, -radius*2)) _cannon = Rect2(Vector2(-5, 0), Vector2(10, -radius * 2))
_color = collision_shape.outer_color _color = collision_shape.outer_color
@ -35,10 +34,10 @@ func setup(
angular_speed_max: float angular_speed_max: float
) -> void: ) -> void:
face = GSAIFace.new(agent, player_agent) face = GSAIFace.new(agent, player_agent)
face.alignment_tolerance = align_tolerance face.alignment_tolerance = align_tolerance
face.deceleration_radius = deceleration_radius face.deceleration_radius = deceleration_radius
agent.angular_acceleration_max = angular_accel_max agent.angular_acceleration_max = angular_accel_max
agent.angular_speed_max = angular_speed_max agent.angular_speed_max = angular_speed_max
agent.angular_drag_percentage = _angular_drag agent.angular_drag_percentage = _angular_drag

View File

@ -1,9 +1,7 @@
extends Node2D extends Node2D
signal path_established(points) signal path_established(points)
var active_points := [] var active_points := []
var is_drawing := false var is_drawing := false
var distance_threshold := 100.0 var distance_threshold := 100.0

View File

@ -1,12 +1,11 @@
extends Node extends Node
export (float, 0, 2000, 40) var linear_speed_max := 600.0 setget set_linear_speed_max
export(float, 0, 2000, 40) var linear_speed_max := 600.0 setget set_linear_speed_max export (float, 0, 200, 10.0) var linear_acceleration_max := 40.0 setget set_linear_acceleration_max
export(float, 0, 200, 10.0) var linear_acceleration_max := 40.0 setget set_linear_acceleration_max export (float, 0, 100, 0.1) var arrival_tolerance := 10.0 setget set_arrival_tolerance
export(float, 0, 100, 0.1) var arrival_tolerance := 10.0 setget set_arrival_tolerance export (float, 0, 500, 10) var deceleration_radius := 100.0 setget set_deceleration_radius
export(float, 0, 500, 10) var deceleration_radius := 100.0 setget set_deceleration_radius export (float, 0, 5, 0.1) var predict_time := 0.3 setget set_predict_time
export(float, 0, 5, 0.1) var predict_time := 0.3 setget set_predict_time export (float, 0, 200, 10.0) var path_offset := 20.0 setget set_path_offset
export(float, 0, 200, 10.0) var path_offset := 20.0 setget set_path_offset
onready var drawer := $Drawer onready var drawer := $Drawer
onready var follower := $PathFollower onready var follower := $PathFollower
@ -14,12 +13,12 @@ onready var follower := $PathFollower
func _ready() -> void: func _ready() -> void:
follower.setup( follower.setup(
path_offset, path_offset,
predict_time, predict_time,
linear_acceleration_max, linear_acceleration_max,
linear_speed_max, linear_speed_max,
deceleration_radius, deceleration_radius,
arrival_tolerance arrival_tolerance
) )
@ -27,7 +26,7 @@ func set_linear_speed_max(value: float) -> void:
linear_speed_max = value linear_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
follower.agent.linear_speed_max = value follower.agent.linear_speed_max = value
@ -35,7 +34,7 @@ func set_linear_acceleration_max(value: float) -> void:
linear_acceleration_max = value linear_acceleration_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
follower.agent.linear_acceleration_max = value follower.agent.linear_acceleration_max = value
@ -43,7 +42,7 @@ func set_arrival_tolerance(value: float) -> void:
arrival_tolerance = value arrival_tolerance = value
if not is_inside_tree(): if not is_inside_tree():
return return
follower.follow.arrival_tolerance = value follower.follow.arrival_tolerance = value
@ -51,7 +50,7 @@ func set_deceleration_radius(value: float) -> void:
deceleration_radius = value deceleration_radius = value
if not is_inside_tree(): if not is_inside_tree():
return return
follower.follow.deceleration_radius = value follower.follow.deceleration_radius = value
@ -59,7 +58,7 @@ func set_predict_time(value: float) -> void:
predict_time = value predict_time = value
if not is_inside_tree(): if not is_inside_tree():
return return
follower.follow.prediction_time = value follower.follow.prediction_time = value
@ -67,5 +66,5 @@ func set_path_offset(value: float) -> void:
path_offset = value path_offset = value
if not is_inside_tree(): if not is_inside_tree():
return return
follower.follow.path_offset = value follower.follow.path_offset = value

View File

@ -1,34 +1,35 @@
extends KinematicBody2D extends KinematicBody2D
var _velocity := Vector2.ZERO var _velocity := Vector2.ZERO
var _accel := GSAITargetAcceleration.new() var _accel := GSAITargetAcceleration.new()
var _valid := false var _valid := false
var _drag := 0.1 var _drag := 0.1
onready var agent := GSAIKinematicBody2DAgent.new(self) onready var agent := GSAIKinematicBody2DAgent.new(self)
onready var path := GSAIPath.new([ onready var path := GSAIPath.new(
[
Vector3(global_position.x, global_position.y, 0), Vector3(global_position.x, global_position.y, 0),
Vector3(global_position.x, global_position.y, 0) Vector3(global_position.x, global_position.y, 0)
], true) ],
true
)
onready var follow := GSAIFollowPath.new(agent, path, 0, 0) onready var follow := GSAIFollowPath.new(agent, path, 0, 0)
func setup( func setup(
path_offset: float, path_offset: float,
predict_time: float, predict_time: float,
accel_max: float, accel_max: float,
speed_max: float, speed_max: float,
decel_radius: float, decel_radius: float,
arrival_tolerance: float arrival_tolerance: float
) -> void: ) -> void:
owner.drawer.connect("path_established", self, "_on_Drawer_path_established") owner.drawer.connect("path_established", self, "_on_Drawer_path_established")
follow.path_offset = path_offset follow.path_offset = path_offset
follow.prediction_time = predict_time follow.prediction_time = predict_time
follow.deceleration_radius = decel_radius follow.deceleration_radius = decel_radius
follow.arrival_tolerance = arrival_tolerance follow.arrival_tolerance = arrival_tolerance
agent.linear_acceleration_max = accel_max agent.linear_acceleration_max = accel_max
agent.linear_speed_max = speed_max agent.linear_speed_max = speed_max
agent.linear_drag_percentage = _drag agent.linear_drag_percentage = _drag

View File

@ -1,14 +1,13 @@
extends Node extends Node
onready var spawner := $Spawner onready var spawner := $Spawner
export(float, 0, 2000, 40.0) var linear_speed_max := 600.0 setget set_linear_speed_max export (float, 0, 2000, 40.0) var linear_speed_max := 600.0 setget set_linear_speed_max
export(float, 0, 200, 2.0) var linear_accel_max := 40.0 setget set_linear_accel_max export (float, 0, 200, 2.0) var linear_accel_max := 40.0 setget set_linear_accel_max
export(float, 0, 300, 2.0) var proximity_radius := 140.0 setget set_proximity_radius export (float, 0, 300, 2.0) var proximity_radius := 140.0 setget set_proximity_radius
export(float, 0, 10000, 100) var separation_decay_coefficient := 2000.0 setget set_separation_decay_coef export (float, 0, 10000, 100) var separation_decay_coefficient := 2000.0 setget set_separation_decay_coef
export(float, 0, 2, 0.1) var cohesion_strength := 0.1 setget set_cohesion_strength export (float, 0, 2, 0.1) var cohesion_strength := 0.1 setget set_cohesion_strength
export(float, 0, 6, 0.1) var separation_strength := 1.5 setget set_separation_strength export (float, 0, 6, 0.1) var separation_strength := 1.5 setget set_separation_strength
export var show_proximity_radius := true setget set_show_proximity_radius export var show_proximity_radius := true setget set_show_proximity_radius
@ -28,7 +27,7 @@ func set_linear_speed_max(value: float) -> void:
linear_speed_max = value linear_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_linear_speed_max(value) spawner.set_linear_speed_max(value)
@ -36,7 +35,7 @@ func set_linear_accel_max(value: float) -> void:
linear_accel_max = value linear_accel_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_linear_accel_max(value) spawner.set_linear_accel_max(value)
@ -44,7 +43,7 @@ func set_proximity_radius(value: float) -> void:
proximity_radius = value proximity_radius = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_proximity_radius(value) spawner.set_proximity_radius(value)
@ -52,7 +51,7 @@ func set_show_proximity_radius(value: bool) -> void:
show_proximity_radius = value show_proximity_radius = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_show_proximity_radius(value) spawner.set_show_proximity_radius(value)
@ -60,7 +59,7 @@ func set_separation_decay_coef(value: float) -> void:
separation_decay_coefficient = value separation_decay_coefficient = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_separation_decay_coef(value) spawner.set_separation_decay_coef(value)
@ -68,7 +67,7 @@ func set_cohesion_strength(value: float) -> void:
cohesion_strength = value cohesion_strength = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_cohesion_strength(value) spawner.set_cohesion_strength(value)
@ -76,5 +75,5 @@ func set_separation_strength(value: float) -> void:
separation_strength = value separation_strength = value
if not is_inside_tree(): if not is_inside_tree():
return return
spawner.set_separation_strength(value) spawner.set_separation_strength(value)

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
var separation: GSAISeparation var separation: GSAISeparation
var cohesion: GSAICohesion var cohesion: GSAICohesion
var proximity: GSAIRadiusProximity var proximity: GSAIRadiusProximity
@ -16,20 +15,20 @@ onready var collision_shape := $CollisionShape2D
func setup( func setup(
linear_speed_max: float, linear_speed_max: float,
linear_accel_max: float, linear_accel_max: float,
proximity_radius: float, proximity_radius: float,
separation_decay_coefficient: float, separation_decay_coefficient: float,
cohesion_strength: float, cohesion_strength: float,
separation_strength: float separation_strength: float
) -> void: ) -> void:
_color = Color(rand_range(0.5, 1), rand_range(0.25, 1), rand_range(0, 1)) _color = Color(rand_range(0.5, 1), rand_range(0.25, 1), rand_range(0, 1))
collision_shape.inner_color = _color collision_shape.inner_color = _color
agent.linear_acceleration_max = linear_accel_max agent.linear_acceleration_max = linear_accel_max
agent.linear_speed_max = linear_speed_max agent.linear_speed_max = linear_speed_max
agent.linear_drag_percentage = 0.1 agent.linear_drag_percentage = 0.1
proximity = GSAIRadiusProximity.new(agent, [], proximity_radius) proximity = GSAIRadiusProximity.new(agent, [], proximity_radius)
separation = GSAISeparation.new(agent, proximity) separation = GSAISeparation.new(agent, proximity)
separation.decay_coefficient = separation_decay_coefficient separation.decay_coefficient = separation_decay_coefficient

View File

@ -1,6 +1,5 @@
extends Node2D extends Node2D
export var member: PackedScene export var member: PackedScene

View File

@ -1,6 +1,5 @@
extends ItemList extends ItemList
signal demo_selected(scene_path) signal demo_selected(scene_path)
var file_paths := PoolStringArray() var file_paths := PoolStringArray()
@ -28,15 +27,17 @@ func sentencify(line: String) -> String:
var regex := RegEx.new() var regex := RegEx.new()
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
regex.compile("[A-Z]") regex.compile("[A-Z]")
line = line.split(".", true, 1)[0] line = line.split(".", true, 1)[0]
line = regex.sub(line, " $0", true) line = regex.sub(line, " $0", true)
return line return line
func _find_files(dirpath := "", patterns := PoolStringArray(), is_recursive := false, do_skip_hidden := true) -> PoolStringArray: func _find_files(
var paths: = PoolStringArray() dirpath := "", patterns := PoolStringArray(), is_recursive := false, do_skip_hidden := true
var directory: = Directory.new() ) -> PoolStringArray:
var paths := PoolStringArray()
var directory := Directory.new()
if not directory.dir_exists(dirpath): if not directory.dir_exists(dirpath):
printerr("The directory does not exist: %s" % dirpath) printerr("The directory does not exist: %s" % dirpath)
@ -47,10 +48,10 @@ func _find_files(dirpath := "", patterns := PoolStringArray(), is_recursive := f
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
directory.list_dir_begin(true, do_skip_hidden) directory.list_dir_begin(true, do_skip_hidden)
var file_name: = directory.get_next() var file_name := directory.get_next()
while file_name != "": while file_name != "":
if directory.current_is_dir() and is_recursive: if directory.current_is_dir() and is_recursive:
var subdirectory: = dirpath.plus_file(file_name) var subdirectory := dirpath.plus_file(file_name)
paths.append_array(_find_files(subdirectory, patterns, is_recursive)) paths.append_array(_find_files(subdirectory, patterns, is_recursive))
else: else:
for pattern in patterns: for pattern in patterns:

View File

@ -1,14 +1,12 @@
extends Node2D extends Node2D
# Wraps the ships' positions around the world border. # Wraps the ships' positions around the world border.
var _world_bounds: Vector2 var _world_bounds: Vector2
func _ready() -> void: func _ready() -> void:
_world_bounds = Vector2( _world_bounds = Vector2(
ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/height"]
ProjectSettings["display/window/size/height"]
) )

View File

@ -1,7 +1,6 @@
extends KinematicBody2D extends KinematicBody2D
# Controls the player ship's movements based on player input. # Controls the player ship's movements based on player input.
export var thruster_strength := 175.0 export var thruster_strength := 175.0
export var side_thruster_strength := 10.0 export var side_thruster_strength := 10.0
export var velocity_max := 300.0 export var velocity_max := 300.0
@ -26,7 +25,7 @@ func _physics_process(delta: float) -> void:
delta delta
) )
rotation += _angular_velocity * delta rotation += _angular_velocity * delta
_linear_velocity = _calculate_linear_velocity( _linear_velocity = _calculate_linear_velocity(
movement.y, movement.y,
_linear_velocity, _linear_velocity,
@ -36,7 +35,7 @@ func _physics_process(delta: float) -> void:
velocity_max, velocity_max,
delta delta
) )
_linear_velocity = move_and_slide(_linear_velocity) _linear_velocity = move_and_slide(_linear_velocity)
_update_agent() _update_agent()
@ -54,9 +53,9 @@ func _calculate_angular_velocity(
-_velocity_max, -_velocity_max,
_velocity_max _velocity_max
) )
velocity = lerp(velocity, 0, ship_drag) velocity = lerp(velocity, 0, ship_drag)
return velocity return velocity
@ -73,17 +72,19 @@ func _calculate_linear_velocity(
if vertical_movement > 0: if vertical_movement > 0:
actual_strength = strength actual_strength = strength
elif vertical_movement < 0: elif vertical_movement < 0:
actual_strength = -strength/1.5 actual_strength = -strength / 1.5
var velocity := current_velocity + facing_direction * actual_strength * delta var velocity := current_velocity + facing_direction * actual_strength * delta
velocity = velocity.linear_interpolate(Vector2.ZERO, ship_drag_coefficient) velocity = velocity.linear_interpolate(Vector2.ZERO, ship_drag_coefficient)
return velocity.clamped(speed_max) return velocity.clamped(speed_max)
func _get_movement() -> Vector2: func _get_movement() -> Vector2:
return Vector2( Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"), return Vector2(
Input.get_action_strength("sf_up") - Input.get_action_strength("sf_down")) Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"),
Input.get_action_strength("sf_up") - Input.get_action_strength("sf_down")
)
func _update_agent() -> void: func _update_agent() -> void:

View File

@ -1,9 +1,8 @@
extends Node extends Node
export (float, 0, 2000, 40) var linear_speed_max := 120.0 setget set_linear_speed_max
export(float, 0, 2000, 40) var linear_speed_max := 120.0 setget set_linear_speed_max export (float, 0, 200, 2) var linear_accel_max := 10.0 setget set_linear_accel_max
export(float, 0, 200, 2) var linear_accel_max := 10.0 setget set_linear_accel_max export (float, 0, 5, 0.1) var predict_time := 1.0 setget set_predict_time
export(float, 0, 5, 0.1) var predict_time := 1.0 setget set_predict_time
onready var pursuer := $BoundaryManager/Pursuer onready var pursuer := $BoundaryManager/Pursuer
onready var seeker := $BoundaryManager/Seeker onready var seeker := $BoundaryManager/Seeker
@ -18,7 +17,7 @@ func set_linear_speed_max(value: float) -> void:
linear_speed_max = value linear_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
pursuer.agent.linear_speed_max = value pursuer.agent.linear_speed_max = value
seeker.agent.linear_speed_max = value seeker.agent.linear_speed_max = value
@ -27,7 +26,7 @@ func set_linear_accel_max(value: float) -> void:
linear_accel_max = value linear_accel_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
pursuer.agent.linear_acceleration_max = value pursuer.agent.linear_acceleration_max = value
seeker.agent.linear_acceleration_max = value seeker.agent.linear_acceleration_max = value
@ -36,5 +35,5 @@ func set_predict_time(value: float) -> void:
predict_time = value predict_time = value
if not is_inside_tree(): if not is_inside_tree():
return return
pursuer._behavior.predict_time_max = value pursuer._behavior.predict_time_max = value

View File

@ -1,7 +1,6 @@
extends KinematicBody2D extends KinematicBody2D
# Represents a ship that chases after the player. # Represents a ship that chases after the player.
export var use_seek: bool = false export var use_seek: bool = false
var _blend: GSAIBlend var _blend: GSAIBlend
@ -22,28 +21,23 @@ func _ready() -> void:
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
_direction_face.position = agent.position + accel.linear.normalized() _direction_face.position = agent.position + accel.linear.normalized()
_blend.calculate_steering(accel) _blend.calculate_steering(accel)
agent.angular_velocity = clamp( agent.angular_velocity = clamp(
agent.angular_velocity + accel.angular, agent.angular_velocity + accel.angular, -agent.angular_speed_max, agent.angular_speed_max
-agent.angular_speed_max,
agent.angular_speed_max
) )
agent.angular_velocity = lerp(agent.angular_velocity, 0, _angular_drag) agent.angular_velocity = lerp(agent.angular_velocity, 0, _angular_drag)
rotation += agent.angular_velocity * delta rotation += agent.angular_velocity * delta
var linear_velocity := ( var linear_velocity := (
GSAIUtils.to_vector2(agent.linear_velocity) + GSAIUtils.to_vector2(agent.linear_velocity)
(GSAIUtils.angle_to_vector2(rotation) * -agent.linear_acceleration_max) + (GSAIUtils.angle_to_vector2(rotation) * -agent.linear_acceleration_max)
) )
linear_velocity = linear_velocity.clamped(agent.linear_speed_max) linear_velocity = linear_velocity.clamped(agent.linear_speed_max)
linear_velocity = linear_velocity.linear_interpolate( linear_velocity = linear_velocity.linear_interpolate(Vector2.ZERO, _linear_drag_coefficient)
Vector2.ZERO,
_linear_drag_coefficient
)
linear_velocity = move_and_slide(linear_velocity) linear_velocity = move_and_slide(linear_velocity)
agent.linear_velocity = GSAIUtils.to_vector3(linear_velocity) agent.linear_velocity = GSAIUtils.to_vector3(linear_velocity)
@ -54,18 +48,18 @@ func setup(predict_time: float, linear_speed_max: float, linear_accel_max: float
behavior = GSAISeek.new(agent, player_agent) behavior = GSAISeek.new(agent, player_agent)
else: else:
behavior = GSAIPursue.new(agent, player_agent, predict_time) behavior = GSAIPursue.new(agent, player_agent, predict_time)
var orient_behavior := GSAIFace.new(agent, _direction_face) var orient_behavior := GSAIFace.new(agent, _direction_face)
orient_behavior.alignment_tolerance = deg2rad(5) orient_behavior.alignment_tolerance = deg2rad(5)
orient_behavior.deceleration_radius = deg2rad(5) orient_behavior.deceleration_radius = deg2rad(5)
_blend = GSAIBlend.new(agent) _blend = GSAIBlend.new(agent)
_blend.add(behavior, 1) _blend.add(behavior, 1)
_blend.add(orient_behavior, 1) _blend.add(orient_behavior, 1)
agent.angular_acceleration_max = deg2rad(40) agent.angular_acceleration_max = deg2rad(40)
agent.angular_speed_max = deg2rad(90) agent.angular_speed_max = deg2rad(90)
agent.linear_acceleration_max = linear_accel_max agent.linear_acceleration_max = linear_accel_max
agent.linear_speed_max = linear_speed_max agent.linear_speed_max = linear_speed_max
set_physics_process(true) set_physics_process(true)

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
# Maximum possible linear velocity # Maximum possible linear velocity
export var speed_max := 450.0 export var speed_max := 450.0
# Maximum change in linear velocity # Maximum change in linear velocity
@ -73,7 +72,7 @@ func _ready() -> void:
# Face turns the agent to keep looking towards its target. It will be enabled while the agent # Face turns the agent to keep looking towards its target. It will be enabled while the agent
# is not fleeing due to low health. It tries to arrive 'on alignment' with 0 remaining velocity. # is not fleeing due to low health. It tries to arrive 'on alignment' with 0 remaining velocity.
var face := GSAIFace.new(agent, player_agent) var face := GSAIFace.new(agent, player_agent)
# We use deg2rad because the math in the toolkit assumes radians. # We use deg2rad because the math in the toolkit assumes radians.
# How close for the agent to be 'aligned', if not exact. # How close for the agent to be 'aligned', if not exact.
face.alignment_tolerance = deg2rad(5) face.alignment_tolerance = deg2rad(5)
@ -119,22 +118,20 @@ func _physics_process(delta: float) -> void:
# We add the discovered acceleration to our linear velocity. The toolkit does not limit # We add the discovered acceleration to our linear velocity. The toolkit does not limit
# velocity, just acceleration, so we clamp the result ourselves here. # velocity, just acceleration, so we clamp the result ourselves here.
velocity = (velocity + Vector2( velocity = (velocity + Vector2(acceleration.linear.x, acceleration.linear.y)).clamped(
acceleration.linear.x, acceleration.linear.y) agent.linear_speed_max
).clamped(agent.linear_speed_max) )
# This applies drag on the agent's motion, helping it to slow down naturally. # This applies drag on the agent's motion, helping it to slow down naturally.
velocity = velocity.linear_interpolate(Vector2.ZERO, linear_drag) velocity = velocity.linear_interpolate(Vector2.ZERO, linear_drag)
# And since we're using a KinematicBody2D, we use Godot's excellent move_and_slide to actually # And since we're using a KinematicBody2D, we use Godot's excellent move_and_slide to actually
# apply the final movement, and record any change in velocity the physics engine discovered. # apply the final movement, and record any change in velocity the physics engine discovered.
velocity = move_and_slide(velocity) velocity = move_and_slide(velocity)
# We then do something similar to apply our agent's rotational speed. # We then do something similar to apply our agent's rotational speed.
angular_velocity = clamp( angular_velocity = clamp(
angular_velocity + acceleration.angular, angular_velocity + acceleration.angular, -agent.angular_speed_max, agent.angular_speed_max
-agent.angular_speed_max,
agent.angular_speed_max
) )
# This applies drag on the agent's rotation, helping it slow down naturally. # This applies drag on the agent's rotation, helping it slow down naturally.
angular_velocity = lerp(angular_velocity, 0, angular_drag) angular_velocity = lerp(angular_velocity, 0, angular_drag)

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
export var speed := 1500.0 export var speed := 1500.0
var velocity := Vector2.ZERO var velocity := Vector2.ZERO

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
export var speed_max := 650.0 export var speed_max := 650.0
export var acceleration_max := 70.0 export var acceleration_max := 70.0
export var rotation_speed_max := 240 export var rotation_speed_max := 240
@ -25,27 +24,27 @@ func _ready() -> void:
agent.angular_acceleration_max = deg2rad(rotation_accel_max) agent.angular_acceleration_max = deg2rad(rotation_accel_max)
agent.bounding_radius = calculate_radius($CollisionPolygon2D.polygon) agent.bounding_radius = calculate_radius($CollisionPolygon2D.polygon)
update_agent() update_agent()
var mouse_pos := get_global_mouse_position() var mouse_pos := get_global_mouse_position()
proxy_target.position.x = mouse_pos.x proxy_target.position.x = mouse_pos.x
proxy_target.position.y = mouse_pos.y proxy_target.position.y = mouse_pos.y
face.alignment_tolerance = deg2rad(5) face.alignment_tolerance = deg2rad(5)
face.deceleration_radius = deg2rad(45) face.deceleration_radius = deg2rad(45)
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
update_agent() update_agent()
var movement := get_movement() var movement := get_movement()
direction = GSAIUtils.angle_to_vector2(rotation) direction = GSAIUtils.angle_to_vector2(rotation)
velocity += direction * acceleration_max * movement velocity += direction * acceleration_max * movement
velocity = velocity.clamped(speed_max) velocity = velocity.clamped(speed_max)
velocity = velocity.linear_interpolate(Vector2.ZERO, 0.1) velocity = velocity.linear_interpolate(Vector2.ZERO, 0.1)
velocity = move_and_slide(velocity) velocity = move_and_slide(velocity)
face.calculate_steering(accel) face.calculate_steering(accel)
angular_velocity += accel.angular angular_velocity += accel.angular
angular_velocity = clamp(angular_velocity, -agent.angular_speed_max, agent.angular_speed_max) angular_velocity = clamp(angular_velocity, -agent.angular_speed_max, agent.angular_speed_max)
@ -60,8 +59,11 @@ func _unhandled_input(event: InputEvent) -> void:
proxy_target.position.y = mouse_pos.y proxy_target.position.y = mouse_pos.y
elif event is InputEventMouseButton: elif event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed: if event.button_index == BUTTON_LEFT and event.pressed:
var next_bullet: = bullet.instance() var next_bullet := bullet.instance()
next_bullet.global_position = global_position - direction * (agent.bounding_radius-5) next_bullet.global_position = (
global_position
- direction * (agent.bounding_radius - 5)
)
next_bullet.player = self next_bullet.player = self
next_bullet.start(-direction) next_bullet.start(-direction)
bullets.add_child(next_bullet) bullets.add_child(next_bullet)

View File

@ -1,35 +1,32 @@
extends Node2D extends Node2D
const COLOR := Color("8fde5d") const COLOR := Color("8fde5d")
func _ready() -> void: func _ready() -> void:
get_tree().root.connect("size_changed", self, "_on_SceneTree_size_changed") get_tree().root.connect("size_changed", self, "_on_SceneTree_size_changed")
_on_SceneTree_size_changed() _on_SceneTree_size_changed()
func _draw() -> void: func _draw() -> void:
for b in get_children(): for b in get_children():
var extents: Vector2 = b.get_node("CollisionShape2D").shape.extents var extents: Vector2 = b.get_node("CollisionShape2D").shape.extents
draw_rect(Rect2(b.global_position-extents, extents*2), COLOR) draw_rect(Rect2(b.global_position - extents, extents * 2), COLOR)
func _on_SceneTree_size_changed() -> void: func _on_SceneTree_size_changed() -> void:
var size := Vector2( var size := Vector2(
ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/height"]
ProjectSettings["display/window/size/height"]
) )
for b in get_children(): for b in get_children():
var boundary: String = b.name.rsplit("Boundary")[0] var boundary: String = b.name.rsplit("Boundary")[0]
match boundary: match boundary:
"Left": "Left":
b.global_position = Vector2(0, size.y/2) b.global_position = Vector2(0, size.y / 2)
"Right": "Right":
b.global_position = Vector2(size.x, size.y/2) b.global_position = Vector2(size.x, size.y / 2)
"Top": "Top":
b.global_position = Vector2(size.x/2, 0) b.global_position = Vector2(size.x / 2, 0)
"Bottom": "Bottom":
b.global_position = Vector2(size.x/2, size.y) b.global_position = Vector2(size.x / 2, size.y)
update() update()

View File

@ -1,7 +1,6 @@
extends KinematicBody2D extends KinematicBody2D
# Class to control the player in basic left/right up/down movement. # Class to control the player in basic left/right up/down movement.
var speed: float var speed: float
onready var agent := GSAIAgentLocation.new() onready var agent := GSAIAgentLocation.new()
@ -14,7 +13,7 @@ func _physics_process(_delta: float) -> void:
var movement := _get_movement() var movement := _get_movement()
if movement.length_squared() < 0.01: if movement.length_squared() < 0.01:
return return
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
move_and_slide(movement * speed) move_and_slide(movement * speed)
agent.position = GSAIUtils.to_vector3(global_position) agent.position = GSAIUtils.to_vector3(global_position)
@ -22,5 +21,6 @@ func _physics_process(_delta: float) -> void:
func _get_movement() -> Vector2: func _get_movement() -> Vector2:
return Vector2( return Vector2(
Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"), Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"),
Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up")) Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up")
)

View File

@ -1,13 +1,12 @@
extends Node extends Node
# Access helper class for children to access window boundaries. # Access helper class for children to access window boundaries.
enum Mode { FLEE, SEEK } enum Mode { FLEE, SEEK }
export(Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode export (Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode
export(float, 0, 2000, 40) var linear_speed_max := 200.0 setget set_linear_speed_max export (float, 0, 2000, 40) var linear_speed_max := 200.0 setget set_linear_speed_max
export(float, 0, 500, 0.5) var linear_accel_max := 10.0 setget set_linear_accel_max export (float, 0, 500, 0.5) var linear_accel_max := 10.0 setget set_linear_accel_max
export(float) var player_speed := 600.0 setget set_player_speed export (float) var player_speed := 600.0 setget set_player_speed
var camera_boundaries: Rect2 var camera_boundaries: Rect2
@ -21,18 +20,18 @@ func _ready() -> void:
Vector2( Vector2(
ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/width"],
ProjectSettings["display/window/size/height"] ProjectSettings["display/window/size/height"]
)
) )
)
var rng := RandomNumberGenerator.new() var rng := RandomNumberGenerator.new()
rng.randomize() rng.randomize()
player.speed = player_speed player.speed = player_speed
for i in range(spawner.entity_count): for i in range(spawner.entity_count):
var new_pos := Vector2( var new_pos := Vector2(
rng.randf_range(0, camera_boundaries.size.x), rng.randf_range(0, camera_boundaries.size.x),
rng.randf_range(0, camera_boundaries.size.y) rng.randf_range(0, camera_boundaries.size.y)
) )
var entity: KinematicBody2D = spawner.Entity.instance() var entity: KinematicBody2D = spawner.Entity.instance()
entity.global_position = new_pos entity.global_position = new_pos
@ -47,7 +46,7 @@ func set_behavior_mode(mode: int) -> void:
behavior_mode = mode behavior_mode = mode
if not is_inside_tree(): if not is_inside_tree():
return return
match mode: match mode:
Mode.SEEK: Mode.SEEK:
for child in spawner.get_children(): for child in spawner.get_children():
@ -61,7 +60,7 @@ func set_linear_speed_max(value: float) -> void:
linear_speed_max = value linear_speed_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
for child in spawner.get_children(): for child in spawner.get_children():
child.agent.linear_speed_max = value child.agent.linear_speed_max = value
@ -70,7 +69,7 @@ func set_linear_accel_max(value: float) -> void:
linear_accel_max = value linear_accel_max = value
if not is_inside_tree(): if not is_inside_tree():
return return
for child in spawner.get_children(): for child in spawner.get_children():
child.agent.linear_acceleration_max = value child.agent.linear_acceleration_max = value
@ -79,5 +78,5 @@ func set_player_speed(value: float) -> void:
player_speed = value player_speed = value
if not is_inside_tree(): if not is_inside_tree():
return return
player.speed = player_speed player.speed = player_speed

View File

@ -1,6 +1,5 @@
extends KinematicBody2D extends KinematicBody2D
var player_agent: GSAIAgentLocation var player_agent: GSAIAgentLocation
var velocity := Vector2.ZERO var velocity := Vector2.ZERO
var start_speed: float var start_speed: float
@ -21,10 +20,10 @@ func _ready() -> void:
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
if not player_agent: if not player_agent:
return return
if use_seek: if use_seek:
seek.calculate_steering(accel) seek.calculate_steering(accel)
else: else:
flee.calculate_steering(accel) flee.calculate_steering(accel)
agent._apply_steering(accel, delta) agent._apply_steering(accel, delta)

View File

@ -1,7 +1,6 @@
extends Node2D extends Node2D
# Holds data to instantiate and configure a number of agent entities. # Holds data to instantiate and configure a number of agent entities.
export (PackedScene) var Entity: PackedScene
export(PackedScene) var Entity: PackedScene export var entity_count := 10
export var entity_count:= 10 export var entity_color := Color.blue
export var entity_color:= Color.blue

View File

@ -1,9 +1,9 @@
tool tool
extends CollisionShape2D extends CollisionShape2D
export(Color) var inner_color := Color() setget set_inner_color export (Color) var inner_color := Color() setget set_inner_color
export(Color) var outer_color := Color() setget set_outer_color export (Color) var outer_color := Color() setget set_outer_color
export(float) var stroke := 0.0 setget set_stroke export (float) var stroke := 0.0 setget set_stroke
func _draw() -> void: func _draw() -> void:

View File

@ -1,13 +1,12 @@
tool tool
extends PanelContainer extends PanelContainer
export (String, MULTILINE) var text_bbcode := "" setget set_text_bbcode
export(String, MULTILINE) var text_bbcode := "" setget set_text_bbcode
onready var rich_text_label: RichTextLabel = $MarginContainer/RichTextLabel onready var rich_text_label: RichTextLabel = $MarginContainer/RichTextLabel
func set_text_bbcode(value: String)-> void: func set_text_bbcode(value: String) -> void:
text_bbcode = value text_bbcode = value
if not rich_text_label: if not rich_text_label:
yield(self, "ready") yield(self, "ready")

View File

@ -1,8 +1,7 @@
tool tool
extends Line2D extends Line2D
export (Color) var inner_color := Color() setget set_inner_color
export(Color) var inner_color := Color() setget set_inner_color
func _draw() -> void: func _draw() -> void:

View File

@ -3,13 +3,11 @@
extends GSAISpecializedAgent extends GSAISpecializedAgent
class_name GSAIKinematicBody2DAgent class_name GSAIKinematicBody2DAgent
# SLIDE uses `move_and_slide` # SLIDE uses `move_and_slide`
# COLLIDE uses `move_and_collide` # COLLIDE uses `move_and_collide`
# POSITION changes the `global_position` directly # POSITION changes the `global_position` directly
enum MovementType { SLIDE, COLLIDE, POSITION } enum MovementType { SLIDE, COLLIDE, POSITION }
# The KinematicBody2D to keep track of # The KinematicBody2D to keep track of
var body: KinematicBody2D setget _set_body var body: KinematicBody2D setget _set_body
@ -22,10 +20,10 @@ var _last_position: Vector2
func _init(_body: KinematicBody2D, _movement_type: int = MovementType.SLIDE) -> void: func _init(_body: KinematicBody2D, _movement_type: int = MovementType.SLIDE) -> void:
if not _body.is_inside_tree(): if not _body.is_inside_tree():
yield(_body, "ready") yield(_body, "ready")
self.body = _body self.body = _body
self.movement_type = _movement_type self.movement_type = _movement_type
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame") body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame")
@ -41,7 +39,7 @@ func _apply_steering(acceleration: GSAITargetAcceleration, delta: float) -> void
_apply_sliding_steering(acceleration.linear) _apply_sliding_steering(acceleration.linear)
_: _:
_apply_position_steering(acceleration.linear, delta) _apply_position_steering(acceleration.linear, delta)
_apply_orientation_steering(acceleration.angular, delta) _apply_orientation_steering(acceleration.angular, delta)
@ -57,10 +55,7 @@ func _apply_sliding_steering(accel: Vector3) -> void:
func _apply_collide_steering(accel: Vector3, delta: float) -> void: func _apply_collide_steering(accel: Vector3, delta: float) -> void:
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max) var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag: if apply_linear_drag:
velocity = velocity.linear_interpolate( velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
Vector3.ZERO,
linear_drag_percentage
)
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
body.move_and_collide(GSAIUtils.to_vector2(velocity) * delta) body.move_and_collide(GSAIUtils.to_vector2(velocity) * delta)
if calculate_velocities: if calculate_velocities:
@ -70,10 +65,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
func _apply_position_steering(accel: Vector3, delta: float) -> void: func _apply_position_steering(accel: Vector3, delta: float) -> void:
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max) var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag: if apply_linear_drag:
velocity = velocity.linear_interpolate( velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
Vector3.ZERO,
linear_drag_percentage
)
body.global_position += GSAIUtils.to_vector2(velocity) * delta body.global_position += GSAIUtils.to_vector2(velocity) * delta
if calculate_velocities: if calculate_velocities:
linear_velocity = velocity linear_velocity = velocity
@ -90,10 +82,10 @@ func _apply_orientation_steering(angular_acceleration: float, delta: float) -> v
func _set_body(value: KinematicBody2D) -> void: func _set_body(value: KinematicBody2D) -> void:
body = value body = value
_last_position = body.global_position _last_position = body.global_position
_last_orientation = body.rotation _last_orientation = body.rotation
position = GSAIUtils.to_vector3(_last_position) position = GSAIUtils.to_vector3(_last_position)
orientation = _last_orientation orientation = _last_orientation
@ -101,36 +93,28 @@ func _set_body(value: KinematicBody2D) -> void:
func _on_SceneTree_physics_frame() -> void: func _on_SceneTree_physics_frame() -> void:
var current_position := body.global_position var current_position := body.global_position
var current_orientation := body.rotation var current_orientation := body.rotation
position = GSAIUtils.to_vector3(current_position) position = GSAIUtils.to_vector3(current_position)
orientation = current_orientation orientation = current_orientation
if calculate_velocities: if calculate_velocities:
if _applied_steering: if _applied_steering:
_applied_steering = false _applied_steering = false
else: else:
linear_velocity = GSAIUtils.clampedv3( linear_velocity = GSAIUtils.clampedv3(
GSAIUtils.to_vector3(_last_position - current_position), GSAIUtils.to_vector3(_last_position - current_position), linear_speed_max
linear_speed_max
) )
if apply_linear_drag: if apply_linear_drag:
linear_velocity = linear_velocity.linear_interpolate( linear_velocity = linear_velocity.linear_interpolate(
Vector3.ZERO, Vector3.ZERO, linear_drag_percentage
linear_drag_percentage
) )
angular_velocity = clamp( angular_velocity = clamp(
_last_orientation - current_orientation, _last_orientation - current_orientation, -angular_speed_max, angular_speed_max
-angular_speed_max,
angular_speed_max
) )
if apply_angular_drag: if apply_angular_drag:
angular_velocity = lerp( angular_velocity = lerp(angular_velocity, 0, angular_drag_percentage)
angular_velocity,
0,
angular_drag_percentage
)
_last_position = current_position _last_position = current_position
_last_orientation = current_orientation _last_orientation = current_orientation

View File

@ -8,7 +8,6 @@ class_name GSAIKinematicBody3DAgent
# POSITION changes the global_position directly # POSITION changes the global_position directly
enum MovementType { SLIDE, COLLIDE, POSITION } enum MovementType { SLIDE, COLLIDE, POSITION }
# The KinematicBody to keep track of # The KinematicBody to keep track of
var body: KinematicBody setget _set_body var body: KinematicBody setget _set_body
@ -21,10 +20,10 @@ var _last_position: Vector3
func _init(_body: KinematicBody, _movement_type: int = MovementType.SLIDE) -> void: func _init(_body: KinematicBody, _movement_type: int = MovementType.SLIDE) -> void:
if not _body.is_inside_tree(): if not _body.is_inside_tree():
yield(_body, "ready") yield(_body, "ready")
self.body = _body self.body = _body
self.movement_type = _movement_type self.movement_type = _movement_type
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
self.body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame") self.body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame")
@ -40,7 +39,7 @@ func _apply_steering(acceleration: GSAITargetAcceleration, delta: float) -> void
_apply_sliding_steering(acceleration.linear) _apply_sliding_steering(acceleration.linear)
_: _:
_apply_position_steering(acceleration.linear, delta) _apply_position_steering(acceleration.linear, delta)
_apply_orientation_steering(acceleration.angular, delta) _apply_orientation_steering(acceleration.angular, delta)
@ -56,10 +55,7 @@ func _apply_sliding_steering(accel: Vector3) -> void:
func _apply_collide_steering(accel: Vector3, delta: float) -> void: func _apply_collide_steering(accel: Vector3, delta: float) -> void:
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max) var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag: if apply_linear_drag:
velocity = velocity.linear_interpolate( velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
Vector3.ZERO,
linear_drag_percentage
)
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
body.move_and_collide(velocity * delta) body.move_and_collide(velocity * delta)
if calculate_velocities: if calculate_velocities:
@ -69,10 +65,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
func _apply_position_steering(accel: Vector3, delta: float) -> void: func _apply_position_steering(accel: Vector3, delta: float) -> void:
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max) var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag: if apply_linear_drag:
velocity = velocity.linear_interpolate( velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
Vector3.ZERO,
linear_drag_percentage
)
body.global_position += velocity * delta body.global_position += velocity * delta
if calculate_velocities: if calculate_velocities:
linear_velocity = velocity linear_velocity = velocity
@ -89,10 +82,10 @@ func _apply_orientation_steering(angular_acceleration: float, delta: float) -> v
func _set_body(value: KinematicBody) -> void: func _set_body(value: KinematicBody) -> void:
body = value body = value
_last_position = body.transform.origin _last_position = body.transform.origin
_last_orientation = body.rotation.y _last_orientation = body.rotation.y
position = _last_position position = _last_position
orientation = _last_orientation orientation = _last_orientation
@ -100,36 +93,28 @@ func _set_body(value: KinematicBody) -> void:
func _on_SceneTree_physics_frame() -> void: func _on_SceneTree_physics_frame() -> void:
var current_position := body.transform.origin var current_position := body.transform.origin
var current_orientation := body.rotation.y var current_orientation := body.rotation.y
position = current_position position = current_position
orientation = current_orientation orientation = current_orientation
if calculate_velocities: if calculate_velocities:
if _applied_steering: if _applied_steering:
_applied_steering = false _applied_steering = false
else: else:
linear_velocity = GSAIUtils.clampedv3( linear_velocity = GSAIUtils.clampedv3(
_last_position - current_position, _last_position - current_position, linear_speed_max
linear_speed_max
) )
if apply_linear_drag: if apply_linear_drag:
linear_velocity = linear_velocity.linear_interpolate( linear_velocity = linear_velocity.linear_interpolate(
Vector3.ZERO, Vector3.ZERO, linear_drag_percentage
linear_drag_percentage
) )
angular_velocity = clamp( angular_velocity = clamp(
_last_orientation - current_orientation, _last_orientation - current_orientation, -angular_speed_max, angular_speed_max
-angular_speed_max,
angular_speed_max
) )
if apply_angular_drag: if apply_angular_drag:
angular_velocity = lerp( angular_velocity = lerp(angular_velocity, 0, angular_drag_percentage)
angular_velocity,
0,
angular_drag_percentage
)
_last_position = current_position _last_position = current_position
_last_orientation = current_orientation _last_orientation = current_orientation

View File

@ -3,7 +3,6 @@
extends GSAISpecializedAgent extends GSAISpecializedAgent
class_name GSAIRigidBody2DAgent class_name GSAIRigidBody2DAgent
# The RigidBody2D to keep track of # The RigidBody2D to keep track of
var body: RigidBody2D setget _set_body var body: RigidBody2D setget _set_body
@ -13,7 +12,7 @@ var _last_position: Vector2
func _init(_body: RigidBody2D) -> void: func _init(_body: RigidBody2D) -> void:
if not _body.is_inside_tree(): if not _body.is_inside_tree():
yield(_body, "ready") yield(_body, "ready")
self.body = _body self.body = _body
@ -30,10 +29,10 @@ func _apply_steering(acceleration: GSAITargetAcceleration, _delta: float) -> voi
func _set_body(value: RigidBody2D) -> void: func _set_body(value: RigidBody2D) -> void:
body = value body = value
_last_position = body.global_position _last_position = body.global_position
_last_orientation = body.rotation _last_orientation = body.rotation
position = GSAIUtils.to_vector3(_last_position) position = GSAIUtils.to_vector3(_last_position)
orientation = _last_orientation orientation = _last_orientation
@ -47,10 +46,10 @@ func _on_body_ready() -> void:
func _on_SceneTree_frame() -> void: func _on_SceneTree_frame() -> void:
var current_position := body.global_position var current_position := body.global_position
var current_orientation := body.rotation var current_orientation := body.rotation
position = GSAIUtils.to_vector3(current_position) position = GSAIUtils.to_vector3(current_position)
orientation = current_orientation orientation = current_orientation
if calculate_velocities: if calculate_velocities:
if _applied_steering: if _applied_steering:
_applied_steering = false _applied_steering = false

View File

@ -3,7 +3,6 @@
extends GSAISpecializedAgent extends GSAISpecializedAgent
class_name GSAIRigidBody3DAgent class_name GSAIRigidBody3DAgent
# The RigidBody to keep track of # The RigidBody to keep track of
var body: RigidBody setget _set_body var body: RigidBody setget _set_body
@ -13,11 +12,10 @@ var _last_position: Vector3
func _init(_body: RigidBody) -> void: func _init(_body: RigidBody) -> void:
if not _body.is_inside_tree(): if not _body.is_inside_tree():
yield(_body, "ready") yield(_body, "ready")
self.body = _body self.body = _body
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
self.body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame") self.body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
# Moves the agent's `body` by target `acceleration`. # Moves the agent's `body` by target `acceleration`.
@ -33,10 +31,10 @@ func _apply_steering(acceleration: GSAITargetAcceleration, _delta: float) -> voi
func _set_body(value: RigidBody) -> void: func _set_body(value: RigidBody) -> void:
body = value body = value
_last_position = body.transform.origin _last_position = body.transform.origin
_last_orientation = body.rotation.y _last_orientation = body.rotation.y
position = _last_position position = _last_position
orientation = _last_orientation orientation = _last_orientation
@ -50,10 +48,10 @@ func _on_body_ready() -> void:
func _on_SceneTree_frame() -> void: func _on_SceneTree_frame() -> void:
var current_position := body.transform.origin var current_position := body.transform.origin
var current_orientation := body.rotation.y var current_orientation := body.rotation.y
position = current_position position = current_position
orientation = current_orientation orientation = current_orientation
if calculate_velocities: if calculate_velocities:
if _applied_steering: if _applied_steering:
_applied_steering = false _applied_steering = false

View File

@ -4,7 +4,6 @@
extends GSAISteeringAgent extends GSAISteeringAgent
class_name GSAISpecializedAgent class_name GSAISpecializedAgent
# If `true`, calculates linear and angular velocities based on the previous # If `true`, calculates linear and angular velocities based on the previous
# frame. When `false`, the user must keep those values updated. # frame. When `false`, the user must keep those values updated.
var calculate_velocities := true var calculate_velocities := true

View File

@ -3,7 +3,6 @@
class_name GSAIArrive class_name GSAIArrive
extends GSAISteeringBehavior extends GSAISteeringBehavior
# Target agent to arrive to. # Target agent to arrive to.
var target: GSAIAgentLocation var target: GSAIAgentLocation
# Distance from the target for the agent to be considered successfully # Distance from the target for the agent to be considered successfully
@ -31,9 +30,9 @@ func _arrive(acceleration: GSAITargetAcceleration, target_position: Vector3) ->
if distance <= deceleration_radius: if distance <= deceleration_radius:
desired_speed *= distance / deceleration_radius desired_speed *= distance / deceleration_radius
var desired_velocity := to_target * desired_speed/distance var desired_velocity := to_target * desired_speed / distance
desired_velocity = (desired_velocity - agent.linear_velocity) * 1.0 / time_to_reach desired_velocity = ((desired_velocity - agent.linear_velocity) * 1.0 / time_to_reach)
acceleration.linear = GSAIUtils.clampedv3(desired_velocity, agent.linear_acceleration_max) acceleration.linear = GSAIUtils.clampedv3(desired_velocity, agent.linear_acceleration_max)
acceleration.angular = 0 acceleration.angular = 0

View File

@ -3,7 +3,6 @@
class_name GSAIAvoidCollisions class_name GSAIAvoidCollisions
extends GSAIGroupBehavior extends GSAIGroupBehavior
var _first_neighbor: GSAISteeringAgent var _first_neighbor: GSAISteeringAgent
var _shortest_time: float var _shortest_time: float
var _first_minimum_separation: float var _first_minimum_separation: float
@ -27,14 +26,18 @@ func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
if neighbor_count == 0 or not _first_neighbor: if neighbor_count == 0 or not _first_neighbor:
acceleration.set_zero() acceleration.set_zero()
else: else:
if( if (
_first_minimum_separation <= 0 or _first_minimum_separation <= 0
_first_distance < agent.bounding_radius + _first_neighbor.bounding_radius): or _first_distance < agent.bounding_radius + _first_neighbor.bounding_radius
):
acceleration.linear = _first_neighbor.position - agent.position acceleration.linear = _first_neighbor.position - agent.position
else: else:
acceleration.linear = _first_relative_position + (_first_relative_velocity * _shortest_time) acceleration.linear = (
_first_relative_position
+ (_first_relative_velocity * _shortest_time)
)
acceleration.linear = acceleration.linear.normalized() * -agent.linear_acceleration_max acceleration.linear = (acceleration.linear.normalized() * -agent.linear_acceleration_max)
acceleration.angular = 0 acceleration.angular = 0
@ -55,7 +58,10 @@ func _report_neighbor(neighbor: GSAISteeringAgent) -> bool:
return false return false
else: else:
var distance = relative_position.length() var distance = relative_position.length()
var minimum_separation: float = distance - sqrt(relative_speed_squared) * time_to_collision var minimum_separation: float = (
distance
- sqrt(relative_speed_squared) * time_to_collision
)
if minimum_separation > agent.bounding_radius + neighbor.bounding_radius: if minimum_separation > agent.bounding_radius + neighbor.bounding_radius:
return false return false
else: else:

View File

@ -9,7 +9,6 @@
class_name GSAIBlend class_name GSAIBlend
extends GSAISteeringBehavior extends GSAISteeringBehavior
var _behaviors := [] var _behaviors := []
var _accel := GSAITargetAcceleration.new() var _accel := GSAITargetAcceleration.new()
@ -44,7 +43,5 @@ func _calculate_steering(blended_accel: GSAITargetAcceleration) -> void:
blended_accel.linear = GSAIUtils.clampedv3(blended_accel.linear, agent.linear_acceleration_max) blended_accel.linear = GSAIUtils.clampedv3(blended_accel.linear, agent.linear_acceleration_max)
blended_accel.angular = clamp( blended_accel.angular = clamp(
blended_accel.angular, blended_accel.angular, -agent.angular_acceleration_max, agent.angular_acceleration_max
-agent.angular_acceleration_max,
agent.angular_acceleration_max
) )

View File

@ -3,7 +3,6 @@
class_name GSAICohesion class_name GSAICohesion
extends GSAIGroupBehavior extends GSAIGroupBehavior
var _center_of_mass: Vector3 var _center_of_mass: Vector3
@ -17,7 +16,10 @@ func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
var neighbor_count = proximity._find_neighbors(_callback) var neighbor_count = proximity._find_neighbors(_callback)
if neighbor_count > 0: if neighbor_count > 0:
_center_of_mass *= 1.0 / neighbor_count _center_of_mass *= 1.0 / neighbor_count
acceleration.linear = (_center_of_mass - agent.position).normalized() * agent.linear_acceleration_max acceleration.linear = (
(_center_of_mass - agent.position).normalized()
* agent.linear_acceleration_max
)
# Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position # Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position

View File

@ -4,10 +4,9 @@ class_name GSAIEvade
extends GSAIPursue extends GSAIPursue
func _init( func _init(agent: GSAISteeringAgent, target: GSAISteeringAgent, predict_time_max := 1.0).(
agent: GSAISteeringAgent, agent, target, predict_time_max
target: GSAISteeringAgent, ):
predict_time_max := 1.0).(agent, target, predict_time_max):
pass pass

View File

@ -4,7 +4,9 @@ class_name GSAIFace
extends GSAIMatchOrientation extends GSAIMatchOrientation
func _init(agent: GSAISteeringAgent, target: GSAIAgentLocation, use_z := false).(agent, target, use_z) -> void: func _init(agent: GSAISteeringAgent, target: GSAIAgentLocation, use_z := false).(
agent, target, use_z
) -> void:
pass pass
@ -15,7 +17,8 @@ func _face(acceleration: GSAITargetAcceleration, target_position: Vector3) -> vo
if distance_squared < agent.zero_linear_speed_threshold: if distance_squared < agent.zero_linear_speed_threshold:
acceleration.set_zero() acceleration.set_zero()
else: else:
var orientation = (GSAIUtils.vector3_to_angle(to_target) var orientation = (
GSAIUtils.vector3_to_angle(to_target)
if use_z if use_z
else GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(to_target)) else GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(to_target))
) )

View File

@ -9,5 +9,7 @@ func _init(agent: GSAISteeringAgent, target: GSAIAgentLocation).(agent, target)
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void: func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
acceleration.linear = ( acceleration.linear = (
(agent.position - target.position).normalized() * agent.linear_acceleration_max) (agent.position - target.position).normalized()
* agent.linear_acceleration_max
)
acceleration.angular = 0 acceleration.angular = 0

View File

@ -2,7 +2,6 @@
class_name GSAIFollowPath class_name GSAIFollowPath
extends GSAIArrive extends GSAIArrive
# The path to follow and travel along. # The path to follow and travel along.
var path: GSAIPath var path: GSAIPath
# The distance along the path to generate the next target position. # The distance along the path to generate the next target position.
@ -15,11 +14,9 @@ var is_arrive_enabled := true
var prediction_time := 0.0 var prediction_time := 0.0
func _init( func _init(agent: GSAISteeringAgent, _path: GSAIPath, _path_offset := 0.0, _prediction_time := 0.0).(
agent: GSAISteeringAgent, agent, null
_path: GSAIPath, ) -> void:
_path_offset := 0.0,
_prediction_time := 0.0).(agent, null) -> void:
self.path = _path self.path = _path
self.path_offset = _path_offset self.path_offset = _path_offset
self.prediction_time = _prediction_time self.prediction_time = _prediction_time
@ -27,18 +24,20 @@ func _init(
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void: func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
var location := ( var location := (
agent.position if prediction_time == 0 agent.position
else agent.position + (agent.linear_velocity * prediction_time)) if prediction_time == 0
else agent.position + (agent.linear_velocity * prediction_time)
)
var distance := path.calculate_distance(location) var distance := path.calculate_distance(location)
var target_distance := distance + path_offset var target_distance := distance + path_offset
if prediction_time > 0 and path.is_open: if prediction_time > 0 and path.is_open:
if target_distance < path.calculate_distance(agent.position): if target_distance < path.calculate_distance(agent.position):
target_distance = path.length target_distance = path.length
var target_position := path.calculate_target_position(target_distance) var target_position := path.calculate_target_position(target_distance)
if is_arrive_enabled and path.is_open: if is_arrive_enabled and path.is_open:
if path_offset >= 0: if path_offset >= 0:
if target_distance > path.length - deceleration_radius: if target_distance > path.length - deceleration_radius:

View File

@ -12,7 +12,8 @@ func _calculate_steering(accel: GSAITargetAcceleration) -> void:
if agent.linear_velocity.length_squared() < agent.zero_linear_speed_threshold: if agent.linear_velocity.length_squared() < agent.zero_linear_speed_threshold:
accel.set_zero() accel.set_zero()
else: else:
var orientation := (GSAIUtils.vector3_to_angle(agent.linear_velocity) var orientation := (
GSAIUtils.vector3_to_angle(agent.linear_velocity)
if use_z if use_z
else GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(agent.linear_velocity)) else GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(agent.linear_velocity))
) )

View File

@ -4,7 +4,6 @@
class_name GSAIMatchOrientation class_name GSAIMatchOrientation
extends GSAISteeringBehavior extends GSAISteeringBehavior
# The target orientation for the behavior to try and match rotations to. # The target orientation for the behavior to try and match rotations to.
var target: GSAIAgentLocation var target: GSAIAgentLocation
# The amount of distance in radians for the behavior to consider itself close # The amount of distance in radians for the behavior to consider itself close
@ -39,11 +38,11 @@ func _match_orientation(acceleration: GSAITargetAcceleration, desired_orientatio
desired_rotation *= rotation / rotation_size desired_rotation *= rotation / rotation_size
acceleration.angular = (desired_rotation - agent.angular_velocity) / time_to_reach acceleration.angular = ((desired_rotation - agent.angular_velocity) / time_to_reach)
var limited_acceleration := abs(acceleration.angular) var limited_acceleration := abs(acceleration.angular)
if limited_acceleration > agent.angular_acceleration_max: if limited_acceleration > agent.angular_acceleration_max:
acceleration.angular *= agent.angular_acceleration_max / limited_acceleration acceleration.angular *= (agent.angular_acceleration_max / limited_acceleration)
acceleration.linear = Vector3.ZERO acceleration.linear = Vector3.ZERO

View File

@ -3,7 +3,6 @@
class_name GSAIPriority class_name GSAIPriority
extends GSAISteeringBehavior extends GSAISteeringBehavior
var _behaviors := [] var _behaviors := []
# The index of the last behavior the container prioritized. # The index of the last behavior the container prioritized.

View File

@ -3,7 +3,6 @@
class_name GSAIPursue class_name GSAIPursue
extends GSAISteeringBehavior extends GSAISteeringBehavior
# The target agent that the behavior is trying to intercept. # The target agent that the behavior is trying to intercept.
var target: GSAISteeringAgent var target: GSAISteeringAgent
# The maximum amount of time in the future the behavior predicts the target's # The maximum amount of time in the future the behavior predicts the target's
@ -11,10 +10,7 @@ var target: GSAISteeringAgent
var predict_time_max: float var predict_time_max: float
func _init( func _init(agent: GSAISteeringAgent, _target: GSAISteeringAgent, _predict_time_max := 1.0).(agent) -> void:
agent: GSAISteeringAgent,
_target: GSAISteeringAgent,
_predict_time_max := 1.0).(agent) -> void:
self.target = _target self.target = _target
self.predict_time_max = _predict_time_max self.predict_time_max = _predict_time_max
@ -31,8 +27,7 @@ func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
if predict_time_squared < predict_time_max * predict_time_max: if predict_time_squared < predict_time_max * predict_time_max:
predict_time = sqrt(predict_time_squared) predict_time = sqrt(predict_time_squared)
acceleration.linear = (( acceleration.linear = ((target_position + (target.linear_velocity * predict_time)) - agent.position).normalized()
target_position + (target.linear_velocity * predict_time))-agent.position).normalized()
acceleration.linear *= _get_modified_acceleration() acceleration.linear *= _get_modified_acceleration()
acceleration.angular = 0 acceleration.angular = 0

View File

@ -3,7 +3,6 @@
class_name GSAISeek class_name GSAISeek
extends GSAISteeringBehavior extends GSAISteeringBehavior
# The target that the behavior aims to move the agent to. # The target that the behavior aims to move the agent to.
var target: GSAIAgentLocation var target: GSAIAgentLocation
@ -14,5 +13,7 @@ func _init(agent: GSAISteeringAgent, _target: GSAIAgentLocation).(agent) -> void
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void: func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
acceleration.linear = ( acceleration.linear = (
(target.position - agent.position).normalized() * agent.linear_acceleration_max) (target.position - agent.position).normalized()
* agent.linear_acceleration_max
)
acceleration.angular = 0 acceleration.angular = 0

View File

@ -7,7 +7,6 @@
class_name GSAISeparation class_name GSAISeparation
extends GSAIGroupBehavior extends GSAIGroupBehavior
# The coefficient to calculate how fast the separation strength decays with distance. # The coefficient to calculate how fast the separation strength decays with distance.
var decay_coefficient := 1.0 var decay_coefficient := 1.0

View File

@ -1,7 +1,6 @@
# Represents an agent with only a location and an orientation. # Represents an agent with only a location and an orientation.
class_name GSAIAgentLocation class_name GSAIAgentLocation
# The agent's position in space. # The agent's position in space.
var position := Vector3.ZERO var position := Vector3.ZERO
# The agent's orientation on its Y axis rotation. # The agent's orientation on its Y axis rotation.

View File

@ -2,7 +2,6 @@
class_name GSAIGroupBehavior class_name GSAIGroupBehavior
extends GSAISteeringBehavior extends GSAISteeringBehavior
# Container to find neighbors of the agent and calculate group behavior. # Container to find neighbors of the agent and calculate group behavior.
var proximity: GSAIProximity var proximity: GSAIProximity

View File

@ -3,7 +3,6 @@
class_name GSAIPath class_name GSAIPath
extends Reference extends Reference
# If `false`, the path loops. # If `false`, the path loops.
var is_open: bool var is_open: bool
# Total length of the path. # Total length of the path.
@ -56,10 +55,8 @@ func calculate_distance(agent_current_position: Vector3) -> float:
for i in range(_segments.size()): for i in range(_segments.size()):
var segment: GSAISegment = _segments[i] var segment: GSAISegment = _segments[i]
var distance_squared := _calculate_point_segment_distance_squared( var distance_squared := _calculate_point_segment_distance_squared(
segment.begin, segment.begin, segment.end, agent_current_position
segment.end, )
agent_current_position
)
if distance_squared < smallest_distance_squared: if distance_squared < smallest_distance_squared:
_nearest_point_on_path = _nearest_point_on_segment _nearest_point_on_path = _nearest_point_on_segment
@ -67,8 +64,9 @@ func calculate_distance(agent_current_position: Vector3) -> float:
nearest_segment = segment nearest_segment = segment
var length_on_path := ( var length_on_path := (
nearest_segment.cumulative_length - nearest_segment.cumulative_length
_nearest_point_on_path.distance_to(nearest_segment.end)) - _nearest_point_on_path.distance_to(nearest_segment.end)
)
return length_on_path return length_on_path
@ -96,8 +94,9 @@ func calculate_target_position(target_distance: float) -> Vector3:
var distance := desired_segment.cumulative_length - target_distance var distance := desired_segment.cumulative_length - target_distance
return ( return (
(desired_segment.begin - desired_segment.end) * ((desired_segment.begin - desired_segment.end) * (distance / desired_segment.length))
(distance / desired_segment.length) + desired_segment.end) + desired_segment.end
)
# Returns the position of the first point on the path. # Returns the position of the first point on the path.
@ -127,7 +126,6 @@ class GSAISegment:
var length: float var length: float
var cumulative_length: float var cumulative_length: float
func _init(_begin: Vector3, _end: Vector3) -> void: func _init(_begin: Vector3, _end: Vector3) -> void:
self.begin = _begin self.begin = _begin
self.end = _end self.end = _end

View File

@ -5,7 +5,6 @@
extends GSAIAgentLocation extends GSAIAgentLocation
class_name GSAISteeringAgent class_name GSAISteeringAgent
# The amount of velocity to be considered effectively not moving. # The amount of velocity to be considered effectively not moving.
var zero_linear_speed_threshold := 0.01 var zero_linear_speed_threshold := 0.01
# The maximum speed at which the agent can move. # The maximum speed at which the agent can move.

View File

@ -7,7 +7,6 @@
# Individual steering behaviors encapsulate the steering logic. # Individual steering behaviors encapsulate the steering logic.
class_name GSAISteeringBehavior class_name GSAISteeringBehavior
# If `false`, all calculations return zero amounts of acceleration. # If `false`, all calculations return zero amounts of acceleration.
var is_enabled := true var is_enabled := true
# The AI agent on which the steering behavior bases its calculations. # The AI agent on which the steering behavior bases its calculations.

View File

@ -2,7 +2,6 @@
# system. # system.
class_name GSAITargetAcceleration class_name GSAITargetAcceleration
# Linear acceleration # Linear acceleration
var linear := Vector3.ZERO var linear := Vector3.ZERO
# Angular acceleration # Angular acceleration

View File

@ -1,7 +1,6 @@
# Math and vector utility functions. # Math and vector utility functions.
class_name GSAIUtils class_name GSAIUtils
# Returns the `vector` with its length capped to `limit`. # Returns the `vector` with its length capped to `limit`.
static func clampedv3(vector: Vector3, limit: float) -> Vector3: static func clampedv3(vector: Vector3, limit: float) -> Vector3:
var length_squared := vector.length_squared() var length_squared := vector.length_squared()
@ -10,7 +9,6 @@ static func clampedv3(vector: Vector3, limit: float) -> Vector3:
vector *= sqrt(limit_squared / length_squared) vector *= sqrt(limit_squared / length_squared)
return vector return vector
# Returns an angle in radians between the positive X axis and the `vector`. # Returns an angle in radians between the positive X axis and the `vector`.
# #
# This assumes orientation for 3D agents that are upright and rotate # This assumes orientation for 3D agents that are upright and rotate
@ -18,12 +16,10 @@ static func clampedv3(vector: Vector3, limit: float) -> Vector3:
static func vector3_to_angle(vector: Vector3) -> float: static func vector3_to_angle(vector: Vector3) -> float:
return atan2(vector.x, vector.z) return atan2(vector.x, vector.z)
# Returns an angle in radians between the positive X axis and the `vector`. # Returns an angle in radians between the positive X axis and the `vector`.
static func vector2_to_angle(vector: Vector2) -> float: static func vector2_to_angle(vector: Vector2) -> float:
return atan2(vector.x, -vector.y) return atan2(vector.x, -vector.y)
# Returns a directional vector from the given orientation angle. # Returns a directional vector from the given orientation angle.
# #
# This assumes orientation for 2D agents or 3D agents that are upright and # This assumes orientation for 2D agents or 3D agents that are upright and
@ -31,12 +27,10 @@ static func vector2_to_angle(vector: Vector2) -> float:
static func angle_to_vector2(angle: float) -> Vector2: static func angle_to_vector2(angle: float) -> Vector2:
return Vector2(sin(-angle), cos(angle)) return Vector2(sin(-angle), cos(angle))
# Returns a vector2 with `vector`'s x and y components. # Returns a vector2 with `vector`'s x and y components.
static func to_vector2(vector: Vector3) -> Vector2: static func to_vector2(vector: Vector3) -> Vector2:
return Vector2(vector.x, vector.y) return Vector2(vector.x, vector.y)
# Returns a vector3 with `vector`'s x and y components and 0 in z. # Returns a vector3 with `vector`'s x and y components and 0 in z.
static func to_vector3(vector: Vector2) -> Vector3: static func to_vector3(vector: Vector2) -> Vector3:
return Vector3(vector.x, vector.y, 0) return Vector3(vector.x, vector.y, 0)

View File

@ -2,7 +2,6 @@
extends Reference extends Reference
class_name GSAIProximity class_name GSAIProximity
# The owning agent whose neighbors are found in the group # The owning agent whose neighbors are found in the group
var agent: GSAISteeringAgent var agent: GSAISteeringAgent
# The agents who are part of this group and could be potential neighbors # The agents who are part of this group and could be potential neighbors

View File

@ -3,7 +3,6 @@
extends GSAIProximity extends GSAIProximity
class_name GSAIRadiusProximity class_name GSAIRadiusProximity
# The radius around the owning agent to find neighbors in # The radius around the owning agent to find neighbors in
var radius := 0.0 var radius := 0.0