hotel-madness/scripts/entities/npc.gd

127 lines
3.9 KiB
GDScript

## SPDX-License-Identifier: GPL-3.0-or-later
## Copyright (c) 2024 interstellardevelopment.org
class_name NPC
extends CharacterBody3D
var movement_speed: float = 3.0
var angry_meter: int = 0
var target: Node3D
var carried_object: Carryable
var carrying: bool = false
# Legend:
# 0 - get an objective
# 1 - wait
# 2 - get carryable object
# 3 - chase a player
var phase: int = 0
@onready var timer: Timer = $Timer
var skip: bool = false
@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
func _ready() -> void:
if Networking.isManager:
if timer.timeout.connect(_timer_done):
pass
find_objective()
func _timer_done() -> void:
if angry_meter > 0:
angry_meter -= 1
if target is Objective:
Networking.npc_text_sync_call("%s %ds left" % [(target as Objective).displayed, angry_meter], self)
else:
Networking.npc_text_sync_call("", self)
(target as Objective).occupied = false
timer.stop()
phase = 2
func set_text(text: String) -> void:
($Label3D as Label3D).text = text
# determines the closest resting place and navigates to it.
#TODO: Desynchronizes the path if there is an obstacle.
func find_objective() -> void:
var lowest_distance: float = 99999999999999
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/NPCRestPlaces").get_children():
if child is Objective and position.distance_to((child as Objective).position) < lowest_distance and !(child as Objective).occupied:
target = child
navigation_agent.set_target_position(target.position)
# determines the closest resting place and navigates to it.
#TODO: Desynchronizes the path if the box is moved.
func find_carryable() -> void:
var lowest_distance: float = 99999999999999
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/NPCCarryables").get_children():
if child is Carryable and position.distance_to((child as Carryable).position) < lowest_distance and !(child as Carryable).carried:
target = child
navigation_agent.set_target_position(target.position)
# determines the closest player and navigates to them.
func find_player(lowest_distance: float) -> void:
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/").get_children():
if child is Player and position.distance_to((child as Player).position) < lowest_distance and !(child as Player).incapacitated:
target = child
navigation_agent.set_target_position(target.position)
func pick_up(node: Carryable) -> void:
carrying = true
carried_object = node
node.get_parent().remove_child(node)
node.carry()
node.position = Vector3(0, 1, -1)
node.rotation = Vector3(0, 0, 0)
add_child(node)
find_player(99999999999)
phase = 3
func set_down(node: Carryable) -> void:
carrying = false
carried_object = null
node.get_parent().remove_child(node)
node.uncarry()
node.rotation = rotation
node.position += position
get_tree().root.get_node("/root/"+Game.mapname+"/NPCCarryables").add_child(node)
find_player(99999999999)
phase = 0
func _physics_process(_delta: float) -> void:
if Networking.isManager and phase != 1:
if phase == 3:
find_player(position.distance_to(target.position))
elif phase == 2 and target is Objective:
find_carryable()
elif phase == 0 and target is Player:
find_objective()
if navigation_agent.is_navigation_finished():
if phase == 0:
if target is Objective:
if !(target as Objective).occupied:
find_objective()
(target as Objective).occupied = true
phase = 1
angry_meter = (target as Objective).time + 1
_timer_done()
timer.start()
return
var current_agent_position: Vector3 = global_position
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
velocity = current_agent_position.direction_to(next_path_position) * movement_speed
look_at(Vector3(target.position.x, position.y, target.position.z))
if move_and_slide():
pass
Networking.npc_sync_call(position, rotation, name)