Skip to content

Commit

Permalink
wrapping up stealth
Browse files Browse the repository at this point in the history
  • Loading branch information
SlashScreen committed Aug 17, 2024
1 parent 2abe04a commit 2995923
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 24 deletions.
30 changes: 23 additions & 7 deletions npc_template.tscn
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
[gd_scene load_steps=6 format=3 uid="uid://dj6sa6ksct1xi"]
[gd_scene load_steps=8 format=3 uid="uid://dj6sa6ksct1xi"]

[ext_resource type="Script" path="res://addons/skelerealms/scripts/ai/perception_eyes.gd" id="2_fvcq2"]
[ext_resource type="Script" path="res://addons/skelerealms/scripts/puppets/npc_puppet.gd" id="2_fw0xe"]
[ext_resource type="PackedScene" uid="uid://itfouqmshnrx" path="res://addons/skelerealms/light_probe.tscn" id="3_64vvy"]
[ext_resource type="Script" path="res://scripts/stealth/npc_eyes.gd" id="2_qscej"]
[ext_resource type="Script" path="res://scripts/cone_shape_3d.gd" id="3_7cfmb"]
[ext_resource type="PackedScene" uid="uid://wvu1t6men2oh" path="res://scripts/stealth/detector.tscn" id="4_midro"]

