Skip to content

Commit

Permalink
Merge pull request #83 from red031000/main
Browse files Browse the repository at this point in the history
separate AgSpaceStuff and AgShipShake
  • Loading branch information
MickVermeulen authored Dec 7, 2021
2 parents a873de5 + 7bead89 commit 5281253
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 89 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ if (__include_backtrace__ AND __compile_backtrace__)

link_directories(${backtrace_SOURCE_DIR}/.libs/)
include_directories(${backtrace_SOURCE_DIR})
endif(__include_backtrace__)
endif(__include_backtrace__ AND __compile_backtrace__)

endif()
endif(UNIX)
Expand Down
12 changes: 9 additions & 3 deletions dGame/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,13 +1222,19 @@ void Entity::UpdateXMLDoc(tinyxml2::XMLDocument* doc) {
}

void Entity::Update(const float deltaTime) {
for (int i = 0; i < m_Timers.size(); i++) {
int timerSize = m_Timers.size();
for (int i = 0; i < timerSize; i++) {
m_Timers[i]->Update(deltaTime);
if (m_Timers[i]->GetTime() <= 0) {
const auto timerName = m_Timers[i]->GetName();

delete m_Timers[i];
m_Timers.erase(m_Timers.begin() + i);
do { //sometimes, due to a race condition, m_Timers.erase doesn't actually erase, repeat until it does
if (m_Timers[i]->GetName() != timerName) {
break;
}
delete m_Timers[i];
m_Timers.erase(m_Timers.begin() + i);
} while (m_Timers.size() == timerSize); //timer size indicates whether it's actually successfully been erased or not

for (CppScripts::Script* script : CppScripts::GetEntityScripts(this)) {
script->OnTimerDone(this, timerName);
Expand Down
93 changes: 93 additions & 0 deletions dScripts/AgShipShake.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "AgShipShake.h"
#include "GeneralUtils.h"
#include "GameMessages.h"
#include "dZoneManager.h"
#include "EntityManager.h"
#include "Game.h"

void AgShipShake::OnStartup(Entity* self) {
EntityInfo info{};

info.pos = { -418, 585, -30 };
info.lot = 33;
info.spawnerID = self->GetObjectID();

auto* ref = EntityManager::Instance()->CreateEntity(info);

EntityManager::Instance()->ConstructEntity(ref);

self->SetVar(u"ShakeObject", ref->GetObjectID());

self->AddTimer("ShipShakeIdle", 1.0f);
self->SetVar(u"RandomTime", 10);
}

void AgShipShake::OnTimerDone(Entity* self, std::string timerName) {
if (timerName == "ShipShakeExplode") {
DoShake(self, true);
}
else if (timerName == "ShipShakeIdle") {
DoShake(self, false);
}
}

void AgShipShake::DoShake(Entity* self, bool explodeIdle) {
if (!explodeIdle) {
auto* ref = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"ShakeObject"));

const auto randomTime = self->GetVar<int>(u"RandomTime");
auto time = GeneralUtils::GenerateRandomNumber<int>(0, randomTime + 1);

if (time < randomTime / 2) {
time += randomTime / 2;
}

self->AddTimer("ShipShakeIdle", static_cast<float>(time));

if (ref)
GameMessages::SendPlayEmbeddedEffectOnAllClientsNearObject(ref, FXName, ref->GetObjectID(), 500.0f);

auto* debrisObject = GetEntityInGroup(DebrisFX);

if (debrisObject)
GameMessages::SendPlayFXEffect(debrisObject, -1, u"DebrisFall", "Debris", LWOOBJID_EMPTY, 1.0f, 1.0f, true);

const auto randomFx = GeneralUtils::GenerateRandomNumber<int>(0, 3);

auto* shipFxObject = GetEntityInGroup(ShipFX);
if (shipFxObject) {
std::string effectType = "shipboom" + std::to_string(randomFx);
GameMessages::SendPlayFXEffect(shipFxObject, 559, GeneralUtils::ASCIIToUTF16(effectType), "FX", LWOOBJID_EMPTY, 1.0f, 1.0f, true);
}

self->AddTimer("ShipShakeExplode", 5.0f);

auto* shipFxObject2 = GetEntityInGroup(ShipFX2);
if (shipFxObject2)
GameMessages::SendPlayAnimation(shipFxObject2, u"explosion");
}
else {
auto* shipFxObject = GetEntityInGroup(ShipFX);
auto* shipFxObject2 = GetEntityInGroup(ShipFX2);

if (shipFxObject)
GameMessages::SendPlayAnimation(shipFxObject, u"idle");

if (shipFxObject2)
GameMessages::SendPlayAnimation(shipFxObject2, u"idle");
}
}

Entity* AgShipShake::GetEntityInGroup(const std::string& group) {
auto entities = EntityManager::Instance()->GetEntitiesInGroup(group);
Entity* en = nullptr;

for (auto entity : entities) {
if (entity) {
en = entity;
break;
}
}

return en;
}
18 changes: 18 additions & 0 deletions dScripts/AgShipShake.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include "CppScripts.h"

class AgShipShake : public CppScripts::Script {
public:
void OnStartup(Entity* self);
void OnTimerDone(Entity* self, std::string timerName);
void DoShake(Entity* self, bool explodeIdle);

std::string DebrisFX = "DebrisFX";
std::string ShipFX = "ShipFX";
std::string ShipFX2 = "ShipFX2";
std::u16string FXName = u"camshake-bridge";

private:
Entity* GetEntityInGroup(const std::string& group);
};

74 changes: 0 additions & 74 deletions dScripts/AgSpaceStuff.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "AgSpaceStuff.h"
#include "GeneralUtils.h"
#include "GameMessages.h"
#include "dZoneManager.h"
#include "EntityManager.h"
#include "Game.h"

Expand All @@ -17,11 +16,6 @@ void AgSpaceStuff::OnStartup(Entity* self) {
auto* ref = EntityManager::Instance()->CreateEntity(info);

EntityManager::Instance()->ConstructEntity(ref);

self->SetVar(u"ShakeObject", ref->GetObjectID());

self->AddTimer("ShipShakeIdle", 1.0f);
self->SetVar(u"RandomTime", 10);
}

void AgSpaceStuff::OnTimerDone(Entity* self, std::string timerName) {
Expand All @@ -38,72 +32,4 @@ void AgSpaceStuff::OnTimerDone(Entity* self, std::string timerName) {
GameMessages::SendPlayAnimation(self, u"path_0" + (GeneralUtils::to_u16string(pathType)));
self->AddTimer("FloaterScale", randTime);
}
else if (timerName == "ShipShakeExplode") {
DoShake(self, true);
}
else if (timerName == "ShipShakeIdle") {
DoShake(self, false);
}
}

void AgSpaceStuff::DoShake(Entity* self, bool explodeIdle) {

if (!explodeIdle) {
auto* ref = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"ShakeObject"));

const auto randomTime = self->GetVar<int>(u"RandomTime");
auto time = GeneralUtils::GenerateRandomNumber<int>(0, randomTime + 1);

if (time < randomTime / 2) {
time += randomTime / 2;
}

self->AddTimer("ShipShakeIdle", static_cast<float>(time));

if (ref)
GameMessages::SendPlayEmbeddedEffectOnAllClientsNearObject(ref, FXName, ref->GetObjectID(), 500.0f);

auto* debrisObject = GetEntityInGroup(DebrisFX);

if (debrisObject)
GameMessages::SendPlayFXEffect(debrisObject, -1, u"DebrisFall", "Debris", LWOOBJID_EMPTY, 1.0f, 1.0f, true);

const auto randomFx = GeneralUtils::GenerateRandomNumber<int>(0, 3);

auto* shipFxObject = GetEntityInGroup(ShipFX);
if (shipFxObject) {
std::string effectType = "shipboom" + std::to_string(randomFx);
GameMessages::SendPlayFXEffect(shipFxObject, 559, GeneralUtils::ASCIIToUTF16(effectType), "FX", LWOOBJID_EMPTY, 1.0f, 1.0f, true);
}

self->AddTimer("ShipShakeExplode", 5.0f);

auto* shipFxObject2 = GetEntityInGroup(ShipFX2);
if (shipFxObject2)
GameMessages::SendPlayAnimation(shipFxObject2, u"explosion");
}
else {
auto* shipFxObject = GetEntityInGroup(ShipFX);
auto* shipFxObject2 = GetEntityInGroup(ShipFX2);

if (shipFxObject)
GameMessages::SendPlayAnimation(shipFxObject, u"idle");

if (shipFxObject2)
GameMessages::SendPlayAnimation(shipFxObject2, u"idle");
}
}

Entity* AgSpaceStuff::GetEntityInGroup(const std::string& group) {
auto entities = EntityManager::Instance()->GetEntitiesInGroup(group);
Entity* en = nullptr;

for (auto entity : entities) {
if (entity) {
en = entity;
break;
}
}

return en;
}
10 changes: 0 additions & 10 deletions dScripts/AgSpaceStuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,4 @@ class AgSpaceStuff : public CppScripts::Script {
public:
void OnStartup(Entity* self);
void OnTimerDone(Entity* self, std::string timerName);
void DoShake(Entity* self, bool explodeIdle);

std::string DebrisFX = "DebrisFX";
std::string ShipFX = "ShipFX";
std::string ShipFX2 = "ShipFX2";
std::u16string FXName = u"camshake-bridge";

private:
Entity* GetEntityInGroup(const std::string& group);
};

5 changes: 4 additions & 1 deletion dScripts/CppScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//VE / AG scripts:
#include "AgShipPlayerDeathTrigger.h"
#include "AgShipPlayerShockServer.h"
#include "AgShipShake.h"
#include "AgSpaceStuff.h"
#include "AgImagSmashable.h"
#include "NpcNpSpacemanBob.h"
Expand Down Expand Up @@ -290,7 +291,9 @@ CppScripts::Script* CppScripts::GetScript(Entity* parent, const std::string& scr
script = new AgShipPlayerDeathTrigger();
else if (scriptName == "scripts\\ai\\NP\\L_NPC_NP_SPACEMAN_BOB.lua")
script = new NpcNpSpacemanBob();
else if (scriptName == "scripts\\ai\\AG\\L_AG_SPACE_STUFF.lua") // Broken, will (sometimes) display all animations at once on initial login
else if (scriptName == "scripts\\ai\\AG\\L_AG_SHIP_SHAKE.lua")
script = new AgShipShake();
else if (scriptName == "scripts\\ai\\AG\\L_AG_SPACE_STUFF.lua")
script = new AgSpaceStuff();
else if (scriptName == "scripts\\ai\\AG\\L_AG_SHIP_PLAYER_SHOCK_SERVER.lua")
script = new AgShipPlayerShockServer();
Expand Down

0 comments on commit 5281253

Please sign in to comment.