godot-psd-training/sceen/people/Player.gd

67 lines
1.8 KiB
GDScript3
Raw Normal View History

2024-06-17 13:15:00 +08:00
extends CharacterBody3D
##玩家
class_name Player
@onready var people: Node3D = $people
@onready var state_machine: StateMachine = $StateMachine
##导航
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D
2024-06-18 15:47:23 +08:00
## 运动方式的枚举
enum Exercise_type{
no, #没用运动
navigation, #导航代理
pathFollow, #路径跟随
}
var currentExerciseType :Exercise_type = Exercise_type.no
2024-06-17 13:15:00 +08:00
var speed:float = 4
var stop_distance:float = 1
signal ArriveTargetPos
##设置目标位(寻路方式运动)
func setTargetPos(targetPos:Vector3,stopDistance = 1)-> void:
stop_distance = stopDistance
navigation_agent_3d.target_position = targetPos
navigation_agent_3d.target_position =navigation_agent_3d.get_final_position()
##去目标位
func goToTarget()-> void:
2024-06-18 15:47:23 +08:00
currentExerciseType = Exercise_type.navigation
2024-06-17 13:15:00 +08:00
$NavigationTimer.start()
2024-06-18 15:47:23 +08:00
state_machine.change_state("Walk")
2024-06-17 13:15:00 +08:00
##停止移动
func stopToTarget()-> void:
$NavigationTimer.stop()
state_machine.change_state("Idle")
func _on_navigation_timer_timeout() -> void:
var direction_3d = (navigation_agent_3d.get_next_path_position() - global_position).normalized()
velocity = direction_3d*speed
look_at_from_position(global_position, navigation_agent_3d.get_next_path_position())
if navigation_agent_3d.distance_to_target() > stop_distance :
move_and_slide()
else :
stopToTarget()
ArriveTargetPos.emit()
2024-06-18 15:47:23 +08:00
currentExerciseType = Exercise_type.no
2024-06-17 13:15:00 +08:00
##区域碰撞检测相关
signal AreaEnter(area: Area3D)
signal AreaExit(area: Area3D)
func _on_area_3d_area_entered(area: Area3D) -> void:
AreaEnter.emit(area)
2024-06-18 15:47:23 +08:00
if currentExerciseType == Exercise_type.navigation and area.is_in_group('needStopWalk') :
stopToTarget()
2024-06-17 13:15:00 +08:00
func _on_area_3d_area_exited(area: Area3D) -> void:
AreaExit.emit(area)
2024-06-18 15:47:23 +08:00
if currentExerciseType == Exercise_type.navigation and area.is_in_group('needStopWalk') :
goToTarget()