Files
gaim/npc.gd
T

113 lines
2.9 KiB
GDScript

extends CharacterBody3D
class_name NPC_interactable
@export var speed = 3.0
# set direction seperate for each npc instance
@export var direction := Vector3()
var turning = false
var tmp_dir : Vector3
var tmp_rotation : Vector3
var rot_y = 0
var facing_interacter = false
@export var prompt_message = ""
@export var dialogue = "res://Interact/dialogue/test_dialogue.dialogue"
@export var talkable = false
@export var dissapear_after_dialogoue = false
@export var game_balloon = "/game_balloons/game_balloon_v_2.tscn"
@export var alienID = 0
signal dialogue_finished
func _physics_process(delta: float) -> void:
# move the character based on its set direction
velocity.x = speed * direction.x
velocity.z = speed * direction.z
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
move_and_slide()
if $RayCast3D.is_colliding() and not turning:
turn_around()
if $RayCast3D2.is_colliding() and not turning:
turn_half()
func turn_around():
turning = true
var dir = direction
var turn_tween = create_tween()
turn_tween.tween_property(self, "rotation_degrees", Vector3(0, 180, 0), 0.6).as_relative()
await get_tree().create_timer(0.6).timeout
direction.x = dir.x * -1
direction.z = dir.z * -1
turning = false
func turn_half():
turning = true
var dir = direction
var turn_tween = create_tween()
turn_tween.tween_property(self, "rotation_degrees", Vector3(0, 90, 0), 0.6).as_relative()
await get_tree().create_timer(0.6).timeout
direction.x = dir.z
direction.z = -dir.x
turning = false
func stop_walking(body: Node3D):
#stop moving
if direction != Vector3.ZERO:
tmp_dir = direction
direction = Vector3.ZERO
func resume_walking():
self.rotate(Vector3(0,1,0), deg_to_rad(-rot_y))
print(rot_y)
rot_y = 0
facing_interacter = false
direction.x = tmp_dir.x
direction.z = tmp_dir.z
func turn_to_face_interacter(body: Node3D):
if not facing_interacter:
# turn to face player
tmp_rotation = self.rotation_degrees
var turn_tween = create_tween()
rot_y = -(self.rotation_degrees.y - body.rotation_degrees.y)
print(self.rotation_degrees.y, ' ', body.rotation_degrees.y, ' ', rot_y)
turn_tween.tween_property(self, "rotation_degrees", Vector3(0, rot_y, 0), 0.2).as_relative()
facing_interacter = true
func _on_interaction_checker_body_entered(body: Node3D) -> void:
print(body.name + " is here")
stop_walking(body)
# if alienID == 99:
# $helper.wave()
func _on_interaction_checker_body_exited(body: Node3D) -> void:
print(body.name + " is gone, resume walking")
resume_walking()
if alienID == 99:
$helper.play_idle()
func dialogue_ended() -> void:
dialogue_finished.emit()
DialogueState.setAlienFlag(alienID)
if dissapear_after_dialogoue:
print("disappear")
if $CollisionShape3D:
$CollisionShape3D.disabled = true
await get_tree().create_timer(1.0).timeout
queue_free()
if alienID == 99:
$helper.play_idle()