Implement review suggestions to code

Razvan reviewed the code and suggested a number of changes to improve
readability and make facets of the code more explicit and consistent.
This commit is contained in:
Francois Belair 2019-12-23 11:38:27 -05:00
parent 35d9b2e1a6
commit 9f945cbf85
18 changed files with 129 additions and 113 deletions

View File

@ -1,37 +1,37 @@
extends Node2D extends Node2D
onready var _target: = $Target onready var target: = $Target
onready var _arriver: = $Arriver onready var arriver: = $Arriver
onready var _gui: = $GUI onready var gui: = $GUI
func _ready() -> void: func _ready() -> void:
_gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_changed") gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_changed")
_gui.connect("decel_radius_changed", self, "_on_GUI_decel_radius_changed") gui.connect("decel_radius_changed", self, "_on_GUI_decel_radius_changed")
_gui.connect("max_speed_changed", self, "_on_GUI_max_speed_changed") gui.connect("max_speed_changed", self, "_on_GUI_max_speed_changed")
_gui.connect("max_accel_changed", self, "_on_GUI_max_accel_changed") gui.connect("max_accel_changed", self, "_on_GUI_max_accel_changed")
_gui.max_speed.text = str(_arriver._agent.max_linear_speed) gui.max_speed.text = str(arriver._agent.max_linear_speed)
_gui.max_accel.text = str(_arriver._agent.max_linear_acceleration) gui.max_accel.text = str(arriver._agent.max_linear_acceleration)
_gui.arrival_tolerance.text = str(_arriver._arrive.arrival_tolerance) gui.arrival_tolerance.text = str(arriver._arrive.arrival_tolerance)
_gui.deceleration_radius.text = str(_arriver._arrive.deceleration_radius) gui.deceleration_radius.text = str(arriver._arrive.deceleration_radius)
func draw(location: Vector2) -> void: func draw(location: Vector2) -> void:
_target.draw(location) target.draw(location)
func _on_GUI_align_tolerance_changed(value: int) -> void: func _on_GUI_align_tolerance_changed(value: int) -> void:
_arriver._arrive.arrival_tolerance = value arriver._arrive.arrival_tolerance = value
func _on_GUI_decel_radius_changed(value: int) -> void: func _on_GUI_decel_radius_changed(value: int) -> void:
_arriver._arrive.deceleration_radius = value arriver._arrive.deceleration_radius = value
func _on_GUI_max_speed_changed(value: int) -> void: func _on_GUI_max_speed_changed(value: int) -> void:
_arriver._agent.max_linear_speed = value arriver._agent.max_linear_speed = value
func _on_GUI_max_accel_changed(value: int) -> void: func _on_GUI_max_accel_changed(value: int) -> void:
_arriver._agent.max_linear_acceleration = value arriver._agent.max_linear_acceleration = value

View File

@ -8,7 +8,7 @@
[sub_resource type="CircleShape2D" id=1] [sub_resource type="CircleShape2D" id=1]
radius = 15.0 radius = 15.0
[node name="Arrive" type="Node2D"] [node name="ArriveDemo" type="Node2D"]
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="Arriver" type="KinematicBody2D" parent="."] [node name="Arriver" type="KinematicBody2D" parent="."]

View File

@ -1,18 +1,20 @@
extends KinematicBody2D extends KinematicBody2D
onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius onready var collision_shape: = $CollisionShape2D
onready var _agent: = GSTSteeringAgent.new() var _radius: = 0.0
onready var _target: = GSTAgentLocation.new() var _agent: = GSTSteeringAgent.new()
onready var _arrive: = GSTArrive.new(_agent, _target) var _target: = GSTAgentLocation.new()
onready var _accel: = GSTTargetAcceleration.new() var _arrive: = GSTArrive.new(_agent, _target)
var _accel: = GSTTargetAcceleration.new()
var _velocity: = Vector2() var _velocity: = Vector2()
var _drag: = 1.0 var _drag: = 1.0
func _ready() -> void: func _ready() -> void:
_radius = collision_shape.shape.radius
_agent.max_linear_acceleration = 10 _agent.max_linear_acceleration = 10
_agent.max_linear_speed = 200 _agent.max_linear_speed = 200
_arrive.arrival_tolerance = 25 _arrive.arrival_tolerance = 25

