hotel-madness/scripts/entities/npc.gd

123 lines
3.7 KiB
GDScript3
Raw Normal View History

## SPDX-License-Identifier: GPL-3.0-or-later
## Copyright (c) 2024 interstellardevelopment.org
2024-12-13 21:18:52 +01:00
class_name NPC
extends CharacterBody3D
var movement_speed: float = 3.0
2024-12-13 21:18:52 +01:00
var angry_meter: int = 0
var target: Node3D
2024-12-13 21:18:52 +01:00
var carrying: bool = false
# Legend:
# 0 - get an objective
# 1 - wait
2024-12-13 21:18:52 +01:00
# 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:
2024-12-12 15:23:16 +01:00
if timer.timeout.connect(_timer_done):
pass
2024-12-13 21:18:52 +01:00
find_objective()
func _timer_done() -> void:
if angry_meter > 0:
angry_meter -= 1
2024-12-13 18:28:06 +01:00
if target is Objective:
2024-12-19 14:01:18 +01:00
Networking.npc_text_sync_call("%s %ds left" % [(target as Objective).displayed, angry_meter], self)
else:
2024-12-19 14:01:18 +01:00
Networking.npc_text_sync_call("", self)
2024-12-13 21:18:52 +01:00
(target as Objective).occupied = false
timer.stop()
phase = 2
2024-12-19 14:01:18 +01:00
func set_text(text: String) -> void:
($Label3D as Label3D).text = text
# determines the closest resting place and navigates to it.
2024-12-13 21:18:52 +01:00
func find_objective() -> 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)
2024-12-13 21:18:52 +01:00
# determines the closest resting place and navigates to it.
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:
target = child
navigation_agent.set_target_position(target.position)
2024-12-13 21:18:52 +01:00
func pick_up(node: Carryable) -> void:
carrying = true
node.get_parent().remove_child(node)
node.carry()
node.position = Vector3(0, 1, -1)
2024-12-19 14:01:18 +01:00
node.rotation = Vector3(0, 0, 0)
2024-12-13 21:18:52 +01:00
add_child(node)
find_player(99999999999)
phase = 3
func set_down(node: Carryable) -> void:
carrying = false
node.get_parent().remove_child(node)
node.uncarry()
2024-12-19 14:01:18 +01:00
node.rotation = rotation
node.position += position
2024-12-13 21:18:52 +01:00
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:
2024-12-13 21:18:52 +01:00
if phase == 3:
find_player(position.distance_to(target.position))
2024-12-13 21:18:52 +01:00
elif phase == 2 and target is Objective:
find_carryable()
elif phase == 0 and target is Player:
2024-12-13 21:18:52 +01:00
find_objective()
if navigation_agent.is_navigation_finished():
if phase == 0:
2024-12-13 18:28:06 +01:00
if target is Objective:
2024-12-13 21:18:52 +01:00
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
2024-12-13 21:18:52 +01:00
look_at(Vector3(target.position.x, position.y, target.position.z))
if move_and_slide():
pass
Networking.npc_sync_call(position, rotation, name)