refactor
This commit is contained in:
52
scenes/entities/npc/npc.gd
Normal file
52
scenes/entities/npc/npc.gd
Normal file
@@ -0,0 +1,52 @@
|
||||
extends StaticBody3D
|
||||
|
||||
@export var profile: NpcProfile
|
||||
@export var profile_id: StringName = &""
|
||||
|
||||
@onready var mesh: MeshInstance3D = $Mesh
|
||||
@onready var label: Label3D = $Label
|
||||
@onready var prompt: Label3D = $Prompt
|
||||
@onready var interact_area: Area3D = $InteractArea
|
||||
|
||||
var nearby_player: Node = null
|
||||
|
||||
func _enter_tree() -> void:
|
||||
set_multiplayer_authority(1)
|
||||
|
||||
func _ready() -> void:
|
||||
add_to_group("npc")
|
||||
if profile == null and profile_id != &"":
|
||||
var path := "res://resources/npcs/%s.tres" % str(profile_id)
|
||||
if ResourceLoader.exists(path):
|
||||
profile = load(path) as NpcProfile
|
||||
if profile == null:
|
||||
profile = NpcProfile.new()
|
||||
profile.display_name = "Villager"
|
||||
profile.lore = "A simple villager."
|
||||
profile.personality = "Friendly, curious."
|
||||
profile.fallback_text = "Hello there!"
|
||||
label.text = profile.display_name
|
||||
prompt.visible = false
|
||||
if profile.color != Color.WHITE:
|
||||
var mat: StandardMaterial3D = StandardMaterial3D.new()
|
||||
mat.albedo_color = profile.color
|
||||
mesh.material_override = mat
|
||||
interact_area.body_entered.connect(_on_player_near)
|
||||
interact_area.body_exited.connect(_on_player_left)
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if nearby_player and nearby_player.is_multiplayer_authority():
|
||||
prompt.visible = true
|
||||
if Input.is_action_just_pressed("interact"):
|
||||
EventBus.dialog_opened.emit(nearby_player, self)
|
||||
else:
|
||||
prompt.visible = false
|
||||
|
||||
func _on_player_near(body: Node) -> void:
|
||||
if body.is_in_group("player"):
|
||||
if body.is_multiplayer_authority():
|
||||
nearby_player = body
|
||||
|
||||
func _on_player_left(body: Node) -> void:
|
||||
if body == nearby_player:
|
||||
nearby_player = null
|
||||
1
scenes/entities/npc/npc.gd.uid
Normal file
1
scenes/entities/npc/npc.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://chul3jysytie2
|
||||
53
scenes/entities/npc/npc.tscn
Normal file
53
scenes/entities/npc/npc.tscn
Normal file
@@ -0,0 +1,53 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://b0npc0001"]
|
||||
|
||||
[ext_resource type="Script" path="res://scenes/entities/npc/npc.gd" id="1"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_1"]
|
||||
height = 1.7
|
||||
radius = 0.35
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_1"]
|
||||
height = 1.7
|
||||
radius = 0.35
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="NpcMat"]
|
||||
albedo_color = Color(0.85, 0.7, 0.5, 1)
|
||||
|
||||
[sub_resource type="SphereShape3D" id="InteractShape"]
|
||||
radius = 2.5
|
||||
|
||||
[node name="Npc" type="StaticBody3D"]
|
||||
collision_layer = 0
|
||||
collision_mask = 0
|
||||
script = ExtResource("1")
|
||||
|
||||
[node name="Collision" type="CollisionShape3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.85, 0)
|
||||
shape = SubResource("CapsuleShape3D_1")
|
||||
|
||||
[node name="Mesh" type="MeshInstance3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.85, 0)
|
||||
mesh = SubResource("CapsuleMesh_1")
|
||||
material_override = SubResource("NpcMat")
|
||||
|
||||
[node name="Label" type="Label3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.2, 0)
|
||||
billboard = 1
|
||||
text = "Villager"
|
||||
font_size = 22
|
||||
outline_size = 3
|
||||
modulate = Color(0.95, 0.85, 0.5, 1)
|
||||
|
||||
[node name="Prompt" type="Label3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.55, 0)
|
||||
billboard = 1
|
||||
text = "[E] Talk"
|
||||
font_size = 16
|
||||
outline_size = 2
|
||||
|
||||
[node name="InteractArea" type="Area3D" parent="."]
|
||||
collision_layer = 0
|
||||
collision_mask = 2
|
||||
|
||||
[node name="InteractShape" type="CollisionShape3D" parent="InteractArea"]
|
||||
shape = SubResource("InteractShape")
|
||||
Reference in New Issue
Block a user