Skip to content

Commit

Permalink
add set/get velocity for NPCs
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyrAhmady committed Oct 18, 2024
1 parent 58861e3 commit 700d2fe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion SDK
23 changes: 23 additions & 0 deletions Server/Components/NPCs/NPC/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NPC::NPC(NPCComponent* component, IPlayer* playerPtr)
, targetPosition_({ 0.0f, 0.0f, 0.0f })
, velocity_({ 0.0f, 0.0f, 0.0f })
, moving_(false)
, needsVelocityUpdate_(false)
{
// Keep a handle of NPC copmonent instance internally
npcComponent_ = component;
Expand Down Expand Up @@ -235,6 +236,22 @@ unsigned int NPC::getInterior() const
return 0;
}

Vector3 NPC::getVelocity() const
{
return player_->getPosition();
}

void NPC::setVelocity(Vector3 velocity, bool update)
{
if (moving_ && !update)
{
velocity_ = velocity;
footSync_.Velocity = velocity;
}

needsVelocityUpdate_ = update;
}

void NPC::sendFootSync()
{
// Only send foot sync if player is spawned
Expand Down Expand Up @@ -294,6 +311,12 @@ void NPC::tick(Microseconds elapsed, TimePoint now)
// Only process the NPC if it is spawned
if (player_ && (player_->getState() == PlayerState_OnFoot || player_->getState() == PlayerState_Driver || player_->getState() == PlayerState_Passenger || player_->getState() == PlayerState_Spawned))
{
if (needsVelocityUpdate_)
{
setPosition(getPosition() + velocity_);
setVelocity({ 0.0f, 0.0f, 0.0f }, false);
}

if (moving_)
{
advance(now);
Expand Down
6 changes: 5 additions & 1 deletion Server/Components/NPCs/NPC/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class NPC : public INPC, public PoolIDProvider, public NoCopy

unsigned int getInterior() const override;

Vector3 getVelocity() const override;

void setVelocity(Vector3 position, bool update = false) override;

void sendFootSync();

void tick(Microseconds elapsed, TimePoint now);
Expand Down Expand Up @@ -82,7 +86,7 @@ class NPC : public INPC, public PoolIDProvider, public NoCopy
Vector3 targetPosition_;
Vector3 velocity_;
bool moving_;

bool needsVelocityUpdate_;
// Packets
NetCode::Packet::PlayerFootSync footSync_;

Expand Down

0 comments on commit 700d2fe

Please sign in to comment.