mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-11-26 15:37:20 +00:00
SwitchComponent pass
This commit is contained in:
parent
197d1bcdee
commit
cf53e35af5
@ -3,40 +3,36 @@
|
||||
#include "eTriggerEventType.h"
|
||||
#include "RenderComponent.h"
|
||||
|
||||
std::vector<SwitchComponent*> SwitchComponent::petSwitches;
|
||||
std::vector<SwitchComponent*> SwitchComponent::switches;
|
||||
|
||||
SwitchComponent::SwitchComponent(Entity* parent) : Component(parent) {
|
||||
m_Active = false;
|
||||
}
|
||||
|
||||
m_ResetTime = m_ParentEntity->GetVarAs<int32_t>(u"switch_reset_time");
|
||||
|
||||
void SwitchComponent::Startup() {
|
||||
m_Rebuild = m_ParentEntity->GetComponent<QuickBuildComponent>();
|
||||
}
|
||||
|
||||
SwitchComponent::~SwitchComponent() {
|
||||
const auto& iterator = std::find(petSwitches.begin(), petSwitches.end(), this);
|
||||
|
||||
if (iterator != petSwitches.end()) {
|
||||
petSwitches.erase(iterator);
|
||||
void SwitchComponent::LoadConfigData() {
|
||||
m_ResetTime = m_ParentEntity->GetVarAs<int32_t>(u"switch_reset_time");
|
||||
}
|
||||
|
||||
SwitchComponent::~SwitchComponent() {
|
||||
switches.erase(std::remove(switches.begin(), switches.end(), this), switches.end());
|
||||
}
|
||||
|
||||
void SwitchComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
|
||||
outBitStream->Write(m_Active);
|
||||
}
|
||||
|
||||
void SwitchComponent::SetActive(bool active) {
|
||||
void SwitchComponent::SetActive(const bool active) {
|
||||
if (m_Active == active) return;
|
||||
m_Active = active;
|
||||
|
||||
if (m_PetBouncer) m_PetBouncer->SetBouncerEnabled(active);
|
||||
}
|
||||
|
||||
bool SwitchComponent::GetActive() const {
|
||||
return m_Active;
|
||||
if (m_Bouncer) m_Bouncer->SetBouncerEnabled(active);
|
||||
}
|
||||
|
||||
void SwitchComponent::EntityEnter(Entity* entity) {
|
||||
if (!m_Active) {
|
||||
if (m_Active) return;
|
||||
if (m_Rebuild) {
|
||||
if (m_Rebuild->GetState() != eRebuildState::COMPLETED) return;
|
||||
}
|
||||
@ -56,28 +52,24 @@ void SwitchComponent::EntityEnter(Entity* entity) {
|
||||
|
||||
m_Timer = m_ResetTime;
|
||||
|
||||
if (m_PetBouncer != nullptr) {
|
||||
if (m_Bouncer) {
|
||||
GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 2602, u"pettriggeractive", "BounceEffect", LWOOBJID_EMPTY, 1, 1, true);
|
||||
RenderComponent::PlayAnimation(m_ParentEntity, u"engaged");
|
||||
m_PetBouncer->SetBouncerEnabled(true);
|
||||
m_Bouncer->SetBouncerEnabled(true);
|
||||
} else {
|
||||
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchComponent::EntityLeave(Entity* entity) {
|
||||
|
||||
}
|
||||
|
||||
void SwitchComponent::Update(float deltaTime) {
|
||||
if (m_Active) {
|
||||
void SwitchComponent::Update(const float deltaTime) {
|
||||
if (!m_Active) return;
|
||||
m_Timer -= deltaTime;
|
||||
|
||||
if (m_Timer <= 0.0f) {
|
||||
if (m_Timer > 0.0f) return;
|
||||
|
||||
m_Active = false;
|
||||
if (!m_ParentEntity) return;
|
||||
|
||||
m_ParentEntity->TriggerEvent(eTriggerEventType::DEACTIVATED, m_ParentEntity);
|
||||
|
||||
const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name");
|
||||
@ -90,46 +82,34 @@ void SwitchComponent::Update(float deltaTime) {
|
||||
}
|
||||
}
|
||||
|
||||
if (m_PetBouncer) {
|
||||
m_PetBouncer->SetBouncerEnabled(false);
|
||||
if (m_Bouncer) {
|
||||
m_Bouncer->SetBouncerEnabled(false);
|
||||
} else {
|
||||
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Entity* SwitchComponent::GetParentEntity() const {
|
||||
return m_ParentEntity;
|
||||
}
|
||||
|
||||
SwitchComponent* SwitchComponent::GetClosestSwitch(NiPoint3 position) {
|
||||
float closestDistance = 0;
|
||||
SwitchComponent* SwitchComponent::GetClosestSwitch(const NiPoint3& position) {
|
||||
float closestDistance = 0.0f;
|
||||
SwitchComponent* closest = nullptr;
|
||||
|
||||
for (SwitchComponent* petSwitch : petSwitches) {
|
||||
for (auto* petSwitch : switches) {
|
||||
float distance = Vector3::DistanceSquared(petSwitch->m_ParentEntity->GetPosition(), position);
|
||||
|
||||
if (closest == nullptr || distance < closestDistance) {
|
||||
if (closest && distance >= closestDistance) continue;
|
||||
closestDistance = distance;
|
||||
closest = petSwitch;
|
||||
}
|
||||
}
|
||||
|
||||
return closest;
|
||||
}
|
||||
|
||||
|
||||
void SwitchComponent::SetPetBouncer(BouncerComponent* value) {
|
||||
m_PetBouncer = value;
|
||||
m_Bouncer = value;
|
||||
|
||||
if (value != nullptr) {
|
||||
m_PetBouncer->SetBounceOnCollision(true);
|
||||
EntityManager::Instance()->SerializeEntity(m_PetBouncer->GetParentEntity());
|
||||
petSwitches.push_back(this);
|
||||
}
|
||||
}
|
||||
|
||||
BouncerComponent* SwitchComponent::GetPetBouncer() const {
|
||||
return m_PetBouncer;
|
||||
if (!value) return;
|
||||
m_Bouncer->SetBounceOnCollision(true);
|
||||
EntityManager::Instance()->SerializeEntity(m_Bouncer->GetParentEntity());
|
||||
switches.push_back(this);
|
||||
}
|
||||
|
@ -21,22 +21,22 @@ public:
|
||||
SwitchComponent(Entity* parent);
|
||||
~SwitchComponent() override;
|
||||
|
||||
void LoadConfigData() override;
|
||||
void Startup() override;
|
||||
void Update(float deltaTime) override;
|
||||
|
||||
Entity* GetParentEntity() const;
|
||||
|
||||
void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags);
|
||||
|
||||
/**
|
||||
* Sets whether the switch is on or off.
|
||||
* @param active whether the switch is on or off.
|
||||
*/
|
||||
void SetActive(bool active);
|
||||
void SetActive(const bool active);
|
||||
|
||||
/**
|
||||
* Returns whether the switch is on or off.
|
||||
*/
|
||||
bool GetActive() const;
|
||||
bool GetActive() const { return m_Active; }
|
||||
|
||||
/**
|
||||
* Sets the attached pet bouncer
|
||||
@ -47,30 +47,25 @@ public:
|
||||
/**
|
||||
* Returns the attached pet bouncer
|
||||
*/
|
||||
BouncerComponent* GetPetBouncer() const;
|
||||
BouncerComponent* GetBouncer() const { return m_Bouncer; }
|
||||
|
||||
/**
|
||||
* Invoked when a entity enters the trigger area.
|
||||
*/
|
||||
void EntityEnter(Entity* entity);
|
||||
|
||||
/**
|
||||
* Invoked when a entity leaves the trigger area.
|
||||
*/
|
||||
void EntityLeave(Entity* entity);
|
||||
|
||||
/**
|
||||
* Returns the closest switch from a given position
|
||||
* @param position the position to check
|
||||
* @return the closest switch from a given position
|
||||
*/
|
||||
static SwitchComponent* GetClosestSwitch(NiPoint3 position);
|
||||
static SwitchComponent* GetClosestSwitch(const NiPoint3& position);
|
||||
|
||||
private:
|
||||
/**
|
||||
* A list of all pet switches.
|
||||
*/
|
||||
static std::vector<SwitchComponent*> petSwitches;
|
||||
static std::vector<SwitchComponent*> switches;
|
||||
|
||||
/**
|
||||
* Attached rebuild component.
|
||||
@ -100,7 +95,7 @@ private:
|
||||
/**
|
||||
* Attached pet bouncer
|
||||
*/
|
||||
BouncerComponent* m_PetBouncer = nullptr;
|
||||
BouncerComponent* m_Bouncer = nullptr;
|
||||
};
|
||||
|
||||
#endif // SWITCHCOMPONENT_H
|
||||
|
@ -1013,9 +1013,6 @@ void Entity::OnCollisionLeavePhantom(const LWOOBJID otherEntity) {
|
||||
|
||||
TriggerEvent(eTriggerEventType::EXIT, other);
|
||||
|
||||
auto* switchComponent = GetComponent<SwitchComponent>();
|
||||
if (switchComponent) switchComponent->EntityLeave(other);
|
||||
|
||||
const auto index = std::find(m_TargetsInPhantom.begin(), m_TargetsInPhantom.end(), otherEntity);
|
||||
|
||||
if (index == m_TargetsInPhantom.end()) return;
|
||||
|
@ -379,7 +379,7 @@ protected:
|
||||
std::vector<LWOOBJID> m_TargetsInPhantom;
|
||||
|
||||
static const std::vector<ComponentWhitelist> m_ComponentWhitelists;
|
||||
static const std::array<eReplicaComponentType> m_ComponentOrder;
|
||||
static const std::array<eReplicaComponentType, 80> m_ComponentOrder;
|
||||
};
|
||||
|
||||
#include "Entity.tcc"
|
||||
|
Loading…
Reference in New Issue
Block a user