improvements to ai but still needs a lot of work

This commit is contained in:
derek
2025-04-28 16:32:34 -05:00
parent f0ec7bc953
commit 80cf7ec2c5
8 changed files with 83 additions and 34 deletions

View File

@@ -218,6 +218,7 @@ transform = Transform3D(1, 0, 0, 0, 0.992332, 0.123601, 0, -0.123601, 0.992332,
[node name="BulletRay" type="RayCast3D" parent="Head/Recoil/Camera3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.487738)
visible = false
target_position = Vector3(0, 0, -200)
collision_mask = 105
collide_with_areas = true
@@ -227,6 +228,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -4.482)
[node name="InteractRay" type="RayCast3D" parent="Head/Recoil/Camera3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.00322104, -0.0232438)
visible = false
target_position = Vector3(0, 0, -2.5)
collision_mask = 162
hit_back_faces = false

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=91 format=4 uid="uid://djr7vnr1hcx82"]
[gd_scene load_steps=94 format=4 uid="uid://djr7vnr1hcx82"]
[ext_resource type="Script" uid="uid://cdofgtwevbray" path="res://scripts/spider.gd" id="1_7e7fe"]
[ext_resource type="PackedScene" uid="uid://h5ojldugfyyu" path="res://assets/bullet_enemy.tscn" id="2_aew5r"]
@@ -832,11 +832,26 @@ stream_0/stream = ExtResource("29_nhal8")
stream_1/stream = ExtResource("29_nhal8")
stream_2/stream = ExtResource("29_nhal8")
[sub_resource type="SphereMesh" id="SphereMesh_aasxo"]
radius = 0.1
height = 0.2
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_1b3cb"]
transparency = 1
blend_mode = 1
shading_mode = 0
albedo_color = Color(0, 1, 0, 0.498039)
[sub_resource type="CylinderMesh" id="CylinderMesh_ycipa"]
top_radius = 0.1
bottom_radius = 0.01
[node name="spider" type="CharacterBody3D" node_paths=PackedStringArray("nav_agent", "hit_targets", "visibility_areas", "line_of_sight_ray") groups=["enemy", "persist"]]
collision_layer = 8
collision_mask = 521
up_direction = Vector3(0.0449994, 0.998987, 0)
script = ExtResource("1_7e7fe")
debug_tools = true
nav_agent = NodePath("NavigationAgent3D")
hit_targets = [NodePath("body/backTarget")]
visibility_areas = [NodePath("VisibilityArea")]
@@ -964,6 +979,7 @@ collision_mask = 5
[node name="LineOfSight" type="RayCast3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00161432, 1.62046, 0.00134204)
top_level = true
target_position = Vector3(0, 0, -50)
collision_mask = 5
@@ -1128,6 +1144,21 @@ unit_size = 5.0
[node name="Alarm" type="AudioStreamPlayer3D" parent="AUIDO"]
stream = SubResource("AudioStreamRandomizer_aasxo")
[node name="DebugTracker" type="Node3D" parent="."]
top_level = true
visible = false
[node name="Sphere" type="MeshInstance3D" parent="DebugTracker"]
mesh = SubResource("SphereMesh_aasxo")
skeleton = NodePath("../MeshHandle")
surface_material_override/0 = SubResource("StandardMaterial3D_1b3cb")
[node name="Mesh" type="MeshInstance3D" parent="DebugTracker"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
mesh = SubResource("CylinderMesh_ycipa")
skeleton = NodePath("../..")
surface_material_override/0 = SubResource("StandardMaterial3D_1b3cb")
[connection signal="body_entered" from="body/backTarget" to="body/backTarget" method="_on_body_entered"]
[connection signal="body_part_hit" from="body/backTarget" to="." method="_on_area_3d_body_part_hit"]
[connection signal="timeout" from="Timers/prefire_timer" to="." method="_on_prefire_timer_timeout"]

View File

@@ -194,5 +194,5 @@ surface_material_override/0 = SubResource("StandardMaterial3D_fmyhe")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_776o6")
&"": SubResource("AnimationLibrary_776o6")
}

View File

@@ -22,7 +22,7 @@ func Enter():
alarm_sound.play()
func update_waypoint():
enemy.nav_agent.set_target_position(enemy.character_follow.global_position)
enemy.nav_agent.set_target_position(move_target())
func current_target():
if enemy.is_player_visible():
@@ -65,7 +65,7 @@ func Update(delta):
Transitioned.emit(self,"reload")
func Physics_Update(delta):
enemy.turret_look_next.look_at(enemy.character_follow.global_position)
enemy.turret_look_next.look_at(move_target())
enemy.turret_look.rotation = lerp(enemy.turret_look.rotation,enemy.turret_look_next.rotation,delta * enemy.turret_look_speed)
var destination = enemy.nav_agent.get_next_path_position()

View File

@@ -13,15 +13,6 @@ const WANDER_AMT = 50
const TURRET_TURN_AMT : float = 90.0
const TURRET_SCAN_SPEED : float = .5
func search_point():
enemy.nav_agent.set_target_position(enemy.point_of_interest)
search_time = randf_range(3,10)
func randomize_turret_scan():
scan_direction = -scan_direction
enemy.turret_look_next.rotation = Vector3(0,scan_direction,0)
scan_time = randf_range(5,10)
func Enter():
super()
search_point()
@@ -56,3 +47,12 @@ func Physics_Update(delta : float):
if enemy.turret_look.is_colliding() and enemy.turret_look.get_collider() is Player:
Transitioned.emit(self,"attack")
func search_point():
enemy.nav_agent.set_target_position(move_target())
search_time = randf_range(3,10)
func randomize_turret_scan():
scan_direction = -scan_direction
enemy.turret_look_next.rotation = Vector3(0,scan_direction,0)
scan_time = randf_range(5,10)

View File

@@ -7,19 +7,18 @@ class_name EnemyState
func Update(delta):
if enemy.visibility_areas != null:
for area in enemy.visibility_areas:
var bodies = area.get_overlapping_bodies()
if bodies != null:
for i in bodies:
if i is Player:
enemy.character_follow = i
else:
enemy.character_follow = null
if enemy.character_follow != null:
if can_see:
if enemy.is_player_visible():
enemy.cache_player_pos()
Transitioned.emit(self,"attack")
if can_see:
if enemy.is_player_in_area() and enemy.is_player_visible():
Transitioned.emit(self,"attack")
func move_target():
if enemy.player_last_seen != null:
return enemy.player_last_seen
elif enemy.points_of_interest.size() > 0:
var move_point = {"loudness" : 0.0,"point" : Vector3.ZERO}
for point in enemy.points_of_interest:
if point["loudness"] > move_point["loudness"]:
move_point = point
return move_point["point"]
else:
Transitioned.emit(self,"idle")

View File

@@ -84,7 +84,7 @@ func _process(delta):
cam_target.global_position = lerp(cam_target.global_position,target_pos_adjusted,delta * MOVE_SPEED)
look_ray.global_position = target_pos_adjusted
look_ray.look_at(Vector3(target.global_position), Vector3.UP)
camera.rotation = lerp(camera.rotation,look_ray.rotation,delta * CAMERA_LOOK_SPEED)
camera.rotation = lerp_angle(camera.rotation,look_ray.rotation,delta * CAMERA_LOOK_SPEED)
camera.fov = lerp(camera.fov, 40.0, delta * 5)
1:
#aim at player from enemy

View File

@@ -5,6 +5,7 @@ signal last_enemy_dead()
var player
var last_enemy : bool = false
@export var debug_tools : bool = false
@export var start_health = 3
@export var SPEED = 3.0
@export var turret_look_speed = 6
@@ -60,6 +61,8 @@ $body/bodyoutline]
@onready var health_bar_sprite: Sprite3D = $HealthBarSprite
@onready var servo_single: AudioStreamPlayer3D = $AUIDO/ServoSingle
@onready var servo_loop: AudioStreamPlayer3D = $AUIDO/ServoLoop
@onready var debug_tracker: Node3D = $DebugTracker
@onready var line_of_sight: RayCast3D = $LineOfSight
var gravity = 9.8
@@ -77,9 +80,10 @@ var stunned = false
var health
var ammo
var point_of_interest
var line_of_sight_target
var character_follow
var player_last_seen
var points_of_interest
func _ready():
health = start_health
@@ -97,9 +101,13 @@ func _ready():
target.body_part_hit.connect(take_damage)
func _process(delta):
line_of_sight.global_position = global_position
move_and_slide()
look_at_player()
cache_player_pos()
if debug_tools:
debug_marker()
func stun():
change_state_to("stunned")
@@ -121,8 +129,8 @@ func change_state_to(set_state):
state_machine.on_child_transition(state_machine.current_state,set_state)
func look_at_player():
if character_follow != null:
line_of_sight_ray.look_at(character_follow.global_position)
if line_of_sight_target != null:
line_of_sight_ray.look_at(line_of_sight_target.global_position)
func is_player_visible():
if line_of_sight_ray.is_colliding():
@@ -138,6 +146,8 @@ func is_player_in_area():
if bodies != null:
for i in bodies:
if i is Player:
if line_of_sight_target == null:
line_of_sight_target = i
return true
return false
@@ -145,7 +155,14 @@ func is_player_in_area():
func cache_player_pos():
if line_of_sight_ray.is_colliding():
if line_of_sight_ray.get_collider() is Player:
point_of_interest = line_of_sight_ray.get_collision_point()
player_last_seen = line_of_sight_ray.get_collision_point()
func debug_marker():
if player_last_seen != null:
debug_tracker.visible = true
debug_tracker.global_position = player_last_seen
else:
debug_tracker.visible = false
func save():
var save_dict = {