Skip to content

Commit

Permalink
add moveSpeed to npc move
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyrAhmady committed Feb 5, 2025
1 parent f4e9095 commit e1b2a63
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion SDK
52 changes: 41 additions & 11 deletions Server/Components/NPCs/NPC/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void NPC::spawn()
npcComponent_->getEventDispatcher_internal().dispatch(&NPCEventHandler::onNPCSpawn, *this);
}

bool NPC::move(Vector3 pos, NPCMoveType moveType)
bool NPC::move(Vector3 pos, NPCMoveType moveType, float moveSpeed)
{
if (moveType == NPCMoveType_None)
{
Expand All @@ -187,19 +187,49 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)
float distance = glm::distance(position, pos);

// Determine which speed to use based on moving type
float speed = 0.0f;
if (moveType == NPCMoveType_Sprint)
float moveSpeed_ = 0.0f;
moveType_ = moveType;

if (isEqualFloat(moveSpeed, NPC_MOVE_SPEED_AUTO))
{
speed = NPC_MOVE_SPEED_SPRINT;
applyKey(Key::SPRINT);
if (moveType_ == NPCMoveType_Sprint)
{
moveSpeed_ = NPC_MOVE_SPEED_SPRINT;
}
else if (moveType_ == NPCMoveType_Jog)
{
moveSpeed_ = NPC_MOVE_SPEED_JOG;
}
else
{
moveSpeed_ = NPC_MOVE_SPEED_WALK;
}
}
else if (moveType == NPCMoveType_Jog)
else
{
speed = NPC_MOVE_SPEED_JOG;
DynamicArray<float> speedValues = { NPC_MOVE_SPEED_WALK, NPC_MOVE_SPEED_JOG, NPC_MOVE_SPEED_SPRINT };
float nearestSpeed = getNearestFloatValue(moveSpeed_, speedValues);

if (isEqualFloat(nearestSpeed, NPC_MOVE_SPEED_SPRINT))
{
moveType_ = NPCMoveType_Sprint;
}
else if (isEqualFloat(nearestSpeed, NPC_MOVE_SPEED_JOG))
{
moveType_ = NPCMoveType_Jog;
}
else if (isEqualFloat(nearestSpeed, NPC_MOVE_SPEED_WALK))
{
moveType_ = NPCMoveType_Walk;
}
}
else

if (moveType == NPCMoveType_Sprint)
{
applyKey(Key::SPRINT);
}
else if (moveType == NPCMoveType_Walk)
{
speed = NPC_MOVE_SPEED_WALK;
applyKey(Key::WALK);
}

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

// Calculate velocity to use on tick
velocity_ = front * (speed / 100.0f);
velocity_ = front * (moveSpeed_ / 100.0f);

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

// Set internal variables
moveSpeed_ = speed;
moveSpeed_ = moveSpeed_;
targetPosition_ = pos;
moving_ = true;
moveType_ = moveType;
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/NPCs/NPC/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class NPC : public INPC, public PoolIDProvider, public NoCopy

void spawn() override;

bool move(Vector3 position, NPCMoveType moveType) override;
bool move(Vector3 position, NPCMoveType moveType, float moveSpeed = NPC_MOVE_SPEED_AUTO) override;

void stopMove() override;

Expand Down
20 changes: 20 additions & 0 deletions Server/Components/NPCs/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,23 @@ inline void* getClosestEntityInBetween(NPCComponent* npcs, const Vector3& hitOri

return closestEntity;
}

inline float getNearestFloatValue(float value, const DynamicArray<float>& floatArray)
{
float nearest = floatArray[0];

for (auto f : floatArray)
{
if (std::abs(f - value) < std::abs(nearest - value))
{
nearest = f;
}
}

return nearest;
}

inline bool isEqualFloat(float a, float b)
{
return glm::epsilonEqual(a, b, glm::epsilon<float>());
}
4 changes: 2 additions & 2 deletions Server/Components/Pawn/Scripting/NPC/Natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ SCRIPT_API(NPC_GetVirtualWorld, int(INPC& npc))
return npc.getVirtualWorld();
}

SCRIPT_API(NPC_Move, bool(INPC& npc, Vector3 targetPos, int moveType))
SCRIPT_API(NPC_Move, bool(INPC& npc, Vector3 targetPos, int moveType, float moveSpeed))
{
return npc.move(targetPos, NPCMoveType(moveType));
return npc.move(targetPos, NPCMoveType(moveType), moveSpeed);
}

SCRIPT_API(NPC_StopMove, bool(INPC& npc))
Expand Down

0 comments on commit e1b2a63

Please sign in to comment.