godot-psd-training/sceen/people/peopleWalkFollow.gd
2024-07-25 17:05:28 +08:00

72 lines
2.1 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extends Node3D
##玩家
class_name PlayerPathFollow
@onready var player: Player = $Player
@onready var path_3d: Path3D = $Path3D
##生成时候的全局位置坐标用于之后player偏移量计算
var buildPos = Vector3()
##默认是在A区域生成
var generateArea = 'A'
var speed:float = 1
#生成时候的id
var id = 'A0'
##设置路线点坐标
func setPathPoints(points : Array,angle :Vector3 = Vector3()) -> void:
var curve = Curve3D.new()
for index in range(points.size()):
if angle != null and index ==0 :
curve.add_point(points[index],Vector3(),angle)
if index > 0 :
curve.add_point(points[index])
path_3d.curve = curve
$Path3D/PathFollow3D.progress = 0
##沿路线走
func peopleBeginWalk(state = "Walk") -> void:
player.currentExerciseType = player.Exercise_type.pathFollow
if not $Path3D/PathFollowTimer.is_inside_tree() :
return
$Path3D/PathFollowTimer.start()
$Player.state_machine.change_state(state)
##沿路线停
func peopleStopWalk()-> void:
$Path3D/PathFollowTimer.stop()
$Player.state_machine.change_state("Idle")
func _on_path_follow_timer_timeout() -> void:
var lastProgress = $Path3D/PathFollow3D.progress
$Path3D/PathFollow3D.progress += 0.068 * speed
if lastProgress == $Path3D/PathFollow3D.progress and !$Path3D/PathFollow3D.loop :
peopleStopWalk()
player.ArriveTargetPos.emit()
player.currentExerciseType = player.Exercise_type.no
##不旋转角度
func playerRotate(rotate:bool)-> void:
$Path3D/PathFollow3D/RemoteTransform3D.update_rotation = rotate
func _on_player_area_enter(area: Area3D) -> void:
if player.currentExerciseType == player.Exercise_type.pathFollow and (area.is_in_group('needStopWalk') or area.is_in_group('player') ) and player.frontPeople == 1 :
peopleStopWalk()
var waitTimeWalkAgain = 0.2
func _on_player_area_exit(area: Area3D) -> void:
if player.currentExerciseType == player.Exercise_type.pathFollow and (area.is_in_group('needStopWalk') or area.is_in_group('player')) :
await get_tree().create_timer(waitTimeWalkAgain).timeout
peopleBeginWalk()
##特定区域改变运动
signal peopleInDesignatedAreaChangeMoveSignal(areaName:String,peopleWalkFollow:PlayerPathFollow)