Review smart agents

I made minimal changes, mostly cosmetic like so:

- rename KinematicMovementType to MovementType since
  GSTKinematicBody2DAgent.KinematicMovementType.COLLIDE for example is
  really more than a mouthful with repeated Kinematic in the name
- add optional movement_type parameter to the constructor, otherwise
  we'd be forced to construct the object and then specify as an
  aditional step the type of movement if we want something else than the
  default
- rewrote the constructor to yield on ready and removed _on_body_ready
- renamed _apply_steering to apply_steering as this is a public method
- renamed _on_SceneTree_frame to _on_SceneTree_physics_frame
This commit is contained in:
Răzvan C. Rădulescu 2020-02-07 12:29:45 +02:00
parent 2ae06d3da3
commit 1baed58659
11 changed files with 39 additions and 47 deletions

View File

@ -12,7 +12,7 @@ var _drag := 0.1
func _physics_process(delta: float) -> void:
arrive.calculate_steering(_accel)
agent._apply_steering(_accel, delta)
agent.apply_steering(_accel, delta)
func setup(

View File

@ -31,7 +31,7 @@ func _physics_process(delta: float) -> void:
target.position.y = agent.position.y + _direction.y*_radius
priority.calculate_steering(_accel)
agent._apply_steering(_accel, delta)
agent.apply_steering(_accel, delta)
func setup(

View File

@ -20,7 +20,7 @@ func _ready() -> void:
func _physics_process(delta: float) -> void:
face.calculate_steering(_accel)
agent._apply_steering(_accel, delta)
agent.apply_steering(_accel, delta)
func _draw() -> void:

View File

@ -36,7 +36,7 @@ func setup(
func _physics_process(delta: float) -> void:
if _valid:
follow.calculate_steering(_accel)
agent._apply_steering(_accel, delta)
agent.apply_steering(_accel, delta)
func _on_Drawer_path_established(points: Array) -> void:

View File

@ -46,7 +46,7 @@ func _draw() -> void:
func _physics_process(delta: float) -> void:
if blend:
blend.calculate_steering(acceleration)
agent._apply_steering(acceleration, delta)
agent.apply_steering(acceleration, delta)
func set_neighbors(neighbor: Array) -> void:

View File

@ -27,4 +27,4 @@ func _physics_process(delta: float) -> void:
else:
flee.calculate_steering(accel)
agent._apply_steering(accel, delta)
agent.apply_steering(accel, delta)

View File

@ -4,7 +4,7 @@ extends GSTSpecializedAgent
class_name GSTKinematicBody2DAgent
enum KinematicMovementType { SLIDE, COLLIDE, POSITION }
enum MovementType { SLIDE, COLLIDE, POSITION }
# The KinematicBody2D to keep track of
@ -15,30 +15,31 @@ var body: KinematicBody2D setget _set_body
# SLIDE uses use move_and_slide
# COLLIDE uses move_and_collide
# POSITION changes the global_position directly
var kinematic_movement_type: int = KinematicMovementType.SLIDE
var movement_type: int
var _last_position: Vector2
func _init(body: KinematicBody2D) -> void:
func _init(body: KinematicBody2D, movement_type: int = MovementType.SLIDE) -> void:
yield(body, "ready")
self.body = body
if body.is_inside_tree():
body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
else:
body.connect("ready", self, "_on_body_ready")
self.movement_type = movement_type
body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame")
# Moves the agent's `body` by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
_applied_steering = true
match kinematic_movement_type:
KinematicMovementType.COLLIDE:
match movement_type:
MovementType.COLLIDE:
_apply_collide_steering(acceleration.linear, delta)
KinematicMovementType.SLIDE:
MovementType.SLIDE:
_apply_sliding_steering(acceleration.linear)
_:
_apply_normal_steering(acceleration.linear, delta)
_apply_position_steering(acceleration.linear, delta)
_apply_orientation_steering(acceleration.angular, delta)
@ -64,7 +65,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
linear_velocity = velocity
func _apply_normal_steering(accel: Vector3, delta: float) -> void:
func _apply_position_steering(accel: Vector3, delta: float) -> void:
var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(
@ -95,12 +96,7 @@ func _set_body(value: KinematicBody2D) -> void:
orientation = _last_orientation
func _on_body_ready() -> void:
body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
_set_body(body)
func _on_SceneTree_frame() -> void:
func _on_SceneTree_physics_frame() -> void:
var current_position: Vector2 = body.global_position
var current_orientation: float = body.rotation

View File

@ -4,7 +4,7 @@ extends GSTSpecializedAgent
class_name GSTKinematicBodyAgent
enum KinematicMovementType { SLIDE, COLLIDE, POSITION }
enum MovementType { SLIDE, COLLIDE, POSITION }
# The KinematicBody to keep track of
@ -15,30 +15,31 @@ var body: KinematicBody setget _set_body
# SLIDE uses use move_and_slide
# COLLIDE uses move_and_collide
# POSITION changes the global_position directly
var kinematic_movement_type: int = KinematicMovementType.SLIDE
var movement_type: int
var _last_position: Vector3
func _init(body: KinematicBody) -> void:
func _init(body: KinematicBody, movement_type: int = MovementType.SLIDE) -> void:
yield(body, "ready")
self.body = body
if body.is_inside_tree():
body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
else:
body.connect("ready", self, "_on_body_ready")
self.movement_type = movement_type
body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame")
# Moves the agent's `body` by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
_applied_steering = true
match kinematic_movement_type:
KinematicMovementType.COLLIDE:
match movement_type:
MovementType.COLLIDE:
_apply_collide_steering(acceleration.linear, delta)
KinematicMovementType.SLIDE:
MovementType.SLIDE:
_apply_sliding_steering(acceleration.linear)
_:
_apply_normal_steering(acceleration.linear, delta)
_apply_position_steering(acceleration.linear, delta)
_apply_orientation_steering(acceleration.angular, delta)
@ -64,7 +65,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
linear_velocity = velocity
func _apply_normal_steering(accel: Vector3, delta: float) -> void:
func _apply_position_steering(accel: Vector3, delta: float) -> void:
var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(
@ -95,12 +96,7 @@ func _set_body(value: KinematicBody) -> void:
orientation = _last_orientation
func _on_body_ready() -> void:
body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
_set_body(body)
func _on_SceneTree_frame() -> void:
func _on_SceneTree_physics_frame() -> void:
var current_position: Vector3 = body.global_position
var current_orientation: float = body.rotation.y

View File

@ -20,7 +20,7 @@ func _init(body: RigidBody2D) -> void:
# Moves the agent's `body` by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
_applied_steering = true
body.apply_central_impulse(GSTUtils.to_vector2(acceleration.linear))
body.apply_torque_impulse(acceleration.angular)

View File

@ -20,7 +20,7 @@ func _init(body: RigidBody) -> void:
# Moves the agent's `body` by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
_applied_steering = true
body.apply_central_impulse(acceleration.linear)
body.apply_torque_impulse(Vector3.UP * acceleration.angular)

View File

@ -36,5 +36,5 @@ var _applied_steering := false
# Moves the agent's body by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
pass