Skip to content

Commit

Permalink
More progress for being rid of the old stealth system
Browse files Browse the repository at this point in the history
  • Loading branch information
SlashScreen committed Aug 8, 2024
1 parent 0871f12 commit 2abe04a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
11 changes: 9 additions & 2 deletions scripts/ai/Modules/default_stealth_detection.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ extends AIModule
func _update_fsm(data:Dictionary, delta:float) -> void:
for ref_id:StringName in data:
if has_node(NodePath(ref_id)):
pass
(get_node(NodePath(ref_id)) as FSM).update(data[ref_id], delta, _npc._puppet.global_position.distance_to(data[ref_id].last_seen_position))
else:
var fsm := FSM.new()
fsm.name = ref_id
fsm.state_changed.connect(func(s:int) -> void:
_npc.awareness_state_changed.emit(ref_id, s)
)
add_child(fsm)
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:
Expand All @@ -35,9 +39,12 @@ class FSM:
var state:int = UNAWARE
var seek_timer:float = 0.0

signal state_changed(new_state:int)

func update(data:Dictionary, delta:float, dist:float, in_view_cone:bool) -> void:

func update(data:Dictionary, delta:float, dist:float) -> void:
var vis:float = data[&"visibility"]
var in_view_cone:bool = not is_zero_approx(vis)

match state:
UNAWARE, WARY:
Expand Down
21 changes: 12 additions & 9 deletions scripts/ai/Modules/default_threat_response.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const THREAT_LEVEL_GREATER_INTERVAL = 1
# How many levels greater to be considered "significantly stronger".
const THREAT_LEVEL_MUCH_GREATER_INTERVAL = 5

const StealthDetectorModule = preload("default_stealth_detection.gd")

@export_category("Combat info")
## Will this actor initiate combat? [br]
## Peaceful: Will not initiate combat. [br]
Expand Down Expand Up @@ -41,29 +43,30 @@ const THREAT_LEVEL_MUCH_GREATER_INTERVAL = 5

## Thread for an NPC to keep watch when alerted
var vigilant_thread:Thread
## Set to true to stop [memer vigilant_thread]
## Set to true to stop [member vigilant_thread]
var pull_out_of_thread = false


func _ready() -> void:
_npc.perception_transition.connect(_handle_perception_info.bind())
_npc.awareness_state_changed.connect(_handle_perception_info.bind())
_npc.hit_by.connect(func(who): _aggress(SKEntityManager.instance.get_entity(who)))


func _handle_perception_info(what:StringName, transition:String, fsm:PerceptionFSM_Machine) -> void:
func _handle_perception_info(what:StringName, state:int) -> void:
var opinion = _npc.determine_opinion_of(what)
var last_seen:Vector3 = _npc.perception_memory[what].last_seen_position
var below_attack_threshold = (opinion <= attack_threshold) or aggression == 3 # will be below attack threshold by default if frenzied

match transition:
"AwareInvisible":
match state:
StealthDetectorModule.FSM.AWARE_INVISIBLE:
if aggression == 0: # if peaceful
return
# if threat, seek last known position
if below_attack_threshold:
_npc.printe("seek last known position")
_npc.goap_memory["last_seen_position"] = NavPoint.new(fsm.last_seen_world, fsm.last_seen_position) # commit to memory
_npc.goap_memory["last_seen_position"] = NavPoint.new(_npc.parent_entity.world, last_seen) # commit to memory
_npc.add_objective({"enemy_sought" = true}, true, 10) # add goal to seek position
"AwareVisible":
StealthDetectorModule.FSM.AWARE_VISIBLE:
if aggression == 0: # if peaceful
return

Expand All @@ -85,10 +88,10 @@ func _handle_perception_info(what:StringName, transition:String, fsm:PerceptionF
vigilant_thread.start(_stay_vigilant.bind(e))
else:
_npc.printe("needs to attack")
"Lost":
StealthDetectorModule.FSM.WARY:
# may be useless
return
"Unaware":
StealthDetectorModule.FSM.UNAWARE:
if aggression == 0: # if peaceful
return

Expand Down
2 changes: 1 addition & 1 deletion scripts/components/npc_component.gd
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ signal schedule_updated(ev:ScheduleEvent)
## Signal emitted when this NPC enters dialogue.
signal start_dialogue
## Signal emitted when the awareness state changes on an entity. Used for stealth mechanics.
signal awareness_State_changed(ref_id:String, state:int)
signal awareness_state_changed(ref_id:String, state:int)
## Signal emitted when it wants to flee from an entity. Passes ref id of who it is warning.
signal flee(ref_id:String)
## Signal emitted when it hears an audio event.
Expand Down

0 comments on commit 2abe04a

Please sign in to comment.