144 lines
4.5 KiB
GDScript
144 lines
4.5 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
|
|
# 4 - leave the building
|
|
var phase: int = 0
|
|
|
|
@onready var timer: Timer = $Timer
|
|
|
|
var skip: bool = false
|
|
|
|
@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
|
|
|
|
func _ready() -> void:
|
|
if ($Area3D as Area3D).body_entered.connect(_on_body_entered):
|
|
pass
|
|
if Networking.isManager:
|
|
if timer.timeout.connect(_timer_done):
|
|
pass
|
|
find_objective()
|
|
|
|
func _on_body_entered(body: Node) -> void:
|
|
if body is Player and target is Objective:
|
|
(body as Player).serve_chance(target as Objective, self)
|
|
|
|
func _timer_done() -> void:
|
|
if angry_meter > 0 and target is Objective:
|
|
angry_meter -= 1
|
|
Networking.npc_text_sync_call("I want a %s! (%ds left)" % [(target as Objective).subtype, angry_meter], target as Objective, self)
|
|
else:
|
|
(target as Objective).occupied = false
|
|
Networking.npc_text_sync_call("", target as Objective, self)
|
|
timer.stop()
|
|
phase = 2
|
|
|
|
func set_text_and_target(text: String, new_target: Objective) -> void:
|
|
target = new_target
|
|
($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 served() -> void:
|
|
set_text_and_target("", null)
|
|
phase = 4
|
|
|
|
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 == null:
|
|
find_carryable()
|
|
elif phase == 0 and target is Player:
|
|
find_objective()
|
|
elif phase == 4 and target == null:
|
|
target = get_tree().root.get_node("/root/"+Game.mapname+"/NPCExit")
|
|
navigation_agent.set_target_position(target.position)
|
|
timer.stop()
|
|
|
|
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()
|
|
if phase == 4:
|
|
Networking.npc_free_sync_call(self)
|
|
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)
|