View File

@ -2,33 +2,33 @@ extends Node2D
onready var player: = $Player onready var player: = $Player
onready var _gui: = $GUI onready var gui: = $GUI
onready var _turret: = $Turret onready var turret: = $Turret
func _ready() -> void: func _ready() -> void:
_gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_changed") gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_changed")
_gui.connect("decel_radius_changed", self, "_on_GUI_decel_radius_changed") gui.connect("decel_radius_changed", self, "_on_GUI_decel_radius_changed")
_gui.connect("max_accel_changed", self, "_on_GUI_max_accel_changed") gui.connect("max_accel_changed", self, "_on_GUI_max_accel_changed")
_gui.connect("max_speed_changed", self, "_on_GUI_max_speed_changed") gui.connect("max_speed_changed", self, "_on_GUI_max_speed_changed")
_turret.setup() turret.setup()
_gui.align_tolerance.text = str(int(rad2deg(_turret._face.alignment_tolerance))) gui.align_tolerance.text = str(int(rad2deg(turret._face.alignment_tolerance)))
_gui.decel_radius.text = str(int(rad2deg(_turret._face.deceleration_radius))) gui.decel_radius.text = str(int(rad2deg(turret._face.deceleration_radius)))
_gui.max_speed.text = str(int(rad2deg(_turret._agent.max_angular_speed))) gui.max_speed.text = str(int(rad2deg(turret._agent.max_angular_speed)))
_gui.max_accel.text = str(int(rad2deg(_turret._agent.max_angular_acceleration))) gui.max_accel.text = str(int(rad2deg(turret._agent.max_angular_acceleration)))
func _on_GUI_align_tolerance_changed(value: int) -> void: func _on_GUI_align_tolerance_changed(value: int) -> void:
_turret._face.alignment_tolerance = deg2rad(value) turret._face.alignment_tolerance = deg2rad(value)
func _on_GUI_decel_radius_changed(value: int) -> void: func _on_GUI_decel_radius_changed(value: int) -> void:
_turret._face.deceleration_radius = deg2rad(value) turret._face.deceleration_radius = deg2rad(value)
func _on_GUI_max_accel_changed(value: int) -> void: func _on_GUI_max_accel_changed(value: int) -> void:
_turret._agent.max_angular_acceleration = deg2rad(value) turret._agent.max_angular_acceleration = deg2rad(value)
func _on_GUI_max_speed_changed(value: int) -> void: func _on_GUI_max_speed_changed(value: int) -> void:
_turret._agent.max_angular_speed = deg2rad(value) turret._agent.max_angular_speed = deg2rad(value)

View File

@ -1,8 +1,8 @@
[gd_scene load_steps=7 format=2] [gd_scene load_steps=7 format=2]
[ext_resource path="res://demos/face/Player.gd" type="Script" id=1] [ext_resource path="res://demos/face/Turret.gd" type="Script" id=1]
[ext_resource path="res://demos/face/Turret.gd" type="Script" id=2] [ext_resource path="res://demos/face/FaceDemo.gd" type="Script" id=2]
[ext_resource path="res://demos/face/FaceDemo.gd" type="Script" id=3] [ext_resource path="res://demos/face/Player.gd" type="Script" id=3]
[ext_resource path="res://demos/face/GUI.tscn" type="PackedScene" id=4] [ext_resource path="res://demos/face/GUI.tscn" type="PackedScene" id=4]
[sub_resource type="CircleShape2D" id=1] [sub_resource type="CircleShape2D" id=1]
@ -11,22 +11,22 @@ radius = 20.0
[sub_resource type="CircleShape2D" id=2] [sub_resource type="CircleShape2D" id=2]
radius = 30.0 radius = 30.0
[node name="Face" type="Node2D"] [node name="FaceDemo" type="Node2D"]
script = ExtResource( 3 ) script = ExtResource( 2 )
__meta__ = { __meta__ = {
"_editor_description_": "A demo showing the usage of the Face steering behavior." "_editor_description_": "A demo showing the usage of the Face steering behavior."
} }
[node name="Player" type="KinematicBody2D" parent="."] [node name="Player" type="KinematicBody2D" parent="."]
position = Vector2( 512, 450 ) position = Vector2( 512, 450 )
script = ExtResource( 1 ) script = ExtResource( 3 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Player"]
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="Turret" type="KinematicBody2D" parent="."] [node name="Turret" type="KinematicBody2D" parent="."]
position = Vector2( 512, 150 ) position = Vector2( 512, 150 )
script = ExtResource( 2 ) script = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Turret"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Turret"]
shape = SubResource( 2 ) shape = SubResource( 2 )

