This repository was archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRVONavAgent.cs
187 lines (179 loc) · 5.25 KB
/
RVONavAgent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using GameFramework;
using RVO;
using System;
using UnityEngine;
/// <summary>
/// RVO2寻路对象
/// </summary>
public class RVONavAgent : IReference
{
readonly int GROUND_LAYER = LayerMask.GetMask("Ground");
const float MIN_FLOAT_CHECK = 0.01f;
public int Id { get; private set; }
private float m_AvoidanceWeight = 0.5f;
/// <summary>
/// 移动速度
/// </summary>
public Vector2 Velocity
{
get => Simulator.Instance.getAgentVelocity(Id);
}
private Vector2 m_MoveDirection;
/// <summary>
/// 移动方向
/// </summary>
public Vector2 MoveDirection
{
get => m_MoveDirection;
set
{
m_MoveDirection = value;
Simulator.Instance.setAgentPrefVelocity(Id, m_MoveDirection);
}
}
private float m_Radius;
/// <summary>
/// 碰撞体半径
/// </summary>
public float Radius
{
get => m_Radius;
set
{
m_Radius = value;
Simulator.Instance.setAgentRadius(Id, m_Radius);
}
}
private float m_MaxSpeed;
/// <summary>
/// 最大移动速度
/// </summary>
public float MaxSpeed
{
get => m_MaxSpeed;
set
{
m_MaxSpeed = value;
Simulator.Instance.setAgentMaxSpeed(Id, m_MaxSpeed / LevelEntity.m_SyncStepTime);
}
}
public Vector2 Position
{
get => Simulator.Instance.getAgentPosition(Id);
}
public float AvoidanceWeight
{
get => Simulator.Instance.getAgentWeight(Id);
set
{
Simulator.Instance.setAgentWeight(Id, Mathf.Clamp(value, 0.01f, 1f));
}
}
public bool IsReached { get; private set; }
private bool m_StopMoving;
public bool StopMoving
{
get => m_StopMoving;
set
{
if (m_StopMoving == value) return;
m_StopMoving = value;
if (m_StopMoving)
{
Simulator.Instance.setAgentWeight(Id, 0.01f);
}
else
{
Simulator.Instance.setAgentWeight(Id, m_AvoidanceWeight);
}
}
}
private Transform m_Transform;
private Transform m_MoveTarget; //移动目标节点
private Vector3 m_MoveTargetPos; //移动目标点
private float m_RotateSmoothSpeed = 10f;
private float m_MoveSmoothSpeed = 20f;
private Vector3 mCurrentPosition;
private Quaternion mCurrentRotation;
public static RVONavAgent Acquire(Transform transform, float radius, float maxSpeed)
{
var inst = ReferencePool.Acquire<RVONavAgent>();
inst.m_Transform = transform;
inst.Id = Simulator.Instance.addAgent(new Vector2(transform.position.x, transform.position.z));
inst.Radius = radius;
inst.MaxSpeed = maxSpeed;
inst.m_StopMoving = false;
inst.mCurrentPosition = transform.position;
inst.mCurrentRotation = transform.rotation;
GF.Event.Fire(null, ReferencePool.Acquire<RVONavAgentEventArgs>().Fill(inst));
return inst;
}
/// <summary>
/// 设置Agent移动和转向平滑度
/// </summary>
/// <param name="moveSmooth"></param>
/// <param name="rotateSmooth"></param>
public void SetAgentActionSmooth(float moveSmooth, float rotateSmooth)
{
this.m_MoveSmoothSpeed = moveSmooth;
this.m_RotateSmoothSpeed = rotateSmooth;
}
public void LogicUpdate(float elapseSeconds)
{
if (m_MoveTarget == null) return;
m_MoveTargetPos = m_MoveTarget.position;
m_Transform.position = mCurrentPosition;
m_Transform.rotation = mCurrentRotation;
}
/// <summary>
/// 此方法由子线程调用,计算当前位置和方向
/// </summary>
/// <param name="elapseSeconds"></param>
public void UpdatePositionAndRotation(float elapseSeconds)
{
var rvoPos = this.Position;
var nextPos = mCurrentPosition;
nextPos.x = rvoPos.x;
nextPos.z = rvoPos.y;
mCurrentPosition = Vector3.Lerp(mCurrentPosition, nextPos, elapseSeconds * m_MoveSmoothSpeed);
if (StopMoving)
{
MoveDirection = Vector2.zero;
return;
}
var offset = m_MoveTargetPos - mCurrentPosition;
offset.y = 0;
rvoPos.x = offset.x;
rvoPos.y = offset.z;
this.MoveDirection = RVOMath.normalize(rvoPos);
var rvoForward = this.Velocity;
if (RVOMath.absSq(rvoForward) > MIN_FLOAT_CHECK)
{
rvoForward = RVOMath.normalize(rvoForward);
nextPos.Set(rvoForward.x, 0, rvoForward.y);
}
else
{
offset.y = 0;
nextPos = offset.normalized;
}
mCurrentRotation = Quaternion.Lerp(mCurrentRotation, Quaternion.LookRotation(nextPos), elapseSeconds * m_RotateSmoothSpeed);
}
public void Clear()
{
Simulator.Instance.removeAgent(Id);
Id = -1;
m_Radius = 1;
m_MaxSpeed = 0;
}
internal void SetMoveTarget(Transform transform)
{
m_MoveTarget = transform;
m_MoveTargetPos = transform.position;
}
internal void SetMoveTarget(Vector3 point)
{
m_MoveTarget = null;
m_MoveTargetPos = point;
}
}