diff --git a/project/demos/arrive/ArriveDemo.gd b/project/demos/arrive/ArriveDemo.gd index a60cc04..3df302c 100644 --- a/project/demos/arrive/ArriveDemo.gd +++ b/project/demos/arrive/ArriveDemo.gd @@ -1,37 +1,37 @@ extends Node2D -onready var _target: = $Target -onready var _arriver: = $Arriver -onready var _gui: = $GUI +onready var target: = $Target +onready var arriver: = $Arriver +onready var gui: = $GUI func _ready() -> void: - _gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_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_accel_changed", self, "_on_GUI_max_accel_changed") - _gui.max_speed.text = str(_arriver._agent.max_linear_speed) - _gui.max_accel.text = str(_arriver._agent.max_linear_acceleration) - _gui.arrival_tolerance.text = str(_arriver._arrive.arrival_tolerance) - _gui.deceleration_radius.text = str(_arriver._arrive.deceleration_radius) + gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_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_accel_changed", self, "_on_GUI_max_accel_changed") + gui.max_speed.text = str(arriver._agent.max_linear_speed) + gui.max_accel.text = str(arriver._agent.max_linear_acceleration) + gui.arrival_tolerance.text = str(arriver._arrive.arrival_tolerance) + gui.deceleration_radius.text = str(arriver._arrive.deceleration_radius) func draw(location: Vector2) -> void: - _target.draw(location) + target.draw(location) 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: - _arriver._arrive.deceleration_radius = value + arriver._arrive.deceleration_radius = value 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: - _arriver._agent.max_linear_acceleration = value + arriver._agent.max_linear_acceleration = value diff --git a/project/demos/arrive/Arrive.tscn b/project/demos/arrive/ArriveDemo.tscn similarity index 95% rename from project/demos/arrive/Arrive.tscn rename to project/demos/arrive/ArriveDemo.tscn index 144bd97..23a18f5 100644 --- a/project/demos/arrive/Arrive.tscn +++ b/project/demos/arrive/ArriveDemo.tscn @@ -8,7 +8,7 @@ [sub_resource type="CircleShape2D" id=1] radius = 15.0 -[node name="Arrive" type="Node2D"] +[node name="ArriveDemo" type="Node2D"] script = ExtResource( 2 ) [node name="Arriver" type="KinematicBody2D" parent="."] diff --git a/project/demos/arrive/Arriver.gd b/project/demos/arrive/Arriver.gd index b38fee7..f582263 100644 --- a/project/demos/arrive/Arriver.gd +++ b/project/demos/arrive/Arriver.gd @@ -1,18 +1,20 @@ extends KinematicBody2D -onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius +onready var collision_shape: = $CollisionShape2D -onready var _agent: = GSTSteeringAgent.new() -onready var _target: = GSTAgentLocation.new() -onready var _arrive: = GSTArrive.new(_agent, _target) -onready var _accel: = GSTTargetAcceleration.new() +var _radius: = 0.0 +var _agent: = GSTSteeringAgent.new() +var _target: = GSTAgentLocation.new() +var _arrive: = GSTArrive.new(_agent, _target) +var _accel: = GSTTargetAcceleration.new() var _velocity: = Vector2() var _drag: = 1.0 func _ready() -> void: + _radius = collision_shape.shape.radius _agent.max_linear_acceleration = 10 _agent.max_linear_speed = 200 _arrive.arrival_tolerance = 25 diff --git a/project/demos/face/FaceDemo.gd b/project/demos/face/FaceDemo.gd index c85ff14..df51ca2 100644 --- a/project/demos/face/FaceDemo.gd +++ b/project/demos/face/FaceDemo.gd @@ -2,33 +2,33 @@ extends Node2D onready var player: = $Player -onready var _gui: = $GUI -onready var _turret: = $Turret +onready var gui: = $GUI +onready var turret: = $Turret func _ready() -> void: - _gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_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_speed_changed", self, "_on_GUI_max_speed_changed") - _turret.setup() - _gui.align_tolerance.text = str(int(rad2deg(_turret._face.alignment_tolerance))) - _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_accel.text = str(int(rad2deg(_turret._agent.max_angular_acceleration))) + gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_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_speed_changed", self, "_on_GUI_max_speed_changed") + turret.setup() + gui.align_tolerance.text = str(int(rad2deg(turret._face.alignment_tolerance))) + 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_accel.text = str(int(rad2deg(turret._agent.max_angular_acceleration))) 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: - _turret._face.deceleration_radius = deg2rad(value) + turret._face.deceleration_radius = deg2rad(value) 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: - _turret._agent.max_angular_speed = deg2rad(value) + turret._agent.max_angular_speed = deg2rad(value) diff --git a/project/demos/face/Face.tscn b/project/demos/face/FaceDemo.tscn similarity index 95% rename from project/demos/face/Face.tscn rename to project/demos/face/FaceDemo.tscn index 42b34d0..8960aed 100644 --- a/project/demos/face/Face.tscn +++ b/project/demos/face/FaceDemo.tscn @@ -1,8 +1,8 @@ [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=2] -[ext_resource path="res://demos/face/FaceDemo.gd" type="Script" id=3] +[ext_resource path="res://demos/face/Turret.gd" type="Script" id=1] +[ext_resource path="res://demos/face/FaceDemo.gd" type="Script" id=2] +[ext_resource path="res://demos/face/Player.gd" type="Script" id=3] [ext_resource path="res://demos/face/GUI.tscn" type="PackedScene" id=4] [sub_resource type="CircleShape2D" id=1] @@ -11,22 +11,22 @@ radius = 20.0 [sub_resource type="CircleShape2D" id=2] radius = 30.0 -[node name="Face" type="Node2D"] -script = ExtResource( 3 ) +[node name="FaceDemo" type="Node2D"] +script = ExtResource( 2 ) __meta__ = { "_editor_description_": "A demo showing the usage of the Face steering behavior." } [node name="Player" type="KinematicBody2D" parent="."] position = Vector2( 512, 450 ) -script = ExtResource( 1 ) +script = ExtResource( 3 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="Player"] shape = SubResource( 1 ) [node name="Turret" type="KinematicBody2D" parent="."] position = Vector2( 512, 150 ) -script = ExtResource( 2 ) +script = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="Turret"] shape = SubResource( 2 ) diff --git a/project/demos/face/Player.gd b/project/demos/face/Player.gd index a453ebb..d5fa879 100644 --- a/project/demos/face/Player.gd +++ b/project/demos/face/Player.gd @@ -1,11 +1,17 @@ extends KinematicBody2D -onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius +onready var collision_shape: = $CollisionShape2D onready var agent: = GSTAgentLocation.new() export var speed: = 125.0 +var _radius: = 0.0 + + +func _ready() -> void: + _radius = collision_shape.shape.radius + func _draw() -> void: draw_circle(Vector2.ZERO, _radius, Color.red) diff --git a/project/demos/face/Turret.gd b/project/demos/face/Turret.gd index 2573e6d..9209af4 100644 --- a/project/demos/face/Turret.gd +++ b/project/demos/face/Turret.gd @@ -1,22 +1,29 @@ extends KinematicBody2D -onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius -onready var _cannon: = Rect2(Vector2(-5, 0), Vector2(10, -_radius*2)) -onready var _agent: = GSTSteeringAgent.new() -onready var _accel: = GSTTargetAcceleration.new() +onready var collision_shape: = $CollisionShape2D + +var _radius: = 0.0 +var _cannon: Rect2 + +var _agent: = GSTSteeringAgent.new() +var _accel: = GSTTargetAcceleration.new() var _angular_velocity: = 0.0 var _angular_drag: = 1.0 var _face: GSTFace +func _ready() -> void: + _radius = collision_shape.shape.radius + _cannon = Rect2(Vector2(-5, 0), Vector2(10, -_radius*2)) + + func _draw() -> void: draw_rect(_cannon, Color.blue) draw_circle(Vector2.ZERO, _radius, Color.teal) - func _physics_process(delta: float) -> void: if not _face: return diff --git a/project/demos/pursue_vs_seek/BoundaryManager.gd b/project/demos/pursue_vs_seek/BoundaryManager.gd index a78469e..ea5bb00 100644 --- a/project/demos/pursue_vs_seek/BoundaryManager.gd +++ b/project/demos/pursue_vs_seek/BoundaryManager.gd @@ -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 ships: = [$Player, $Pursuer, $Seeker] var _clones: = {} var _world_bounds: Vector2 @@ -17,8 +17,8 @@ func _ready() -> void: ProjectSettings["display/window/size/height"] ) - for i in range(_ships.size()): - var ship: Node2D = _ships[i] + for i in range(ships.size()): + var ship: Node2D = ships[i] var world_pos: = ship.position for i in range(3): diff --git a/project/demos/pursue_vs_seek/PursueVSSeekDemo.gd b/project/demos/pursue_vs_seek/PursueVSSeekDemo.gd index 5683ea3..5f1dbb1 100644 --- a/project/demos/pursue_vs_seek/PursueVSSeekDemo.gd +++ b/project/demos/pursue_vs_seek/PursueVSSeekDemo.gd @@ -1,23 +1,23 @@ extends Node2D -onready var _gui: = $GUI -onready var _pursuer: = $BoundaryManager/Pursuer -onready var _seeker: = $BoundaryManager/Seeker +onready var gui: = $GUI +onready var pursuer: = $BoundaryManager/Pursuer +onready var seeker: = $BoundaryManager/Seeker func _ready() -> void: - _gui.linear_speed.text = str(_pursuer.agent.max_linear_speed) - _gui.linear_accel.text = str(_pursuer.agent.max_linear_acceleration) - _gui.connect("linear_accel_changed", self, "_on_GUI_linear_accel_changed") - _gui.connect("linear_speed_changed", self, "_on_GUI_linear_speed_changed") + gui.linear_speed.text = str(pursuer.agent.max_linear_speed) + gui.linear_accel.text = str(pursuer.agent.max_linear_acceleration) + gui.connect("linear_accel_changed", self, "_on_GUI_linear_accel_changed") + gui.connect("linear_speed_changed", self, "_on_GUI_linear_speed_changed") func _on_GUI_linear_accel_changed(value: int) -> void: - _pursuer.agent.max_linear_acceleration = float(value) - _seeker.agent.max_linear_acceleration = float(value) + pursuer.agent.max_linear_acceleration = float(value) + seeker.agent.max_linear_acceleration = float(value) func _on_GUI_linear_speed_changed(value: int) -> void: - _pursuer.agent.max_linear_speed = float(value) - _seeker.agent.max_linear_speed = float(value) + pursuer.agent.max_linear_speed = float(value) + seeker.agent.max_linear_speed = float(value) diff --git a/project/demos/pursue_vs_seek/PursueVSSeek.tscn b/project/demos/pursue_vs_seek/PursueVSSeekDemo.tscn similarity index 89% rename from project/demos/pursue_vs_seek/PursueVSSeek.tscn rename to project/demos/pursue_vs_seek/PursueVSSeekDemo.tscn index 3545a89..2691452 100644 --- a/project/demos/pursue_vs_seek/PursueVSSeek.tscn +++ b/project/demos/pursue_vs_seek/PursueVSSeekDemo.tscn @@ -1,25 +1,25 @@ [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/BoundaryManager.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/GUI.tscn" type="PackedScene" id=4] -[ext_resource path="res://demos/pursue_vs_seek/PursueVSSeekDemo.gd" type="Script" id=5] +[ext_resource path="res://demos/pursue_vs_seek/Player.gd" type="Script" id=2] +[ext_resource path="res://demos/pursue_vs_seek/BoundaryManager.gd" type="Script" id=3] +[ext_resource path="res://demos/pursue_vs_seek/PursueVSSeekDemo.gd" type="Script" id=4] +[ext_resource path="res://demos/pursue_vs_seek/GUI.tscn" type="PackedScene" id=5] -[node name="PursueVSSeek" type="Node2D"] -script = ExtResource( 5 ) +[node name="PursueVSSeekDemo" type="Node2D"] +script = ExtResource( 4 ) __meta__ = { "_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="."] -script = ExtResource( 2 ) +script = ExtResource( 3 ) [node name="Player" type="KinematicBody2D" parent="BoundaryManager"] position = Vector2( 49.2031, 556.936 ) rotation = 1.5708 collision_mask = 2 -script = ExtResource( 3 ) +script = ExtResource( 2 ) color = Color( 1, 0, 0, 1 ) [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BoundaryManager/Player"] @@ -46,6 +46,6 @@ use_seek = true modulate = Color( 0.317647, 0.317647, 0.317647, 1 ) 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_bottom = 150.0 diff --git a/project/demos/seek_and_flee/Player.gd b/project/demos/seek_and_flee/Player.gd index 4be9822..bc943b6 100644 --- a/project/demos/seek_and_flee/Player.gd +++ b/project/demos/seek_and_flee/Player.gd @@ -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() export var speed: = 150.0 +var _radius: = 0.0 + + +func _ready() -> void: + _radius = collision_shape.shape.radius + func _draw() -> void: draw_circle(Vector2.ZERO, _radius, Color.red) diff --git a/project/demos/seek_and_flee/SeekFleeDemo.gd b/project/demos/seek_and_flee/SeekFleeDemo.gd index c1965f5..d5a26d8 100644 --- a/project/demos/seek_and_flee/SeekFleeDemo.gd +++ b/project/demos/seek_and_flee/SeekFleeDemo.gd @@ -6,7 +6,7 @@ Access helper class for children to access window boundaries. onready var player: KinematicBody2D = $Player onready var spawner: Node2D = $Spawner -onready var _gui: = $GUI +onready var gui: = $GUI var camera_boundaries: Rect2 @@ -35,5 +35,5 @@ func _ready() -> void: entity.player_agent = player.agent entity.speed = rng.randf_range(spawner.min_speed, spawner.max_speed) 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) diff --git a/project/demos/seek_and_flee/SeekFlee.tscn b/project/demos/seek_and_flee/SeekFleeDemo.tscn similarity index 89% rename from project/demos/seek_and_flee/SeekFlee.tscn rename to project/demos/seek_and_flee/SeekFleeDemo.tscn index dce36b1..cb19392 100644 --- a/project/demos/seek_and_flee/SeekFlee.tscn +++ b/project/demos/seek_and_flee/SeekFleeDemo.tscn @@ -1,11 +1,11 @@ [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/Spawner.gd" type="Script" id=2] +[ext_resource path="res://demos/seek_and_flee/Boundary.gd" type="Script" id=1] +[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/Seeker.tscn" type="PackedScene" 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=7] +[ext_resource path="res://demos/seek_and_flee/Spawner.gd" type="Script" id=4] +[ext_resource path="res://demos/seek_and_flee/GUI.tscn" type="PackedScene" id=5] +[ext_resource path="res://demos/seek_and_flee/Seeker.tscn" type="PackedScene" id=6] [sub_resource type="CircleShape2D" id=1] radius = 30.0 @@ -27,7 +27,7 @@ current = true [node name="Player" type="KinematicBody2D" parent="."] collision_mask = 2 -script = ExtResource( 1 ) +script = ExtResource( 2 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="Player"] shape = SubResource( 1 ) @@ -36,7 +36,7 @@ shape = SubResource( 1 ) position = Vector2( -512, 0 ) collision_layer = 2 collision_mask = 5 -script = ExtResource( 5 ) +script = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="LeftBoundary"] shape = SubResource( 2 ) @@ -45,7 +45,7 @@ shape = SubResource( 2 ) position = Vector2( 512, 0 ) collision_layer = 2 collision_mask = 5 -script = ExtResource( 5 ) +script = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="RightBoundary"] shape = SubResource( 2 ) @@ -54,7 +54,7 @@ shape = SubResource( 2 ) position = Vector2( 0, -300 ) collision_layer = 2 collision_mask = 5 -script = ExtResource( 5 ) +script = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="TopBoundary"] shape = SubResource( 3 ) @@ -63,16 +63,16 @@ shape = SubResource( 3 ) position = Vector2( 0, 300 ) collision_layer = 2 collision_mask = 5 -script = ExtResource( 5 ) +script = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="BottomBoundary"] shape = SubResource( 3 ) [node name="Spawner" type="Node2D" parent="."] -script = ExtResource( 2 ) -Entity = ExtResource( 4 ) +script = ExtResource( 4 ) +Entity = ExtResource( 6 ) -[node name="GUI" parent="." instance=ExtResource( 7 )] +[node name="GUI" parent="." instance=ExtResource( 5 )] margin_left = -512.0 margin_top = -300.0 margin_right = -414.0 diff --git a/project/demos/seek_and_flee/Seeker.gd b/project/demos/seek_and_flee/Seeker.gd index 8c8b2a0..3dcc860 100644 --- a/project/demos/seek_and_flee/Seeker.gd +++ b/project/demos/seek_and_flee/Seeker.gd @@ -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 accel: = GSTTargetAcceleration.new() @@ -16,11 +16,13 @@ var player_agent: GSTAgentLocation var velocity: = Vector2.ZERO var speed: float var color: Color +var radius: = 0.0 func _ready() -> void: agent.max_linear_acceleration = speed/10 agent.max_linear_speed = speed + radius = collision_shape.shape.radius func _draw() -> void: diff --git a/project/src/GSTTargetAcceleration.gd b/project/src/GSTTargetAcceleration.gd index 180f7f7..8445262 100644 --- a/project/src/GSTTargetAcceleration.gd +++ b/project/src/GSTTargetAcceleration.gd @@ -20,9 +20,9 @@ func add_scaled_accel(accel: GSTTargetAcceleration, scalar: float) -> void: angular += accel.angular * scalar -func get_squared_magnitude() -> float: +func get_magnitude_squared() -> float: return linear.length_squared() + angular * angular func get_magnitude() -> float: - return sqrt(get_squared_magnitude()) + return sqrt(get_magnitude_squared()) diff --git a/project/src/Utils.gd b/project/src/Utils.gd index fa2a4e5..bed6dd3 100644 --- a/project/src/Utils.gd +++ b/project/src/Utils.gd @@ -5,8 +5,8 @@ Useful math and utility functions to complement Godot's own. static func clampedv3(vector: Vector3, limit: float) -> Vector3: - var len2: = vector.length_squared() - var limit2: = limit * limit - if len2 > limit2: - vector *= sqrt(limit2 / len2) + var length_squared: = vector.length_squared() + var limit_squared: = limit * limit + if length_squared > limit_squared: + vector *= sqrt(limit_squared / length_squared) return vector diff --git a/project/src/behaviors/GSTBlend.gd b/project/src/behaviors/GSTBlend.gd index de1783d..1705553 100644 --- a/project/src/behaviors/GSTBlend.gd +++ b/project/src/behaviors/GSTBlend.gd @@ -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, 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: 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: return _behaviors[index] printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size())) - return null + return {} func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAcceleration: blended_accel.set_zero() for i in range(_behaviors.size()): - var bw: GSTBehaviorAndWeight = _behaviors[i] + var bw: Dictionary = _behaviors[i] bw.behavior.calculate_steering(_accel) blended_accel.add_scaled_accel(_accel, bw.weight) blended_accel.linear = Utils.clampedv3(blended_accel.linear, agent.max_linear_acceleration) - if blended_accel.angular > agent.max_angular_acceleration: - blended_accel.angular = agent.max_angular_acceleration + blended_accel.angular = min(blended_accel.angular, agent.max_angular_acceleration) return blended_accel - - -class GSTBehaviorAndWeight: - var behavior: GSTSteeringBehavior - var weight: float - - func _init(behavior: GSTSteeringBehavior, weight: float) -> void: - self.behavior = behavior - self.weight = weight diff --git a/project/src/behaviors/GSTMatchOrientation.gd b/project/src/behaviors/GSTMatchOrientation.gd index bf653cf..cce5055 100644 --- a/project/src/behaviors/GSTMatchOrientation.gd +++ b/project/src/behaviors/GSTMatchOrientation.gd @@ -19,7 +19,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void: func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation: float) -> GSTTargetAcceleration: 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: acceleration.set_zero() @@ -33,7 +33,7 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation 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: acceleration.angular *= agent.max_angular_acceleration / limited_acceleration