SwitchComponent pass

This commit is contained in:
David Markowitz 2023-07-07 12:13:26 -07:00
parent 197d1bcdee
commit cf53e35af5
4 changed files with 72 additions and 100 deletions

View File

@ -3,40 +3,36 @@
#include "eTriggerEventType.h" #include "eTriggerEventType.h"
#include "RenderComponent.h" #include "RenderComponent.h"
std::vector<SwitchComponent*> SwitchComponent::petSwitches; std::vector<SwitchComponent*> SwitchComponent::switches;
SwitchComponent::SwitchComponent(Entity* parent) : Component(parent) { SwitchComponent::SwitchComponent(Entity* parent) : Component(parent) {
m_Active = false; m_Active = false;
}
m_ResetTime = m_ParentEntity->GetVarAs<int32_t>(u"switch_reset_time"); void SwitchComponent::Startup() {
m_Rebuild = m_ParentEntity->GetComponent<QuickBuildComponent>(); m_Rebuild = m_ParentEntity->GetComponent<QuickBuildComponent>();
} }
SwitchComponent::~SwitchComponent() { void SwitchComponent::LoadConfigData() {
const auto& iterator = std::find(petSwitches.begin(), petSwitches.end(), this); m_ResetTime = m_ParentEntity->GetVarAs<int32_t>(u"switch_reset_time");
}
if (iterator != petSwitches.end()) { SwitchComponent::~SwitchComponent() {
petSwitches.erase(iterator); switches.erase(std::remove(switches.begin(), switches.end(), this), switches.end());
}
} }
void SwitchComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) { void SwitchComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
outBitStream->Write(m_Active); outBitStream->Write(m_Active);
} }
void SwitchComponent::SetActive(bool active) { void SwitchComponent::SetActive(const bool active) {
if (m_Active == active) return;
m_Active = active; m_Active = active;
if (m_Bouncer) m_Bouncer->SetBouncerEnabled(active);
if (m_PetBouncer) m_PetBouncer->SetBouncerEnabled(active);
}
bool SwitchComponent::GetActive() const {
return m_Active;
} }
void SwitchComponent::EntityEnter(Entity* entity) { void SwitchComponent::EntityEnter(Entity* entity) {
if (!m_Active) { if (m_Active) return;
if (m_Rebuild) { if (m_Rebuild) {
if (m_Rebuild->GetState() != eRebuildState::COMPLETED) return; if (m_Rebuild->GetState() != eRebuildState::COMPLETED) return;
} }
@ -56,28 +52,24 @@ void SwitchComponent::EntityEnter(Entity* entity) {
m_Timer = m_ResetTime; 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); GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 2602, u"pettriggeractive", "BounceEffect", LWOOBJID_EMPTY, 1, 1, true);
RenderComponent::PlayAnimation(m_ParentEntity, u"engaged"); RenderComponent::PlayAnimation(m_ParentEntity, u"engaged");
m_PetBouncer->SetBouncerEnabled(true); m_Bouncer->SetBouncerEnabled(true);
} else { } else {
EntityManager::Instance()->SerializeEntity(m_ParentEntity); EntityManager::Instance()->SerializeEntity(m_ParentEntity);
} }
}
} }
void SwitchComponent::EntityLeave(Entity* entity) { void SwitchComponent::Update(const float deltaTime) {
if (!m_Active) return;
}
void SwitchComponent::Update(float deltaTime) {
if (m_Active) {
m_Timer -= deltaTime; m_Timer -= deltaTime;
if (m_Timer <= 0.0f) { if (m_Timer > 0.0f) return;
m_Active = false; m_Active = false;
if (!m_ParentEntity) return; if (!m_ParentEntity) return;
m_ParentEntity->TriggerEvent(eTriggerEventType::DEACTIVATED, m_ParentEntity); m_ParentEntity->TriggerEvent(eTriggerEventType::DEACTIVATED, m_ParentEntity);
const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name"); const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name");
@ -90,46 +82,34 @@ void SwitchComponent::Update(float deltaTime) {
} }
} }
if (m_PetBouncer) { if (m_Bouncer) {
m_PetBouncer->SetBouncerEnabled(false); m_Bouncer->SetBouncerEnabled(false);
} else { } else {
EntityManager::Instance()->SerializeEntity(m_ParentEntity); EntityManager::Instance()->SerializeEntity(m_ParentEntity);
} }
}
}
} }
Entity* SwitchComponent::GetParentEntity() const { SwitchComponent* SwitchComponent::GetClosestSwitch(const NiPoint3& position) {
return m_ParentEntity; float closestDistance = 0.0f;
}
SwitchComponent* SwitchComponent::GetClosestSwitch(NiPoint3 position) {
float closestDistance = 0;
SwitchComponent* closest = nullptr; SwitchComponent* closest = nullptr;
for (SwitchComponent* petSwitch : petSwitches) { for (auto* petSwitch : switches) {
float distance = Vector3::DistanceSquared(petSwitch->m_ParentEntity->GetPosition(), position); float distance = Vector3::DistanceSquared(petSwitch->m_ParentEntity->GetPosition(), position);
if (closest == nullptr || distance < closestDistance) { if (closest && distance >= closestDistance) continue;
closestDistance = distance; closestDistance = distance;
closest = petSwitch; closest = petSwitch;
} }
}
return closest; return closest;
} }
void SwitchComponent::SetPetBouncer(BouncerComponent* value) { void SwitchComponent::SetPetBouncer(BouncerComponent* value) {
m_PetBouncer = value; m_Bouncer = value;
if (value != nullptr) { if (!value) return;
m_PetBouncer->SetBounceOnCollision(true); m_Bouncer->SetBounceOnCollision(true);
EntityManager::Instance()->SerializeEntity(m_PetBouncer->GetParentEntity()); EntityManager::Instance()->SerializeEntity(m_Bouncer->GetParentEntity());
petSwitches.push_back(this); switches.push_back(this);
}
}
BouncerComponent* SwitchComponent::GetPetBouncer() const {
return m_PetBouncer;
} }

