Fix the style of booleans and virtual functions in the API

Made booleans start with a prefix, e.g. is_open
Added a leading underscore to two virtual functions
Added VIRTUAL comment above several virtual functions
Renamed accel -> acceleration in a function
This commit is contained in:
Nathan Lovato 2020-01-29 15:53:57 -06:00
parent bd41d5987c
commit 0586878fcf
15 changed files with 36 additions and 37 deletions

View File

@ -123,7 +123,7 @@ func _ready() -> void:
# Behaviors that are not enabled produce 0 acceleration.
# Adding our fleeing behaviors to a blend. The order does not matter.
flee_blend.enabled = false
flee_blend.is_enabled = false
flee_blend.add(look, 1)
flee_blend.add(flee, 1)
@ -144,8 +144,8 @@ func _physics_process(delta: float) -> void:
update_agent()
if current_health <= flee_health_threshold:
pursue_blend.enabled = false
flee_blend.enabled = true
pursue_blend.is_enabled = false
flee_blend.is_enabled = true
# Calculate the desired acceleration.
priority.calculate_steering(acceleration)

View File

@ -3,7 +3,6 @@
[ext_resource path="res://demos/GroupBehaviors/Member.gd" type="Script" id=1]
[ext_resource path="res://assets/sprites/small_circle.png" type="Texture" id=2]
[sub_resource type="CircleShape2D" id=1]
radius = 16.0

View File

@ -90,7 +90,7 @@ func _ready() -> void:
# Behaviors that are not enabled produce 0 acceleration.
# Adding our fleeing behaviors to a blend. The order does not matter.
flee_blend.enabled = false
flee_blend.is_enabled = false
flee_blend.add(look, 1)
flee_blend.add(flee, 1)
@ -111,8 +111,8 @@ func _physics_process(delta: float) -> void:
update_agent()
if current_health <= flee_health_threshold:
pursue_blend.enabled = false
flee_blend.enabled = true
pursue_blend.is_enabled = false
flee_blend.is_enabled = true
# Calculate the desired acceleration.
priority.calculate_steering(acceleration)

View File

@ -3,7 +3,6 @@
[ext_resource path="res://demos/SeekFlee/Seeker.gd" type="Script" id=1]
[ext_resource path="res://assets/sprites/small_circle.png" type="Texture" id=2]
[sub_resource type="CircleShape2D" id=1]
radius = 16.0

View File

@ -22,7 +22,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
first_minimum_separation = 0
first_distance = 0
var neighbor_count := proximity.find_neighbors(_callback)
var neighbor_count := proximity._find_neighbors(_callback)
if neighbor_count == 0 or not first_neighbor:
acceleration.set_zero()
@ -42,7 +42,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
# Callback for the proximity to call when finding neighbors. Keeps track of every `neighbor`
# that was found but only keeps the one the owning agent will most likely collide with.
func report_neighbor(neighbor: GSTSteeringAgent) -> bool:
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
var relative_position := neighbor.position - agent.position
var relative_velocity := neighbor.linear_velocity - agent.linear_velocity
var relative_speed_squared := relative_velocity.length_squared()

View File

@ -14,7 +14,7 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
acceleration.set_zero()
center_of_mass = Vector3.ZERO
var neighbor_count = proximity.find_neighbors(_callback)
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.linear_acceleration_max
@ -23,6 +23,6 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
# Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position
# to the center of mass of the group.
func report_neighbor(neighbor: GSTSteeringAgent) -> bool:
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
center_of_mass += neighbor.position
return true

View File

@ -9,7 +9,7 @@ var path: GSTPath
var path_offset := 0.0
# Whether to use `GSTArrive` behavior on an open path.
var arrive_enabled := true
var is_arrive_enabled := true
# The amount of time in the future to predict the owning agent's position along
# the path. Setting it to 0.0 will force non-predictive path following.
var prediction_time := 0.0
@ -35,7 +35,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
var target_position := path.calculate_target_position(target_distance)
if arrive_enabled and path.open:
if is_arrive_enabled and path.is_open:
if path_offset >= 0:
if target_distance > path.length - deceleration_radius:
return _arrive(acceleration, target_position)

View File

@ -21,13 +21,13 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
acceleration.set_zero()
self.acceleration = acceleration
proximity.find_neighbors(_callback)
proximity._find_neighbors(_callback)
return acceleration
# Callback for the proximity to call when finding neighbors. Determines the amount of
# acceleration that `neighbor` imposes based on its distance from the owner agent.
func report_neighbor(neighbor: GSTSteeringAgent) -> bool:
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
var to_agent := agent.position - neighbor.position
var distance_squared := to_agent.length_squared()

View File

