扣血同步

This commit is contained in:
2026-09-22 17:32:42 +08:00
parent 93222895a5
commit 2ecab991f8
3 changed files with 48 additions and 2 deletions
+33
View File
@@ -15,6 +15,13 @@ extends CharacterBody3D
## 跳跃输入缓冲帧数(输入事件在渲染帧到达,物理帧读取,避免漏跳)
const JUMP_BUFFER_FRAMES: int = 6
@export_group("属性")
## 血量
@export var health: int = 100:
set(value):
health = clampi(value, 0, 100)
_refresh_health_label()
@export_group("移动")
## 常规移动速度(米/秒)
@export var walk_speed: float = 5.0
@@ -47,9 +54,16 @@ var _pitch: float = 0.0
var _jump_buffer: int = 0
## 本节点是否由本机玩家操控。由世界节点在生成时设置 authority 决定,见 world/main.gd。
var _is_local: bool = true
# 血条
var _health_label: Label = null
func _ready() -> void:
print("[观察] 我是 %d,这个节点归我管吗?%s,我的名字是 %s"
% [multiplayer.get_unique_id(), is_multiplayer_authority(), name])
# 生成血条
_make_health_label()
_is_local = is_multiplayer_authority()
if not _is_local:
# 远端玩家只是一具「回放身体」:
@@ -82,6 +96,10 @@ func _unhandled_input(event: InputEvent) -> void:
set_mouse_captured(Input.mouse_mode != Input.MOUSE_MODE_CAPTURED)
elif event is InputEventMouseButton and event.pressed and Input.mouse_mode != Input.MOUSE_MODE_CAPTURED:
set_mouse_captured(true)
elif event is InputEventKey and event.pressed and not event.echo and event.keycode == KEY_H:
# 只有权威端能改被同步的属性,别人改的会被覆盖回去
if is_multiplayer_authority():
health -= 10
func set_mouse_captured(captured: bool) -> void:
@@ -129,3 +147,18 @@ func _physics_process(delta: float) -> void:
velocity.z = horizontal.y
move_and_slide()
func _make_health_label() -> void:
var layer := CanvasLayer.new()
_health_label = Label.new()
_health_label.position = Vector2(16, 56)
layer.add_child(_health_label)
add_child(layer)
_refresh_health_label()
func _refresh_health_label() -> void:
# 远端玩家没有血条,_health_label 是 null,这里直接跳过
if _health_label == null:
return
_health_label.text = "HP %d / 100" % health