player.gd 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # 玩家控制脚本
  2. extends CharacterBody2D
  3. # 移动速度
  4. @export var speed: float = 200.0
  5. # 当前可交互的NPC
  6. var nearby_npc: Node = null
  7. # 交互状态 (交互时禁用移动)
  8. var is_interacting: bool = false
  9. # 节点引用
  10. @onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
  11. @onready var camera: Camera2D = $Camera2D
  12. # 音效引用 ⭐
  13. @onready var interact_sound: AudioStreamPlayer = null # 交互音效
  14. @onready var running_sound: AudioStreamPlayer = null # 走路音效
  15. # 走路音效状态 ⭐
  16. var is_playing_running_sound: bool = false
  17. func _ready():
  18. # 添加到player组 (重要!NPC需要通过这个组来识别玩家)
  19. add_to_group("player")
  20. # 获取音效节点 (可选,如果不存在也不会报错) ⭐
  21. interact_sound = get_node_or_null("InteractSound")
  22. running_sound = get_node_or_null("RunningSound")
  23. if interact_sound:
  24. print("[INFO] 玩家交互音效已启用")
  25. else:
  26. print("[WARN] 玩家没有InteractSound节点,交互音效已禁用")
  27. if running_sound:
  28. print("[INFO] 玩家走路音效已启用")
  29. else:
  30. print("[WARN] 玩家没有RunningSound节点,走路音效已禁用")
  31. Config.log_info("玩家初始化完成")
  32. # 启用相机
  33. camera.enabled = true
  34. # 播放默认动画
  35. if animated_sprite.sprite_frames != null and animated_sprite.sprite_frames.has_animation("idle"):
  36. animated_sprite.play("idle")
  37. func _physics_process(_delta: float):
  38. # 如果正在交互,禁用移动
  39. if is_interacting:
  40. velocity = Vector2.ZERO
  41. move_and_slide()
  42. # 播放idle动画
  43. if animated_sprite.sprite_frames != null and animated_sprite.sprite_frames.has_animation("idle"):
  44. animated_sprite.play("idle")
  45. # 停止走路音效 ⭐
  46. stop_running_sound()
  47. return
  48. # 获取输入方向
  49. var input_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
  50. # 设置速度
  51. velocity = input_direction * speed
  52. # 移动
  53. move_and_slide()
  54. # 更新动画和朝向
  55. update_animation(input_direction)
  56. # 更新走路音效 ⭐
  57. update_running_sound(input_direction)
  58. func update_animation(direction: Vector2):
  59. """更新角色动画 (支持4方向)"""
  60. if animated_sprite.sprite_frames == null:
  61. return
  62. # 根据移动方向播放动画
  63. if direction.length() > 0:
  64. # 移动中 - 判断主要方向
  65. if abs(direction.x) > abs(direction.y):
  66. # 左右移动
  67. if direction.x > 0:
  68. # 向右
  69. if animated_sprite.sprite_frames.has_animation("walk_right"):
  70. animated_sprite.play("walk_right")
  71. animated_sprite.flip_h = false
  72. elif animated_sprite.sprite_frames.has_animation("walk"):
  73. animated_sprite.play("walk")
  74. animated_sprite.flip_h = false
  75. else:
  76. # 向左
  77. if animated_sprite.sprite_frames.has_animation("walk_left"):
  78. animated_sprite.play("walk_left")
  79. animated_sprite.flip_h = false
  80. elif animated_sprite.sprite_frames.has_animation("walk"):
  81. animated_sprite.play("walk")
  82. animated_sprite.flip_h = true
  83. else:
  84. # 上下移动
  85. if direction.y > 0:
  86. # 向下
  87. if animated_sprite.sprite_frames.has_animation("walk_down"):
  88. animated_sprite.play("walk_down")
  89. elif animated_sprite.sprite_frames.has_animation("walk"):
  90. animated_sprite.play("walk")
  91. else:
  92. # 向上
  93. if animated_sprite.sprite_frames.has_animation("walk_up"):
  94. animated_sprite.play("walk_up")
  95. elif animated_sprite.sprite_frames.has_animation("walk"):
  96. animated_sprite.play("walk")
  97. else:
  98. # 静止
  99. if animated_sprite.sprite_frames.has_animation("idle"):
  100. animated_sprite.play("idle")
  101. func _input(event: InputEvent):
  102. # 按E键与NPC交互
  103. # 检查E键 (KEY_E = 69)
  104. if event is InputEventKey:
  105. if event.pressed and not event.echo:
  106. # 调试: 打印所有按键
  107. print("[DEBUG] 按键: ", event.keycode, " (E=69, Enter=4194309)")
  108. if event.keycode == KEY_E or event.keycode == KEY_ENTER or event.is_action_pressed("ui_accept"):
  109. print("[DEBUG] 检测到E键, nearby_npc=", nearby_npc)
  110. if nearby_npc != null:
  111. interact_with_npc()
  112. print("[INFO] E键触发交互")
  113. else:
  114. print("[WARN] 没有附近的NPC可以交互")
  115. func interact_with_npc():
  116. """与附近的NPC交互"""
  117. if nearby_npc != null:
  118. # 播放交互音效 ⭐
  119. if interact_sound:
  120. interact_sound.play()
  121. Config.log_info("与NPC交互: " + nearby_npc.npc_name)
  122. # 发送信号给对话系统
  123. get_tree().call_group("dialogue_system", "start_dialogue", nearby_npc.npc_name)
  124. func set_nearby_npc(npc: Node):
  125. """设置附近的NPC"""
  126. nearby_npc = npc
  127. if npc != null:
  128. print("[INFO] ✅ 进入NPC范围: ", npc.npc_name)
  129. Config.log_info("进入NPC范围: " + npc.npc_name)
  130. else:
  131. print("[INFO] ❌ 离开NPC范围")
  132. Config.log_info("离开NPC范围")
  133. func get_nearby_npc() -> Node:
  134. """获取附近的NPC"""
  135. return nearby_npc
  136. func set_interacting(interacting: bool):
  137. """设置交互状态"""
  138. is_interacting = interacting
  139. if interacting:
  140. print("[INFO] 🔒 玩家进入交互状态,移动已禁用")
  141. # 停止走路音效 ⭐
  142. stop_running_sound()
  143. else:
  144. print("[INFO] 🔓 玩家退出交互状态,移动已启用")
  145. # ⭐ 更新走路音效
  146. func update_running_sound(direction: Vector2):
  147. """更新走路音效"""
  148. if running_sound == null:
  149. return
  150. # 如果正在移动
  151. if direction.length() > 0:
  152. # 如果音效还没播放,开始播放
  153. if not is_playing_running_sound:
  154. running_sound.play()
  155. is_playing_running_sound = true
  156. print("[INFO] 🎵 开始播放走路音效")
  157. else:
  158. # 如果停止移动,停止音效
  159. stop_running_sound()
  160. # ⭐ 停止走路音效
  161. func stop_running_sound():
  162. """停止走路音效"""
  163. if running_sound and is_playing_running_sound:
  164. running_sound.stop()
  165. is_playing_running_sound = false
  166. print("[INFO] 🔇 停止走路音效")