TinyAdventure/ai/tasks/look_at_location.gd

52 lines
1.8 KiB
GDScript
Raw Normal View History

@tool
extends BTAction
@export var target_location_var: StringName = &"target_location"
var target_position: Vector3 = Vector3.ZERO
var _current_look_direction:Vector3 = Vector3.BACK
var _current_look_angle:float = 0
var _target_look_direction:Vector3 = Vector3.BACK
var _target_look_angle:float = 0
var _look_angle_damper:SpringDamper = SpringDamper.new(0, 2, 0.06, 0.003)
var _agent_npc:NonPlayerCharacter = null
func _generate_name() -> String:
2024-12-28 18:30:23 +01:00
return "LookAtTarget(%s)" % LimboUtility.decorate_var(target_location_var)
func _tick(delta: float) -> Status:
_agent_npc = agent as NonPlayerCharacter
target_position = blackboard.get_var(target_location_var)
update_look_direction(delta)
if absf(_target_look_angle - _current_look_angle) < deg_to_rad(5):
return SUCCESS
return RUNNING
func clamp_current_look_angle() -> void:
if _target_look_angle - _current_look_angle > PI:
_current_look_angle = _current_look_angle + PI * 2
elif _current_look_angle - _target_look_angle > PI:
_current_look_angle = _current_look_angle - PI * 2
func update_look_direction(delta:float) -> void:
_target_look_direction = (target_position - agent.global_position).normalized()
_current_look_direction = agent.basis.z
_current_look_angle = _current_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
clamp_current_look_angle()
_target_look_angle = _target_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
#if _agent_npc and _agent_npc.get_path() == blackboard.get_var("debug_npc_path"): breakpoint
_current_look_angle = _look_angle_damper.calc(_current_look_angle, _target_look_angle, delta)
clamp_current_look_angle()
_current_look_direction = Vector3(sin(-_current_look_angle), 0, cos(-_current_look_angle))
agent.basis = Basis.looking_at(-_current_look_direction)