2020-02-06 22:29:48 +01:00
|
|
|
# A specialized steering agent that updates itself every frame so the user does
|
|
|
|
# not have to using a RigidBody2D
|
2020-04-03 02:31:59 +02:00
|
|
|
# @category - Specialized agents
|
2020-02-11 18:33:25 +01:00
|
|
|
extends GSAISpecializedAgent
|
|
|
|
class_name GSAIRigidBody2DAgent
|
2020-02-06 22:29:48 +01:00
|
|
|
|
|
|
|
# The RigidBody2D to keep track of
|
|
|
|
var body: RigidBody2D setget _set_body
|
|
|
|
|
|
|
|
var _last_position: Vector2
|
2020-02-20 16:58:00 +01:00
|
|
|
var _body_ref: WeakRef
|
2020-02-06 22:29:48 +01:00
|
|
|
|
|
|
|
|
2020-02-11 20:36:06 +01:00
|
|
|
func _init(_body: RigidBody2D) -> void:
|
|
|
|
if not _body.is_inside_tree():
|
|
|
|
yield(_body, "ready")
|
2020-05-24 19:00:40 +02:00
|
|
|
self.body = _body
|
2020-02-14 17:35:18 +01:00
|
|
|
|
2020-02-20 16:58:00 +01:00
|
|
|
# warning-ignore:return_value_discarded
|
|
|
|
_body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
|
2020-02-06 22:29:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Moves the agent's `body` by target `acceleration`.
|
2020-04-03 02:31:59 +02:00
|
|
|
# @tags - virtual
|
2020-02-11 20:36:06 +01:00
|
|
|
func _apply_steering(acceleration: GSAITargetAcceleration, _delta: float) -> void:
|
2020-02-20 16:58:00 +01:00
|
|
|
var _body: RigidBody2D = _body_ref.get_ref()
|
|
|
|
if not _body:
|
|
|
|
return
|
2020-05-08 18:40:48 +02:00
|
|
|
|
2020-02-06 22:29:48 +01:00
|
|
|
_applied_steering = true
|
2020-02-20 16:58:00 +01:00
|
|
|
_body.apply_central_impulse(GSAIUtils.to_vector2(acceleration.linear))
|
|
|
|
_body.apply_torque_impulse(acceleration.angular)
|
2020-02-06 22:29:48 +01:00
|
|
|
if calculate_velocities:
|
2020-02-20 16:58:00 +01:00
|
|
|
linear_velocity = GSAIUtils.to_vector3(_body.linear_velocity)
|
|
|
|
angular_velocity = _body.angular_velocity
|
2020-02-06 22:29:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _set_body(value: RigidBody2D) -> void:
|
2020-05-08 18:40:48 +02:00
|
|
|
body = value
|
2020-02-20 16:58:00 +01:00
|
|
|
_body_ref = weakref(value)
|
2020-02-14 17:35:18 +01:00
|
|
|
|
2020-02-20 16:58:00 +01:00
|
|
|
_last_position = value.global_position
|
|
|
|
_last_orientation = value.rotation
|
2020-02-14 17:35:18 +01:00
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
position = GSAIUtils.to_vector3(_last_position)
|
2020-02-06 22:29:48 +01:00
|
|
|
orientation = _last_orientation
|
|
|
|
|
|
|
|
|
|
|
|
func _on_SceneTree_frame() -> void:
|
2020-02-20 16:58:00 +01:00
|
|
|
var _body: RigidBody2D = _body_ref.get_ref()
|
|
|
|
if not _body:
|
|
|
|
return
|
2020-05-08 18:40:48 +02:00
|
|
|
|
|
|
|
var current_position := _body.global_position
|
|
|
|
var current_orientation := _body.rotation
|
2020-02-14 17:35:18 +01:00
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
position = GSAIUtils.to_vector3(current_position)
|
2020-02-06 22:29:48 +01:00
|
|
|
orientation = current_orientation
|
2020-02-14 17:35:18 +01:00
|
|
|
|
2020-02-06 22:29:48 +01:00
|
|
|
if calculate_velocities:
|
|
|
|
if _applied_steering:
|
|
|
|
_applied_steering = false
|
|
|
|
else:
|
2020-05-08 18:40:48 +02:00
|
|
|
linear_velocity = GSAIUtils.to_vector3(_body.linear_velocity)
|
|
|
|
angular_velocity = _body.angular_velocity
|