[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_utii8"]

[sub_resource type="CapsuleMesh" id="CapsuleMesh_s5g5c"]

[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_uo6sh"]
points = PackedVector3Array(0, 0, 0, 24, 32, 0, 16.9706, 32, 16.9706, 1.46958e-15, 32, 24, -16.9706, 32, 16.9706, -24, 32, 2.93915e-15, -16.9706, 32, -16.9706, -4.40873e-15, 32, -24, 16.9706, 32, -16.9706)
script = ExtResource("3_7cfmb")
radius = 24.0
height_multiplier = 1.0
width_multiplier = 1.0
height = 32.0
resolution = 8

[node name="CharacterBody3D" type="CharacterBody3D"]
collision_mask = 17
script = ExtResource("2_fw0xe")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
Expand All @@ -24,8 +35,13 @@ path_desired_distance = 0.5
target_desired_distance = 0.5
avoidance_enabled = true

[node name="EyesPerception" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0.534157)
script = ExtResource("2_fvcq2")
[node name="Eyes" type="Area3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.44687, 0)
collision_mask = 16
script = ExtResource("2_qscej")

[node name="CollisionShape3D" type="CollisionShape3D" parent="Eyes"]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0)
shape = SubResource("ConvexPolygonShape3D_uo6sh")

[node name="Probe" parent="EyesPerception" instance=ExtResource("3_64vvy")]
[node name="Detector" parent="Eyes" instance=ExtResource("4_midro")]
14 changes: 7 additions & 7 deletions scripts/ai/Modules/default_stealth_detection.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ extends AIModule
@export var view_dist:float = 30


func _process(delta: float) -> void:
_update_fsm(_npc.perception_memory, delta)


func _update_fsm(data:Dictionary, delta:float) -> void:
for ref_id:StringName in data:
if has_node(NodePath(ref_id)):
Expand All @@ -18,10 +22,6 @@ func _update_fsm(data:Dictionary, delta:float) -> void:
fsm.update(data[ref_id], delta, _npc._puppet.global_position.distance_to(data[ref_id].last_seen_position))


func in_view(vis:float, dist:float) -> bool:
return dist <= view_dist * vis


class FSM:
extends Node

Expand All @@ -34,10 +34,10 @@ class FSM:
}


const LOSE_TIMER := 120.0
const LOSE_TIMER_MAX := 120.0

var state:int = UNAWARE
var seek_timer:float = 0.0
var seek_timer:float = INF

signal state_changed(new_state:int)

Expand All @@ -53,7 +53,7 @@ class FSM:
AWARE_VISIBLE:
if is_zero_approx(vis):
state = AWARE_INVISIBLE
seek_timer = LOSE_TIMER
seek_timer = LOSE_TIMER_MAX
AWARE_INVISIBLE:
if not is_zero_approx(vis):
state = AWARE_VISIBLE
Expand Down
45 changes: 38 additions & 7 deletions scripts/components/npc_component.gd
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,22 @@ static func get_npc_component(id:StringName) -> NPCComponent:
return null


#* PERCPETION
#region perception


## Wrapper for stealth providers' get_visible_objects. Empty if there is no puppet.
## See the docs section on stealth providers for more info.
func get_visible_objects() -> Dictionary:
if _puppet == null:
return {}
if _puppet.eyes == null:
return {}
return _puppet.eyes.get_visible_objects()


#endregion perception

#* ### OVERRIDES
#region overrides


func _init() -> void:
Expand Down Expand Up @@ -248,7 +251,26 @@ func _process(delta):
_next_point() # get next point
parent_entity.world = _current_target_point.world # set world
parent_entity.position = parent_entity.position.move_toward(_current_target_point.position, delta * _walk_speed) # move towards position


if _puppet:
if _puppet.eyes:
var d:Dictionary = _puppet.eyes.get_visible_objects()
for obj:Object in d:
if obj is not Node:
continue
var e:SKEntity = SkeleRealmsGlobal.get_entity_in_tree(obj)
if not e:
continue

if perception_memory.has(e.name):
perception_memory[e.name][&"visibility"] = d[obj][&"visibility"]
perception_memory[e.name][&"last_seen_position"] = d[obj][&"last_seen_position"]
else:
perception_memory[e.name] = {
&"visibility": d[obj][&"visibility"],
&"last_seen_position": d[obj][&"last_seen_position"],
}

updated.emit(delta)


Expand All @@ -266,7 +288,9 @@ func _exit_tree() -> void:
m._clean_up()


#* ### DIALOGUE AND INTERACTIVITY
#endregion overrides

#region dialogue


## Make this NPC Leave dialogue.
Expand All @@ -292,7 +316,9 @@ func remove_from_conversation() -> void:
removed_from_conversation.emit()


#* ### PATHFINDING
#endregion dialogue

#region pathfinding


## Calculate this NPC's path to a [NavPoint].
Expand Down Expand Up @@ -372,7 +398,9 @@ func remove_objective_by_goals(goals:Dictionary) -> void:
_goap_component.remove_objective_by_goals(goals)


#* ### SCHEDULE
#endregion pathfinding

#region schedule


## Ask this NPC to go to its schedule point.
Expand Down Expand Up @@ -414,7 +442,9 @@ func _calculate_new_schedule() -> void:
schedule_updated.emit(null)


#* ### MISC
#endregion schedule

#region misc


## Get a relationship this NPC has of [RelationshipType]. Pass in the type's key. Returns the relationship if found, none if none found.
Expand Down Expand Up @@ -513,6 +543,7 @@ func get_translated_name() -> String:
else:
return t

#endregion misc

## Current simulation level for an NPC.
enum SimulationLevel {
Expand Down
2 changes: 1 addition & 1 deletion scripts/puppets/npc_puppet.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extends CharacterBody3D

@onready var movement_target_position: Vector3 = position # No world because this agent only works in the scene.
## This is a stealth provider. See the "Sealth Provider" article i nthe documentation for details.
var eyes:Object
@export var eyes:Node
var npc_component:NPCComponent
var puppeteer:PuppetSpawnerComponent
var view_dir:ViewDirectionComponent
Expand Down
5 changes: 5 additions & 0 deletions scripts/system/world_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ func load_world(wid:String) -> void:
GameInfo.game_loading.emit(wid)
GameInfo.is_loading = true
await get_tree().process_frame
print("processed frame. Unloading world...")
_unload_world()
# Spawn waiting thread
print("spawned waiting thread")
ResourceLoader.load_threaded_request(world_paths[wid], "PackedScene", true)
if load_check_thread.is_started():
load_check_thread.wait_to_finish()
load_check_thread = Thread.new()
print("Atarting thread")
load_check_thread.start(_load_check_thread.bind(world_paths[wid]))


Expand All @@ -57,10 +60,12 @@ func _load_check_thread(path:String) -> void:
# wait until done
var prog = []
var last_progress = 0
print("waiting for load to finish.")
while not ResourceLoader.load_threaded_get_status(path, prog) == ResourceLoader.THREAD_LOAD_LOADED:
if not last_progress == prog[0]:
(func(): load_scene_progess_updated.emit(prog[0])).call_deferred()
last_progress = prog[0]
print("Finishing up...")
_finish_load.call_deferred(ResourceLoader.load_threaded_get(path) as PackedScene)


Expand Down
2 changes: 0 additions & 2 deletions scripts/world_objects/world_entity.gd
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,4 @@ func _sync() -> void:
e.position = global_position
e.world = EditorInterface.get_edited_scene_root().name

print(e.world)

entity.pack(e)

0 comments on commit 2995923

Please sign in to comment.