From a6c9909f26faa5b182f0807fdfcba571deb1c4b3 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Fri, 13 Dec 2024 18:28:06 +0100 Subject: [PATCH] Reworked the AI Objectives --- scenes/entities/npc.tscn | 6 ++++++ scenes/entities/player.tscn | 7 +++++++ scenes/maps/elements/rest.tscn | 2 +- scenes/maps/testmap.tscn | 5 ++--- scripts/entities/npc.gd | 11 ++++++----- scripts/entities/player.gd | 3 ++- scripts/maps/objectives/hungry.gd | 5 +++++ scripts/maps/{rest.gd => objectives/objective.gd} | 3 ++- 8 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 scripts/maps/objectives/hungry.gd rename scripts/maps/{rest.gd => objectives/objective.gd} (76%) diff --git a/scenes/entities/npc.tscn b/scenes/entities/npc.tscn index 5c4960b..7257ba7 100644 --- a/scenes/entities/npc.tscn +++ b/scenes/entities/npc.tscn @@ -24,3 +24,9 @@ avoidance_enabled = true height = 2.0 [node name="Timer" type="Timer" parent="."] + +[node name="Label3D" type="Label3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.25, 0) +pixel_size = 0.01 +billboard = 1 +no_depth_test = true diff --git a/scenes/entities/player.tscn b/scenes/entities/player.tscn index 4bfb2a4..f2097be 100644 --- a/scenes/entities/player.tscn +++ b/scenes/entities/player.tscn @@ -21,3 +21,10 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) [node name="Label" type="Label" parent="."] offset_right = 40.0 offset_bottom = 23.0 + +[node name="Label3D" type="Label3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.25, 0) +pixel_size = 0.01 +billboard = 1 +no_depth_test = true +text = "SamplePlayer" diff --git a/scenes/maps/elements/rest.tscn b/scenes/maps/elements/rest.tscn index 065de21..4d4e2ff 100644 --- a/scenes/maps/elements/rest.tscn +++ b/scenes/maps/elements/rest.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=3 uid="uid://bn60ywtwfn7bq"] -[ext_resource type="Script" path="res://scripts/maps/rest.gd" id="1_m76x3"] +[ext_resource type="Script" path="res://scripts/maps/objectives/objective.gd" id="1_m76x3"] [node name="Rest" type="Node3D"] script = ExtResource("1_m76x3") diff --git a/scenes/maps/testmap.tscn b/scenes/maps/testmap.tscn index ddc742d..4401a62 100644 --- a/scenes/maps/testmap.tscn +++ b/scenes/maps/testmap.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=9 format=3 uid="uid://cbyee7drds7qu"] [ext_resource type="Script" path="res://scripts/maps/map.gd" id="1_4npcs"] -[ext_resource type="Script" path="res://scripts/maps/rest.gd" id="2_yv1d0"] +[ext_resource type="Script" path="res://scripts/maps/objectives/objective.gd" id="2_yv1d0"] [ext_resource type="PackedScene" uid="uid://bsghm187n6ykx" path="res://scenes/objects/closet.tscn" id="2_yvpvm"] [ext_resource type="PackedScene" uid="uid://cvnjpnvchvakj" path="res://scenes/entities/npc.tscn" id="3_x3gyc"] @@ -23,7 +23,6 @@ script = ExtResource("1_4npcs") [node name="PlayerSpawn" type="Node3D" parent="."] [node name="0" type="Node3D" parent="PlayerSpawn"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -17, 3, 0) [node name="1" type="Node3D" parent="PlayerSpawn"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 16, 4, 0) @@ -55,7 +54,7 @@ environment = SubResource("Environment_pq0iv") transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 10, 2) [node name="Npc1" parent="." instance=ExtResource("3_x3gyc")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 40, 20, 40) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 25, 6, -33) [node name="NavigationRegion3D" type="NavigationRegion3D" parent="."] navigation_mesh = SubResource("NavigationMesh_hqhxb") diff --git a/scripts/entities/npc.gd b/scripts/entities/npc.gd index 27f0a89..82f259b 100644 --- a/scripts/entities/npc.gd +++ b/scripts/entities/npc.gd @@ -28,7 +28,10 @@ func _ready() -> void: func _timer_done() -> void: if angry_meter > 0: angry_meter -= 1 + if target is Objective: + ($Label3D as Label3D).text = "%s %ds left" % [(target as Objective).displayed, angry_meter] else: + ($Label3D as Label3D).text = "" timer.stop() phase = 2 find_player(99999999999999) @@ -37,7 +40,7 @@ func _timer_done() -> void: func find_rest() -> void: var lowest_distance: float = 99999999999999 for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/NPCRestPlaces").get_children(): - if child is Rest and position.distance_to((child as Rest).position) < lowest_distance and !(child as Rest).occupied: + if child is Objective and position.distance_to((child as Objective).position) < lowest_distance and !(child as Objective).occupied: target = child navigation_agent.set_target_position(target.position) @@ -59,8 +62,8 @@ func _physics_process(_delta: float) -> void: if navigation_agent.is_navigation_finished(): if phase == 0: - if target is Rest: - (target as Rest).occupied = false + if target is Objective: + (target as Objective).occupied = false phase = 1 timer.start() return @@ -74,5 +77,3 @@ func _physics_process(_delta: float) -> void: pass Networking.npc_sync_call(position, rotation, name) - - diff --git a/scripts/entities/player.gd b/scripts/entities/player.gd index 8bac497..41ccb57 100644 --- a/scripts/entities/player.gd +++ b/scripts/entities/player.gd @@ -15,11 +15,12 @@ var freecam: bool = false var camera_rotation: Vector2 = Vector2(0,0) func _ready() -> void: + ($Label3D as Label3D).text = name if name == Game.username: activated = true camera.current = true Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) - + ($Label3D as Label3D).hide() func _physics_process(delta: float) -> void: if activated: diff --git a/scripts/maps/objectives/hungry.gd b/scripts/maps/objectives/hungry.gd new file mode 100644 index 0000000..66f79c4 --- /dev/null +++ b/scripts/maps/objectives/hungry.gd @@ -0,0 +1,5 @@ +class_name Hungry +extends Objective + +func _ready() -> void: + displayed = "Hungry" diff --git a/scripts/maps/rest.gd b/scripts/maps/objectives/objective.gd similarity index 76% rename from scripts/maps/rest.gd rename to scripts/maps/objectives/objective.gd index e09d4f3..ffc30ca 100644 --- a/scripts/maps/rest.gd +++ b/scripts/maps/objectives/objective.gd @@ -1,7 +1,8 @@ ## SPDX-License-Identifier: GPL-3.0-or-later ## Copyright (c) 2024 interstellardevelopment.org -class_name Rest +class_name Objective extends Node3D var occupied: bool = false +var displayed: String