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 "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);
void SwitchComponent::LoadConfigData() {
m_ResetTime = m_ParentEntity->GetVarAs<int32_t>(u"switch_reset_time");
}
if (iterator != petSwitches.end()) {
petSwitches.erase(iterator);
}
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_Rebuild) {
if (m_Rebuild->GetState() != eRebuildState::COMPLETED) return;
if (m_Active) return;
if (m_Rebuild) {
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");
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);
}
m_Timer = m_ResetTime;
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) {
if (m_Active) {
m_Timer -= deltaTime;
m_Active = false;
if (!m_ParentEntity) return;
if (m_Timer <= 0.0f) {
m_Active = false;
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");
if (!grpName.empty()) {
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
if (!grpName.empty()) {
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
for (auto* entity : entities) {
entity->OnFireEventServerSide(entity, "OnDectivated");
}
}
if (m_PetBouncer) {
m_PetBouncer->SetBouncerEnabled(false);
} else {
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
}
for (auto* entity : entities) {
entity->OnFireEventServerSide(entity, "OnDectivated");
}
}
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) {
closestDistance = distance;
closest = petSwitch;
}
if (closest && distance >= closestDistance) continue;
closestDistance = distance;
closest = petSwitch;
}
return closest;
@ -121,15 +106,10 @@ SwitchComponent* SwitchComponent::GetClosestSwitch(NiPoint3 position) {
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);
}

View File

@ -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

View File

@ -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;

View File

@ -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"