View File

@ -21,22 +21,22 @@ public:
SwitchComponent(Entity* parent); SwitchComponent(Entity* parent);
~SwitchComponent() override; ~SwitchComponent() override;
void LoadConfigData() override;
void Startup() override;
void Update(float deltaTime) override; void Update(float deltaTime) override;
Entity* GetParentEntity() const;
void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags); void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags);
/** /**
* Sets whether the switch is on or off. * Sets whether the switch is on or off.
* @param active 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. * Returns whether the switch is on or off.
*/ */
bool GetActive() const; bool GetActive() const { return m_Active; }
/** /**
* Sets the attached pet bouncer * Sets the attached pet bouncer
@ -47,30 +47,25 @@ public:
/** /**
* Returns the attached pet bouncer * Returns the attached pet bouncer
*/ */
BouncerComponent* GetPetBouncer() const; BouncerComponent* GetBouncer() const { return m_Bouncer; }
/** /**
* Invoked when a entity enters the trigger area. * Invoked when a entity enters the trigger area.
*/ */
void EntityEnter(Entity* entity); void EntityEnter(Entity* entity);
/**
* Invoked when a entity leaves the trigger area.
*/
void EntityLeave(Entity* entity);
/** /**
* Returns the closest switch from a given position * Returns the closest switch from a given position
* @param position the position to check * @param position the position to check
* @return the closest switch from a given position * @return the closest switch from a given position
*/ */
static SwitchComponent* GetClosestSwitch(NiPoint3 position); static SwitchComponent* GetClosestSwitch(const NiPoint3& position);
private: private:
/** /**
* A list of all pet switches. * A list of all pet switches.
*/ */
static std::vector<SwitchComponent*> petSwitches; static std::vector<SwitchComponent*> switches;
/** /**
* Attached rebuild component. * Attached rebuild component.
@ -100,7 +95,7 @@ private:
/** /**
* Attached pet bouncer * Attached pet bouncer
*/ */
BouncerComponent* m_PetBouncer = nullptr; BouncerComponent* m_Bouncer = nullptr;
}; };
#endif // SWITCHCOMPONENT_H #endif // SWITCHCOMPONENT_H

View File

@ -1013,9 +1013,6 @@ void Entity::OnCollisionLeavePhantom(const LWOOBJID otherEntity) {
TriggerEvent(eTriggerEventType::EXIT, other); 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); const auto index = std::find(m_TargetsInPhantom.begin(), m_TargetsInPhantom.end(), otherEntity);
if (index == m_TargetsInPhantom.end()) return; if (index == m_TargetsInPhantom.end()) return;

View File

@ -379,7 +379,7 @@ protected:
std::vector<LWOOBJID> m_TargetsInPhantom; std::vector<LWOOBJID> m_TargetsInPhantom;
static const std::vector<ComponentWhitelist> m_ComponentWhitelists; 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" #include "Entity.tcc"