抱起物品
This commit is contained in:
+133
-11
@@ -6,18 +6,25 @@ extends CharacterBody3D
|
||||
## Player (CharacterBody3D) ← 只负责移动与碰撞,自身不旋转
|
||||
## ├─ Collision (CollisionShape3D, CapsuleShape3D)
|
||||
## ├─ CameraRig (Node3D) ← 眼睛,架在 eye_height 高,承担偏航 yaw
|
||||
## │ └─ Camera (Camera3D) ← 承担俯仰 pitch(第一人称,没有弹簧臂)
|
||||
## │ ├─ Camera (Camera3D) ← 承担俯仰 pitch(第一人称,没有弹簧臂)
|
||||
## │ └─ CarrySocket (Marker3D) ← 抱持点:抱起来的物品就摆在这上面
|
||||
## ├─ Body (Node3D) ← 视觉层,始终朝视线方向;换角色模型时挂在它下面
|
||||
## │ ├─ mouse (Node3D) ← 模型(mouse.glb):MouseRig/Skeleton3D + 各部位 MeshInstance3D
|
||||
## │ └─ AnimationPlayer ← 动作库 + 表现层(挂 player_animator.gd):只读运动、只写动画
|
||||
## │ ├─ AnimationPlayer ← 动作库 + 表现层(挂 player_animator.gd):只读运动、只写动画
|
||||
## │ └─ CarryArms (Node) ← 抱东西时的胳膊姿势 + 走路晃动(挂 carry_arms.gd)
|
||||
## ├─ Sync (MultiplayerSynchronizer) ← 同步 position、Body.rotation、health、动画状态
|
||||
## ├─ HealthLabel (Label3D) ← 头顶血条(节点配置,不是代码建的)
|
||||
## └─ InteractHint (Label3D) ← 靠近滑索时的「按 E」提示,同样在场景里配好
|
||||
## └─ InteractHint (Label3D) ← 交互提示,同样在场景里配好
|
||||
##
|
||||
## 滑索:走到缆下按 E 挂上,沿缆滑向低端,缆上按 S 刹车、空格跳离。
|
||||
## 手感参数(速度/刹车/吸附半径)全在 Zipline 组件上,本文件只读不定义 —— 见文件末尾。
|
||||
##
|
||||
## 操作:WASD/方向键移动,鼠标转视角,Shift 疾跑,Ctrl 蹲下(蹲着走即潜行),空格跳跃,E 滑索,Esc 释放/锁定鼠标
|
||||
## 抱起:左键抱起最近的可抱物品,长按 Q 蓄力、松手扔出去。
|
||||
## 谁能被抱由物品自己决定(挂 Carryable 脚本、进 carryable 组),手感参数同样在物品上;
|
||||
## 本文件只管「找到它、摆到抱持点、扔出去」这三件事。
|
||||
##
|
||||
## 操作:WASD/方向键移动,鼠标转视角,Shift 疾跑,Ctrl 蹲下(蹲着走即潜行),空格跳跃,
|
||||
## E 滑索,左键抱起,长按 Q 扔出,Esc 释放/锁定鼠标
|
||||
|
||||
## 跳跃输入缓冲帧数(输入事件在渲染帧到达,物理帧读取,避免漏跳)
|
||||
const JUMP_BUFFER_FRAMES: int = 6
|
||||
@@ -55,6 +62,18 @@ 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
|
||||
|
||||
@onready var body: Node3D = $Body
|
||||
## 眼睛分两层转:CameraRig 绕竖直轴转(yaw),Camera 原地抬头低头(pitch)。
|
||||
## 分开是为了让 pitch 不掺进移动方向 —— 移动只用 rig 的 basis,低头看地也不会走歪。
|
||||
@@ -64,6 +83,10 @@ const JUMP_BUFFER_FRAMES: int = 6
|
||||
@onready var _health_label: Label3D = $HealthLabel
|
||||
## 滑索操作提示,同样在 player.tscn 里配好,默认隐藏
|
||||
@onready var _hint: Label3D = $InteractHint
|
||||
## 抱持点:抱起来的物品每物理帧摆到它上面
|
||||
@onready var _carry_socket: Node3D = $CameraRig/CarrySocket
|
||||
## 抱东西时的胳膊表现层(挂 carry_arms.gd):只收「抱没抱、走多快」
|
||||
@onready var _carry_arms: CarryArms = $Body/CarryArms
|
||||
|
||||
## 动画状态机(挂在 Body/AnimationPlayer 上)。只把「运动结果」交给它,也只知道它会自己播动画。
|
||||
@onready var animator: PlayerAnimator = $Body/AnimationPlayer
|
||||
@@ -87,6 +110,13 @@ var _zip_speed: float = 0.0
|
||||
## 挂上瞬间「人相对缆」的偏移,随后衰减到 0 —— 吸附是滑过去的,不是瞬移
|
||||
var _zip_grab_offset: Vector3 = Vector3.ZERO
|
||||
|
||||
## 手上抱着的物品。非 null 时每物理帧把它摆到抱持点上。
|
||||
## 只在权威端写;远端玩家的物品位置由物品自己的 Sync 复制过来。
|
||||
var _carried: Carryable = null
|
||||
## 长按 Q 的蓄力进度 0~1
|
||||
var _throw_charge: float = 0.0
|
||||
var _throw_charging: bool = false
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_report_identity()
|
||||
@@ -133,6 +163,15 @@ func _unhandled_input(event: InputEvent) -> void:
|
||||
_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()
|
||||
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:
|
||||
@@ -159,11 +198,37 @@ func _physics_process(delta: float) -> void:
|
||||
if not _is_local:
|
||||
return
|
||||
|
||||
# 靠近缆时给个操作提示。判定与 _attach_zipline 共用 _find_zipline(),
|
||||
# 交互提示:**同一处判定**既决定「显示什么」又决定「按键干什么」,
|
||||
# 免得出现「提示亮了但按 E 挂不上」这种对不上的情况。
|
||||
_hint.visible = _zipline != null or _find_zipline() != null
|
||||
if _hint.visible:
|
||||
_hint.text = "空格 跳离 · E 松手" if _zipline != null else "按 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 的蓄力。
|
||||
# 物品的位置由持有者写 —— 远端玩家的那份由物品自己的 Sync 复制过去。
|
||||
if _carried != null:
|
||||
_carried.apply_hold_transform(_carry_socket)
|
||||
if _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:
|
||||
@@ -196,7 +261,13 @@ func _physics_process(delta: float) -> void:
|
||||
wish = wish.normalized()
|
||||
# 身体朝向由 _apply_look() 跟着视线走 —— 第一人称不再朝移动方向转身
|
||||
|
||||
var speed := sprint_speed if Input.is_action_pressed("sprint") else walk_speed
|
||||
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 accel := acceleration if is_on_floor() else air_acceleration
|
||||
@@ -212,11 +283,60 @@ func _physics_process(delta: float) -> void:
|
||||
animator.set_motion(
|
||||
velocity,
|
||||
is_on_floor(),
|
||||
Input.is_action_pressed("sprint"),
|
||||
sprinting,
|
||||
Input.is_action_pressed("crouch"),
|
||||
)
|
||||
|
||||
|
||||
# ---------------- 抱起 ----------------
|
||||
# 三件事:找物品(_find_carryable)、抱起来(_pick_up)、扔出去(_release_throw)。
|
||||
# 谁能被抱、抱持偏移、投掷速度全在 Carryable 组件上,这里只读不定义 —— 和滑索一个套路。
|
||||
|
||||
## 找最近能抱的物品。够不着、或者已经被人抱着的都不算。
|
||||
## 提示文案和左键共用这一套判定,避免「提示亮着却抱不起来」。
|
||||
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
|
||||
|
||||
|
||||
## 抱起最近的可抱物品。手上已经有东西、或者正挂在缆上都不抱。
|
||||
func _pick_up() -> void:
|
||||
if _carried != null or _zipline != null:
|
||||
return
|
||||
var item := _find_carryable()
|
||||
if item == null:
|
||||
return
|
||||
item.begin_carry(self)
|
||||
item.apply_hold_transform(_carry_socket)
|
||||
_carried = item
|
||||
|
||||
|
||||
## 松手扔出:方向就是视线方向,速度按蓄力插值(轻点也能扔出去,只是近)。
|
||||
## 跑着扔会把自己当前的速度也叠上,所以助跑能扔更远。
|
||||
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
|
||||
item.end_carry(launch)
|
||||
|
||||
|
||||
# ---------------- 滑索 ----------------
|
||||
# 拆成三件事:找缆(_find_zipline)、挂上(_attach_zipline)、缆上运动(_ride_zipline)。
|
||||
# 缆的几何与手感参数都在 Zipline 组件里,这里只管「人」的状态,参数只读不定义。
|
||||
@@ -239,8 +359,10 @@ func _find_zipline() -> Zipline:
|
||||
return best
|
||||
|
||||
|
||||
## 挂上最近的一条缆,人就近吸附到缆上最近一点。
|
||||
## 挂上最近的一条缆,人就近吸附到缆上最近一点。抱着东西时不给挂(会腾不出手)。
|
||||
func _attach_zipline() -> void:
|
||||
if _carried != null:
|
||||
return
|
||||
var zip := _find_zipline()
|
||||
if zip == null:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user