DarkflameServer/dScripts/AmSkullkinTower.cpp

244 lines
6.1 KiB
C++
Raw Normal View History

#include "AmSkullkinTower.h"
#include "EntityManager.h"
#include "DestroyableComponent.h"
#include "MovingPlatformComponent.h"
#include "GameMessages.h"
#include "MissionComponent.h"
2022-07-28 13:39:57 +00:00
void AmSkullkinTower::OnStartup(Entity* self) {
self->SetProximityRadius(20, "Tower");
2022-07-28 13:39:57 +00:00
// onPhysicsComponentReady
2022-07-28 13:39:57 +00:00
auto* movingPlatformComponent = self->GetComponent<MovingPlatformComponent>();
2022-07-28 13:39:57 +00:00
if (movingPlatformComponent != nullptr) {
movingPlatformComponent->StopPathing();
}
2022-07-28 13:39:57 +00:00
SpawnLegs(self, "Left");
SpawnLegs(self, "Right");
SpawnLegs(self, "Rear");
}
2022-07-28 13:39:57 +00:00
void AmSkullkinTower::SpawnLegs(Entity* self, const std::string& loc) {
auto pos = self->GetPosition();
auto rot = self->GetRotation();
pos.y += self->GetVarAs<float>(u"vert_offset");
auto newRot = rot;
auto offset = self->GetVarAs<float>(u"hort_offset");
auto legLOT = self->GetVar<LOT>(u"legLOT");
if (legLOT == 0) {
return;
}
std::vector<LDFBaseData*> config = { new LDFData<std::string>(u"Leg", loc) };
EntityInfo info{};
info.lot = legLOT;
info.spawnerID = self->GetObjectID();
info.settings = config;
info.rot = newRot;
if (loc == "Right") {
const auto dir = rot.GetForwardVector();
pos.x += dir.x * offset;
pos.z += dir.z * offset;
info.pos = pos;
} else if (loc == "Rear") {
const auto dir = rot.GetRightVector();
pos.x += dir.x * offset;
pos.z += dir.z * offset;
info.pos = pos;
} else if (loc == "Left") {
const auto dir = rot.GetForwardVector() * -1;
pos.x += dir.x * offset;
pos.z += dir.z * offset;
info.pos = pos;
}
info.rot = NiQuaternion::LookAt(info.pos, self->GetPosition());
auto* entity = EntityManager::Instance()->CreateEntity(info);
EntityManager::Instance()->ConstructEntity(entity);
OnChildLoaded(self, entity);
}
2022-07-28 13:39:57 +00:00
void AmSkullkinTower::OnChildLoaded(Entity* self, Entity* child) {
auto legTable = self->GetVar<std::vector<LWOOBJID>>(u"legTable");
2022-07-28 13:39:57 +00:00
legTable.push_back(child->GetObjectID());
2022-07-28 13:39:57 +00:00
self->SetVar(u"legTable", legTable);
2022-07-28 13:39:57 +00:00
const auto selfID = self->GetObjectID();
2022-07-28 13:39:57 +00:00
child->AddDieCallback([this, selfID, child]() {
auto* self = EntityManager::Instance()->GetEntity(selfID);
auto* destroyableComponent = child->GetComponent<DestroyableComponent>();
2022-07-28 13:39:57 +00:00
if (destroyableComponent == nullptr || self == nullptr) {
return;
}
2022-07-28 13:39:57 +00:00
NotifyDie(self, child, destroyableComponent->GetKiller());
});
}
2022-07-28 13:39:57 +00:00
void AmSkullkinTower::NotifyDie(Entity* self, Entity* other, Entity* killer) {
auto players = self->GetVar<std::vector<LWOOBJID>>(u"Players");
2022-07-28 13:39:57 +00:00
const auto& iter = std::find(players.begin(), players.end(), killer->GetObjectID());
2022-07-28 13:39:57 +00:00
if (iter == players.end()) {
players.push_back(killer->GetObjectID());
}
2022-07-28 13:39:57 +00:00
self->SetVar(u"Players", players);
2022-07-28 13:39:57 +00:00
OnChildRemoved(self, other);
}
2022-07-28 13:39:57 +00:00
void AmSkullkinTower::OnChildRemoved(Entity* self, Entity* child) {
auto legTable = self->GetVar<std::vector<LWOOBJID>>(u"legTable");
2022-07-28 13:39:57 +00:00
const auto& iter = std::find(legTable.begin(), legTable.end(), child->GetObjectID());
2022-07-28 13:39:57 +00:00
if (iter != legTable.end()) {
legTable.erase(iter);
}
2022-07-28 13:39:57 +00:00
self->SetVar(u"legTable", legTable);
2022-07-28 13:39:57 +00:00
if (legTable.size() == 2) {
GameMessages::SendPlayAnimation(self, u"wobble-1");
} else if (legTable.size() == 1) {
GameMessages::SendPlayAnimation(self, u"wobble-2");
} else if (legTable.empty()) {
const auto animTime = 2.5f;
2022-07-28 13:39:57 +00:00
GameMessages::SendPlayAnimation(self, u"fall");
2022-07-28 13:39:57 +00:00
self->AddTimer("spawnGuys", animTime - 0.2f);
2022-07-28 13:39:57 +00:00
self->CancelTimer("RespawnLeg");
self->CancelTimer("RespawnLeg");
self->CancelTimer("RespawnLeg");
2022-07-28 13:39:57 +00:00
std::vector<int32_t> missionIDs;
2022-07-28 13:39:57 +00:00
auto missionsString = self->GetVar<std::u16string>(u"missions");
2022-07-28 13:39:57 +00:00
if (!missionsString.empty()) {
// Split the missions string by '_'
const auto missions = GeneralUtils::SplitString(
GeneralUtils::UTF16ToWTF8(missionsString),
'_'
);
2022-07-28 13:39:57 +00:00
for (const auto& mission : missions) {
int32_t missionID = 0;
2022-07-28 13:39:57 +00:00
if (!GeneralUtils::TryParse(mission, missionID)) {
continue;
}
2022-07-28 13:39:57 +00:00
missionIDs.push_back(missionID);
}
}
2022-07-28 13:39:57 +00:00
const auto& players = self->GetVar<std::vector<LWOOBJID>>(u"Players");
2022-07-28 13:39:57 +00:00
for (const auto& playerID : players) {
auto* player = EntityManager::Instance()->GetEntity(playerID);
2022-07-28 13:39:57 +00:00
if (player == nullptr) {
continue;
}
2022-07-28 13:39:57 +00:00
auto* missionComponent = player->GetComponent<MissionComponent>();
2022-07-28 13:39:57 +00:00
if (missionComponent == nullptr) {
continue;
}
2022-07-28 13:39:57 +00:00
for (const auto missionID : missionIDs) {
missionComponent->ForceProgressValue(missionID, 1, self->GetLOT());
}
2022-07-28 13:39:57 +00:00
//missionComponent->ForceProgressValue(1305, 1, self->GetLOT());
}
}
2022-07-28 13:39:57 +00:00
auto deadLegs = self->GetVar<std::vector<std::string>>(u"DeadLegs");
2022-07-28 13:39:57 +00:00
const auto& leg = child->GetVar<std::string>(u"Leg");
2022-07-28 13:39:57 +00:00
const auto& legIter = std::find(deadLegs.begin(), deadLegs.end(), leg);
2022-07-28 13:39:57 +00:00
if (legIter == deadLegs.end()) {
deadLegs.push_back(leg);
}
2022-07-28 13:39:57 +00:00
self->SetVar(u"DeadLegs", deadLegs);
self->AddTimer("RespawnLeg", 20);
}
2022-07-28 13:39:57 +00:00
void AmSkullkinTower::OnProximityUpdate(Entity* self, Entity* entering, std::string name, std::string status) {
if (status != "LEAVE") {
return;
}
2022-07-28 13:39:57 +00:00
auto players = self->GetVar<std::vector<LWOOBJID>>(u"Players");
2022-07-28 13:39:57 +00:00
const auto& iter = std::find(players.begin(), players.end(), entering->GetObjectID());
2022-07-28 13:39:57 +00:00
if (iter != players.end()) {
players.erase(iter);
}
2022-07-28 13:39:57 +00:00
self->SetVar(u"Players", players);
}
2022-07-28 13:39:57 +00:00
void AmSkullkinTower::OnTimerDone(Entity* self, std::string timerName) {
if (timerName == "RespawnLeg") {
auto deadLegs = self->GetVar<std::vector<std::string>>(u"DeadLegs");
if (deadLegs.empty()) {
return;
}
SpawnLegs(self, deadLegs[0]);
deadLegs.erase(deadLegs.begin());
self->SetVar<std::vector<std::string>>(u"DeadLegs", deadLegs);
} else if (timerName == "spawnGuys") {
EntityInfo info{};
info.lot = self->GetVar<LOT>(u"enemyToSpawn");
auto pos = self->GetPosition();
pos.y += 7;
info.pos = pos;
info.rot = self->GetRotation();
info.spawnerID = self->GetObjectID();
for (size_t i = 0; i < 2; i++) {
info.pos.x += i * 2; // Just to set the apart a bit
auto* entity = EntityManager::Instance()->CreateEntity(info);
EntityManager::Instance()->ConstructEntity(entity);
}
self->AddTimer("killTower", 0.7f);
} else if (timerName == "killTower") {
self->Smash(self->GetObjectID());
}
}