View File

@ -1,11 +1,17 @@
extends KinematicBody2D extends KinematicBody2D
onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius onready var collision_shape: = $CollisionShape2D
onready var agent: = GSTAgentLocation.new() onready var agent: = GSTAgentLocation.new()
export var speed: = 125.0 export var speed: = 125.0
var _radius: = 0.0
func _ready() -> void:
_radius = collision_shape.shape.radius
func _draw() -> void: func _draw() -> void:
draw_circle(Vector2.ZERO, _radius, Color.red) draw_circle(Vector2.ZERO, _radius, Color.red)

View File

@ -1,22 +1,29 @@
extends KinematicBody2D extends KinematicBody2D
onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius onready var collision_shape: = $CollisionShape2D
onready var _cannon: = Rect2(Vector2(-5, 0), Vector2(10, -_radius*2))
onready var _agent: = GSTSteeringAgent.new() var _radius: = 0.0
onready var _accel: = GSTTargetAcceleration.new() var _cannon: Rect2
var _agent: = GSTSteeringAgent.new()
var _accel: = GSTTargetAcceleration.new()
var _angular_velocity: = 0.0 var _angular_velocity: = 0.0
var _angular_drag: = 1.0 var _angular_drag: = 1.0
var _face: GSTFace var _face: GSTFace
func _ready() -> void:
_radius = collision_shape.shape.radius
_cannon = Rect2(Vector2(-5, 0), Vector2(10, -_radius*2))
func _draw() -> void: func _draw() -> void:
draw_rect(_cannon, Color.blue) draw_rect(_cannon, Color.blue)
draw_circle(Vector2.ZERO, _radius, Color.teal) draw_circle(Vector2.ZERO, _radius, Color.teal)
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
if not _face: if not _face:
return return

View File

@ -4,8 +4,8 @@ Wraps the ships' positions around the world border, and controls their rendering
""" """
onready var _ships: = [$Player, $Pursuer, $Seeker]
onready var ShipType: = preload("res://demos/pursue_vs_seek/Ship.gd") onready var ShipType: = preload("res://demos/pursue_vs_seek/Ship.gd")
onready var ships: = [$Player, $Pursuer, $Seeker]
var _clones: = {} var _clones: = {}
var _world_bounds: Vector2 var _world_bounds: Vector2
@ -17,8 +17,8 @@ func _ready() -> void:
ProjectSettings["display/window/size/height"] ProjectSettings["display/window/size/height"]
) )
for i in range(_ships.size()): for i in range(ships.size()):
var ship: Node2D = _ships[i] var ship: Node2D = ships[i]
var world_pos: = ship.position var world_pos: = ship.position
for i in range(3): for i in range(3):

View File

