Now About Social Code
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--addons/fpc/character.tscn1
-rw-r--r--prefabs/enemy.tscn16
-rw-r--r--project.godot1
-rw-r--r--scripts/bsp_level_generator.gd10
-rw-r--r--scripts/dungeon_manager.gd4
-rw-r--r--scripts/enemy.gd86
6 files changed, 57 insertions, 61 deletions
diff --git a/addons/fpc/character.tscn b/addons/fpc/character.tscn
index a43e940..da78a6f 100644
--- a/addons/fpc/character.tscn
+++ b/addons/fpc/character.tscn
@@ -352,6 +352,7 @@ MarginContainer/constants/margin_top = 10
[sub_resource type="SphereShape3D" id="SphereShape3D_k4wwl"]
[node name="Character" type="CharacterBody3D" node_paths=PackedStringArray("HEAD", "CAMERA", "HEADBOB_ANIMATION", "JUMP_ANIMATION", "CROUCH_ANIMATION", "COLLISION_MESH")]
+collision_layer = 5
script = ExtResource("1_0t4e8")
default_reticle = "res://addons/fpc/reticles/reticle_1.tscn"
HEAD = NodePath("Head")
diff --git a/prefabs/enemy.tscn b/prefabs/enemy.tscn
index 71e35f8..415b9b8 100644
--- a/prefabs/enemy.tscn
+++ b/prefabs/enemy.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=5 format=3 uid="uid://cuad3khwmhnsa"]
+[gd_scene load_steps=6 format=3 uid="uid://cuad3khwmhnsa"]
[ext_resource type="Script" path="res://scripts/enemy.gd" id="1_aut2k"]
@@ -8,6 +8,9 @@
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_s5qw4"]
+[sub_resource type="BoxShape3D" id="BoxShape3D_cyd43"]
+size = Vector3(20, 2, 20)
+
[node name="Enemy" type="CharacterBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1.54414, -1.58742)
motion_mode = 1
@@ -28,4 +31,15 @@ wait_time = 0.2
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
+[node name="VisionVolume" type="Area3D" parent="."]
+collision_layer = 4
+collision_mask = 4
+input_ray_pickable = false
+
+[node name="VisionVolumeShape" type="CollisionShape3D" parent="VisionVolume"]
+transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
+shape = SubResource("BoxShape3D_cyd43")
+
[connection signal="timeout" from="HitTimer" to="." method="_on_hit_timer_timeout"]
+[connection signal="body_entered" from="VisionVolume" to="." method="_on_body_entered"]
+[connection signal="body_exited" from="VisionVolume" to="." method="_on_body_exited"]
diff --git a/project.godot b/project.godot
index 14e2e29..568104f 100644
--- a/project.godot
+++ b/project.godot
@@ -62,6 +62,7 @@ attack={
[layer_names]
3d_physics/layer_2="grapple"
+3d_physics/layer_3="player"
[rendering]
diff --git a/scripts/bsp_level_generator.gd b/scripts/bsp_level_generator.gd
index 57778d4..f4191a3 100644
--- a/scripts/bsp_level_generator.gd
+++ b/scripts/bsp_level_generator.gd
@@ -74,11 +74,11 @@ func check_door(grid: Array[Tile.Tile], axis: int, door_pos: Vector2i) -> bool:
var room_right = door_pos
room_right[axis] += 1
- if door_pos.x > grid_width or door_pos.y > grid_height:
+ if door_pos.x >= grid_width or door_pos.y >= grid_height:
return false
- if room_left.x > grid_width or room_left.y > grid_height:
+ if room_left.x >= grid_width or room_left.y >= grid_height:
return false
- if room_right.x > grid_width or room_right.y > grid_height:
+ if room_right.x >= grid_width or room_right.y >= grid_height:
return false
# Check if there are two spaces to connect
@@ -106,9 +106,9 @@ func generate_grid(map: BSPNode, grid: Array[Tile.Tile]) -> void:
var test_door = randi_range(map.min_dims[other_axis], map.max_dims[other_axis] - 1)
var door_pos = Vector2i.ZERO
door_pos[map.axis] = split_axis
- door_pos[other_axis] = test_door + 1
+ door_pos[other_axis] = test_door + 2
var door_pos2 = door_pos
- door_pos[other_axis] += 1
+ door_pos2[other_axis] += 1
if check_door(grid, map.axis, door_pos) and check_door(grid, map.axis, door_pos2):
have_door = true
diff --git a/scripts/dungeon_manager.gd b/scripts/dungeon_manager.gd
index 513fcd6..ec3f5d4 100644
--- a/scripts/dungeon_manager.gd
+++ b/scripts/dungeon_manager.gd
@@ -15,7 +15,9 @@ func create_enemies() -> void:
for i in range(num_enemies):
var enemy = enemy_inst.instantiate()
- enemy.target = $Player
+
+ # Target is controlled through enemy wander AI
+ enemy.target = null
var found_pos = false
while not found_pos:
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