@ -1,12 +1,12 @@
# Base type for group-based steering behaviors.
extends GSTSteeringBehavior
class_name GSTGroupBehavior
extends GSTSteeringBehavior
# Container to find neighbors of the agent and calculate group behavior.
var proximity: GSTProximity
var _callback := funcref(self, "report_neighbor")
var _callback := funcref(self, "_report_neighbor")
func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent) -> void:
@ -15,5 +15,5 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent) -> void:
# Internal callback for the behavior to define whether or not a member is
# relevant
func report_neighbor(neighbor: GSTSteeringAgent) -> bool:
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
return false

View File

@ -1,10 +1,11 @@
# Represents a path made up of Vector3 waypoints, split into segments path
# follow behaviors can use.
extends Reference class_name GSTPath
class_name GSTPath
extends Reference
# If `false`, the path loops.
var open: bool
var is_open: bool
# Total length of the path.
var length: float
@ -14,8 +15,8 @@ var _nearest_point_on_segment: Vector3
var _nearest_point_on_path: Vector3
func _init(waypoints: Array, open := false) -> void:
self.open = open
func _init(waypoints: Array, is_open := false) -> void:
self.is_open = is_open
create_path(waypoints)
_nearest_point_on_segment = waypoints[0]
_nearest_point_on_path = waypoints[0]
@ -36,7 +37,7 @@ func create_path(waypoints: Array) -> void:
previous = current
if i < waypoints.size():
current = waypoints[i]
elif open:
elif is_open:
break
else:
current = waypoints[0]
@ -74,7 +75,7 @@ func calculate_distance(agent_current_position: Vector3) -> float:
# Calculates a target position from the path's starting point based on the `target_distance`.
func calculate_target_position(target_distance: float) -> Vector3:
if open:
if is_open:
target_distance = clamp(target_distance, 0, length)
else:
if target_distance < 0:

View File

@ -25,4 +25,4 @@ var angular_velocity := 0.0
var bounding_radius := 0.0
# Used internally by group behaviors and proximities to mark the agent as already
# considered.
var tagged := false
var is_tagged := false

View File

@ -9,7 +9,7 @@ class_name GSTSteeringBehavior
# If `false`, all calculations return zero amounts of acceleration.
var enabled := true
var is_enabled := true
# The AI agent on which the steering behavior bases its calculations.
var agent: GSTSteeringAgent
@ -21,7 +21,7 @@ func _init(agent: GSTSteeringAgent) -> void:
# Returns the `acceleration` modified with the behavior's desired amount of
# acceleration.
func calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
if enabled:
if is_enabled:
return _calculate_steering(acceleration)
else:
acceleration.set_zero()

View File

@ -10,9 +10,9 @@ func _init(agent: GSTSteeringAgent, agents: Array).(agent, agents) -> void:
# Returns a number of neighbors based on a `callback` function.
#
# `find_neighbors` calls `callback` for each agent in the `agents` array and
# `_find_neighbors` calls `callback` for each agent in the `agents` array and
# adds one to the count if its `callback` returns true.
func find_neighbors(callback: FuncRef) -> int:
func _find_neighbors(callback: FuncRef) -> int:
var neighbor_count := 0
var agent_count := agents.size()
for i in range(agent_count):

View File

@ -16,7 +16,7 @@ func _init(agent: GSTSteeringAgent, agents: Array) -> void:
# Returns a number of neighbors based on a `callback` function.
#
# `find_neighbors` calls `callback` for each agent in the `agents` array and
# `_find_neighbors` calls `callback` for each agent in the `agents` array and
# adds one to the count if its `callback` returns true.
func find_neighbors(callback: FuncRef) -> int:
func _find_neighbors(callback: FuncRef) -> int:
return 0

View File

@ -18,9 +18,9 @@ func _init(agent: GSTSteeringAgent, agents: Array, radius: float).(agent, agents
# Returns a number of neighbors based on a `callback` function.
#
# `find_neighbors` calls `callback` for each agent in the `agents` array that lie within
# `_find_neighbors` calls `callback` for each agent in the `agents` array that lie within
# the radius around the owning agent and adds one to the count if its `callback` returns true.
func find_neighbors(callback: FuncRef) -> int:
func _find_neighbors(callback: FuncRef) -> int:
var agent_count := agents.size()
var neighbor_count := 0
@ -40,16 +40,16 @@ func find_neighbors(callback: FuncRef) -> int:
if distance_squared < range_to * range_to:
if callback.call_func(current_agent):
current_agent.tagged = true
current_agent.is_tagged = true
neighbor_count += 1
continue
current_agent.tagged = false
current_agent.is_tagged = false
else:
for i in range(agent_count):
var current_agent = agents[i] as GSTSteeringAgent
if current_agent != agent and current_agent.tagged:
if current_agent != agent and current_agent.is_tagged:
if callback.call_func(current_agent):
neighbor_count += 1