diff --git a/character/player/carry_arms.gd b/character/player/carry_arms.gd deleted file mode 100644 index 59dbec4..0000000 --- a/character/player/carry_arms.gd +++ /dev/null @@ -1,133 +0,0 @@ -@tool -class_name CarryArms -extends Node -## 抱东西时把两条胳膊伸出去抱住物品,走路时人和东西一起晃。 -## -## 模型里没有「抱」这个动作片段,所以这里在动画**之后**叠加一段手臂姿态: -## 只动 arm_l_* / arm_r_* 这几根骨骼,其余骨骼照旧交给 AnimationPlayer。 -## process_priority 设得比播放器大,保证在它写完骨骼之后才覆盖 —— 否则会被动画冲掉。 -## -## 晃动同时作用在两条胳膊和「抱持点」上:物品每帧跟住抱持点,于是东西和人一起晃, -## 不会出现「胳膊在晃、东西钉在手上」的穿帮。 -## -## 控制器只负责说「抱没抱、走多快」,具体摆成什么样全由这里的参数决定。 -## -## 标了 @tool 是为了能在编辑器里跑测试(非 @tool 脚本在编辑器里是 placeholder,方法调不动)。 - -@export_group("接线") -## 骨架。本节点挂在 Body 下,所以要先 .. 回到 Body -@export var skeleton_path: NodePath = ^"../mouse/MouseRig/Skeleton3D" -## 抱持点(Marker3D)。物品就摆在它上面:Body/CarryArms → ../../CameraRig/CarrySocket -@export var socket_path: NodePath = ^"../../CameraRig/CarrySocket" - -@export_group("抱持姿势") -## 大臂前摆(度):把手伸到身前 -@export var shoulder_swing_deg: float = 40.0 -## 大臂内收(度):两只手往中间合,抱住东西 -@export var shoulder_tuck_deg: float = 50.0 -## 大臂侧收(度):绕身体前后轴转,把胳膊从上往下收到身前 -@export var shoulder_roll_deg: float = 0.0 -## 小臂弯曲(度) -@export var elbow_deg: float = 15.0 -## 胳膊沿自身方向拉长的倍数(>1 就是「延伸」) -@export var stretch: float = 1.25 - -@export_group("走路晃动") -## 胳膊晃动幅度(度) -@export var wobble_deg: float = 8.0 -## 抱持点晃动幅度(米)。物品跟着它晃,所以别太大 -@export var wobble_socket: float = 0.035 -## 每秒晃几下 -@export var wobble_hz: float = 1.7 -## 姿态过渡速度(越大越跟手) -@export var blend_speed: float = 8.0 - -var _skeleton: Skeleton3D -var _socket: Node3D -var _socket_rest := Transform3D.IDENTITY -## 手臂骨骼索引,按 [左大臂, 左小臂, 左手, 右大臂, 右小臂, 右手] 的顺序 -var _bones: Array[int] = [] -var _carrying := false -## 姿态权重 0~1:抱起来时涨到 1,放下时退回 0(退回 0 之后就完全交给动画) -var _weight := 0.0 -var _phase := 0.0 -## 当前速度 / 常规走速,用来决定晃多厉害 -var _speed_ratio := 0.0 - - -func _ready() -> void: - # 跑在 AnimationPlayer 之后,否则我们摆的骨骼会被动画覆盖 - process_priority = 100 - _skeleton = get_node_or_null(skeleton_path) as Skeleton3D - if _skeleton == null: - push_warning("%s 找不到骨架(%s),胳膊不会动" % [get_path(), skeleton_path]) - _socket = get_node_or_null(socket_path) as Node3D - if _socket != null: - _socket_rest = _socket.transform - for bone_name in ["arm_l_01", "arm_l_02", "arm_l_03", "arm_r_01", "arm_r_02", "arm_r_03"]: - if _skeleton == null: - break - var index := _skeleton.find_bone(bone_name) - if index < 0: - push_warning("%s 里没有骨骼 %s,胳膊姿势会不完整" % [_skeleton.get_path(), bone_name]) - _bones.append(index) - - -## 控制器每帧告诉它「抱没抱、走多快」。speed_ratio 用「当前水平速度 / 常规走速」。 -func set_carrying(carrying: bool, speed_ratio: float) -> void: - _carrying = carrying - _speed_ratio = speed_ratio - - -func _process(delta: float) -> void: - var target := 1.0 if _carrying else 0.0 - _weight = move_toward(_weight, target, blend_speed * delta) - if _weight <= 0.0: - # 没抱东西:不碰骨骼也不碰抱持点,全部交回给动画 - return - - # 走得越快晃得越猛;站着不动时 phase 不推进,姿态是静的 - var wobble_scale := clampf(_speed_ratio, 0.0, 1.5) - _phase += delta * TAU * wobble_hz * wobble_scale - var wobble := sin(_phase) * wobble_deg * wobble_scale * _weight - - if _socket != null: - # 抱持点跟着晃,物品每帧跟住它 —— 东西和胳膊就同步了 - var bob := sin(_phase) * wobble_socket * wobble_scale * _weight - _socket.transform = _socket_rest.translated_local(Vector3(0.0, bob, 0.0)) - - if _skeleton == null or _bones.size() < 6: - return - # 左右两组,同相位(抱着重物是两只手一起上下),内收方向相反 - for side in 2: - var shoulder := _bones[side * 3] - var elbow := _bones[side * 3 + 1] - var wrist := _bones[side * 3 + 2] - if shoulder < 0: - continue - var mirror := -1.0 if side == 0 else 1.0 - # 参数是**世界方向**:X 负向 = 往前摆,Y = 左右收,Z = 侧收(左右手相反) - _apply_bone(shoulder, Vector3( - -(shoulder_swing_deg + wobble), - shoulder_tuck_deg * mirror, - shoulder_roll_deg * mirror, - )) - # 胳膊沿自身拉长 —— 这就是「可延伸」 - _skeleton.set_bone_pose_scale(shoulder, Vector3(1.0, lerpf(1.0, stretch, _weight), 1.0)) - if elbow >= 0: - _apply_bone(elbow, Vector3(-elbow_deg, 0.0, 0.0)) - if wrist >= 0: - _apply_bone(wrist, Vector3(-elbow_deg * 0.4, 0.0, 0.0)) - - -## 在动画写好的姿态上再叠一个旋转。 -## -## 传进来的角度按**世界方向**理解(往前摆 = 绕 X 轴负向),内部用骨骼自身的静止姿态 -## 把它换算到骨骼本地空间 —— 否则得先猜出每根骨骼本地轴朝哪,换个模型就全错。 -## 叠在动画姿态上是乘法,所以走路原本的摆臂不会被抹掉。 -func _apply_bone(index: int, offset_deg: Vector3) -> void: - var rest_basis := _skeleton.get_bone_rest(index).basis - var world := Basis.from_euler(offset_deg * PI / 180.0) - var local := rest_basis.inverse() * world * rest_basis - var posed := _skeleton.get_bone_pose_rotation(index) - _skeleton.set_bone_pose_rotation(index, posed * local.get_rotation_quaternion()) diff --git a/character/player/carry_arms.gd.uid b/character/player/carry_arms.gd.uid deleted file mode 100644 index a5a6053..0000000 --- a/character/player/carry_arms.gd.uid +++ /dev/null @@ -1 +0,0 @@ -uid://cca1hl0g2bku diff --git a/character/player/player.gd b/character/player/player.gd index 49df9e0..478f624 100644 --- a/character/player/player.gd +++ b/character/player/player.gd @@ -1,51 +1,12 @@ extends CharacterBody3D -## 第一人称控制器:只管「怎么动」—— 输入、移动物理、视角、血条、联机分叉。 -## 表现层(动画)在 Body/AnimationPlayer 上(挂 player_animator.gd),本文件不认识任何片段名。 -## -## 场景结构: -## Player (CharacterBody3D) ← 只负责移动与碰撞,自身不旋转 -## ├─ Collision (CollisionShape3D, CapsuleShape3D) -## ├─ CameraRig (Node3D) ← 眼睛,架在 eye_height 高,承担偏航 yaw -## │ ├─ Camera (Camera3D) ← 承担俯仰 pitch(第一人称,没有弹簧臂) -## │ └─ CarrySocket (Marker3D) ← 抱持点:抱起来的物品就摆在这上面 -## ├─ Body (Node3D) ← 视觉层,始终朝视线方向;换角色模型时挂在它下面 -## │ ├─ mouse (Node3D) ← 模型(mouse.glb):MouseRig/Skeleton3D + 各部位 MeshInstance3D -## │ ├─ AnimationPlayer ← 动作库 + 表现层(挂 player_animator.gd):只读运动、只写动画 -## │ └─ CarryArms (Node) ← 抱东西时的胳膊姿势 + 走路晃动(挂 carry_arms.gd) -## ├─ Sync (MultiplayerSynchronizer) ← 同步 position、Body.rotation、health、动画状态 -## ├─ HealthLabel (Label3D) ← 头顶血条(节点配置,不是代码建的) -## └─ InteractHint (Label3D) ← 交互提示,同样在场景里配好 -## -## 滑索:走到缆下按 E 挂上,沿缆滑向低端,缆上按 S 刹车、空格跳离。 -## 手感参数(速度/刹车/吸附半径)全在 Zipline 组件上,本文件只读不定义 —— 见文件末尾。 -## -## 抱起:左键抱起最近的可抱物品,长按 Q 蓄力、松手扔出去。 -## 谁能被抱由物品自己决定(挂 Carryable 脚本、进 carryable 组),手感参数同样在物品上; -## 本文件只管「找到它、摆到抱持点、扔出去」这三件事。 -## -## 操作:WASD/方向键移动,鼠标转视角,Shift 疾跑,Ctrl 蹲下(蹲着走即潜行),空格跳跃, -## E 滑索,左键抱起,长按 Q 扔出,Esc 释放/锁定鼠标 -## 跳跃输入缓冲帧数(输入事件在渲染帧到达,物理帧读取,避免漏跳) -const JUMP_BUFFER_FRAMES: int = 6 - -@export_group("属性") -## 血量 -@export var health: int = 100: - set(value): - health = clampi(value, 0, 100) - _refresh_health_label() +## 跳跃输入缓冲(秒):按下跳跃后这段时间内若落地,自动补上一次起跳 +const JUMP_BUFFER_TIME: float = 0.1 @export_group("视角") -## 眼睛高度(米)。相机就架在这个高度上 —— 按碰撞胶囊定,跟模型高矮无关。 -@export var eye_height: float = 1.6 -## 第一人称下藏掉自己的身体。**两条胳膊会留下来** —— 抱东西时得看得见自己的手。 -## 想连胳膊也看不见就把这个关掉(那样相机要么在模型里、要么贴着头顶,画面会糊一片)。 -## 只影响本机这一份 —— 远端玩家的模型照常显示。 -@export var hide_own_body: bool = true ## 鼠标灵敏度(弧度/像素) @export var mouse_sensitivity: float = 0.0025 -## 最低俯角(向下看)。第一人称得能抬头低头,所以放到接近 ±90° +## 最低俯角(向下看) @export var pitch_min_deg: float = -89.0 ## 最高仰角(向上看) @export var pitch_max_deg: float = 89.0 @@ -62,108 +23,40 @@ const JUMP_BUFFER_FRAMES: int = 6 ## 起跳初速度(米/秒) @export var jump_velocity: float = 5.0 -@export_group("抱起") -## 多远内的物品能抱起来(米) -@export var pickup_radius: float = 3.0 -## 抱着东西时的移动速度倍率 -@export var carry_speed_scale: float = 0.6 -## 抱着东西时禁止疾跑 -@export var carry_blocks_sprint: bool = true -## 长按 Q 蓄满力要多久(秒) -@export var throw_charge_time: float = 0.8 -## 扔出去时额外叠上玩家自身的速度(跑着扔会飞更远) -@export var throw_inherits_velocity: bool = true -## 等服务器确认抱起的宽限时间(秒)。联机下被别人先抢走时,超时就自动松手 -@export var pickup_confirm_time: float = 0.6 - @onready var body: Node3D = $Body ## 眼睛分两层转:CameraRig 绕竖直轴转(yaw),Camera 原地抬头低头(pitch)。 ## 分开是为了让 pitch 不掺进移动方向 —— 移动只用 rig 的 basis,低头看地也不会走歪。 @onready var camera_rig: Node3D = $CameraRig @onready var camera: Camera3D = $CameraRig/Camera -## 头顶血条是 player.tscn 里的 Label3D 节点(3D 世界里的文字,会跟着角色走) -@onready var _health_label: Label3D = $HealthLabel -## 滑索操作提示,同样在 player.tscn 里配好,默认隐藏 -@onready var _hint: Label3D = $InteractHint -## 抱东西时的胳膊表现层(挂 carry_arms.gd):只收「抱没抱、走多快」。 -## 抱持点(CameraRig/CarrySocket)不在这里引用 —— 物品自己按名字找持有者的抱持点, -## 这样每端算出来的位置都一样,联机不用同步物品位置。 -@onready var _carry_arms: CarryArms = $Body/CarryArms - -## 动画状态机(挂在 Body/AnimationPlayer 上)。只把「运动结果」交给它,也只知道它会自己播动画。 +## 动画状态机。控制器只把「运动结果」交给它,它也只管自己播动画。 @onready var animator: PlayerAnimator = $Body/AnimationPlayer +@onready var interaction_pos: Marker3D = $CameraRig/Camera/InteractionPos +@onready var interaction_ray: RayCast3D = $CameraRig/Camera/InteractionRay var _gravity: float = float(ProjectSettings.get_setting("physics/3d/default_gravity", 9.8)) var _yaw: float = 0.0 var _pitch: float = 0.0 -var _jump_buffer: int = 0 -## 本节点是否由本机玩家操控。由世界节点在生成时设置 authority 决定,见 world/main.gd。 -var _is_local: bool = true - -## 当前挂着的滑索。非 null 时 _physics_process 走缆上分支,常规移动/重力/贴地全部让位。 -## 只在本机权威端写;远端玩家的位置由 Sync 复制过来,跟着缆一起动。 -var _zipline: Zipline = null -## 沿缆的进度参数 t∈[0,1]。缆的几何由 Zipline 负责,这里只记「我走到哪了」。 -var _zip_t: float = 0.0 -## 滑动方向:+1 往 AnchorB、-1 往 AnchorA。永远是往低的那一端 -var _zip_dir: int = 1 -## 沿缆速度(米/秒) -var _zip_speed: float = 0.0 -## 挂上瞬间「人相对缆」的偏移,随后衰减到 0 —— 吸附是滑过去的,不是瞬移 -var _zip_grab_offset: Vector3 = Vector3.ZERO - -## 手上抱着的物品。位置由物品自己跟住抱持点(见 Carryable),玩家不插手 —— -## 这样每端算出来的位置都一样,联机不用同步物品位置。 -var _carried: Carryable = null -## 长按 Q 的蓄力进度 0~1 -var _throw_charge: float = 0.0 -var _throw_charging: bool = false -## 等服务器确认抱起的剩余时间。服务器没给(被别人先抢了)就自己松手 -var _pickup_wait: float = 0.0 +var _jump_buffer: float = 0.0 +# 拾取 +var _pickObj = null +var _pickPower = 10 func _ready() -> void: - _report_identity() - # 动画准备不在这里 —— PlayerAnimator 有自己的 _ready(),而子节点先于本节点 ready - _is_local = is_multiplayer_authority() - if not _is_local: - # 远端玩家只是一具「回放身体」: - # 拆掉相机 —— 否则每多一个玩家就多一台 Camera3D 互相抢渲染; - # 关掉输入与物理 —— 否则与权威端双重模拟,表现为抖动、漂移、穿地。 - # 它的 position 与 Body.rotation 由子节点 Sync 同步过来; - # 头顶血条保留 —— health 同样是同步过来的,这样谁都能看到别人还剩多少血。 + # 远端玩家只是一具「回放身体」: + # 拆掉相机 —— 否则每多一个玩家就多一台 Camera3D 抢渲染; + # 关掉输入与物理 —— 否则与权威端双重模拟,表现为抖动、漂移、穿地。 + # 它的 position 与 Body.rotation 由 Sync 子节点同步;血条由 health 同步。 + if not is_multiplayer_authority(): camera_rig.queue_free() set_physics_process(false) set_process_unhandled_input(false) return - camera_rig.position.y = eye_height - if hide_own_body: - _hide_body_keep_arms() + _yaw = camera_rig.rotation.y _pitch = clampf(camera.rotation.x, deg_to_rad(pitch_min_deg), deg_to_rad(pitch_max_deg)) _apply_look() set_mouse_captured(true) - # 同步可能在 _ready() 之前就把 health 写进来了,那时血条还没刷新,这里补一次 - _refresh_health_label() - - -## 第一人称:藏掉身体,但**把两条胳膊留下** —— 抱东西时得看得见自己的手。 -## 模型各部位是独立 mesh,按名字挑出来即可;名字对不上(换模型了)就退化成整个藏掉。 -func _hide_body_keep_arms() -> void: - var stack: Array[Node] = [body] - var kept := 0 - while not stack.is_empty(): - var node: Node = stack.pop_back() - for child in node.get_children(): - stack.append(child) - var mesh := child as MeshInstance3D - if mesh == null: - continue - var is_arm := str(mesh.name).begins_with("arm_") - mesh.visible = is_arm - kept += 1 if is_arm else 0 - if kept == 0: - body.visible = false func _unhandled_input(event: InputEvent) -> void: @@ -176,40 +69,23 @@ func _unhandled_input(event: InputEvent) -> void: ) _apply_look() elif event.is_action_pressed("jump"): - # 缆上按跳 = 主动脱手,额外给一个向上的小跳,方便够到旁边的平台 - if _zipline != null: - _detach_zipline(true) - else: - _jump_buffer = JUMP_BUFFER_FRAMES - elif event.is_action_pressed("interact"): - if _zipline != null: - _detach_zipline() - else: - _attach_zipline() - elif event.is_action_pressed("carry") and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: - # 鼠标没锁定时左键是用来重新锁定视角的,那种情况留给下面那个分支 - _pick_up() - elif event.is_action_pressed("throw_carry"): - # 按住开始蓄力,松手才真正出手(见 _release_throw) - _throw_charging = _carried != null - _throw_charge = 0.0 - elif event.is_action_released("throw_carry"): - _release_throw() + _jump_buffer = JUMP_BUFFER_TIME elif event.is_action_pressed("ui_cancel"): 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 - + + if event is InputEventMouseButton and event.is_action_pressed("carry"): + if _pickObj == null: + _pickObject() + else: + _removePickObject() func set_mouse_captured(captured: bool) -> void: Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if captured else Input.MOUSE_MODE_VISIBLE -## 把 _yaw / _pitch 落到节点上。鼠标一动就调一次(渲染帧),所以视角是跟手的、不等物理帧。 +## 把 _yaw / _pitch 落到节点上。鼠标一动就调一次(渲染帧),所以视角跟手、不等物理帧。 ## 身体跟着一起转 —— 第一人称里「你看哪,身体就朝哪」,别人看你也是这个朝向。 func _apply_look() -> void: camera_rig.rotation.y = _yaw @@ -218,50 +94,6 @@ func _apply_look() -> void: func _physics_process(delta: float) -> void: - if not _is_local: - return - - # 交互提示:**同一处判定**既决定「显示什么」又决定「按键干什么」, - # 免得出现「提示亮了但按 E 挂不上」这种对不上的情况。 - # 优先级:手上有东西 > 可抱物 > 缆上 > 可挂的缆。 - var nearby_carry := _find_carryable() - var nearby_zip := _find_zipline() - if _carried != null: - _hint.visible = true - _hint.text = "长按 Q 扔出 %d%%" % int(_throw_charge * 100.0) - elif nearby_carry != null: - _hint.visible = true - _hint.text = "左键 抱起" - elif _zipline != null: - _hint.visible = true - _hint.text = "空格 跳离 · E 松手" - elif nearby_zip != null: - _hint.visible = true - _hint.text = "按 E 挂上滑索" - else: - _hint.visible = false - - # 抱着东西:推进 Q 的蓄力。物品位置不归玩家管 —— 它自己跟住抱持点(见 Carryable), - # 每端都这么算,所以联机不用同步物品位置。 - if _carried != null: - if _carried.carrier_peer != multiplayer.get_unique_id(): - # 服务器还没确认,或者被别人先抢走了:给一小段时间,超时就松手 - _pickup_wait -= delta - if _pickup_wait <= 0.0: - _carried = null - elif _throw_charging: - _throw_charge = minf(1.0, _throw_charge + delta / maxf(throw_charge_time, 0.01)) - # 胳膊表现层只收「抱没抱、走多快」,摆成什么样是它自己的事 - _carry_arms.set_carrying( - _carried != null, - Vector2(velocity.x, velocity.z).length() / maxf(walk_speed, 0.01), - ) - - # 挂在缆上时,下面的常规重力/输入/贴地全部让位给缆上运动 - if _zipline != null: - _ride_zipline(delta) - return - # --- 重力 --- if is_on_floor(): if velocity.y < 0.0: @@ -270,37 +102,28 @@ func _physics_process(delta: float) -> void: velocity.y -= _gravity * delta # --- 跳跃(读取输入缓冲)--- - if _jump_buffer > 0: - _jump_buffer -= 1 + if _jump_buffer > 0.0: + _jump_buffer -= delta if is_on_floor(): velocity.y = jump_velocity - _jump_buffer = 0 + _jump_buffer = 0.0 # --- 相对视线方向的水平输入 --- # 只用 rig 的 basis(只有 yaw),低头看地时前进方向不会跟着往下扎。 var input_dir := Input.get_vector("move_left", "move_right", "move_back", "move_forward") var look_basis := camera_rig.global_transform.basis - var wish := look_basis.x * input_dir.x + (-look_basis.z) * input_dir.y + var wish := look_basis.x * input_dir.x - look_basis.z * input_dir.y wish.y = 0.0 - - var wants_move := wish.length() > 0.001 - if wants_move: + if wish.length_squared() > 0.000001: wish = wish.normalized() - # 身体朝向由 _apply_look() 跟着视线走 —— 第一人称不再朝移动方向转身 - var sprinting := Input.is_action_pressed("sprint") - # 抱着东西跑不快:禁疾跑 + 整体降速,参数在 @export 里 - if _carried != null and carry_blocks_sprint: - sprinting = false - var speed := sprint_speed if sprinting else walk_speed - if _carried != null: - speed *= carry_speed_scale - if not wants_move: - speed = 0.0 + # 只有真正按了方向键才允许疾跑(避免原地"疾跑"触发跑动动画) + var sprinting := Input.is_action_pressed("sprint") and input_dir.length_squared() > 0.000001 + var target_speed := sprint_speed if sprinting else walk_speed var accel := acceleration if is_on_floor() else air_acceleration var horizontal := Vector2(velocity.x, velocity.z) - horizontal = horizontal.move_toward(Vector2(wish.x, wish.z) * speed, accel * delta) + horizontal = horizontal.move_toward(Vector2(wish.x, wish.z) * target_speed, accel * delta) velocity.x = horizontal.x velocity.z = horizontal.y @@ -313,193 +136,20 @@ func _physics_process(delta: float) -> void: sprinting, Input.is_action_pressed("crouch"), ) + + # 物体拖动 + if _pickObj != null: + var pickObjPos = _pickObj.global_position + var pickPlacePos = interaction_pos.global_position + _pickObj.set_linear_velocity((pickPlacePos - pickObjPos) * _pickPower) -# ---------------- 抱起 ---------------- -# 三件事:找物品(_find_carryable)、抱起来(_pick_up)、扔出去(_release_throw)。 -# 谁能被抱、抱持偏移、投掷速度全在 Carryable 组件上,这里只读不定义 —— 和滑索一个套路。 +func _pickObject() -> void: + var pickCollider = interaction_ray.get_collider() + print(pickCollider) + if pickCollider != null and pickCollider is RigidBody3D: + print(pickCollider) + _pickObj = pickCollider -## 找最近能抱的物品。够不着、或者已经被人抱着的都不算。 -## 提示文案和左键共用这一套判定,避免「提示亮着却抱不起来」。 -func _find_carryable() -> Carryable: - var best: Carryable = null - var best_distance := pickup_radius - for node in get_tree().get_nodes_in_group(Carryable.GROUP): - var item := node as Carryable - if item == null or not item.can_be_carried(): - continue - var distance := global_position.distance_to(item.global_position) - if distance > best_distance: - continue - best_distance = distance - best = item - return best - - -## 抱起最近的可抱物品。手上已经有东西、或者正挂在缆上都不抱。 -## 实际「谁抱到了」由服务器仲裁(先到先得),这里只是发起请求并乐观地先认领, -## 服务器不给就在 _physics_process 里超时松手。 -func _pick_up() -> void: - if _carried != null or _zipline != null: - return - var item := _find_carryable() - if item == null: - return - _carried = item - _pickup_wait = pickup_confirm_time - if multiplayer.is_server(): - item.request_pickup(multiplayer.get_unique_id()) - else: - item.request_pickup.rpc_id(1, multiplayer.get_unique_id()) - - -## 松手扔出:方向就是视线方向,速度按蓄力插值(轻点也能扔出去,只是近)。 -## 跑着扔会把自己当前的速度也叠上,所以助跑能扔更远。 -func _release_throw() -> void: - var item := _carried - _carried = null - _throw_charging = false - if item == null: - _throw_charge = 0.0 - return - var launch := -camera.global_transform.basis.z * item.throw_speed(_throw_charge) - if throw_inherits_velocity: - launch += velocity - _throw_charge = 0.0 - # 出手速度交给服务器:它收回模拟权,让物品带着这个速度接着飞 - if multiplayer.is_server(): - item.request_release(launch) - else: - item.request_release.rpc_id(1, launch) - - -# ---------------- 滑索 ---------------- -# 拆成三件事:找缆(_find_zipline)、挂上(_attach_zipline)、缆上运动(_ride_zipline)。 -# 缆的几何与手感参数都在 Zipline 组件里,这里只管「人」的状态,参数只读不定义。 - -## 找脚下能挂的缆,找不到返回 null。挂索与「按 E」提示共用同一套判定。 -func _find_zipline() -> Zipline: - var best: Zipline = null - var best_distance := INF - for node in get_tree().get_nodes_in_group(Zipline.GROUP): - var zip := node as Zipline - if zip == null or zip.length <= 0.0: - continue - var info := zip.closest_point(global_position) - # 两种情况都不挂:够不着,或者挂上就没几米可滑(后者会让人刚挂上就被弹下来) - if info.distance > zip.attach_radius or info.remaining < zip.min_ride: - continue - if info.distance < best_distance: - best_distance = info.distance - best = zip - return best - - -## 挂上最近的一条缆,人就近吸附到缆上最近一点。抱着东西时不给挂(会腾不出手)。 -func _attach_zipline() -> void: - if _carried != null: - return - var zip := _find_zipline() - if zip == null: - return - var info := zip.closest_point(global_position) - _zipline = zip - _zip_t = info.t - _zip_dir = info.dir - # 带上入缆时的水平速度,冲上去挂索不会突然一顿 - _zip_speed = maxf(Vector2(velocity.x, velocity.z).length() * 0.5, 2.0) - # 把当前偏移记下来交给 _ride_zipline 逐帧衰减,避免瞬移到缆上 - _zip_grab_offset = global_position - zip.ride_point(_zip_t) - velocity = Vector3.ZERO - - -## 脱离缆索。hop=true 时额外给一个向上的小跳(主动跳离用)。 -func _detach_zipline(hop: bool = false) -> void: - if _zipline == null: - return - # 惯性带走:沿缆方向的速度原样保留,脱手后就是一次抛物线 - velocity = _zipline.tangent(_zip_t) * float(_zip_dir) * _zip_speed - if hop: - velocity += Vector3.UP * 3.0 - _zipline = null - _zip_speed = 0.0 - - -## 缆上运动:沿曲线加速下滑,S 刹车,到头自动脱手。 -## -## 位置直接写 global_position,不走 move_and_slide():悬空缆段上没有需要解算的碰撞, -## 让物理引擎去推反而会和「贴着曲线走」这个硬约束打架。 -## velocity 仍然维护 —— 一是给动画器读,二是脱手后能带着惯性飞出去。 -func _ride_zipline(delta: float) -> void: - var tan_dir := _zipline.tangent(_zip_t) * float(_zip_dir) - # 重力沿缆方向的分量:缆越陡加速越猛,平缆上几乎不加速。 - # 下坡时 tan_dir.y 是负的,所以前面要加负号。 - _zip_speed += -_gravity * _zipline.gravity_scale * tan_dir.y * delta - if Input.is_action_pressed("move_back"): - _zip_speed -= _zipline.brake_accel * delta - _zip_speed = clampf(_zip_speed, 0.0, _zipline.max_speed) - - # t 是曲线参数而不是弧长,但 sag 相对缆长很小时两者差别可以忽略 - var next_t := _zip_t + float(_zip_dir) * _zip_speed / _zipline.length * delta - if next_t <= 0.0 or next_t >= 1.0: - # 到头了:先落到端点再脱手,免得停在离端点还差一截的地方 - _zip_t = clampf(next_t, 0.0, 1.0) - global_position = _zipline.ride_point(_zip_t) - _detach_zipline() - return - - _zip_t = next_t - _zip_grab_offset = _zip_grab_offset.lerp(Vector3.ZERO, clampf(delta / 0.12, 0.0, 1.0)) - global_position = _zipline.ride_point(_zip_t) + _zip_grab_offset - velocity = tan_dir * _zip_speed - # 视线跟着摆到行进方向 —— 否则人是横着飞的,看不见前面要去哪。 - # 速率刻意放慢:玩家自己用鼠标看的视角不会被一下子拽回去。 - _yaw = lerp_angle(_yaw, atan2(-tan_dir.x, -tan_dir.z), clampf(3.0 * delta, 0.0, 1.0)) - _apply_look() - # 缆上没有「落地」概念,交给动画器当下落姿态处理(模型里没有专门的滑索片段) - animator.set_motion(velocity, false, false) - - -func _refresh_health_label() -> void: - # 血条节点在 player.tscn 里(Hud/HealthLabel)。 - # 同步可能在 _ready() 之前就把 health 写进来,那时 _health_label 还是 null,直接跳过。 - if _health_label == null: - return - _health_label.text = "HP %d / 100" % health - - -## 关卡 0 的临时自检:把「我这一端」和「这个节点」分层打印出来。 -## 联机框架调通后可以整段删掉(连同 _ready() 里的那一行调用)。 -func _report_identity() -> void: - var me: int = multiplayer.get_unique_id() - var authority: int = get_multiplayer_authority() - - # 传输层类型:以后换成 Steam 时这里会变成 SteamMultiplayerPeer - var transport := "无" - if multiplayer.multiplayer_peer != null: - transport = multiplayer.multiplayer_peer.get_class() - - # 同步配置:字段为空或未绑定是「远端玩家僵在原地」的头号原因,所以顺手打出来 - var sync := get_node_or_null("Sync") as MultiplayerSynchronizer - var fields := "未绑定" - if sync != null and sync.replication_config != null: - fields = str(sync.replication_config.get_properties()) - - print("""┌─ [节点自检] %s -│ 进程级 我是 peer %d | 服务器?%s | 传输 %s | 我知道的 peer:%s -│ 节点级 全路径 %s -│ 节点名 %s | 归属 peer %d | 归我管?%s -│ 同步 Sync 字段 %s -└─ 结论 %s""" % [ - name, - me, - multiplayer.is_server(), - transport, - multiplayer.get_peers(), - get_path(), - name, - authority, - is_multiplayer_authority(), - fields, - "这是【我操控】的角色" if is_multiplayer_authority() else "这是【别人】的角色,我只负责回放", - ]) +func _removePickObject() -> void: + _pickObj = null diff --git a/character/player/player.tscn b/character/player/player.tscn index dc44e09..b8a7da4 100644 --- a/character/player/player.tscn +++ b/character/player/player.tscn @@ -12,7 +12,6 @@ [ext_resource type="AnimationLibrary" uid="uid://dy8a178vlbsof" path="res://character/player/assets/animations/sneak.glb" id="8_jo6ux"] [ext_resource type="AnimationLibrary" uid="uid://db83440o2cyr7" path="res://character/player/assets/animations/walk.glb" id="9_fkbv7"] [ext_resource type="Script" uid="uid://c6l2fl5jgvr1h" path="res://character/player/face/face_rig.gd" id="12_lqp71"] -[ext_resource type="Script" uid="uid://cca1hl0g2bku" path="res://character/player/carry_arms.gd" id="13_carryarms"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_rkbax"] radius = 0.54541016 @@ -32,8 +31,12 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.6, 0) [node name="Camera" type="Camera3D" parent="CameraRig" unique_id=52294557] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.6066488, -0.11179829) -[node name="CarrySocket" type="Marker3D" parent="CameraRig" unique_id=570180284] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.8, -1) +[node name="InteractionRay" type="RayCast3D" parent="CameraRig/Camera" unique_id=388601766] +transform = Transform3D(1, 0, 0, 0, -4.371139e-08, 1, 0, -1, -4.371139e-08, 0, 0, 0) +target_position = Vector3(0, 0.45, 0) + +[node name="InteractionPos" type="Marker3D" parent="CameraRig/Camera" unique_id=816323614] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.44393378) [node name="Body" type="Node3D" parent="." unique_id=394283421] @@ -51,30 +54,8 @@ libraries/sneak = ExtResource("8_jo6ux") libraries/walk = ExtResource("9_fkbv7") script = ExtResource("3_g20gq") -[node name="CarryArms" type="Node" parent="Body" unique_id=301629134] -process_priority = 100 -script = ExtResource("13_carryarms") - [node name="Sync" type="MultiplayerSynchronizer" parent="." unique_id=1979575210] replication_config = ExtResource("3_wu8e1") -[node name="HealthLabel" type="Label3D" parent="." unique_id=300336561] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.05, 0) -billboard = 1 -no_depth_test = true -text = "HP 100 / 100" -font_size = 64 -outline_size = 8 - -[node name="InteractHint" type="Label3D" parent="." unique_id=1718788495] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.75, 0) -visible = false -billboard = 1 -no_depth_test = true -modulate = Color(1, 0.95, 0.6, 1) -text = "按 E 挂上滑索" -font_size = 48 -outline_size = 8 - [node name="FaceRig" type="Node" parent="." unique_id=792293233] script = ExtResource("12_lqp71")