Files
whale-town-front/scenes/characters/player.gd
xiangwang 9259865e72 feat:替换角色资源并增加基础tilesetlayer
- 增加底纹、草坪、河堤、河、小码头以及公会的tilesetlayer
- 替换角色精灵图为 4 行 4 列格式
- 更新 player.tscn:配置上下左右的 idle 和 walk 动画
- 更新 player.gd:重构动画逻辑,支持四方向判断与播放
2026-01-07 23:07:54 +08:00

67 lines
1.5 KiB
GDScript

extends CharacterBody2D
# 信号定义
signal player_moved(position: Vector2)
# 常量定义
const MOVE_SPEED = 200.0
# 节点引用
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var sprite: Sprite2D = $Sprite2D
var last_direction := "down"
func _ready() -> void:
# 播放初始动画
if animation_player.has_animation("idle"):
animation_player.play("idle")
func _physics_process(delta: float) -> void:
_handle_movement(delta)
func _handle_movement(_delta: float) -> void:
# 获取移动向量 (参考 docs/02-开发规范/输入映射配置.md)
var direction := Input.get_vector(
"move_left", "move_right",
"move_up", "move_down"
)
# 应用移动
if direction != Vector2.ZERO:
velocity = direction * MOVE_SPEED
_update_animation_state(direction)
else:
velocity = Vector2.ZERO
_play_idle_animation()
move_and_slide()
# 发送移动事件 (如果位置发生明显变化)
if velocity.length() > 0:
EventSystem.emit_event(EventNames.PLAYER_MOVED, {
"position": global_position
})
func _update_animation_state(direction: Vector2) -> void:
if not animation_player:
return
# Determine primary direction
if abs(direction.x) > abs(direction.y):
if direction.x > 0:
last_direction = "right"
else:
last_direction = "left"
else:
if direction.y > 0:
last_direction = "down"
else:
last_direction = "up"
animation_player.play("walk_" + last_direction)
func _play_idle_animation() -> void:
if animation_player:
animation_player.play("idle_" + last_direction)