Skip to content

Commit e1b2a63

Browse files
committed
add moveSpeed to npc move
1 parent f4e9095 commit e1b2a63

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

SDK

Server/Components/NPCs/NPC/npc.cpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void NPC::spawn()
170170
npcComponent_->getEventDispatcher_internal().dispatch(&NPCEventHandler::onNPCSpawn, *this);
171171
}
172172

173-
bool NPC::move(Vector3 pos, NPCMoveType moveType)
173+
bool NPC::move(Vector3 pos, NPCMoveType moveType, float moveSpeed)
174174
{
175175
if (moveType == NPCMoveType_None)
176176
{
@@ -187,19 +187,49 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)
187187
float distance = glm::distance(position, pos);
188188

189189
// Determine which speed to use based on moving type
190-
float speed = 0.0f;
191-
if (moveType == NPCMoveType_Sprint)
190+
float moveSpeed_ = 0.0f;
191+
moveType_ = moveType;
192+
193+
if (isEqualFloat(moveSpeed, NPC_MOVE_SPEED_AUTO))
192194
{
193-
speed = NPC_MOVE_SPEED_SPRINT;
194-
applyKey(Key::SPRINT);
195+
if (moveType_ == NPCMoveType_Sprint)
196+
{
197+
moveSpeed_ = NPC_MOVE_SPEED_SPRINT;
198+
}
199+
else if (moveType_ == NPCMoveType_Jog)
200+
{
201+
moveSpeed_ = NPC_MOVE_SPEED_JOG;
202+
}
203+
else
204+
{
205+
moveSpeed_ = NPC_MOVE_SPEED_WALK;
206+
}
195207
}
196-
else if (moveType == NPCMoveType_Jog)
208+
else
197209
{
198-
speed = NPC_MOVE_SPEED_JOG;
210+
DynamicArray<float> speedValues = { NPC_MOVE_SPEED_WALK, NPC_MOVE_SPEED_JOG, NPC_MOVE_SPEED_SPRINT };
211+
float nearestSpeed = getNearestFloatValue(moveSpeed_, speedValues);
212+
213+
if (isEqualFloat(nearestSpeed, NPC_MOVE_SPEED_SPRINT))
214+
{
215+
moveType_ = NPCMoveType_Sprint;
216+
}
217+
else if (isEqualFloat(nearestSpeed, NPC_MOVE_SPEED_JOG))
218+
{
219+
moveType_ = NPCMoveType_Jog;
220+
}
221+
else if (isEqualFloat(nearestSpeed, NPC_MOVE_SPEED_WALK))
222+
{
223+
moveType_ = NPCMoveType_Walk;
224+
}
199225
}
200-
else
226+
227+
if (moveType == NPCMoveType_Sprint)
228+
{
229+
applyKey(Key::SPRINT);
230+
}
231+
else if (moveType == NPCMoveType_Walk)
201232
{
202-
speed = NPC_MOVE_SPEED_WALK;
203233
applyKey(Key::WALK);
204234
}
205235

@@ -217,7 +247,7 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)
217247
footSync_.Rotation = rotation; // Do this directly, if you use NPC::setRotation it's going to cause recursion
218248

219249
// Calculate velocity to use on tick
220-
velocity_ = front * (speed / 100.0f);
250+
velocity_ = front * (moveSpeed_ / 100.0f);
221251

222252
if (!(std::fabs(glm::length(velocity_)) < DBL_EPSILON))
223253
{
@@ -229,7 +259,7 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)
229259
}
230260

231261
// Set internal variables
232-
moveSpeed_ = speed;
262+
moveSpeed_ = moveSpeed_;
233263
targetPosition_ = pos;
234264
moving_ = true;
235265
moveType_ = moveType;

Server/Components/NPCs/NPC/npc.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class NPC : public INPC, public PoolIDProvider, public NoCopy
3333

3434
void spawn() override;
3535

36-
bool move(Vector3 position, NPCMoveType moveType) override;
36+
bool move(Vector3 position, NPCMoveType moveType, float moveSpeed = NPC_MOVE_SPEED_AUTO) override;
3737

3838
void stopMove() override;
3939

Server/Components/NPCs/utils.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,3 +569,23 @@ inline void* getClosestEntityInBetween(NPCComponent* npcs, const Vector3& hitOri
569569

570570
return closestEntity;
571571
}
572+
573+
inline float getNearestFloatValue(float value, const DynamicArray<float>& floatArray)
574+
{
575+
float nearest = floatArray[0];
576+
577+
for (auto f : floatArray)
578+
{
579+
if (std::abs(f - value) < std::abs(nearest - value))
580+
{
581+
nearest = f;
582+
}
583+
}
584+
585+
return nearest;
586+
}
587+
588+
inline bool isEqualFloat(float a, float b)
589+
{
590+
return glm::epsilonEqual(a, b, glm::epsilon<float>());
591+
}

Server/Components/Pawn/Scripting/NPC/Natives.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ SCRIPT_API(NPC_GetVirtualWorld, int(INPC& npc))
9292
return npc.getVirtualWorld();
9393
}
9494

95-
SCRIPT_API(NPC_Move, bool(INPC& npc, Vector3 targetPos, int moveType))
95+
SCRIPT_API(NPC_Move, bool(INPC& npc, Vector3 targetPos, int moveType, float moveSpeed))
9696
{
97-
return npc.move(targetPos, NPCMoveType(moveType));
97+
return npc.move(targetPos, NPCMoveType(moveType), moveSpeed);
9898
}
9999

100100
SCRIPT_API(NPC_StopMove, bool(INPC& npc))

0 commit comments

Comments
 (0)