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 May 18, 2024
1 parent 4160bce commit ccec7a7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions SDK/include/Server/Components/NPCs/npcs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ struct INPC : public IExtensible, public IEntity

/// Get NPC interior, just the value that is stored internally
virtual unsigned int getInterior() const;

/// Get NPC velocity
virtual Vector3 getVelocity() const = 0;

/// Set NPC velocity
virtual void setVelocity(Vector3 position, bool update = false) = 0;
};

struct NPCEventHandler
Expand Down
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 ccec7a7

Please sign in to comment.