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,117 +3,102 @@
#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;
}
m_Active = true;
if (!m_ParentEntity) return;
m_ParentEntity->TriggerEvent(eTriggerEventType::ACTIVATED, entity);
const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name");
if (!grpName.empty()) {
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
for (auto* entity : entities) {
entity->OnFireEventServerSide(entity, "OnActivated");
} }
m_Active = true; }
if (!m_ParentEntity) return;
m_ParentEntity->TriggerEvent(eTriggerEventType::ACTIVATED, entity);
const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name"); m_Timer = m_ResetTime;
if (!grpName.empty()) {
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
for (auto* entity : entities) {
entity->OnFireEventServerSide(entity, "OnActivated");
}
}
m_Timer = m_ResetTime;
if (m_PetBouncer != nullptr) {
GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 2602, u"pettriggeractive", "BounceEffect", LWOOBJID_EMPTY, 1, 1, true);
RenderComponent::PlayAnimation(m_ParentEntity, u"engaged");
m_PetBouncer->SetBouncerEnabled(true);
} else {
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
}
if (m_Bouncer) {
GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 2602, u"pettriggeractive", "BounceEffect", LWOOBJID_EMPTY, 1, 1, true);
RenderComponent::PlayAnimation(m_ParentEntity, u"engaged");
m_Bouncer->SetBouncerEnabled(true);
} else {
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
} }
} }
void SwitchComponent::EntityLeave(Entity* entity) { void SwitchComponent::Update(const float deltaTime) {
if (!m_Active) return;
m_Timer -= deltaTime;
} if (m_Timer > 0.0f) return;
void SwitchComponent::Update(float deltaTime) { m_Active = false;
if (m_Active) { if (!m_ParentEntity) return;
m_Timer -= deltaTime;
if (m_Timer <= 0.0f) { m_ParentEntity->TriggerEvent(eTriggerEventType::DEACTIVATED, m_ParentEntity);
m_Active = false;
if (!m_ParentEntity) return;
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");
if (!grpName.empty()) { if (!grpName.empty()) {
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName); const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
for (auto* entity : entities) { for (auto* entity : entities) {
entity->OnFireEventServerSide(entity, "OnDectivated"); entity->OnFireEventServerSide(entity, "OnDectivated");
}
}
if (m_PetBouncer) {
m_PetBouncer->SetBouncerEnabled(false);
} else {
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
}
} }
} }
if (m_Bouncer) {
m_Bouncer->SetBouncerEnabled(false);
} else {
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;
@ -121,15 +106,10 @@ SwitchComponent* SwitchComponent::GetClosestSwitch(NiPoint3 position) {
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"