You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.4 KiB
GDScript

extends Spatial
export(NodePath) var LookAtTarget setget _set_lookAtTarget_path
### Railgun turret entity controller
onready var skeleton = $Armature/Skeleton
onready var turret_yaw_bone = skeleton.find_bone("turret head")
onready var turret_pitch_bone = skeleton.find_bone("vertical joint")
onready var vert_eyes = $VertEyes
onready var turret_eyes = $Armature/Skeleton/turretAttachement/TurretEyes
onready var vision_shape = $Armature/Skeleton/turretAttachement/TurretVision
var first_call: bool = true
var target: Position3D
func _set_lookAtTarget_path(new_value):
# Because get_node doesn't work in the first call, we just want to assign instead.
# This is to get around a issue with NodePaths exposed to the editor.
if first_call:
LookAtTarget = new_value
return
# Assign skeleton_path to whatever value is passed.
LookAtTarget = new_value
if LookAtTarget == null:
return
# Get the node at that location, if there is one.
var temp = get_node(LookAtTarget)
if temp != null:
if temp is Position3D:
target = get_node(LookAtTarget)
return
else:
print("Railgun.gd: Got invalid LookAtTarget: ", LookAtTarget)
return
else:
print("Railgun.gd: Got no LookAtTarget: ", LookAtTarget)
func get_seen_entities() -> Array:
return vision_shape.seen_nodes
func _ready():
pass
func _process(delta):
# Calculating the two points where the vertical joint should point to to aim at the target
if first_call:
first_call = false
if target == null:
_set_lookAtTarget_path(LookAtTarget)
var turret_height_diff = (turret_eyes.global_transform.origin - vert_eyes.global_transform.origin)
vert_eyes.look_at((target.global_transform.origin-turret_height_diff), Vector3.UP)
skeleton.set_bone_pose(turret_pitch_bone, Basis(Vector3.RIGHT, -1*vert_eyes.rotation.x))
var vert_bone_direction = skeleton.get_bone_pose(turret_pitch_bone) * Vector3.UP
var target_pos = skeleton.global_transform.xform_inv(target.global_transform.origin)
var rest = skeleton.get_bone_global_pose(turret_yaw_bone)
rest = rest.looking_at(target_pos, vert_bone_direction)
# Get the rotation euler of the bone and of this node.
#var rest_euler = rest.basis.get_euler()
# Apply additional rotation stored in additional_rotation to the bone.
# Make a new basis with the, potentially, changed euler angles.
#rest.basis = Basis(rest_euler)
skeleton.set_bone_global_pose_override(turret_yaw_bone, rest, 1, true)