@ -1,23 +1,23 @@
extends Node2D extends Node2D
onready var _gui: = $GUI onready var gui: = $GUI
onready var _pursuer: = $BoundaryManager/Pursuer onready var pursuer: = $BoundaryManager/Pursuer
onready var _seeker: = $BoundaryManager/Seeker onready var seeker: = $BoundaryManager/Seeker
func _ready() -> void: func _ready() -> void:
_gui.linear_speed.text = str(_pursuer.agent.max_linear_speed) gui.linear_speed.text = str(pursuer.agent.max_linear_speed)
_gui.linear_accel.text = str(_pursuer.agent.max_linear_acceleration) gui.linear_accel.text = str(pursuer.agent.max_linear_acceleration)
_gui.connect("linear_accel_changed", self, "_on_GUI_linear_accel_changed") gui.connect("linear_accel_changed", self, "_on_GUI_linear_accel_changed")
_gui.connect("linear_speed_changed", self, "_on_GUI_linear_speed_changed") gui.connect("linear_speed_changed", self, "_on_GUI_linear_speed_changed")
func _on_GUI_linear_accel_changed(value: int) -> void: func _on_GUI_linear_accel_changed(value: int) -> void:
_pursuer.agent.max_linear_acceleration = float(value) pursuer.agent.max_linear_acceleration = float(value)
_seeker.agent.max_linear_acceleration = float(value) seeker.agent.max_linear_acceleration = float(value)
func _on_GUI_linear_speed_changed(value: int) -> void: func _on_GUI_linear_speed_changed(value: int) -> void:
_pursuer.agent.max_linear_speed = float(value) pursuer.agent.max_linear_speed = float(value)
_seeker.agent.max_linear_speed = float(value) seeker.agent.max_linear_speed = float(value)

View File

@ -1,25 +1,25 @@
[gd_scene load_steps=6 format=2] [gd_scene load_steps=6 format=2]
[ext_resource path="res://demos/pursue_vs_seek/Pursuer.gd" type="Script" id=1] [ext_resource path="res://demos/pursue_vs_seek/Pursuer.gd" type="Script" id=1]
[ext_resource path="res://demos/pursue_vs_seek/BoundaryManager.gd" type="Script" id=2] [ext_resource path="res://demos/pursue_vs_seek/Player.gd" type="Script" id=2]
[ext_resource path="res://demos/pursue_vs_seek/Player.gd" type="Script" id=3] [ext_resource path="res://demos/pursue_vs_seek/BoundaryManager.gd" type="Script" id=3]
[ext_resource path="res://demos/pursue_vs_seek/GUI.tscn" type="PackedScene" id=4] [ext_resource path="res://demos/pursue_vs_seek/PursueVSSeekDemo.gd" type="Script" id=4]
[ext_resource path="res://demos/pursue_vs_seek/PursueVSSeekDemo.gd" type="Script" id=5] [ext_resource path="res://demos/pursue_vs_seek/GUI.tscn" type="PackedScene" id=5]
[node name="PursueVSSeek" type="Node2D"] [node name="PursueVSSeekDemo" type="Node2D"]
script = ExtResource( 5 ) script = ExtResource( 4 )
__meta__ = { __meta__ = {
"_editor_description_": "Toy demo to demonstrate the use of the Pursue contrasted to the more naive Seek steering behavior." "_editor_description_": "Toy demo to demonstrate the use of the Pursue contrasted to the more naive Seek steering behavior."
} }
[node name="BoundaryManager" type="Node2D" parent="."] [node name="BoundaryManager" type="Node2D" parent="."]
script = ExtResource( 2 ) script = ExtResource( 3 )
[node name="Player" type="KinematicBody2D" parent="BoundaryManager"] [node name="Player" type="KinematicBody2D" parent="BoundaryManager"]
position = Vector2( 49.2031, 556.936 ) position = Vector2( 49.2031, 556.936 )
rotation = 1.5708 rotation = 1.5708
collision_mask = 2 collision_mask = 2
script = ExtResource( 3 ) script = ExtResource( 2 )
color = Color( 1, 0, 0, 1 ) color = Color( 1, 0, 0, 1 )
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BoundaryManager/Player"] [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BoundaryManager/Player"]
@ -46,6 +46,6 @@ use_seek = true
modulate = Color( 0.317647, 0.317647, 0.317647, 1 ) modulate = Color( 0.317647, 0.317647, 0.317647, 1 )
polygon = PoolVector2Array( 0, -30, -25, 25, 25, 25 ) polygon = PoolVector2Array( 0, -30, -25, 25, 25, 25 )
[node name="GUI" parent="." instance=ExtResource( 4 )] [node name="GUI" parent="." instance=ExtResource( 5 )]
margin_right = 261.0 margin_right = 261.0
margin_bottom = 150.0 margin_bottom = 150.0

