Files
DarkflameServer/dScripts/ai/GF/GfBanana.cpp
2025-10-16 13:18:13 +01:00

116 lines
3.1 KiB
C++

#include "GfBanana.h"
#include "Entity.h"
#include "DestroyableComponent.h"
#include "EntityInfo.h"
#include "EntityManager.h"
#include "GeneralUtils.h"
void GfBanana::SpawnBanana(Entity* self) {
auto position = self->GetPosition();
const auto rotation = self->GetRotation();
position.y += 12;
position.x -= QuatUtils::Right(rotation).x * 5;
position.z -= QuatUtils::Right(rotation).z * 5;
EntityInfo info{};
info.pos = position;
info.rot = rotation;
info.lot = 6909;
info.spawnerID = self->GetObjectID();
auto* entity = Game::entityManager->CreateEntity(info);
Game::entityManager->ConstructEntity(entity);
self->SetVar(u"banana", entity->GetObjectID());
entity->AddDieCallback([self]() {
self->SetVar(u"banana", LWOOBJID_EMPTY);
self->AddTimer("bananaTimer", 30);
});
}
void GfBanana::OnStartup(Entity* self) {
SpawnBanana(self);
}
void GfBanana::OnHit(Entity* self, Entity* attacker) {
auto* destroyable = self->GetComponent<DestroyableComponent>();
destroyable->SetHealth(9999);
const auto bananaId = self->GetVar<LWOOBJID>(u"banana");
if (bananaId == LWOOBJID_EMPTY) {
if (GeneralUtils::GenerateRandomNumber<short>(1, 100) == 100 && self->GetVar<LWOOBJID>(u"apeId") == LWOOBJID_EMPTY) {
// ape appears if you hurt the trees feeling while it has no banana
// play fx effect
GameMessages::SendPlayEmbeddedEffectOnAllClientsNearObject(self, u"camshake-bridge", self->GetObjectID(), 100.0f);
auto position = self->GetPosition();
auto rotation = QuatUtils::LookAt(self->GetPosition(), position);
EntityInfo info {};
info.pos = position;
info.rot = rotation;
info.lot = m_GorillaLOT;
info.spawner = nullptr;
info.spawnerID = self->GetObjectID();
info.spawnerNodeID = 0;
auto* entity = Game::entityManager->CreateEntity(info);
Game::entityManager->ConstructEntity(entity, UNASSIGNED_SYSTEM_ADDRESS);
self->SetVar<LWOOBJID>(u"apeId", entity->GetObjectID());
self->AddTimer("apeTimer", 10);
}
return;
}
auto* bananaEntity = Game::entityManager->GetEntity(bananaId);
if (bananaEntity == nullptr) {
self->SetVar(u"banana", LWOOBJID_EMPTY);
self->AddTimer("bananaTimer", 30);
return;
}
auto bananaFloor = (bananaEntity->GetPosition() - NiPoint3Constant::UNIT_Y * 8);
bananaEntity->SetPosition(bananaFloor);
auto* bananaDestroyable = bananaEntity->GetComponent<DestroyableComponent>();
bananaDestroyable->SetHealth(0);
bananaDestroyable->Smash(attacker->GetObjectID());
Game::entityManager->SerializeEntity(self);
}
void GfBanana::OnTimerDone(Entity* self, std::string timerName) {
if (timerName == "bananaTimer") {
SpawnBanana(self);
} else if (timerName == "apeTimer") {
if (self->GetVar<LWOOBJID>(u"apeId") == LWOOBJID_EMPTY) return;
auto entity = Game::entityManager->GetEntity(self->GetVar<LWOOBJID>(u"apeId"));
if (entity) {
GameMessages::SendPlayEmbeddedEffectOnAllClientsNearObject(self, u"camshake-bridge", self->GetObjectID(), 100.0f);
entity->GetComponent<DestroyableComponent>()->Smash(self->GetObjectID(), eKillType::SILENT); // to destroy the anchor too
Game::entityManager->DestroyEntity(entity);
}
self->SetVar(u"apeId", LWOOBJID_EMPTY);
}
}