NPCs can now carry objects.
This commit is contained in:
parent
a6c9909f26
commit
c0d3c68fb3
8 changed files with 130 additions and 23 deletions
|
@ -1,16 +1,19 @@
|
|||
## 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 = 5
|
||||
var angry_meter: int = 0
|
||||
var target: Node3D
|
||||
var carrying: bool = false
|
||||
|
||||
# Legend:
|
||||
# 0 - get an objective
|
||||
# 1 - wait
|
||||
# 2 - chase a player
|
||||
# 2 - get carryable object
|
||||
# 3 - chase a player
|
||||
var phase: int = 0
|
||||
|
||||
@onready var timer: Timer = $Timer
|
||||
|
@ -23,7 +26,7 @@ func _ready() -> void:
|
|||
if Networking.isManager:
|
||||
if timer.timeout.connect(_timer_done):
|
||||
pass
|
||||
find_rest()
|
||||
find_objective()
|
||||
|
||||
func _timer_done() -> void:
|
||||
if angry_meter > 0:
|
||||
|
@ -32,12 +35,12 @@ func _timer_done() -> void:
|
|||
($Label3D as Label3D).text = "%s %ds left" % [(target as Objective).displayed, angry_meter]
|
||||
else:
|
||||
($Label3D as Label3D).text = ""
|
||||
(target as Objective).occupied = false
|
||||
timer.stop()
|
||||
phase = 2
|
||||
find_player(99999999999999)
|
||||
|
||||
# determines the closest resting place and navigates to it.
|
||||
func find_rest() -> void:
|
||||
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:
|
||||
|
@ -45,6 +48,15 @@ func find_rest() -> void:
|
|||
|
||||
navigation_agent.set_target_position(target.position)
|
||||
|
||||
# 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():
|
||||
|
@ -53,19 +65,43 @@ func find_player(lowest_distance: float) -> void:
|
|||
|
||||
navigation_agent.set_target_position(target.position)
|
||||
|
||||
func pick_up(node: Carryable) -> void:
|
||||
carrying = true
|
||||
node.get_parent().remove_child(node)
|
||||
node.carry()
|
||||
node.position = Vector3(0, 1, -1)
|
||||
add_child(node)
|
||||
find_player(99999999999)
|
||||
phase = 3
|
||||
|
||||
func set_down(node: Carryable) -> void:
|
||||
carrying = false
|
||||
node.get_parent().remove_child(node)
|
||||
node.uncarry()
|
||||
node.position = position + Vector3(0,0,1)
|
||||
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 == 2:
|
||||
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_rest()
|
||||
find_objective()
|
||||
|
||||
if navigation_agent.is_navigation_finished():
|
||||
if phase == 0:
|
||||
if target is Objective:
|
||||
(target as Objective).occupied = false
|
||||
phase = 1
|
||||
timer.start()
|
||||
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
|
||||
|
@ -73,6 +109,8 @@ func _physics_process(_delta: float) -> void:
|
|||
|
||||
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
|
||||
|
||||
|
|
|
@ -76,6 +76,5 @@ func _input(event: InputEvent) -> void:
|
|||
camera.rotation.x = -1.6
|
||||
rotate(Vector3.DOWN, camera_rotation.x)
|
||||
|
||||
# this way we can check for the player.
|
||||
func playerstub() -> void:
|
||||
func incapacitate() -> void:
|
||||
pass
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue