Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Footstep system to NPC #262

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions COGITO/Enemies/cogito_basic_enemy.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ enum EnemyState{
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D


#FootstepPlayer variables
@export_group ("Footstep Player")
##Determines if Footsteps are enabled for NPC
@export var footsteps_enabled: bool = true
##Sets Walk volume in dB
@export var walk_volume_db: float = -12
##Sets Walk volume in dB
@export var sprint_volume_db: float = -4
##Determines the footstep occurence frequency for Walking
@export var WIGGLE_ON_WALKING_SPEED: float = 12.0
##Determines the footstep occurence frequency for Sprinting
@export var WIGGLE_ON_SPRINTING_SPEED: float = 16.0

@onready var footstep_player = $FootstepPlayer

var can_play_footstep: bool = true
var wiggle_vector : Vector2 = Vector2.ZERO
var wiggle_index : float = 0.0

func _ready() -> void:
self.add_to_group("Persist") #Adding object to group for persistence
find_cogito_properties()
Expand Down Expand Up @@ -76,13 +95,15 @@ func _physics_process(delta: float) -> void:
pass
EnemyState.DEAD:
pass



if footsteps_enabled:
npc_footsteps(delta)

func handle_chasing(_delta: float):
#Currently just chasing the player. TODO: Change to have a dynamic target.
chase_target = CogitoSceneManager._current_player_node

# This is basicallya a lerped look-at
# This is basically a lerped look-at
_look_at_target_interpolated(chase_target.global_position)

if _target_in_range() and attack_cooldown <= 0:
Expand Down Expand Up @@ -159,6 +180,39 @@ func set_state():
find_cogito_properties()
pass

# NPC Footstep system, adapted from players
func npc_footsteps(delta):
#Need to check if not waiting, as Velocity.length is unreliable here
if not is_waiting:

# Sprinting Case, so using defined number from Chase speed.
# rounded velocity.length used due to tiny speed fluctuations
if round(velocity.length()) >= chase_speed:
wiggle_vector.y = sin(wiggle_index)
wiggle_index += WIGGLE_ON_SPRINTING_SPEED * delta

if can_play_footstep and wiggle_vector.y > 0.9:
footstep_player.volume_db = sprint_volume_db
footstep_player._play_interaction("footstep")
can_play_footstep = false

if !can_play_footstep and wiggle_vector.y < 0.9:
can_play_footstep = true

# Walking Case, so only checks if the NPC has any speed before playing sound
elif velocity.length() >= 0.2:
wiggle_vector.y = sin(wiggle_index)
wiggle_index += WIGGLE_ON_WALKING_SPEED * delta

if can_play_footstep and wiggle_vector.y > 0.9:
footstep_player.volume_db = walk_volume_db
footstep_player._play_interaction("footstep")
can_play_footstep = false

if !can_play_footstep and wiggle_vector.y < 0.9:
can_play_footstep = true



# Function to handle persistence and saving
func save():
Expand Down Expand Up @@ -186,3 +240,4 @@ func _on_body_exited(body: Node) -> void:
# Using this check to only call interactions on other Cogito Objects. #TODO: could be a better check...
if body.has_method("save") and cogito_properties:
cogito_properties.check_for_reaction_timer_interrupt(body)

35 changes: 34 additions & 1 deletion COGITO/Enemies/cogito_basic_enemy.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=14 format=3 uid="uid://dn4e1k7u3suoo"]
[gd_scene load_steps=23 format=3 uid="uid://dn4e1k7u3suoo"]

[ext_resource type="Script" path="res://COGITO/Enemies/cogito_basic_enemy.gd" id="1_0lcvu"]
[ext_resource type="Script" path="res://COGITO/CogitoObjects/cogito_security_camera.gd" id="2_6jysp"]
Expand All @@ -7,6 +7,14 @@
[ext_resource type="PackedScene" uid="uid://bc2hryr610vgo" path="res://COGITO/PackedScenes/simple_particle_puff.tscn" id="5_ni6ul"]
[ext_resource type="Script" path="res://COGITO/Components/HitboxComponent.gd" id="5_qh1ls"]
[ext_resource type="PackedScene" uid="uid://cj0yaeh3yg7tu" path="res://COGITO/Components/Properties/CogitoProperties.tscn" id="7_15n6t"]
[ext_resource type="AudioStream" uid="uid://up2hfhgq1qx6" path="res://COGITO/Assets/Audio/Kenney/Footsteps/footstep00.ogg" id="8_vyi83"]
[ext_resource type="AudioStream" uid="uid://crj07wq4oocwi" path="res://COGITO/Assets/Audio/Kenney/Footsteps/footstep01.ogg" id="9_5fsuu"]
[ext_resource type="AudioStream" uid="uid://dewyukd562k37" path="res://COGITO/Assets/Audio/Kenney/Footsteps/footstep02.ogg" id="10_ab7fy"]
[ext_resource type="AudioStream" uid="uid://c5kfw4o57foju" path="res://COGITO/Assets/Audio/Kenney/Footsteps/footstep03.ogg" id="11_jlhg4"]
[ext_resource type="AudioStream" uid="uid://b4mt1tuxo2144" path="res://COGITO/Assets/Audio/Kenney/Footsteps/footstep04.ogg" id="12_3m7c4"]
[ext_resource type="Script" path="res://COGITO/DynamicFootstepSystem/Scripts/footstep_surface_detector.gd" id="13_mhpd1"]
[ext_resource type="AudioStream" uid="uid://dc03jiw2a6y3j" path="res://COGITO/DynamicFootstepSystem/FootstepProfiles/generic_footstep_profile.tres" id="14_kpkpk"]
[ext_resource type="Resource" uid="uid://ca0q2t6w08ubh" path="res://COGITO/DynamicFootstepSystem/FootstepMaterialLibrary/sample_footstep_material_library.tres" id="15_vx81w"]

[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_p3jhp"]
height = 1.8
Expand All @@ -30,7 +38,21 @@ size = Vector3(0.5, 0.32, 0.5)
custom_solver_bias = 1.0
size = Vector3(4, 4, 4)

[sub_resource type="AudioStreamRandomizer" id="AudioStreamRandomizer_hnplu"]
streams_count = 5
stream_0/stream = ExtResource("8_vyi83")
stream_0/weight = 1.0
stream_1/stream = ExtResource("9_5fsuu")
stream_1/weight = 1.0
stream_2/stream = ExtResource("10_ab7fy")
stream_2/weight = 1.0
stream_3/stream = ExtResource("11_jlhg4")
stream_3/weight = 1.0
stream_4/stream = ExtResource("12_3m7c4")
stream_4/weight = 1.0

[node name="CogitoEnemy" type="CharacterBody3D"]
motion_mode = 1
script = ExtResource("1_0lcvu")
attack_stagger = 20.0

Expand Down Expand Up @@ -100,5 +122,16 @@ elemental_properties = 2
material_properties = 1
burn_damage_amount = 1.0

[node name="FootstepPlayer" type="AudioStreamPlayer3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.0020133, 0)
stream = SubResource("AudioStreamRandomizer_hnplu")
max_polyphony = 2
bus = &"SFX"
script = ExtResource("13_mhpd1")
generic_fallback_footstep_profile = ExtResource("14_kpkpk")
footstep_material_library = ExtResource("15_vx81w")
generic_fallback_landing_profile = ExtResource("14_kpkpk")
landing_material_library = ExtResource("15_vx81w")

[connection signal="object_detected" from="SecurityCamera" to="." method="switch_to_chasing"]
[connection signal="object_no_longer_detected" from="SecurityCamera" to="." method="switch_to_patrolling"]