From 700d2fe68f716ca252ad5205be2651c56fc285df 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 | 2 +- Server/Components/NPCs/NPC/npc.cpp | 23 +++++++++++++++++++++++ Server/Components/NPCs/NPC/npc.hpp | 6 +++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/SDK b/SDK index d578dfc3c..b01f59e89 160000 --- a/SDK +++ b/SDK @@ -1 +1 @@ -Subproject commit d578dfc3cdc91b7c8fedee280994b067e11ab7b5 +Subproject commit b01f59e89905f8d03690a94983876ab964b49f78 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_;