From 8228694713567224f22d8f79bc6399d038ca7fb2 Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Wed, 22 Jan 2020 11:55:49 -0500 Subject: [PATCH] Refactor var names for consistency aka min/max --- project/demos/Arrive/ArriveDemo.gd | 20 ++++----- project/demos/Arrive/Arriver.gd | 10 ++--- .../AvoidCollisions/AvoidCollisionsDemo.gd | 16 +++---- .../AvoidCollisions/AvoidCollisionsDemo.tscn | 1 - project/demos/AvoidCollisions/Avoider.gd | 42 +++++++++---------- project/demos/AvoidCollisions/Spawner.gd | 12 +++--- project/demos/Face/FaceDemo.gd | 28 ++++++------- project/demos/Face/Turret.gd | 14 ++++--- project/demos/FollowPath/FollowPathDemo.gd | 20 ++++----- project/demos/FollowPath/PathFollower.gd | 20 ++++----- .../GroupBehaviors/GroupBehaviorsDemo.gd | 20 ++++----- project/demos/GroupBehaviors/Member.gd | 10 ++--- project/demos/GroupBehaviors/Spawner.gd | 16 +++---- project/demos/PursueSeek/Player.gd | 22 +++++----- project/demos/PursueSeek/PursueVSSeekDemo.gd | 26 ++++++------ .../demos/PursueSeek/PursueVSSeekDemo.tscn | 4 +- project/demos/PursueSeek/Pursuer.gd | 24 +++++------ project/demos/SeekFlee/SeekFleeDemo.gd | 20 ++++----- project/demos/SeekFlee/Seeker.gd | 16 +++---- project/src/Behaviors/GSTArrive.gd | 4 +- project/src/Behaviors/GSTAvoidCollisions.gd | 2 +- project/src/Behaviors/GSTBlend.gd | 8 +++- project/src/Behaviors/GSTCohesion.gd | 2 +- project/src/Behaviors/GSTEvade.gd | 6 +-- project/src/Behaviors/GSTFlee.gd | 2 +- project/src/Behaviors/GSTFollowPath.gd | 2 +- project/src/Behaviors/GSTMatchOrientation.gd | 6 +-- project/src/Behaviors/GSTPriority.gd | 8 ++-- project/src/Behaviors/GSTPursue.gd | 14 +++---- project/src/Behaviors/GSTSeek.gd | 2 +- project/src/Behaviors/GSTSeparation.gd | 6 +-- project/src/GSTSteeringAgent.gd | 8 ++-- 32 files changed, 209 insertions(+), 202 deletions(-) diff --git a/project/demos/Arrive/ArriveDemo.gd b/project/demos/Arrive/ArriveDemo.gd index bb44db2..a412870 100644 --- a/project/demos/Arrive/ArriveDemo.gd +++ b/project/demos/Arrive/ArriveDemo.gd @@ -1,8 +1,8 @@ extends Node2D -export(float, 0, 2000, 40) var max_linear_speed := 800.0 setget set_max_linear_speed -export(float, 0, 200, 2.0) var max_linear_acceleration := 80.0 setget set_max_linear_acceleration +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, 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 @@ -16,8 +16,8 @@ onready var arriver := $Arriver func _ready() -> void: arriver.setup( - max_linear_speed, - max_linear_acceleration, + linear_speed_max, + linear_acceleration_max, arrival_tolerance, deceleration_radius ) @@ -51,17 +51,17 @@ func set_deceleration_radius(value: float) -> void: arriver.arrive.deceleration_radius = value -func set_max_linear_speed(value: float) -> void: - max_linear_speed = value +func set_linear_speed_max(value: float) -> void: + linear_speed_max = value if not is_inside_tree(): return - arriver.agent.max_linear_speed = value + arriver.agent.linear_speed_max = value -func set_max_linear_acceleration(value: float) -> void: - max_linear_acceleration = value +func set_linear_acceleration_max(value: float) -> void: + linear_acceleration_max = value if not is_inside_tree(): return - arriver.agent.max_linear_acceleration = value + arriver.agent.linear_acceleration_max = value diff --git a/project/demos/Arrive/Arriver.gd b/project/demos/Arrive/Arriver.gd index 83c522c..9a71df8 100644 --- a/project/demos/Arrive/Arriver.gd +++ b/project/demos/Arrive/Arriver.gd @@ -14,18 +14,18 @@ func _physics_process(delta: float) -> void: _update_agent() _accel = arrive.calculate_steering(_accel) _velocity += Vector2(_accel.linear.x, _accel.linear.y) - _velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag).clamped(agent.max_linear_speed) + _velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag).clamped(agent.linear_speed_max) _velocity = move_and_slide(_velocity) func setup( - max_linear_speed: float, - max_linear_acceleration: float, + linear_speed_max: float, + linear_acceleration_max: float, arrival_tolerance: float, deceleration_radius: float ) -> void: - agent.max_linear_speed = max_linear_speed - agent.max_linear_acceleration = max_linear_acceleration + agent.linear_speed_max = linear_speed_max + agent.linear_acceleration_max = linear_acceleration_max agent.position = Vector3(global_position.x, global_position.y, 0) arrive.deceleration_radius = deceleration_radius arrive.arrival_tolerance = arrival_tolerance diff --git a/project/demos/AvoidCollisions/AvoidCollisionsDemo.gd b/project/demos/AvoidCollisions/AvoidCollisionsDemo.gd index bcb89c3..d070e29 100644 --- a/project/demos/AvoidCollisions/AvoidCollisionsDemo.gd +++ b/project/demos/AvoidCollisions/AvoidCollisionsDemo.gd @@ -1,28 +1,28 @@ extends Node2D -export(float, 0, 2000, 40) var max_linear_speed := 350.0 setget set_max_linear_speed -export(float, 0, 100, 2) var max_linear_acceleration := 40.0 setget set_max_linear_accel +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, 500, 10) var proximity_radius := 140.0 setget set_proximity_radius export var draw_proximity := true setget set_draw_proximity onready var spawner := $Spawner -func set_max_linear_speed(value: float) -> void: - max_linear_speed = value +func set_linear_speed_max(value: float) -> void: + linear_speed_max = value if not is_inside_tree(): return - spawner.set_max_linear_speed(value) + spawner.set_linear_speed_max(value) -func set_max_linear_accel(value: float) -> void: - max_linear_acceleration = value +func set_linear_accel_max(value: float) -> void: + linear_acceleration_max = value if not is_inside_tree(): return - spawner.set_max_linear_accel(value) + spawner.set_linear_accel_max(value) func set_proximity_radius(value: float) -> void: diff --git a/project/demos/AvoidCollisions/AvoidCollisionsDemo.tscn b/project/demos/AvoidCollisions/AvoidCollisionsDemo.tscn index 06e81bc..1e22796 100644 --- a/project/demos/AvoidCollisions/AvoidCollisionsDemo.tscn +++ b/project/demos/AvoidCollisions/AvoidCollisionsDemo.tscn @@ -7,7 +7,6 @@ [node name="AvoidCollisionsDemo" type="Node2D"] script = ExtResource( 2 ) -max_linear_speed = 360.0 proximity_radius = 100.0 [node name="Spawner" type="Node2D" parent="."] diff --git a/project/demos/AvoidCollisions/Avoider.gd b/project/demos/AvoidCollisions/Avoider.gd index 0670eb5..de899ad 100644 --- a/project/demos/AvoidCollisions/Avoider.gd +++ b/project/demos/AvoidCollisions/Avoider.gd @@ -1,15 +1,6 @@ extends KinematicBody2D -onready var collision := $CollisionShape2D -onready var agent := GSTSteeringAgent.new() -onready var proximity := GSTRadiusProximity.new(agent, [], 140) -onready var avoid := GSTAvoidCollisions.new(agent, proximity) -onready var target := GSTAgentLocation.new() -onready var seek := GSTSeek.new(agent, target) -onready var priority := GSTPriority.new(agent, 0.0001) -onready var sprite := $Sprite - var draw_proximity: bool var _boundary_right: float @@ -20,6 +11,15 @@ var _velocity := Vector2.ZERO var _direction := Vector2() var _drag: = 0.1 +onready var collision := $CollisionShape2D +onready var agent := GSTSteeringAgent.new() +onready var proximity := GSTRadiusProximity.new(agent, [], 140) +onready var avoid := GSTAvoidCollisions.new(agent, proximity) +onready var target := GSTAgentLocation.new() +onready var seek := GSTSeek.new(agent, target) +onready var priority := GSTPriority.new(agent, 0.0001) +onready var sprite := $Sprite + func _draw() -> void: if draw_proximity: @@ -31,13 +31,13 @@ func _physics_process(delta: float) -> void: _accel = priority.calculate_steering(_accel) _velocity += Vector2(_accel.linear.x, _accel.linear.y) _velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag) - _velocity = _velocity.clamped(agent.max_linear_speed) + _velocity = _velocity.clamped(agent.linear_speed_max) _velocity = move_and_slide(_velocity) func setup( - max_linear_speed: float, - max_linear_accel: float, + linear_speed_max: float, + linear_accel_max: float, proximity_radius: float, boundary_right: float, boundary_bottom: float, @@ -47,8 +47,8 @@ func setup( rng.randomize() _direction = Vector2(rng.randf_range(-1, 1), rng.randf_range(-1, 1)).normalized() _update_agent() - agent.max_linear_speed = max_linear_speed - agent.max_linear_acceleration = max_linear_accel + agent.linear_speed_max = linear_speed_max + agent.linear_acceleration_max = linear_accel_max proximity.radius = proximity_radius _boundary_bottom = boundary_bottom _boundary_right = boundary_right @@ -65,22 +65,22 @@ func set_proximity_agents(agents: Array) -> void: proximity.agents = agents -func set_random_nonoverlapping_position(others: Array, min_distance_from_boundary: float) -> void: +func set_random_nonoverlapping_position(others: Array, distance_from_boundary_min: float) -> void: var rng := RandomNumberGenerator.new() rng.randomize() - var max_tries := max(100, others.size() * others.size()) - while max_tries >= 0: - max_tries -= 1 + var tries_max := max(100, others.size() * others.size()) + while tries_max > 0: + tries_max -= 1 global_position.x = rng.randf_range( - min_distance_from_boundary, _boundary_right-min_distance_from_boundary + distance_from_boundary_min, _boundary_right-distance_from_boundary_min ) global_position.y = rng.randf_range( - min_distance_from_boundary, _boundary_bottom-min_distance_from_boundary + distance_from_boundary_min, _boundary_bottom-distance_from_boundary_min ) var done := true for i in range(others.size()): var other: Node2D = others[i] - if other.global_position.distance_to(position) <= _radius*2 + min_distance_from_boundary: + if other.global_position.distance_to(position) <= _radius*2 + distance_from_boundary_min: done = false if done: break diff --git a/project/demos/AvoidCollisions/Spawner.gd b/project/demos/AvoidCollisions/Spawner.gd index 56c889b..8960d9f 100644 --- a/project/demos/AvoidCollisions/Spawner.gd +++ b/project/demos/AvoidCollisions/Spawner.gd @@ -18,8 +18,8 @@ func _ready() -> void: var avoider := avoider_template.instance() add_child(avoider) avoider.setup( - owner.max_linear_speed, - owner.max_linear_acceleration, + owner.linear_speed_max, + owner.linear_acceleration_max, owner.proximity_radius, boundaries.x, boundaries.y, @@ -39,14 +39,14 @@ func _physics_process(delta: float) -> void: child.global_position = child.global_position.posmodv(boundaries) -func set_max_linear_speed(value: float) -> void: +func set_linear_speed_max(value: float) -> void: for child in get_children(): - child.agent.max_linear_speed = value + child.agent.linear_speed_max = value -func set_max_linear_accel(value: float) -> void: +func set_linear_accel_max(value: float) -> void: for child in get_children(): - child.agent.max_linear_acceleration = value + child.agent.linear_acceleration_max = value func set_proximity_radius(value: float) -> void: diff --git a/project/demos/Face/FaceDemo.gd b/project/demos/Face/FaceDemo.gd index f7fa933..5b19c49 100644 --- a/project/demos/Face/FaceDemo.gd +++ b/project/demos/Face/FaceDemo.gd @@ -1,16 +1,16 @@ extends Node2D +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, 180, 2) var align_tolerance := 5 setget set_align_tolerance +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 + onready var player := $Player onready var gui := $GUI onready var turret := $Turret -export(int, 0, 359, 2) var max_angular_speed := 120 setget set_max_angular_speed -export(int, 0, 359, 2) var max_angular_accel := 10 setget set_max_angular_accel -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(float, 0, 1000, 40) var player_speed := 600.0 setget set_player_speed - func _ready() -> void: player.speed = player_speed @@ -18,8 +18,8 @@ func _ready() -> void: player.agent, deg2rad(align_tolerance), deg2rad(deceleration_radius), - deg2rad(max_angular_accel), - deg2rad(max_angular_speed) + deg2rad(angular_accel_max), + deg2rad(angular_speed_max) ) @@ -39,20 +39,20 @@ func set_deceleration_radius(value: int) -> void: turret.face.deceleration_radius = deg2rad(value) -func set_max_angular_accel(value: int) -> void: - max_angular_accel = value +func set_angular_accel_max(value: int) -> void: + angular_accel_max = value if not is_inside_tree(): return - turret.agent.max_angular_acceleration = deg2rad(value) + turret.agent.angular_acceleration_max = deg2rad(value) -func set_max_angular_speed(value: int) -> void: - max_angular_speed = value +func set_angular_speed_max(value: int) -> void: + angular_speed_max = value if not is_inside_tree(): return - turret.agent.max_angular_speed = deg2rad(value) + turret.agent.angular_speed_max = deg2rad(value) func set_player_speed(value: float) -> void: diff --git a/project/demos/Face/Turret.gd b/project/demos/Face/Turret.gd index 2b90411..d1d03eb 100644 --- a/project/demos/Face/Turret.gd +++ b/project/demos/Face/Turret.gd @@ -18,7 +18,11 @@ func _ready() -> void: func _physics_process(delta: float) -> void: _accel = face.calculate_steering(_accel) - agent.angular_velocity += _accel.angular + agent.angular_velocity = clamp( + agent.angular_velocity + _accel.angular, + -agent.angular_speed_max, + agent.angular_speed_max + ) agent.angular_velocity = lerp(agent.angular_velocity, 0, _angular_drag) agent.orientation += agent.angular_velocity * delta rotation = agent.orientation @@ -32,15 +36,15 @@ func setup( player_agent: GSTAgentLocation, align_tolerance: float, deceleration_radius: float, - max_angular_accel: float, - max_angular_speed: float + angular_accel_max: float, + angular_speed_max: float ) -> void: face = GSTFace.new(agent, player_agent) face.alignment_tolerance = align_tolerance face.deceleration_radius = deceleration_radius - agent.max_angular_acceleration = max_angular_accel - agent.max_angular_speed = max_angular_speed + agent.angular_acceleration_max = angular_accel_max + agent.angular_speed_max = angular_speed_max agent.position = Vector3(global_position.x, global_position.y, 0) diff --git a/project/demos/FollowPath/FollowPathDemo.gd b/project/demos/FollowPath/FollowPathDemo.gd index 081b358..af9edd3 100644 --- a/project/demos/FollowPath/FollowPathDemo.gd +++ b/project/demos/FollowPath/FollowPathDemo.gd @@ -1,8 +1,8 @@ extends Node2D -export(float, 0, 2000, 40) var max_linear_speed := 600.0 setget set_max_linear_speed -export(float, 0, 200, 10.0) var max_linear_acceleration := 40.0 setget set_max_linear_acceleration +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, 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, 5, 0.1) var predict_time := 0.3 setget set_predict_time @@ -16,27 +16,27 @@ func _ready() -> void: follower.setup( path_offset, predict_time, - max_linear_acceleration, - max_linear_speed, + linear_acceleration_max, + linear_speed_max, deceleration_radius, arrival_tolerance ) -func set_max_linear_speed(value: float) -> void: - max_linear_speed = value +func set_linear_speed_max(value: float) -> void: + linear_speed_max = value if not is_inside_tree(): return - follower.agent.max_linear_speed = value + follower.agent.linear_speed_max = value -func set_max_linear_acceleration(value: float) -> void: - max_linear_acceleration = value +func set_linear_acceleration_max(value: float) -> void: + linear_acceleration_max = value if not is_inside_tree(): return - follower.agent.max_linear_acceleration = value + follower.agent.linear_acceleration_max = value func set_arrival_tolerance(value: float) -> void: diff --git a/project/demos/FollowPath/PathFollower.gd b/project/demos/FollowPath/PathFollower.gd index 8b31d25..c19c9a6 100644 --- a/project/demos/FollowPath/PathFollower.gd +++ b/project/demos/FollowPath/PathFollower.gd @@ -1,6 +1,11 @@ extends KinematicBody2D +var _velocity := Vector2.ZERO +var _accel := GSTTargetAcceleration.new() +var _valid := false +var _drag := 0.1 + onready var agent := GSTSteeringAgent.new() onready var path := GSTPath.new([ Vector3(global_position.x, global_position.y, 0), @@ -8,25 +13,20 @@ onready var path := GSTPath.new([ ], true) onready var follow := GSTFollowPath.new(agent, path, 0, 0) -var _velocity := Vector2.ZERO -var _accel := GSTTargetAcceleration.new() -var _valid := false -var _drag := 0.1 - func setup( path_offset: float, predict_time: float, - max_accel: float, - max_speed: float, + accel_max: float, + speed_max: float, decel_radius: float, arrival_tolerance: float ) -> void: owner.drawer.connect("path_established", self, "_on_Drawer_path_established") follow.path_offset = path_offset follow.prediction_time = predict_time - agent.max_linear_acceleration = max_accel - agent.max_linear_speed = max_speed + agent.linear_acceleration_max = accel_max + agent.linear_speed_max = speed_max follow.deceleration_radius = decel_radius follow.arrival_tolerance = arrival_tolerance @@ -37,7 +37,7 @@ func _physics_process(delta: float) -> void: _accel = follow.calculate_steering(_accel) _velocity += Vector2(_accel.linear.x, _accel.linear.y) _velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag) - _velocity = _velocity.clamped(agent.max_linear_speed) + _velocity = _velocity.clamped(agent.linear_speed_max) _velocity = move_and_slide(_velocity) diff --git a/project/demos/GroupBehaviors/GroupBehaviorsDemo.gd b/project/demos/GroupBehaviors/GroupBehaviorsDemo.gd index 42a42a2..a18f05c 100644 --- a/project/demos/GroupBehaviors/GroupBehaviorsDemo.gd +++ b/project/demos/GroupBehaviors/GroupBehaviorsDemo.gd @@ -3,8 +3,8 @@ extends Node2D onready var spawner := $Spawner -export(float, 0, 2000, 40.0) var max_linear_speed := 600.0 setget set_max_linear_speed -export(float, 0, 200, 2.0) var max_linear_accel := 40.0 setget set_max_linear_accel +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, 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, 2, 0.1) var cohesion_strength := 0.1 setget set_cohesion_strength @@ -14,8 +14,8 @@ export var show_proximity_radius := true setget set_show_proximity_radius func _ready() -> void: spawner.setup( - max_linear_speed, - max_linear_accel, + linear_speed_max, + linear_accel_max, proximity_radius, separation_decay_coefficient, cohesion_strength, @@ -24,20 +24,20 @@ func _ready() -> void: ) -func set_max_linear_speed(value: float) -> void: - max_linear_speed = value +func set_linear_speed_max(value: float) -> void: + linear_speed_max = value if not is_inside_tree(): return - spawner.set_max_linear_speed(value) + spawner.set_linear_speed_max(value) -func set_max_linear_accel(value: float) -> void: - max_linear_accel = value +func set_linear_accel_max(value: float) -> void: + linear_accel_max = value if not is_inside_tree(): return - spawner.set_max_linear_accel(value) + spawner.set_linear_accel_max(value) func set_proximity_radius(value: float) -> void: diff --git a/project/demos/GroupBehaviors/Member.gd b/project/demos/GroupBehaviors/Member.gd index 37e9a09..92ebf53 100644 --- a/project/demos/GroupBehaviors/Member.gd +++ b/project/demos/GroupBehaviors/Member.gd @@ -14,8 +14,8 @@ var _velocity := Vector2() func setup( - max_linear_speed: float, - max_linear_accel: float, + linear_speed_max: float, + linear_accel_max: float, proximity_radius: float, separation_decay_coefficient: float, cohesion_strength: float, @@ -24,8 +24,8 @@ func setup( _color = Color(rand_range(0.5, 1), rand_range(0.25, 1), rand_range(0, 1)) $Sprite.modulate = _color - agent.max_linear_acceleration = max_linear_accel - agent.max_linear_speed = max_linear_speed + agent.linear_acceleration_max = linear_accel_max + agent.linear_speed_max = linear_speed_max proximity = GSTRadiusProximity.new(agent, [], proximity_radius) separation = GSTSeparation.new(agent, proximity) @@ -47,7 +47,7 @@ func _physics_process(delta: float) -> void: acceleration = blend.calculate_steering(acceleration) _velocity += Vector2(acceleration.linear.x, acceleration.linear.y) _velocity = _velocity.linear_interpolate(Vector2.ZERO, 0.1) - _velocity = _velocity.clamped(agent.max_linear_speed) + _velocity = _velocity.clamped(agent.linear_speed_max) move_and_slide(_velocity) diff --git a/project/demos/GroupBehaviors/Spawner.gd b/project/demos/GroupBehaviors/Spawner.gd index 80787e0..2455a8c 100644 --- a/project/demos/GroupBehaviors/Spawner.gd +++ b/project/demos/GroupBehaviors/Spawner.gd @@ -5,8 +5,8 @@ export var member: PackedScene func setup( - max_linear_speed: float, - max_linear_accel: float, + linear_speed_max: float, + linear_accel_max: float, proximity_radius: float, separation_decay_coefficient: float, cohesion_strength: float, @@ -20,8 +20,8 @@ func setup( follower.position += Vector2(rand_range(-60, 60), rand_range(-60, 60)) followers.append(follower) follower.setup( - max_linear_speed, - max_linear_accel, + linear_speed_max, + linear_accel_max, proximity_radius, separation_decay_coefficient, cohesion_strength, @@ -37,14 +37,14 @@ func setup( i.proximity.agents = agents -func set_max_linear_speed(value: float) -> void: +func set_linear_speed_max(value: float) -> void: for child in get_children(): - child.agent.max_linear_speed = value + child.agent.linear_speed_max = value -func set_max_linear_accel(value: float) -> void: +func set_linear_accel_max(value: float) -> void: for child in get_children(): - child.agent.max_linear_acceleration = value + child.agent.linear_acceleration_max = value func set_proximity_radius(value: float) -> void: diff --git a/project/demos/PursueSeek/Player.gd b/project/demos/PursueSeek/Player.gd index e94e84d..42f1ae8 100644 --- a/project/demos/PursueSeek/Player.gd +++ b/project/demos/PursueSeek/Player.gd @@ -2,18 +2,18 @@ extends KinematicBody2D # Controls the player ship's movements based on player input. -onready var agent := GSTSteeringAgent.new() - export var thruster_strength := 175.0 export var side_thruster_strength := 10.0 -export var max_velocity := 300.0 -export var max_angular_velocity := 2.0 +export var velocity_max := 300.0 +export var angular_velocity_max := 2.0 export var angular_drag := 0.025 export var linear_drag := 0.025 var _linear_velocity := Vector2() var _angular_velocity := 0.0 +onready var agent := GSTSteeringAgent.new() + func _physics_process(delta: float) -> void: var movement := _get_movement() @@ -21,7 +21,7 @@ func _physics_process(delta: float) -> void: movement.x, _angular_velocity, side_thruster_strength, - max_angular_velocity, + angular_velocity_max, angular_drag, delta ) @@ -33,7 +33,7 @@ func _physics_process(delta: float) -> void: Vector2.UP.rotated(rotation), linear_drag, thruster_strength, - max_velocity, + velocity_max, delta ) @@ -45,14 +45,14 @@ func _calculate_angular_velocity( horizontal_movement: float, current_velocity: float, thruster_strength: float, - max_velocity: float, + velocity_max: float, ship_drag: float, delta: float ) -> float: var velocity := clamp( current_velocity + thruster_strength * horizontal_movement * delta, - -max_velocity, - max_velocity + -velocity_max, + velocity_max ) velocity = lerp(velocity, 0, ship_drag) @@ -66,7 +66,7 @@ func _calculate_linear_velocity( facing_direction: Vector2, ship_drag_coefficient: float, strength: float, - max_speed: float, + speed_max: float, delta: float ) -> Vector2: var actual_strength := 0.0 @@ -78,7 +78,7 @@ func _calculate_linear_velocity( var velocity := current_velocity + facing_direction * actual_strength * delta velocity = velocity.linear_interpolate(Vector2.ZERO, ship_drag_coefficient) - return velocity.clamped(max_speed) + return velocity.clamped(speed_max) func _get_movement() -> Vector2: diff --git a/project/demos/PursueSeek/PursueVSSeekDemo.gd b/project/demos/PursueSeek/PursueVSSeekDemo.gd index bd74ca4..d7b990e 100644 --- a/project/demos/PursueSeek/PursueVSSeekDemo.gd +++ b/project/demos/PursueSeek/PursueVSSeekDemo.gd @@ -1,8 +1,8 @@ extends Node2D -export(float, 0, 2000, 40) var max_linear_speed := 120.0 setget set_max_linear_speed -export(float, 0, 200, 2) var max_linear_accel := 10.0 setget set_max_linear_accel +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, 5, 0.1) var predict_time := 1.0 setget set_predict_time onready var pursuer := $BoundaryManager/Pursuer @@ -10,26 +10,26 @@ onready var seeker := $BoundaryManager/Seeker func _ready() -> void: - pursuer.setup(predict_time, max_linear_speed, max_linear_accel) - seeker.setup(predict_time, max_linear_speed, max_linear_accel) + pursuer.setup(predict_time, linear_speed_max, linear_accel_max) + seeker.setup(predict_time, linear_speed_max, linear_accel_max) -func set_max_linear_speed(value: float) -> void: - max_linear_speed = value +func set_linear_speed_max(value: float) -> void: + linear_speed_max = value if not is_inside_tree(): return - pursuer.agent.max_linear_speed = value - seeker.agent.max_linear_speed = value + pursuer.agent.linear_speed_max = value + seeker.agent.linear_speed_max = value -func set_max_linear_accel(value: float) -> void: - max_linear_accel = value +func set_linear_accel_max(value: float) -> void: + linear_accel_max = value if not is_inside_tree(): return - pursuer.agent.max_linear_acceleration = value - seeker.agent.max_linear_acceleration = value + pursuer.agent.linear_acceleration_max = value + seeker.agent.linear_acceleration_max = value func set_predict_time(value: float) -> void: @@ -37,4 +37,4 @@ func set_predict_time(value: float) -> void: if not is_inside_tree(): return - pursuer._behavior.max_predict_time = value + pursuer._behavior.predict_time_max = value diff --git a/project/demos/PursueSeek/PursueVSSeekDemo.tscn b/project/demos/PursueSeek/PursueVSSeekDemo.tscn index 64aff40..179c5d1 100644 --- a/project/demos/PursueSeek/PursueVSSeekDemo.tscn +++ b/project/demos/PursueSeek/PursueVSSeekDemo.tscn @@ -12,7 +12,8 @@ script = ExtResource( 4 ) __meta__ = { "_editor_description_": "Toy demo to demonstrate the use of the Pursue contrasted to the more naive Seek steering behavior." } -max_linear_speed = 200.0 +linear_speed_max = 240.0 +linear_accel_max = 40.0 [node name="BoundaryManager" type="Node2D" parent="."] script = ExtResource( 3 ) @@ -24,7 +25,6 @@ collision_mask = 2 script = ExtResource( 2 ) thruster_strength = 600.0 side_thruster_strength = 20.0 -max_velocity = 900.0 [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BoundaryManager/Player"] polygon = PoolVector2Array( 0, -32, -24, 32, 24, 32 ) diff --git a/project/demos/PursueSeek/Pursuer.gd b/project/demos/PursueSeek/Pursuer.gd index 195b66d..70620c6 100644 --- a/project/demos/PursueSeek/Pursuer.gd +++ b/project/demos/PursueSeek/Pursuer.gd @@ -2,10 +2,6 @@ extends KinematicBody2D # Represents a ship that chases after the player. -onready var agent := GSTSteeringAgent.new() -onready var accel := GSTTargetAcceleration.new() -onready var player_agent: GSTSteeringAgent = owner.find_node("Player", true, false).agent - export var use_seek: bool = false var _orient_behavior: GSTSteeringBehavior @@ -16,6 +12,10 @@ var _linear_drag_coefficient := 0.025 var _angular_velocity := 0.0 var _angular_drag := 0.1 +onready var agent := GSTSteeringAgent.new() +onready var accel := GSTTargetAcceleration.new() +onready var player_agent: GSTSteeringAgent = owner.find_node("Player", true, false).agent + func _ready() -> void: set_physics_process(false) @@ -29,20 +29,20 @@ func _physics_process(delta: float) -> void: _angular_velocity = clamp( lerp(_angular_velocity, 0, _angular_drag), - -agent.max_angular_speed, - agent.max_angular_speed + -agent.angular_speed_max, + agent.angular_speed_max ) rotation += _angular_velocity * delta accel = _behavior.calculate_steering(accel) _linear_velocity += Vector2(accel.linear.x, accel.linear.y) + _linear_velocity = _linear_velocity.clamped(agent.linear_speed_max) _linear_velocity = _linear_velocity.linear_interpolate(Vector2.ZERO, _linear_drag_coefficient) - _linear_velocity = _linear_velocity.clamped(agent.max_linear_speed) _linear_velocity = move_and_slide(_linear_velocity) -func setup(predict_time: float, max_linear_speed: float, max_linear_accel: float) -> void: +func setup(predict_time: float, linear_speed_max: float, linear_accel_max: float) -> void: if use_seek: _behavior = GSTSeek.new(agent, player_agent) else: @@ -52,10 +52,10 @@ func setup(predict_time: float, max_linear_speed: float, max_linear_accel: float _orient_behavior.alignment_tolerance = 0.001 _orient_behavior.deceleration_radius = PI/2 - agent.max_angular_acceleration = 2 - agent.max_angular_speed = 5 - agent.max_linear_acceleration = max_linear_accel - agent.max_linear_speed = max_linear_speed + agent.angular_acceleration_max = deg2rad(10) + agent.angular_speed_max = deg2rad(45) + agent.linear_acceleration_max = linear_accel_max + agent.linear_speed_max = linear_speed_max _update_agent() set_physics_process(true) diff --git a/project/demos/SeekFlee/SeekFleeDemo.gd b/project/demos/SeekFlee/SeekFleeDemo.gd index 40fb417..8a19aa2 100644 --- a/project/demos/SeekFlee/SeekFleeDemo.gd +++ b/project/demos/SeekFlee/SeekFleeDemo.gd @@ -5,8 +5,8 @@ extends Node2D enum Mode { FLEE, SEEK } export(Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode -export(float, 0, 2000, 40) var max_linear_speed := 200.0 setget set_max_linear_speed -export(float, 0, 500, 0.5) var max_linear_accel := 10.0 setget set_max_linear_accel +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) var player_speed := 600.0 setget set_player_speed var camera_boundaries: Rect2 @@ -37,8 +37,8 @@ func _ready() -> void: var entity: KinematicBody2D = spawner.Entity.instance() entity.global_position = new_pos entity.player_agent = player.agent - entity.start_speed = max_linear_speed - entity.start_accel = max_linear_accel + entity.start_speed = linear_speed_max + entity.start_accel = linear_accel_max spawner.add_child(entity) @@ -56,22 +56,22 @@ func set_behavior_mode(mode: int) -> void: child.use_seek = false -func set_max_linear_speed(value: float) -> void: - max_linear_speed = value +func set_linear_speed_max(value: float) -> void: + linear_speed_max = value if not is_inside_tree(): return for child in spawner.get_children(): - child.agent.max_linear_speed = value + child.agent.linear_speed_max = value -func set_max_linear_accel(value: float) -> void: - max_linear_accel = value +func set_linear_accel_max(value: float) -> void: + linear_accel_max = value if not is_inside_tree(): return for child in spawner.get_children(): - child.agent.max_linear_acceleration = value + child.agent.linear_acceleration_max = value func set_player_speed(value: float) -> void: diff --git a/project/demos/SeekFlee/Seeker.gd b/project/demos/SeekFlee/Seeker.gd index 4247eb0..d27b381 100644 --- a/project/demos/SeekFlee/Seeker.gd +++ b/project/demos/SeekFlee/Seeker.gd @@ -2,21 +2,21 @@ extends KinematicBody2D # AI agent that uses the Seek behavior to hone in on the player's location as directly as possible. -onready var agent := GSTSteeringAgent.new() -onready var accel := GSTTargetAcceleration.new() -onready var seek := GSTSeek.new(agent, player_agent) -onready var flee := GSTFlee.new(agent, player_agent) - var player_agent: GSTAgentLocation var velocity := Vector2.ZERO var start_speed: float var start_accel: float var use_seek := true +onready var agent := GSTSteeringAgent.new() +onready var accel := GSTTargetAcceleration.new() +onready var seek := GSTSeek.new(agent, player_agent) +onready var flee := GSTFlee.new(agent, player_agent) + func _ready() -> void: - agent.max_linear_acceleration = start_accel - agent.max_linear_speed = start_speed + agent.linear_acceleration_max = start_accel + agent.linear_speed_max = start_speed func _physics_process(delta: float) -> void: @@ -29,7 +29,7 @@ func _physics_process(delta: float) -> void: else: accel = flee.calculate_steering(accel) - velocity = (velocity + Vector2(accel.linear.x, accel.linear.y)).clamped(agent.max_linear_speed) + velocity = (velocity + Vector2(accel.linear.x, accel.linear.y)).clamped(agent.linear_speed_max) velocity = move_and_slide(velocity) diff --git a/project/src/Behaviors/GSTArrive.gd b/project/src/Behaviors/GSTArrive.gd index 8c53a7b..652f187 100644 --- a/project/src/Behaviors/GSTArrive.gd +++ b/project/src/Behaviors/GSTArrive.gd @@ -21,7 +21,7 @@ func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> G if distance <= arrival_tolerance: acceleration.set_zero() else: - var desired_speed := agent.max_linear_speed + var desired_speed := agent.linear_speed_max if distance <= deceleration_radius: desired_speed *= distance / deceleration_radius @@ -30,7 +30,7 @@ func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> G desired_velocity = (desired_velocity - agent.linear_velocity) * 1.0 / time_to_reach - acceleration.linear = GSTUtils.clampedv3(desired_velocity, agent.max_linear_acceleration) + acceleration.linear = GSTUtils.clampedv3(desired_velocity, agent.linear_acceleration_max) acceleration.angular = 0 return acceleration diff --git a/project/src/Behaviors/GSTAvoidCollisions.gd b/project/src/Behaviors/GSTAvoidCollisions.gd index 046dd71..4cf9c0c 100644 --- a/project/src/Behaviors/GSTAvoidCollisions.gd +++ b/project/src/Behaviors/GSTAvoidCollisions.gd @@ -33,7 +33,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele else: acceleration.linear = first_relative_position + (first_relative_velocity * shortest_time) - acceleration.linear = acceleration.linear.normalized() * -agent.max_linear_acceleration + acceleration.linear = acceleration.linear.normalized() * -agent.linear_acceleration_max acceleration.angular = 0 return acceleration diff --git a/project/src/Behaviors/GSTBlend.gd b/project/src/Behaviors/GSTBlend.gd index e84b5cd..669f934 100644 --- a/project/src/Behaviors/GSTBlend.gd +++ b/project/src/Behaviors/GSTBlend.gd @@ -38,7 +38,11 @@ func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAccel blended_accel.add_scaled_accel(_accel, bw.weight) - blended_accel.linear = GSTUtils.clampedv3(blended_accel.linear, agent.max_linear_acceleration) - blended_accel.angular = min(blended_accel.angular, agent.max_angular_acceleration) + blended_accel.linear = GSTUtils.clampedv3(blended_accel.linear, agent.linear_acceleration_max) + blended_accel.angular = clamp( + blended_accel.angular, + -agent.angular_acceleration_max, + agent.angular_acceleration_max + ) return blended_accel diff --git a/project/src/Behaviors/GSTCohesion.gd b/project/src/Behaviors/GSTCohesion.gd index 38b8cb4..d549090 100644 --- a/project/src/Behaviors/GSTCohesion.gd +++ b/project/src/Behaviors/GSTCohesion.gd @@ -17,7 +17,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele var neighbor_count = proximity.find_neighbors(_callback) if neighbor_count > 0: center_of_mass *= 1.0 / neighbor_count - acceleration.linear = (center_of_mass - agent.position).normalized() * agent.max_linear_acceleration + acceleration.linear = (center_of_mass - agent.position).normalized() * agent.linear_acceleration_max return acceleration diff --git a/project/src/Behaviors/GSTEvade.gd b/project/src/Behaviors/GSTEvade.gd index 9f257ab..22d7d3c 100644 --- a/project/src/Behaviors/GSTEvade.gd +++ b/project/src/Behaviors/GSTEvade.gd @@ -2,15 +2,15 @@ class_name GSTEvade extends GSTPursue # Calculates acceleration to take an agent away from where a target agent will be. -# # The `max_predict_time` variable represents how far ahead to calculate the intersection point. +# # The `predict_time_max` variable represents how far ahead to calculate the intersection point. func _init( agent: GSTSteeringAgent, target: GSTSteeringAgent, - max_predict_time := 1.0).(agent, target, max_predict_time): + predict_time_max := 1.0).(agent, target, predict_time_max): pass func _get_modified_acceleration() -> float: - return -agent.max_linear_acceleration + return -agent.linear_acceleration_max diff --git a/project/src/Behaviors/GSTFlee.gd b/project/src/Behaviors/GSTFlee.gd index b5565cd..35196f5 100644 --- a/project/src/Behaviors/GSTFlee.gd +++ b/project/src/Behaviors/GSTFlee.gd @@ -9,7 +9,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent, target) -> func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: acceleration.linear = ( - (agent.position - target.position).normalized() * agent.max_linear_acceleration) + (agent.position - target.position).normalized() * agent.linear_acceleration_max) acceleration.angular = 0 return acceleration diff --git a/project/src/Behaviors/GSTFollowPath.gd b/project/src/Behaviors/GSTFollowPath.gd index d4b2461..25149ff 100644 --- a/project/src/Behaviors/GSTFollowPath.gd +++ b/project/src/Behaviors/GSTFollowPath.gd @@ -41,7 +41,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele return _arrive(acceleration, target_position) acceleration.linear = (target_position - agent.position).normalized() - acceleration.linear *= agent.max_linear_acceleration + acceleration.linear *= agent.linear_acceleration_max acceleration.angular = 0 return acceleration diff --git a/project/src/Behaviors/GSTMatchOrientation.gd b/project/src/Behaviors/GSTMatchOrientation.gd index bb0e4dd..e8229db 100644 --- a/project/src/Behaviors/GSTMatchOrientation.gd +++ b/project/src/Behaviors/GSTMatchOrientation.gd @@ -22,7 +22,7 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation if rotation_size <= alignment_tolerance: acceleration.set_zero() else: - var desired_rotation := agent.max_angular_speed + var desired_rotation := agent.angular_speed_max if rotation_size <= deceleration_radius: desired_rotation *= rotation_size / deceleration_radius @@ -32,8 +32,8 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation acceleration.angular = (desired_rotation - agent.angular_velocity) / time_to_reach var limited_acceleration := abs(acceleration.angular) - if limited_acceleration > agent.max_angular_acceleration: - acceleration.angular *= agent.max_angular_acceleration / limited_acceleration + if limited_acceleration > agent.angular_acceleration_max: + acceleration.angular *= agent.angular_acceleration_max / limited_acceleration acceleration.linear = Vector3.ZERO diff --git a/project/src/Behaviors/GSTPriority.gd b/project/src/Behaviors/GSTPriority.gd index 49c89a6..c3fd2bf 100644 --- a/project/src/Behaviors/GSTPriority.gd +++ b/project/src/Behaviors/GSTPriority.gd @@ -7,11 +7,11 @@ extends GSTSteeringBehavior var _behaviors := [] var last_selected_index: int -var threshold_for_zero: float +var zero_threshold: float -func _init(agent: GSTSteeringAgent, threshold_for_zero := 0.001).(agent) -> void: - self.threshold_for_zero = threshold_for_zero +func _init(agent: GSTSteeringAgent, zero_threshold := 0.001).(agent) -> void: + self.zero_threshold = zero_threshold func add(behavior: GSTSteeringBehavior) -> void: @@ -26,7 +26,7 @@ func get_behavior_at(index: int) -> GSTSteeringBehavior: func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration: - var threshold_squared := threshold_for_zero * threshold_for_zero + var threshold_squared := zero_threshold * zero_threshold last_selected_index = -1 diff --git a/project/src/Behaviors/GSTPursue.gd b/project/src/Behaviors/GSTPursue.gd index 16c4c04..1bd4129 100644 --- a/project/src/Behaviors/GSTPursue.gd +++ b/project/src/Behaviors/GSTPursue.gd @@ -2,19 +2,19 @@ class_name GSTPursue extends GSTSteeringBehavior # Calculates acceleration to take an agent to intersect with where a target agent will be. -# # The `max_predict_time` variable represents how far ahead to calculate the intersection point. +# # The `predict_time_max` variable represents how far ahead to calculate the intersection point. var target: GSTSteeringAgent -var max_predict_time: float +var predict_time_max: float func _init( agent: GSTSteeringAgent, target: GSTSteeringAgent, - max_predict_time := 1.0).(agent) -> void: + predict_time_max := 1.0).(agent) -> void: self.target = target - self.max_predict_time = max_predict_time + self.predict_time_max = predict_time_max func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: @@ -22,11 +22,11 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele var distance_squared := (target_position - agent.position).length_squared() var speed_squared := agent.linear_velocity.length_squared() - var predict_time := max_predict_time + var predict_time := predict_time_max if speed_squared > 0: var predict_time_squared := distance_squared / speed_squared - if predict_time_squared < max_predict_time * max_predict_time: + if predict_time_squared < predict_time_max * predict_time_max: predict_time = sqrt(predict_time_squared) acceleration.linear = (( @@ -39,4 +39,4 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele func _get_modified_acceleration() -> float: - return agent.max_linear_acceleration + return agent.linear_acceleration_max diff --git a/project/src/Behaviors/GSTSeek.gd b/project/src/Behaviors/GSTSeek.gd index f2b78e4..6d09a5d 100644 --- a/project/src/Behaviors/GSTSeek.gd +++ b/project/src/Behaviors/GSTSeek.gd @@ -12,7 +12,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void: func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: acceleration.linear = ( - (target.position - agent.position).normalized() * agent.max_linear_acceleration) + (target.position - agent.position).normalized() * agent.linear_acceleration_max) acceleration.angular = 0 return acceleration diff --git a/project/src/Behaviors/GSTSeparation.gd b/project/src/Behaviors/GSTSeparation.gd index 110e769..e11e758 100644 --- a/project/src/Behaviors/GSTSeparation.gd +++ b/project/src/Behaviors/GSTSeparation.gd @@ -28,11 +28,11 @@ func report_neighbor(neighbor: GSTSteeringAgent) -> bool: var to_agent := agent.position - neighbor.position var distance_squared := to_agent.length_squared() - var max_acceleration := agent.max_linear_acceleration + var acceleration_max := agent.linear_acceleration_max var strength := decay_coefficient / distance_squared - if strength > max_acceleration: - strength = max_acceleration + if strength > acceleration_max: + strength = acceleration_max acceleration.linear += to_agent * (strength / sqrt(distance_squared)) diff --git a/project/src/GSTSteeringAgent.gd b/project/src/GSTSteeringAgent.gd index 55f5347..11fe727 100644 --- a/project/src/GSTSteeringAgent.gd +++ b/project/src/GSTSteeringAgent.gd @@ -4,10 +4,10 @@ class_name GSTSteeringAgent var zero_linear_speed_threshold := 0.01 -var max_linear_speed := 0.0 -var max_linear_acceleration := 0.0 -var max_angular_speed := 0.0 -var max_angular_acceleration := 0.0 +var linear_speed_max := 0.0 +var linear_acceleration_max := 0.0 +var angular_speed_max := 0.0 +var angular_acceleration_max := 0.0 var linear_velocity := Vector3.ZERO var angular_velocity := 0.0 var bounding_radius := 0.0