角色动画
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 8.5 KiB |
@@ -0,0 +1,41 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://cq3awe0wjqsih"
|
||||
path.s3tc="res://.godot/imported/colormap.png-4bd9a802f0fbdd1bc388c729ce936ac0.s3tc.ctex"
|
||||
metadata={
|
||||
"imported_formats": ["s3tc_bptc"],
|
||||
"vram_texture": true
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://character/player/assets/Textures/colormap.png"
|
||||
dest_files=["res://.godot/imported/colormap.png-4bd9a802f0fbdd1bc388c729ce936ac0.s3tc.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=2
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/uastc_level=0
|
||||
compress/rdo_quality_loss=0.0
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=0
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=true
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/channel_remap/red=0
|
||||
process/channel_remap/green=1
|
||||
process/channel_remap/blue=2
|
||||
process/channel_remap/alpha=3
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=0
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 6.5 KiB |
@@ -0,0 +1,41 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://chawqtd3fcydp"
|
||||
path.s3tc="res://.godot/imported/texture_01.png-e765476e37c3bd9342318549a3523e3a.s3tc.ctex"
|
||||
metadata={
|
||||
"imported_formats": ["s3tc_bptc"],
|
||||
"vram_texture": true
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://character/player/assets/Textures/texture_01.png"
|
||||
dest_files=["res://.godot/imported/texture_01.png-e765476e37c3bd9342318549a3523e3a.s3tc.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=2
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/uastc_level=0
|
||||
compress/rdo_quality_loss=0.0
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=0
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=true
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/channel_remap/red=0
|
||||
process/channel_remap/green=1
|
||||
process/channel_remap/blue=2
|
||||
process/channel_remap/alpha=3
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=0
|
||||
Binary file not shown.
@@ -0,0 +1,45 @@
|
||||
[remap]
|
||||
|
||||
importer="scene"
|
||||
importer_version=1
|
||||
type="PackedScene"
|
||||
uid="uid://ccf670c6bkob8"
|
||||
path="res://.godot/imported/character-male-e.glb-9d9c3cbc43d44547b0be72ff662d6ebd.scn"
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://character/player/assets/character-male-e.glb"
|
||||
dest_files=["res://.godot/imported/character-male-e.glb-9d9c3cbc43d44547b0be72ff662d6ebd.scn"]
|
||||
|
||||
[params]
|
||||
|
||||
nodes/root_type=""
|
||||
nodes/root_name=""
|
||||
nodes/root_script=null
|
||||
mesh_library/use_node_names_as_mesh_names=false
|
||||
array_mesh/deduplicate_surfaces=true
|
||||
nodes/apply_root_scale=true
|
||||
nodes/root_scale=1.0
|
||||
nodes/import_as_skeleton_bones=false
|
||||
nodes/use_name_suffixes=true
|
||||
nodes/use_node_type_suffixes=true
|
||||
meshes/ensure_tangents=true
|
||||
meshes/generate_lods=true
|
||||
meshes/create_shadow_meshes=true
|
||||
meshes/light_baking=1
|
||||
meshes/lightmap_texel_size=0.2
|
||||
meshes/force_disable_compression=false
|
||||
skins/use_named_skins=true
|
||||
animation/import=true
|
||||
animation/fps=30
|
||||
animation/trimming=false
|
||||
animation/remove_immutable_tracks=true
|
||||
animation/import_rest_as_RESET=false
|
||||
import_script/path=""
|
||||
materials/extract=0
|
||||
materials/extract_format=0
|
||||
materials/extract_path=""
|
||||
_subresources={}
|
||||
gltf/naming_version=1
|
||||
gltf/embedded_image_handling=1
|
||||
gltf/texture_map_mode=0
|
||||
@@ -0,0 +1,305 @@
|
||||
extends CharacterBody3D
|
||||
## 第三人称控制器(胶囊体占位)
|
||||
##
|
||||
## 场景结构:
|
||||
## Player (CharacterBody3D) ← 只负责移动与碰撞,自身不旋转
|
||||
## ├─ Collision (CollisionShape3D, CapsuleShape3D)
|
||||
## ├─ Body (Node3D) ← 视觉层,朝移动方向转身;换角色模型时挂在它下面
|
||||
## │ └─ character-male-e2 (Node3D)
|
||||
## │ ├─ character-male-e/Skeleton3D ← 骨骼 + 蒙皮网格(character-male-e.glb)
|
||||
## │ └─ AnimationPlayer ← 动作库:idle / walk / sprint / jump / fall
|
||||
## ├─ CameraRig (Node3D) ← 承担偏航 yaw,独立于本体旋转
|
||||
## │ └─ SpringArm (SpringArm3D) ← 承担俯仰 pitch,自动避障
|
||||
## │ └─ Camera (Camera3D)
|
||||
## ├─ Sync (MultiplayerSynchronizer) ← 同步 position、Body.rotation、health、anim_state、anim_speed
|
||||
## └─ HealthLabel (Label3D) ← 头顶血条(节点配置,不是代码建的)
|
||||
##
|
||||
## 操作:WASD/方向键移动,鼠标转视角,Shift 疾跑,空格跳跃,Esc 释放/锁定鼠标
|
||||
|
||||
## 跳跃输入缓冲帧数(输入事件在渲染帧到达,物理帧读取,避免漏跳)
|
||||
const JUMP_BUFFER_FRAMES: int = 6
|
||||
|
||||
## 角色动作状态。权威端每物理帧算一次,再靠 Sync 复制给远端回放。
|
||||
enum AnimState { IDLE, WALK, SPRINT, JUMP, FALL }
|
||||
|
||||
## 状态 → 片段名。名字取自 character-male-e.glb 的动作库,改名要同步改这里。
|
||||
const ANIM_CLIPS: Dictionary = {
|
||||
AnimState.IDLE: "idle",
|
||||
AnimState.WALK: "walk",
|
||||
AnimState.SPRINT: "sprint",
|
||||
AnimState.JUMP: "jump",
|
||||
AnimState.FALL: "fall",
|
||||
}
|
||||
|
||||
## 需要循环的状态。glb 里每个片段的 loop_mode 都是 NONE,
|
||||
## 不补循环的话 idle/walk/sprint 播一遍就定在最后一帧。
|
||||
const ANIM_LOOPED: Array[int] = [AnimState.IDLE, AnimState.WALK, AnimState.SPRINT]
|
||||
|
||||
@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
|
||||
## 疾跑速度(米/秒)
|
||||
@export var sprint_speed: float = 8.5
|
||||
## 地面加速度(米/秒²)
|
||||
@export var acceleration: float = 45.0
|
||||
## 空中加速度(米/秒²)
|
||||
@export var air_acceleration: float = 12.0
|
||||
## 起跳初速度(米/秒)
|
||||
@export var jump_velocity: float = 5.0
|
||||
## 视觉层转身平滑速度
|
||||
@export var turn_speed: float = 10.0
|
||||
|
||||
@export_group("相机")
|
||||
## 鼠标灵敏度(弧度/像素)
|
||||
@export var mouse_sensitivity: float = 0.0025
|
||||
## 最低俯角(向下看)
|
||||
@export var pitch_min_deg: float = -80.0
|
||||
## 最高仰角(向上看)
|
||||
@export var pitch_max_deg: float = 45.0
|
||||
|
||||
@export_group("动画")
|
||||
## 动作切换的交叉淡入时间(秒)。0 为硬切
|
||||
@export var anim_blend_time: float = 0.15
|
||||
## 低于此水平速度就视为站立(米/秒)
|
||||
@export var anim_idle_threshold: float = 0.2
|
||||
## 播放倍率下限:被墙抵住、起步那一瞬,脚步不该慢到像在滑冰
|
||||
@export var anim_speed_scale_min: float = 0.65
|
||||
## 播放倍率上限:防止速度被外力拉高时脚步快到抽搐
|
||||
@export var anim_speed_scale_max: float = 1.6
|
||||
|
||||
@onready var body: Node3D = $Body
|
||||
@onready var camera_rig: Node3D = $CameraRig
|
||||
@onready var spring_arm: SpringArm3D = $CameraRig/SpringArm
|
||||
## 头顶血条是 player.tscn 里的 Label3D 节点(3D 世界里的文字,会跟着角色走)
|
||||
@onready var _health_label: Label3D = $HealthLabel
|
||||
|
||||
@onready var animation_player: AnimationPlayer = $"Body/character-male-e2/AnimationPlayer"
|
||||
|
||||
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
|
||||
|
||||
## 当前动作状态(AnimState)。
|
||||
## 权威端每物理帧写一次,Sync 把它复制给远端 —— 远端物理与输入都关着,
|
||||
## 只有靠这个值才知道该播哪个片段。
|
||||
var anim_state: int = AnimState.IDLE:
|
||||
set(value):
|
||||
if anim_state == value:
|
||||
return
|
||||
anim_state = value
|
||||
_apply_anim_state()
|
||||
|
||||
## 当前播放倍率(1.0 = 片段原速)。
|
||||
## 同样要复制:远端只有 position,算不出真实速度,只能照抄权威端的倍率。
|
||||
var anim_speed: float = 1.0:
|
||||
set(value):
|
||||
value = clampf(value, anim_speed_scale_min, anim_speed_scale_max)
|
||||
anim_speed = value
|
||||
if animation_player != null:
|
||||
animation_player.speed_scale = value
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_report_identity()
|
||||
# 本地端与远端都要跑:本地端播自己的动作,远端靠复制来的 anim_state 回放
|
||||
_prepare_animation()
|
||||
_is_local = is_multiplayer_authority()
|
||||
if not _is_local:
|
||||
# 远端玩家只是一具「回放身体」:
|
||||
# 拆掉相机 —— 否则每多一个玩家就多一台 Camera3D 互相抢渲染;
|
||||
# 关掉输入与物理 —— 否则与权威端双重模拟,表现为抖动、漂移、穿地。
|
||||
# 它的 position 与 Body.rotation 由子节点 Sync 同步过来;
|
||||
# 头顶血条保留 —— health 同样是同步过来的,这样谁都能看到别人还剩多少血。
|
||||
camera_rig.queue_free()
|
||||
set_physics_process(false)
|
||||
set_process_unhandled_input(false)
|
||||
return
|
||||
_yaw = camera_rig.rotation.y
|
||||
_pitch = clampf(spring_arm.rotation.x, deg_to_rad(pitch_min_deg), deg_to_rad(pitch_max_deg))
|
||||
spring_arm.rotation.x = _pitch
|
||||
set_mouse_captured(true)
|
||||
# 同步可能在 _ready() 之前就把 health 写进来了,那时血条还没刷新,这里补一次
|
||||
_refresh_health_label()
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
||||
_yaw -= event.relative.x * mouse_sensitivity
|
||||
_pitch = clampf(
|
||||
_pitch - event.relative.y * mouse_sensitivity,
|
||||
deg_to_rad(pitch_min_deg),
|
||||
deg_to_rad(pitch_max_deg)
|
||||
)
|
||||
camera_rig.rotation.y = _yaw
|
||||
spring_arm.rotation.x = _pitch
|
||||
elif event.is_action_pressed("jump"):
|
||||
_jump_buffer = JUMP_BUFFER_FRAMES
|
||||
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
|
||||
|
||||
|
||||
func set_mouse_captured(captured: bool) -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if captured else Input.MOUSE_MODE_VISIBLE
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if not _is_local:
|
||||
return
|
||||
# --- 重力 ---
|
||||
if is_on_floor():
|
||||
if velocity.y < 0.0:
|
||||
velocity.y = 0.0
|
||||
else:
|
||||
velocity.y -= _gravity * delta
|
||||
|
||||
# --- 跳跃(读取输入缓冲)---
|
||||
if _jump_buffer > 0:
|
||||
_jump_buffer -= 1
|
||||
if is_on_floor():
|
||||
velocity.y = jump_velocity
|
||||
_jump_buffer = 0
|
||||
|
||||
# --- 相对相机的水平输入 ---
|
||||
var input_dir := Input.get_vector("move_left", "move_right", "move_back", "move_forward")
|
||||
var cam_basis := camera_rig.global_transform.basis
|
||||
var wish := cam_basis.x * input_dir.x + (-cam_basis.z) * input_dir.y
|
||||
wish.y = 0.0
|
||||
|
||||
var wants_move := wish.length() > 0.001
|
||||
if wants_move:
|
||||
wish = wish.normalized()
|
||||
# 只转视觉层,不动本体,也不影响相机(Godot 前进方向为 -Z)
|
||||
var target_yaw := atan2(-wish.x, -wish.z)
|
||||
body.rotation.y = lerp_angle(body.rotation.y, target_yaw, clampf(turn_speed * delta, 0.0, 1.0))
|
||||
|
||||
var speed := sprint_speed if Input.is_action_pressed("sprint") else walk_speed
|
||||
if not wants_move:
|
||||
speed = 0.0
|
||||
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)
|
||||
velocity.x = horizontal.x
|
||||
velocity.z = horizontal.y
|
||||
|
||||
move_and_slide()
|
||||
|
||||
_update_animation()
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
## 准备动作库。两个端点都要跑,且必须在本地/远端分支之前。
|
||||
func _prepare_animation() -> void:
|
||||
if animation_player == null:
|
||||
push_warning("%s 下没有 AnimationPlayer,角色不会有动画" % name)
|
||||
return
|
||||
for state in ANIM_LOOPED:
|
||||
var clip: String = ANIM_CLIPS[state]
|
||||
if not animation_player.has_animation(clip):
|
||||
continue
|
||||
animation_player.get_animation(clip).loop_mode = Animation.LOOP_LINEAR
|
||||
# 先站在 idle。之后 state 不变时 setter 不会重进去,所以这一句不能省。
|
||||
animation_player.play(ANIM_CLIPS[AnimState.IDLE], 0.0)
|
||||
|
||||
|
||||
## 权威端每物理帧调用:从本帧的真实运动推出动作状态与播放倍率。
|
||||
## 只读 velocity / is_on_floor(),不写任何物理量。
|
||||
func _update_animation() -> void:
|
||||
var h_speed := Vector2(velocity.x, velocity.z).length()
|
||||
|
||||
var next_state: int
|
||||
if not is_on_floor():
|
||||
# 上升用 jump、越过顶点用 fall。jump 片段长 0.5s,
|
||||
# 恰好约等于 jump_velocity 5 / 重力 9.8 的上升时间。
|
||||
next_state = AnimState.JUMP if velocity.y > 0.0 else AnimState.FALL
|
||||
elif h_speed < anim_idle_threshold:
|
||||
next_state = AnimState.IDLE
|
||||
elif Input.is_action_pressed("sprint"):
|
||||
next_state = AnimState.SPRINT
|
||||
else:
|
||||
next_state = AnimState.WALK
|
||||
|
||||
# 先把倍率写下去,再切片段 —— 顺序反了会让新片段用一帧旧倍率。
|
||||
var ref := _anim_reference_speed(next_state)
|
||||
anim_speed = h_speed / ref if ref > 0.0 else 1.0
|
||||
anim_state = next_state # setter 负责切片段
|
||||
|
||||
|
||||
## 该状态对应的「设计速度」。片段在 ref 速度下按 1.0 倍播放,快慢全靠比例缩。
|
||||
func _anim_reference_speed(state: int) -> float:
|
||||
match state:
|
||||
AnimState.WALK:
|
||||
return walk_speed
|
||||
AnimState.SPRINT:
|
||||
return sprint_speed
|
||||
_:
|
||||
return 0.0
|
||||
|
||||
|
||||
func _apply_anim_state() -> void:
|
||||
if animation_player == null:
|
||||
return
|
||||
var clip: String = ANIM_CLIPS.get(anim_state, "")
|
||||
if clip.is_empty() or not animation_player.has_animation(clip):
|
||||
return
|
||||
animation_player.play(clip, anim_blend_time)
|
||||
|
||||
|
||||
## 关卡 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 "这是【别人】的角色,我只负责回放",
|
||||
])
|
||||
@@ -0,0 +1 @@
|
||||
uid://f5ptp3is4agf
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,18 @@
|
||||
[gd_resource type="SceneReplicationConfig" format=3 uid="uid://c4ubbxjfrrs47"]
|
||||
|
||||
[resource]
|
||||
properties/0/path = NodePath(".:position")
|
||||
properties/0/spawn = true
|
||||
properties/0/replication_mode = 1
|
||||
properties/1/path = NodePath("Body:rotation")
|
||||
properties/1/spawn = true
|
||||
properties/1/replication_mode = 1
|
||||
properties/2/path = NodePath(".:health")
|
||||
properties/2/spawn = true
|
||||
properties/2/replication_mode = 1
|
||||
properties/3/path = NodePath(".:anim_state")
|
||||
properties/3/spawn = true
|
||||
properties/3/replication_mode = 1
|
||||
properties/4/path = NodePath(".:anim_speed")
|
||||
properties/4/spawn = true
|
||||
properties/4/replication_mode = 1
|
||||
Reference in New Issue
Block a user