Now About Social Code
summaryrefslogtreecommitdiff
path: root/scripts/enemy.gd
diff options
context:
space:
mode:
authorLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2024-09-23 19:24:56 +0100
committerLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2024-09-23 19:25:46 +0100
commitb262d5d194036571d0b3adad3c9914ba9dbe0c8b (patch)
tree8b2c97809f05450fa7624c811065f0cb6993b2a6 /scripts/enemy.gd
parentb62062c72a2354ba06b53706d94d3db53c224f12 (diff)
enemy: Add line of sight to AI to look for player
Diffstat (limited to 'scripts/enemy.gd')
-rw-r--r--scripts/enemy.gd86
1 files changed, 32 insertions, 54 deletions
diff --git a/scripts/enemy.gd b/scripts/enemy.gd
index 7060ab2..871145b 100644
--- a/scripts/enemy.gd
+++ b/scripts/enemy.gd
@@ -13,15 +13,13 @@ extends CharacterBody3D
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
-var has_astar: bool = false
-var astar: AStar3D
-var tar_vec: Vector3
var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity")
var original_material: StandardMaterial3D
@onready var red_material = preload("res://models/red_material.tres")
var mesh_ready = false
+var found = false
func _ready() -> void:
if gravity_enabled:
@@ -33,22 +31,6 @@ func _ready() -> void:
func map_changed(_rid):
print("Map changed")
mesh_ready = true
-
-func chase_target(delta: float) -> void:
- var target_pos = target.position
- var direction = (target_pos - position).normalized()
-
- if direction:
- velocity.x = direction.x * speed
- velocity.z = direction.z * speed
- else:
- velocity.x = move_toward(velocity.x, 0, speed)
- velocity.z = move_toward(velocity.z, 0, speed)
-
- if not is_on_floor() and gravity_enabled:
- velocity.y -= gravity * delta
-
- move_and_slide()
func update_target_location(target_location: Vector3):
nav_agent.target_position = target_location
@@ -77,58 +59,54 @@ func chase_nav_mesh(delta: float):
velocity.y = 0
move_and_slide()
+
+func wander() -> void:
+ pass
func _physics_process(delta: float) -> void:
- if target == null:
- return
-
if Input.is_action_pressed("ui_right"):
return
- if use_nav_mesh:
- chase_nav_mesh(delta)
- return
+ if target != null and not found and check_target_visible():
+ found = true
+ print("Found player")
- if not has_astar:
- # Just try to chase player directly
- chase_target(delta)
- return
-
- var target_pos = target.position
- var closest_me = astar.get_closest_point(position)
- var closest_target = astar.get_closest_point(target_pos)
-
- var direction: Vector3
- if closest_me == closest_target:
- direction = (target_pos - position).normalized()
- else:
- var target_path = astar.get_point_path(closest_me, closest_target)
- if len(target_path) >= 2:
- direction = (target_path[1] - position).normalized()
+ if not found:
+ wander()
+ elif found:
+ chase_nav_mesh(delta)
- if direction:
- #velocity.x = move_toward(velocity.x, direction.x * speed, 10.0 * delta)
- #velocity.z = move_toward(velocity.z, direction.z * speed, 10.0 * delta)
- velocity.x = direction.x * speed
- velocity.z = direction.z * speed
- else:
- velocity.x = move_toward(velocity.x, 0, speed)
- velocity.z = move_toward(velocity.z, 0, speed)
+func check_target_visible() -> bool:
+ var space_state = get_world_3d().direct_space_state
+ var starting_pos = global_position
+ var ending_pos = target.global_position + Vector3.UP
+ var query = PhysicsRayQueryParameters3D.create(starting_pos, ending_pos, 0x3, [self.get_rid()])
+ var result = space_state.intersect_ray(query)
+
+ print("Checking player")
+ if result:
+ print("Hit ", result.collider, " Target ", target, " Self ", self)
+
+ if result and result.collider == target:
+ return true
- move_and_slide()
+ return false
func on_hit(_hit_position: Vector3) -> void:
if $HitTimer.is_stopped():
$MeshInstance3D.set_surface_override_material(0, red_material)
$HitTimer.start()
-func _on_level_generator_astar_created(in_astar: AStar3D) -> void:
- astar = in_astar
- has_astar = true
-
func _on_hit_timer_timeout() -> void:
$MeshInstance3D.set_surface_override_material(0, original_material)
$HitTimer.stop()
get_parent().remove_child(self)
queue_free()
+
+func _on_body_entered(body: Node3D) -> void:
+ target = body
+
+func _on_body_exited(body: Node3D) -> void:
+ if body == target and not found:
+ target = null