大客流--人物运动控制调整

This commit is contained in:
joylink_zhaoerwei 2024-06-18 15:47:23 +08:00
parent e30dce43c8
commit 9921d7e4c5
5 changed files with 71 additions and 4 deletions

View File

@ -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

View File

@ -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"]

View File

@ -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()

View File

@ -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()

13
util/ArrayUtils.gd Normal file
View File

@ -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