From ccec7a79ffc3bfc4563aedbc0fc6e05c3ed9735f Mon Sep 17 00:00:00 2001 From: iAmir Date: Sat, 18 May 2024 06:47:16 +0330 Subject: [PATCH] add set/get velocity for NPCs --- SDK/include/Server/Components/NPCs/npcs.hpp | 6 ++++++ Server/Components/NPCs/NPC/npc.cpp | 23 +++++++++++++++++++++ Server/Components/NPCs/NPC/npc.hpp | 6 +++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/SDK/include/Server/Components/NPCs/npcs.hpp b/SDK/include/Server/Components/NPCs/npcs.hpp index 0964a3079..576597312 100644 --- a/SDK/include/Server/Components/NPCs/npcs.hpp +++ b/SDK/include/Server/Components/NPCs/npcs.hpp @@ -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 diff --git a/Server/Components/NPCs/NPC/npc.cpp b/Server/Components/NPCs/NPC/npc.cpp index bd36d6b2a..169bdc22a 100644 --- a/Server/Components/NPCs/NPC/npc.cpp +++ b/Server/Components/NPCs/NPC/npc.cpp @@ -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; @@ -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 @@ -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); diff --git a/Server/Components/NPCs/NPC/npc.hpp b/Server/Components/NPCs/NPC/npc.hpp index 6b3f2409f..bda6af35a 100644 --- a/Server/Components/NPCs/NPC/npc.hpp +++ b/Server/Components/NPCs/NPC/npc.hpp @@ -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); @@ -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_;