-
Notifications
You must be signed in to change notification settings - Fork 34
/
agent.cpp
151 lines (139 loc) · 4.35 KB
/
agent.cpp
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
#include "agent.h"
#include "filter.h"
#include "scene.h"
#include "detour.h"
#include <DetourNavMeshQuery.h>
#include <DetourCommon.h>
#include <limits>
namespace NavMeshScene {
const float ZERO[3] = { 0,0,0 };
Agent::Agent()
: mId(0)
, mScene(nullptr)
, mFilter(nullptr)
, mCurPolyRef(0)
{
dtVcopy(mHalfExtents, DEFAULT_HALF_EXTENTS);
dtVcopy(mPosition, ZERO);
dtVcopy(mVelocity, ZERO);
}
Agent::~Agent() {
}
void Agent::Update(float delta) {
if (mVelocity[0] == 0 && mVelocity[1] == 0 && mVelocity[2] == 0) {
return;
}
float endPos[3] = {
mPosition[0] + mVelocity[0] * delta,
mPosition[1] + mVelocity[1] * delta,
mPosition[2] + mVelocity[2] * delta
};
if (auto agent = checkPosByAOI(mPosition[0], mPosition[2], endPos[0], endPos[2], true))
{
dtVcopy(mVelocity, ZERO);
OnHit(agent);
return;
}
bool bHit;
uint64_t realEndPolyRef;
float realEndPos[3];
if (!TryMove(endPos, realEndPolyRef, realEndPos, bHit)) {
return;
}
if (bHit)
{
dtVcopy(mVelocity, ZERO);
OnHit(nullptr);
return;
}
mCurPolyRef = realEndPolyRef;
dtVcopy(mPosition, realEndPos);
if (fabs(X - mPosition[0]) >= std::numeric_limits<float>::epsilon() || fabs(Y - mPosition[2]) >= std::numeric_limits<float>::epsilon())
{
X = mPosition[0];
Y = mPosition[2];
mScene->Update(this);
}
}
bool Agent::TryMove(float endPos[3], uint64_t& realEndPolyRef, float realEndPos[3], bool& bHit) {
if (mScene)
{
Filter& filter = mFilter ? *mFilter : mScene->GetDefaultFilter();
return mScene->GetDetour().TryMove(
mCurPolyRef,
mPosition,
endPos,
mHalfExtents,
filter.Get(),
realEndPolyRef,
realEndPos,
bHit);
}
return false;
}
bool Agent::SetPosition(float v[3]) {
if (mScene) {
if (!checkPosByAOI(mPosition[0], mPosition[2], mPosition[0], mPosition[2], false))
{
Filter& filter = mFilter ? *mFilter : mScene->GetDefaultFilter();
mScene->GetDetour().GetPoly(v, mHalfExtents, filter.Get(), mCurPolyRef, mPosition);
X = mPosition[0];
Y = mPosition[2];
mScene->Update(this);
return true;
}
}
return false;
}
float randf()
{
return (float)(rand() / (float)RAND_MAX);
}
void Agent::RandomPosition() {
if (mScene) {
Filter& filter = mFilter ? *mFilter : mScene->GetDefaultFilter();
LABLE_RANDOM:
mScene->GetDetour().RandomPosition(mHalfExtents, &filter.Get(), randf, mCurPolyRef, mPosition);
if (checkPosByAOI(mPosition[0], mPosition[2], mPosition[0], mPosition[2], false))
{
goto LABLE_RANDOM;
}
X = mPosition[0];
Y = mPosition[2];
mScene->Update(this);
}
}
bool Agent::Raycast(float endPos[3], bool& bHit, float hitPos[3]) {
if (mScene) {
Filter& filter = mFilter ? *mFilter : mScene->GetDefaultFilter();
return mScene->GetDetour().Raycast(
mCurPolyRef,
mPosition,
endPos,
filter.Get(),
bHit,
hitPos);
}
return false;
}
Agent* Agent::checkPosByAOI(float srcX, float srcY, float& dstX, float& dstY, bool bMove)
{
float pos1[3] = { srcX, 0, srcY };
float pos2[3] = { dstX, 0, dstY };
aoi::Rect rect(dstX - mHalfExtents[0], dstX + mHalfExtents[0], dstY - mHalfExtents[2], dstY + mHalfExtents[2]);
auto agents = mScene->Query(this, rect);
for (Agent* a = (Agent*)agents; a; a = (Agent*)a->Next())
{
if (a == this)
{
continue;
}
aoi::Rect tempRect(a->getRect());
if (rect.Intersects(tempRect))
{
return a;
}
}
return nullptr;
}
}