hotel-madness/scripts/entities/npc.gd

80 lines
2.3 KiB
GDScript3
Raw Normal View History

## SPDX-License-Identifier: GPL-3.0-or-later
## Copyright (c) 2024 interstellardevelopment.org
extends CharacterBody3D
var movement_speed: float = 3.0
var angry_meter: int = 5
var target: Node3D
# Legend:
# 0 - get an objective
# 1 - wait
# 2 - 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:
2024-12-12 15:23:16 +01:00
if timer.timeout.connect(_timer_done):
pass
find_rest()
func _timer_done() -> void:
if angry_meter > 0:
angry_meter -= 1
2024-12-13 18:28:06 +01:00
if target is Objective:
($Label3D as Label3D).text = "%s %ds left" % [(target as Objective).displayed, angry_meter]
else:
2024-12-13 18:28:06 +01:00
($Label3D as Label3D).text = ""
timer.stop()
phase = 2
find_player(99999999999999)
# determines the closest resting place and navigates to it.
func find_rest() -> void:
var lowest_distance: float = 99999999999999
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/NPCRestPlaces").get_children():
2024-12-13 18:28:06 +01:00
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 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:
target = child
navigation_agent.set_target_position(target.position)
func _physics_process(_delta: float) -> void:
if Networking.isManager and phase != 1:
if phase == 2:
find_player(position.distance_to(target.position))
elif phase == 0 and target is Player:
find_rest()
if navigation_agent.is_navigation_finished():
if phase == 0:
2024-12-13 18:28:06 +01:00
if target is Objective:
(target as Objective).occupied = false
phase = 1
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
if move_and_slide():
pass
Networking.npc_sync_call(position, rotation, name)