View File

@ -4,11 +4,17 @@ Class to control the player in basic left/right up/down movement.
""" """
onready var _radius: = ($CollisionShape2D.shape as CircleShape2D).radius onready var collision_shape: = $CollisionShape2D
onready var agent: = GSTAgentLocation.new() onready var agent: = GSTAgentLocation.new()
export var speed: = 150.0 export var speed: = 150.0
var _radius: = 0.0
func _ready() -> void:
_radius = collision_shape.shape.radius
func _draw() -> void: func _draw() -> void:
draw_circle(Vector2.ZERO, _radius, Color.red) draw_circle(Vector2.ZERO, _radius, Color.red)

View File

@ -6,7 +6,7 @@ Access helper class for children to access window boundaries.
onready var player: KinematicBody2D = $Player onready var player: KinematicBody2D = $Player
onready var spawner: Node2D = $Spawner onready var spawner: Node2D = $Spawner
onready var _gui: = $GUI onready var gui: = $GUI
var camera_boundaries: Rect2 var camera_boundaries: Rect2
@ -35,5 +35,5 @@ func _ready() -> void:
entity.player_agent = player.agent entity.player_agent = player.agent
entity.speed = rng.randf_range(spawner.min_speed, spawner.max_speed) entity.speed = rng.randf_range(spawner.min_speed, spawner.max_speed)
entity.color = spawner.entity_color entity.color = spawner.entity_color
_gui.connect("mode_changed", entity, "_on_GUI_mode_changed") gui.connect("mode_changed", entity, "_on_GUI_mode_changed")
spawner.add_child(entity) spawner.add_child(entity)

View File

