DarkflameServer/dScripts/CavePrisonCage.cpp

216 lines
6.0 KiB
C++
Raw Permalink Normal View History

#include "CavePrisonCage.h"
#include "EntityManager.h"
#include "RebuildComponent.h"
#include "GameMessages.h"
#include "Character.h"
#include "dZoneManager.h"
2022-07-28 13:39:57 +00:00
void CavePrisonCage::OnStartup(Entity* self) {
const auto& myNum = self->GetVar<std::u16string>(u"myNumber");
2022-07-28 13:39:57 +00:00
if (myNum.empty()) {
return;
}
2022-07-28 13:39:57 +00:00
auto* spawner = dZoneManager::Instance()->GetSpawnersByName("PrisonCounterweight_0" + GeneralUtils::UTF16ToWTF8(myNum))[0];
2022-07-28 13:39:57 +00:00
self->SetVar<Spawner*>(u"CWSpawner", spawner);
2022-07-28 13:39:57 +00:00
Setup(self, spawner);
}
2022-07-28 13:39:57 +00:00
void CavePrisonCage::Setup(Entity* self, Spawner* spawner) {
SpawnCounterweight(self, spawner);
2022-07-28 13:39:57 +00:00
NiPoint3 mypos = self->GetPosition();
NiQuaternion myrot = self->GetRotation();
2022-07-28 13:39:57 +00:00
mypos.y += 1.5;
mypos.z -= 0.5;
2022-07-28 13:39:57 +00:00
EntityInfo info{};
info.lot = m_Villagers[self->GetVarAs<int32_t>(u"myNumber") - 1];
info.pos = mypos;
info.rot = myrot;
info.spawnerID = self->GetObjectID();
2022-07-28 13:39:57 +00:00
// Spawn the villager inside the jail
auto* entity = EntityManager::Instance()->CreateEntity(info);
2022-07-28 13:39:57 +00:00
// Save the villeger ID
self->SetVar<LWOOBJID>(u"villager", entity->GetObjectID());
2022-07-28 13:39:57 +00:00
// Construct the entity
EntityManager::Instance()->ConstructEntity(entity);
}
2022-07-28 13:39:57 +00:00
void CavePrisonCage::OnRebuildNotifyState(Entity* self, eRebuildState state) {
if (state != eRebuildState::REBUILD_RESETTING) {
return;
}
2022-07-28 13:39:57 +00:00
auto* spawner = self->GetVar<Spawner*>(u"CWSpawner");
2022-07-28 13:39:57 +00:00
if (spawner == nullptr) {
return;
}
2022-07-28 13:39:57 +00:00
spawner->Reset();
2022-07-28 13:39:57 +00:00
SpawnCounterweight(self, spawner);
}
2022-07-28 13:39:57 +00:00
void CavePrisonCage::SpawnCounterweight(Entity* self, Spawner* spawner) {
spawner->Reset();
2022-07-28 13:39:57 +00:00
auto* counterweight = spawner->Spawn();
2022-07-28 13:39:57 +00:00
self->SetVar<LWOOBJID>(u"Counterweight", counterweight->GetObjectID());
2022-07-28 13:39:57 +00:00
auto* rebuildComponent = counterweight->GetComponent<RebuildComponent>();
2022-07-28 13:39:57 +00:00
if (rebuildComponent != nullptr) {
rebuildComponent->AddRebuildStateCallback([this, self](eRebuildState state) {
OnRebuildNotifyState(self, state);
});
2022-07-28 13:39:57 +00:00
rebuildComponent->AddRebuildCompleteCallback([this, self](Entity* user) {
// The counterweight is a simple mover, which is not implemented, so we'll just set it's position
auto* counterweight = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"Counterweight"));
2022-07-28 13:39:57 +00:00
if (counterweight == nullptr) {
return;
}
2022-07-28 13:39:57 +00:00
// Move the counterweight down 2 units
counterweight->SetPosition(counterweight->GetPosition() + NiPoint3(0, -2, 0));
2022-07-28 13:39:57 +00:00
// Serialize the counterweight
EntityManager::Instance()->SerializeEntity(counterweight);
2022-07-28 13:39:57 +00:00
// notifyPlatformAtLastWaypoint
2022-07-28 13:39:57 +00:00
// Save the userID as Builder
self->SetVar<LWOOBJID>(u"Builder", user->GetObjectID());
2022-07-28 13:39:57 +00:00
// Get the button and make sure it still exists
auto* button = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"Button"));
2022-07-28 13:39:57 +00:00
if (button == nullptr) {
return;
}
2022-07-28 13:39:57 +00:00
// Play the 'down' animation on the button
GameMessages::SendPlayAnimation(button, u"down");
2022-07-28 13:39:57 +00:00
// Setup a timer named 'buttonGoingDown' to be triggered in 5 seconds
self->AddTimer("buttonGoingDown", 5.0f);
});
}
2022-07-28 13:39:57 +00:00
if (self->GetVar<LWOOBJID>(u"Button")) {
return;
}
GetButton(self);
}
2022-07-28 13:39:57 +00:00
void CavePrisonCage::GetButton(Entity* self) {
const auto buttons = EntityManager::Instance()->GetEntitiesInGroup("PrisonButton_0" + std::to_string(self->GetVarAs<int32_t>(u"myNumber")));
2022-07-28 13:39:57 +00:00
if (buttons.size() == 0) {
// Try again in 0.5 seconds
self->AddCallbackTimer(0.5, [this, self]() {
GetButton(self);
});
2022-07-28 13:39:57 +00:00
return;
}
2022-07-28 13:39:57 +00:00
auto* button = buttons[0];
2022-07-28 13:39:57 +00:00
self->SetVar<LWOOBJID>(u"Button", button->GetObjectID());
}
2022-07-28 13:39:57 +00:00
void CavePrisonCage::OnTimerDone(Entity* self, std::string timerName) {
// the anim of the button down is over
if (timerName == "buttonGoingDown") {
// Play the 'up' animation
GameMessages::SendPlayAnimation(self, u"up");
// Setup a timer named 'CageOpen' to be triggered in 1 second
self->AddTimer("CageOpen", 1.0f);
} else if (timerName == "CageOpen") {
// play the idle open anim
GameMessages::SendPlayAnimation(self, u"idle-up");
// Get the villeger
auto* villager = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"villager"));
if (villager == nullptr) {
return;
}
GameMessages::SendNotifyClientObject(villager->GetObjectID(), u"TimeToChat", 0, 0, LWOOBJID_EMPTY, "", UNASSIGNED_SYSTEM_ADDRESS);
// Get the builder and make sure it still exists
auto* builder = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"Builder"));
if (builder == nullptr) {
return;
}
const auto flagNum = 2020 + self->GetVarAs<int32_t>(u"myNumber");
// Set the flag on the builder character
builder->GetCharacter()->SetPlayerFlag(flagNum, true);
// Setup a timer named 'VillagerEscape' to be triggered in 5 seconds
self->AddTimer("VillagerEscape", 5.0f);
} else if (timerName == "VillagerEscape") {
// Get the villeger and make sure it still exists
auto* villager = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"villager"));
if (villager == nullptr) {
return;
}
// Kill the villager
villager->Kill();
// Setup a timer named 'SmashCounterweight' to be triggered in 2 seconds
self->AddTimer("SmashCounterweight", 2.0f);
} else if (timerName == "SmashCounterweight") {
// Get the counterweight and make sure it still exists
auto* counterweight = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"Counterweight"));
if (counterweight == nullptr) {
return;
}
// Smash the counterweight
counterweight->Smash();
// Get the button and make sure it still exists
auto* button = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"Button"));
if (button == nullptr) {
return;
}
// Play the 'up' animation on the button
GameMessages::SendPlayAnimation(button, u"up");
// Setup a timer named 'CageClosed' to be triggered in 1 second
self->AddTimer("CageClosed", 1.0f);
} else if (timerName == "CageClosed") {
// play the idle closed anim
GameMessages::SendPlayAnimation(self, u"idle");
// Setup a timer named 'ResetPrison' to be triggered in 10 seconds
self->AddTimer("ResetPrison", 10.0f);
} else if (timerName == "ResetPrison") {
Setup(self, self->GetVar<Spawner*>(u"CWSpawner"));
}
}