Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Player activated switches #1655

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions dGame/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1351,11 +1351,6 @@ void Entity::OnCollisionPhantom(const LWOOBJID otherEntity) {
callback(other);
}

SwitchComponent* switchComp = GetComponent<SwitchComponent>();
if (switchComp) {
switchComp->EntityEnter(other);
}

TriggerEvent(eTriggerEventType::ENTER, other);

// POI system
Expand Down
2 changes: 1 addition & 1 deletion dGame/dComponents/PetComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ void PetComponent::Update(float deltaTime) {
float distance = Vector3::DistanceSquared(position, switchPosition);
if (distance < 3 * 3) {
m_Interaction = closestSwitch->GetParentEntity()->GetObjectID();
closestSwitch->EntityEnter(m_Parent);
closestSwitch->OnUse(m_Parent);
} else if (distance < 20 * 20) {
haltDistance = 1;

Expand Down
20 changes: 20 additions & 0 deletions dGame/dComponents/SwitchComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "EntityManager.h"
#include "eTriggerEventType.h"
#include "RenderComponent.h"
#include "DestroyableComponent.h"

std::vector<SwitchComponent*> SwitchComponent::petSwitches;

Expand All @@ -11,6 +12,13 @@ SwitchComponent::SwitchComponent(Entity* parent) : Component(parent) {
m_ResetTime = m_Parent->GetVarAs<int32_t>(u"switch_reset_time");

m_QuickBuild = m_Parent->GetComponent<QuickBuildComponent>();

const auto factions = GeneralUtils::SplitString(m_Parent->GetVar<std::u16string>(u"respond_to_faction"), u':');
for (const auto& faction : factions) {
auto factionID = GeneralUtils::TryParse<int32_t>(GeneralUtils::UTF16ToWTF8(faction));
if (!factionID) continue;
m_FactionsToRespondTo.push_back(factionID.value());
}
}

SwitchComponent::~SwitchComponent() {
Expand All @@ -25,6 +33,17 @@ void SwitchComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitial
outBitStream.Write(m_Active);
}

void SwitchComponent::OnUse(Entity* originator) {
const auto* const destroyableComponent = originator->GetComponent<DestroyableComponent>();
if (!destroyableComponent) return;
for (const auto faction : m_FactionsToRespondTo) {
if (destroyableComponent->HasFaction(faction)) {
EntityEnter(originator);
break;
}
}
}

void SwitchComponent::SetActive(bool active) {
m_Active = active;

Expand Down Expand Up @@ -63,6 +82,7 @@ void SwitchComponent::EntityEnter(Entity* entity) {
RenderComponent::PlayAnimation(m_Parent, u"engaged");
m_PetBouncer->SetPetBouncerEnabled(true);
} else {
GameMessages::SendKnockback(entity->GetObjectID(), m_Parent->GetObjectID(), m_Parent->GetObjectID(), 0.0f, NiPoint3(0.0f, 17.0f, 0.0f));
Game::entityManager->SerializeEntity(m_Parent);
}

Expand Down
3 changes: 3 additions & 0 deletions dGame/dComponents/SwitchComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SwitchComponent final : public Component {
~SwitchComponent() override;

void Update(float deltaTime) override;
void OnUse(Entity* originator) override;

Entity* GetParentEntity() const;

Expand Down Expand Up @@ -101,6 +102,8 @@ class SwitchComponent final : public Component {
* Attached pet bouncer
*/
BouncerComponent* m_PetBouncer = nullptr;

std::vector<int32_t> m_FactionsToRespondTo{};
};

#endif // SWITCHCOMPONENT_H
Loading