@ -1,11 +1,11 @@
[gd_scene load_steps=10 format=2] [gd_scene load_steps=10 format=2]
[ext_resource path="res://demos/seek_and_flee/Player.gd" type="Script" id=1] [ext_resource path="res://demos/seek_and_flee/Boundary.gd" type="Script" id=1]
[ext_resource path="res://demos/seek_and_flee/Spawner.gd" type="Script" id=2] [ext_resource path="res://demos/seek_and_flee/Player.gd" type="Script" id=2]
[ext_resource path="res://demos/seek_and_flee/SeekFleeDemo.gd" type="Script" id=3] [ext_resource path="res://demos/seek_and_flee/SeekFleeDemo.gd" type="Script" id=3]
[ext_resource path="res://demos/seek_and_flee/Seeker.tscn" type="PackedScene" id=4] [ext_resource path="res://demos/seek_and_flee/Spawner.gd" type="Script" id=4]
[ext_resource path="res://demos/seek_and_flee/Boundary.gd" type="Script" id=5] [ext_resource path="res://demos/seek_and_flee/GUI.tscn" type="PackedScene" id=5]
[ext_resource path="res://demos/seek_and_flee/GUI.tscn" type="PackedScene" id=7] [ext_resource path="res://demos/seek_and_flee/Seeker.tscn" type="PackedScene" id=6]
[sub_resource type="CircleShape2D" id=1] [sub_resource type="CircleShape2D" id=1]
radius = 30.0 radius = 30.0
@ -27,7 +27,7 @@ current = true
[node name="Player" type="KinematicBody2D" parent="."] [node name="Player" type="KinematicBody2D" parent="."]
collision_mask = 2 collision_mask = 2
script = ExtResource( 1 ) script = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Player"]
shape = SubResource( 1 ) shape = SubResource( 1 )
@ -36,7 +36,7 @@ shape = SubResource( 1 )
position = Vector2( -512, 0 ) position = Vector2( -512, 0 )
collision_layer = 2 collision_layer = 2
collision_mask = 5 collision_mask = 5
script = ExtResource( 5 ) script = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="LeftBoundary"] [node name="CollisionShape2D" type="CollisionShape2D" parent="LeftBoundary"]
shape = SubResource( 2 ) shape = SubResource( 2 )
@ -45,7 +45,7 @@ shape = SubResource( 2 )
position = Vector2( 512, 0 ) position = Vector2( 512, 0 )
collision_layer = 2 collision_layer = 2
collision_mask = 5 collision_mask = 5
script = ExtResource( 5 ) script = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="RightBoundary"] [node name="CollisionShape2D" type="CollisionShape2D" parent="RightBoundary"]
shape = SubResource( 2 ) shape = SubResource( 2 )
@ -54,7 +54,7 @@ shape = SubResource( 2 )
position = Vector2( 0, -300 ) position = Vector2( 0, -300 )
collision_layer = 2 collision_layer = 2
collision_mask = 5 collision_mask = 5
script = ExtResource( 5 ) script = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="TopBoundary"] [node name="CollisionShape2D" type="CollisionShape2D" parent="TopBoundary"]
shape = SubResource( 3 ) shape = SubResource( 3 )
@ -63,16 +63,16 @@ shape = SubResource( 3 )
position = Vector2( 0, 300 ) position = Vector2( 0, 300 )
collision_layer = 2 collision_layer = 2
collision_mask = 5 collision_mask = 5
script = ExtResource( 5 ) script = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="BottomBoundary"] [node name="CollisionShape2D" type="CollisionShape2D" parent="BottomBoundary"]
shape = SubResource( 3 ) shape = SubResource( 3 )
[node name="Spawner" type="Node2D" parent="."] [node name="Spawner" type="Node2D" parent="."]
script = ExtResource( 2 ) script = ExtResource( 4 )
Entity = ExtResource( 4 ) Entity = ExtResource( 6 )
[node name="GUI" parent="." instance=ExtResource( 7 )] [node name="GUI" parent="." instance=ExtResource( 5 )]
margin_left = -512.0 margin_left = -512.0
margin_top = -300.0 margin_top = -300.0
margin_right = -414.0 margin_right = -414.0

View File

@ -4,7 +4,7 @@ AI agent that uses the Seek behavior to hone in on the player's location as dire
""" """
onready var radius: = ($CollisionShape2D.shape as CircleShape2D).radius onready var collision_shape: = $CollisionShape2D
onready var agent: = GSTSteeringAgent.new() onready var agent: = GSTSteeringAgent.new()
onready var accel: = GSTTargetAcceleration.new() onready var accel: = GSTTargetAcceleration.new()
@ -16,11 +16,13 @@ var player_agent: GSTAgentLocation
var velocity: = Vector2.ZERO var velocity: = Vector2.ZERO
var speed: float var speed: float
var color: Color var color: Color
var radius: = 0.0
func _ready() -> void: func _ready() -> void:
agent.max_linear_acceleration = speed/10 agent.max_linear_acceleration = speed/10
agent.max_linear_speed = speed agent.max_linear_speed = speed
radius = collision_shape.shape.radius
func _draw() -> void: func _draw() -> void:

View File

@ -20,9 +20,9 @@ func add_scaled_accel(accel: GSTTargetAcceleration, scalar: float) -> void:
angular += accel.angular * scalar angular += accel.angular * scalar
func get_squared_magnitude() -> float: func get_magnitude_squared() -> float:
return linear.length_squared() + angular * angular return linear.length_squared() + angular * angular
func get_magnitude() -> float: func get_magnitude() -> float:
return sqrt(get_squared_magnitude()) return sqrt(get_magnitude_squared())

View File

