diff --git a/sceen/Area3D/ScreenDoorWaitArea.gd b/sceen/Area3D/ScreenDoorWaitArea.gd new file mode 100644 index 0000000..555f2dc --- /dev/null +++ b/sceen/Area3D/ScreenDoorWaitArea.gd @@ -0,0 +1,18 @@ +extends Area3D + +var waitPeopleCount = 0 + +signal ScreenDoorAreaEnter(curentArea: Area3D,peopleWalkFollow: PlayerPathFollow) + +func _on_area_entered(area: Area3D) -> void: + var peopleWalkFollow = area.get_parent().get_parent() + if peopleWalkFollow is PlayerPathFollow : + waitPeopleCount += 1 + ScreenDoorAreaEnter.emit(self,peopleWalkFollow) + + + +func _on_area_exited(area: Area3D) -> void: + var peopleWalkFollow = area.get_parent().get_parent() + if peopleWalkFollow is PlayerPathFollow : + waitPeopleCount -= 1 diff --git a/sceen/Area3D/ScreenDoorWaitArea.tscn b/sceen/Area3D/ScreenDoorWaitArea.tscn new file mode 100644 index 0000000..7211a63 --- /dev/null +++ b/sceen/Area3D/ScreenDoorWaitArea.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=3 uid="uid://bylj0hfttmmqf"] + +[ext_resource type="Script" path="res://sceen/Area3D/ScreenDoorWaitArea.gd" id="1_4a0gs"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_np60e"] +size = Vector3(1.6, 0.095, 10) + +[node name="ScreenDoorWaitArea" type="Area3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.1, 5.93628) +script = ExtResource("1_4a0gs") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.187478) +shape = SubResource("BoxShape3D_np60e") + +[connection signal="area_entered" from="." to="." method="_on_area_entered"] +[connection signal="area_exited" from="." to="." method="_on_area_exited"] diff --git a/sceen/people/Player.gd b/sceen/people/Player.gd index 0825f68..351040c 100644 --- a/sceen/people/Player.gd +++ b/sceen/people/Player.gd @@ -8,6 +8,15 @@ class_name Player ##导航 @onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D +## 运动方式的枚举 +enum Exercise_type{ + no, #没用运动 + navigation, #导航代理 + pathFollow, #路径跟随 +} + +var currentExerciseType :Exercise_type = Exercise_type.no + var speed:float = 4 var stop_distance:float = 1 @@ -20,7 +29,9 @@ func setTargetPos(targetPos:Vector3,stopDistance = 1)-> void: ##去目标位 func goToTarget()-> void: + currentExerciseType = Exercise_type.navigation $NavigationTimer.start() + state_machine.change_state("Walk") ##停止移动 func stopToTarget()-> void: @@ -33,10 +44,10 @@ func _on_navigation_timer_timeout() -> void: 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() - state_machine.change_state("Walk") else : stopToTarget() ArriveTargetPos.emit() + currentExerciseType = Exercise_type.no ##区域碰撞检测相关 @@ -46,7 +57,10 @@ signal AreaExit(area: Area3D) func _on_area_3d_area_entered(area: Area3D) -> void: AreaEnter.emit(area) - + if currentExerciseType == Exercise_type.navigation and area.is_in_group('needStopWalk') : + stopToTarget() func _on_area_3d_area_exited(area: Area3D) -> void: AreaExit.emit(area) + if currentExerciseType == Exercise_type.navigation and area.is_in_group('needStopWalk') : + goToTarget() diff --git a/sceen/people/peopleWalkFollow.gd b/sceen/people/peopleWalkFollow.gd index d570150..fd4059c 100644 --- a/sceen/people/peopleWalkFollow.gd +++ b/sceen/people/peopleWalkFollow.gd @@ -7,6 +7,9 @@ class_name PlayerPathFollow @onready var path_3d: Path3D = $Path3D +##生成时候的全局位置坐标,用于之后player偏移量计算 +var buildPos = Vector3() + ##设置路线点坐标 func setPathPoints(points : Array,angle :Vector3 = Vector3()) -> void: var curve = Curve3D.new() @@ -20,6 +23,7 @@ func setPathPoints(points : Array,angle :Vector3 = Vector3()) -> void: ##沿路线走 func peopleBeginWalk() -> void: + player.currentExerciseType = player.Exercise_type.pathFollow $Path3D/PathFollowTimer.start() $Player.state_machine.change_state("Walk") @@ -34,13 +38,14 @@ func _on_path_follow_timer_timeout() -> void: if lastProgress == $Path3D/PathFollow3D.progress and !$Path3D/PathFollow3D.loop : peopleStopWalk() player.ArriveTargetPos.emit() + player.currentExerciseType = player.Exercise_type.no func _on_player_area_enter(area: Area3D) -> void: - if area.is_in_group('needStopWalk') or area.is_in_group('player'): + if player.currentExerciseType == player.Exercise_type.pathFollow and (area.is_in_group('needStopWalk') or area.is_in_group('player')): peopleStopWalk() func _on_player_area_exit(area: Area3D) -> void: - if area.is_in_group('needStopWalk') or area.is_in_group('player') : + if player.currentExerciseType == player.Exercise_type.pathFollow and (area.is_in_group('needStopWalk') or area.is_in_group('player')) : peopleBeginWalk() diff --git a/util/ArrayUtils.gd b/util/ArrayUtils.gd new file mode 100644 index 0000000..d1f9f34 --- /dev/null +++ b/util/ArrayUtils.gd @@ -0,0 +1,13 @@ +extends Object + + +static func isEmpty(array: Array) -> bool: + return array == null or array.size() == 0 + +##求数组中的最小值 +static func findArrayMin(array: Array) : + var min = array[0] + for value in array : + if value < min : + min = value + return min