@ -5,8 +5,8 @@ Useful math and utility functions to complement Godot's own.
static func clampedv3(vector: Vector3, limit: float) -> Vector3: static func clampedv3(vector: Vector3, limit: float) -> Vector3:
var len2: = vector.length_squared() var length_squared: = vector.length_squared()
var limit2: = limit * limit var limit_squared: = limit * limit
if len2 > limit2: if length_squared > limit_squared:
vector *= sqrt(limit2 / len2) vector *= sqrt(limit_squared / length_squared)
return vector return vector

View File

@ -5,6 +5,9 @@ Blends multiple steering behaviors into one, and returns acceleration combining
Each behavior is associated with a weight - a modifier by which the result will be multiplied by, Each behavior is associated with a weight - a modifier by which the result will be multiplied by,
then added to a total acceleration. then added to a total acceleration.
Each behavior is stored internally as a `Dictionary` with a `behavior` key with a value of type
`GSTSteeringBehavior` and a `weight` key with a value of type float.
""" """
@ -18,36 +21,26 @@ func _init(agent: GSTSteeringAgent).(agent) -> void:
func add(behavior: GSTSteeringBehavior, weight: float) -> void: func add(behavior: GSTSteeringBehavior, weight: float) -> void:
behavior.agent = agent behavior.agent = agent
_behaviors.append(GSTBehaviorAndWeight.new(behavior, weight)) _behaviors.append({behavior = behavior, weight = weight})
func get_behavior_at(index: int) -> GSTBehaviorAndWeight: func get_behavior_at(index: int) -> Dictionary:
if _behaviors.size() > index: if _behaviors.size() > index:
return _behaviors[index] return _behaviors[index]
printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size())) printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size()))
return null return {}
func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAcceleration: func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
blended_accel.set_zero() blended_accel.set_zero()
for i in range(_behaviors.size()): for i in range(_behaviors.size()):
var bw: GSTBehaviorAndWeight = _behaviors[i] var bw: Dictionary = _behaviors[i]
bw.behavior.calculate_steering(_accel) bw.behavior.calculate_steering(_accel)
blended_accel.add_scaled_accel(_accel, bw.weight) blended_accel.add_scaled_accel(_accel, bw.weight)
blended_accel.linear = Utils.clampedv3(blended_accel.linear, agent.max_linear_acceleration) blended_accel.linear = Utils.clampedv3(blended_accel.linear, agent.max_linear_acceleration)
if blended_accel.angular > agent.max_angular_acceleration: blended_accel.angular = min(blended_accel.angular, agent.max_angular_acceleration)
blended_accel.angular = agent.max_angular_acceleration
return blended_accel return blended_accel
class GSTBehaviorAndWeight:
var behavior: GSTSteeringBehavior
var weight: float
func _init(behavior: GSTSteeringBehavior, weight: float) -> void:
self.behavior = behavior
self.weight = weight

View File

@ -19,7 +19,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void:
func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation: float) -> GSTTargetAcceleration: func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation: float) -> GSTTargetAcceleration:
var rotation: = wrapf(desired_orientation - agent.orientation, -PI, PI) var rotation: = wrapf(desired_orientation - agent.orientation, -PI, PI)
var rotation_size: = -rotation if rotation < 0 else rotation var rotation_size: = abs(rotation)
if rotation_size <= alignment_tolerance: if rotation_size <= alignment_tolerance:
acceleration.set_zero() acceleration.set_zero()
@ -33,7 +33,7 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation
acceleration.angular = (desired_rotation - agent.angular_velocity) / time_to_reach acceleration.angular = (desired_rotation - agent.angular_velocity) / time_to_reach
var limited_acceleration: = -acceleration.angular if acceleration.angular < 0 else acceleration.angular var limited_acceleration: = abs(acceleration.angular)
if limited_acceleration > agent.max_angular_acceleration: if limited_acceleration > agent.max_angular_acceleration:
acceleration.angular *= agent.max_angular_acceleration / limited_acceleration acceleration.angular *= agent.max_angular_acceleration / limited_acceleration