From 59c23026d2684ab66ae927777da821825fcbf939 Mon Sep 17 00:00:00 2001 From: PlatinumSkink Date: Thu, 22 Oct 2015 10:34:22 +0200 Subject: [PATCH] Put all LifeStuff into a new LifeSystem. Appears to be working. --- include/Core/Engine.h | 4 ++ include/Game/BallSystem.h | 14 ++--- include/Game/LevelSystem.h | 2 +- include/Game/LifeSystem.h | 75 ++++++++++++++++++++++++++ src/game/Game/BallSystem.cpp | 60 +-------------------- src/game/Game/LifeSystem.cpp | 101 +++++++++++++++++++++++++++++++++++ 6 files changed, 186 insertions(+), 70 deletions(-) create mode 100644 include/Game/LifeSystem.h create mode 100644 src/game/Game/LifeSystem.cpp diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 1e90272..807fccd 100755 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -55,6 +55,7 @@ #include "Game/CTravels.h" #include "Game/CStickyAim.h" #include "Game/BallSystem.h" +#include "Game/LifeSystem.h" #include "Game/HitLagSystem.h" #include "Game/TravellingSystem.h" #include "Game/LifebuoySystem.h" @@ -199,6 +200,9 @@ public: m_World->SystemFactory.Register( [this]() { return new Systems::WaterSystem(m_World.get(), m_EventBroker); }); m_World->AddSystem(); + m_World->SystemFactory.Register( + [this]() { return new Systems::LifeSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); diff --git a/include/Game/BallSystem.h b/include/Game/BallSystem.h index 46e50ef..eb159db 100644 --- a/include/Game/BallSystem.h +++ b/include/Game/BallSystem.h @@ -10,14 +10,17 @@ #include "Core/CTemplate.h" #include "Core/ResourceManager.h" #include "Core/ConfigFile.h" + #include "Physics/CPhysics.h" #include "Physics/CCircleShape.h" #include "Physics/CRectangleShape.h" #include "Physics/EContact.h" + #include "Rendering/CModel.h" #include "Rendering/CSprite.h" #include "Rendering/CPointLight.h" #include "Rendering/CAnimation.h" + #include "Game/CBall.h" #include "Game/CPowerUp.h" #include "Game/CLife.h" @@ -43,6 +46,7 @@ #include "Game/EHitLag.h" #include "Game/EActionButton.h" #include "Game/ELifebuoyHit.h" + #include "Physics/ECreateParticleSequence.h" #include "Sound/CCollisionSound.h" @@ -79,7 +83,6 @@ public: void OnEntityRemoved(EntityID entity) override; EntityID CreateBall(); - void CreateLife(int); EntityID Ball() { return m_Ball; }; void SetBall(const EntityID& ball) { m_Ball = ball; } @@ -91,10 +94,6 @@ public: void SetEdgeY(const float& edgeY) { m_EdgeY = edgeY; } int& MultiBalls() { return m_MultiBalls; } void SetMultiBalls(const int& multiBalls) { m_MultiBalls = multiBalls; } - int& Lives() { return m_Lives; } - void SetLives(const int& lives) { m_Lives = lives; } - int& PastLives() { return m_PastLives; } - void SetPastLives(const int& pastLives) { m_PastLives = pastLives; } bool ReplaceBall() const { return m_ReplaceBall; } void SetReplaceBall(const bool& replaceBall) { m_ReplaceBall = replaceBall; } bool IsPaused() const { return m_Pause; } @@ -107,8 +106,6 @@ private: float m_EdgeX = 3.4f; //Actually 3.2, but anyways. float m_EdgeY = 5.2f; int m_MultiBalls = 0; - int m_Lives = 3; - int m_PastLives = 3; bool m_ReplaceBall = false; bool m_Pause = false; bool m_Waiting = true; @@ -120,7 +117,6 @@ private: double m_KrakenCharge = 0; bool m_Restarting = false; int m_StickyCounter = 3; - bool m_GodMode = false; bool m_First = true; @@ -134,7 +130,6 @@ private: std::unordered_map> m_Contacts; void ResolveContacts(); - dd::EventRelay m_ELifeLost; dd::EventRelay m_EMultiBallLost; dd::EventRelay m_EResetBall; dd::EventRelay m_EMultiBall; @@ -151,7 +146,6 @@ private: dd::EventRelay m_EArrivedAtNewStage; bool Contact(const Events::Contact &event); - bool OnLifeLost(const dd::Events::LifeLost &event); bool OnMultiBallLost(const dd::Events::MultiBallLost &event); bool OnResetBall(const dd::Events::ResetBall &event); bool OnMultiBall(const dd::Events::MultiBall &event); diff --git a/include/Game/LevelSystem.h b/include/Game/LevelSystem.h index 95a3397..cc5d75c 100755 --- a/include/Game/LevelSystem.h +++ b/include/Game/LevelSystem.h @@ -144,7 +144,7 @@ private: bool m_Initialized = false; bool m_Pause = false; int m_LooseBricks = 0; - int m_CurrentCluster = 0; + int m_CurrentCluster = 1; int m_CurrentLevel = 1; int m_MultiBalls = 0; int m_PowerUps = 0; diff --git a/include/Game/LifeSystem.h b/include/Game/LifeSystem.h new file mode 100644 index 0000000..249e9ee --- /dev/null +++ b/include/Game/LifeSystem.h @@ -0,0 +1,75 @@ +// +// Created by Adniklastrator on 2015-10-22. +// + +#ifndef DAYDREAM_LIFESYSTEM_H +#define DAYDREAM_LIFESYSTEM_H + + +#include "Core/System.h" +#include "Core/CTransform.h" +#include "Core/CTemplate.h" +#include "Core/EventBroker.h" +#include "Core/World.h" + +#include "Core/ConfigFile.h" + +#include "Transform/EMove.h" +#include "Rendering/CModel.h" + +#include "Game/CLife.h" +#include "Game/EPause.h" +#include "Game/EResume.h" +#include "Game/ELifeLost.h" +#include "Game/EGameOver.h" +#include "Game/EKrakenAttackEnd.h" + + +namespace dd +{ + +namespace Systems +{ + +class LifeSystem : public System +{ +public: + LifeSystem(World* world, std::shared_ptr eventBroker) + : System(world, eventBroker) + { } + + + void Initialize() override; + void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + + bool IsPaused() const { return m_Pause; } + void SetPause(const bool& pause) { m_Pause = pause; } + int& Lives() { return m_Lives; } + void SetLives(const int& lives) { m_Lives = lives; } + int& PastLives() { return m_PastLives; } + void SetPastLives(const int& pastLives) { m_PastLives = pastLives; } + +private: + bool m_Pause = false; + int m_Lives = 3; + int m_PastLives = 3; + + bool m_GodMode = false; + + void CreateLife(int); + + dd::EventRelay m_EPause; + dd::EventRelay m_EResume; + dd::EventRelay m_ELifeLost; + + bool OnPause(const dd::Events::Pause &event); + bool OnResume(const dd::Events::Resume &event); + bool OnLifeLost(const dd::Events::LifeLost &event); +}; + +} + +} + +#endif //DAYDREAM_LIFESYSTEM_H diff --git a/src/game/Game/BallSystem.cpp b/src/game/Game/BallSystem.cpp index 31377f1..786962b 100644 --- a/src/game/Game/BallSystem.cpp +++ b/src/game/Game/BallSystem.cpp @@ -15,7 +15,6 @@ void dd::Systems::BallSystem::Initialize() std::random_device rd; m_RandomGenerator = std::mt19937(rd()); EVENT_SUBSCRIBE_MEMBER(m_Contact, &BallSystem::Contact); - EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, &BallSystem::OnLifeLost); EVENT_SUBSCRIBE_MEMBER(m_EMultiBallLost, &BallSystem::OnMultiBallLost); EVENT_SUBSCRIBE_MEMBER(m_EResetBall, &BallSystem::OnResetBall); EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, &BallSystem::OnMultiBall); @@ -67,33 +66,11 @@ void dd::Systems::BallSystem::Initialize() transform2->Velocity = glm::vec3(0.0f, -10.f, 0.f); } - for (int i = 0; i < Lives(); i++) { - CreateLife(i); - } - SetReplaceBall(true); - - m_GodMode = ResourceManager::Load("Config.ini")->GetValue("Cheat.GodMode", false); } void dd::Systems::BallSystem::Update(double dt) { - if (Lives() == 0) { - if (m_KrakenAttack) { - Events::KrakenAttackEnd e; - EventBroker->Publish(e); - } - - Events::GameOver e; - EventBroker->Publish(e); - - Events::Pause p; - p.Type = "All"; - EventBroker->Publish(p); - - //TODO: Make this not so ugly - SetLives(-1); - } ResolveContacts(); } @@ -189,7 +166,7 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID m_World->RemoveEntity(entity); Events::MultiBallLost e; EventBroker->Publish(e); - } else if (Lives() == PastLives() && !m_Restarting) { + } else if (!m_Restarting) { //If we lose lives when we shouldn't, look here. Events::ResetBall be; EventBroker->Publish(be); Events::LifeLost e; @@ -216,16 +193,6 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID }*/ } - if (Lives() != PastLives()) { - auto life = m_World->GetComponent(entity); - if (life != nullptr) { - if (life->Number + 1 == PastLives()) { - m_World->RemoveEntity(entity); - SetPastLives(Lives()); - } - } - } - if (ballComponent != nullptr) { if (!ballComponent->Sticky) { @@ -452,31 +419,6 @@ EntityID dd::Systems::BallSystem::CreateBall() return ent; } -void dd::Systems::BallSystem::CreateLife(int number) -{ - auto life = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(life); - transform->Position = glm::vec3(-1.5f + number * 0.15f, -2.f, -5.f); - transform->Scale = glm::vec3(0.1f, 0.1f, 0.1f); - - std::shared_ptr lifeNr = m_World->AddComponent(life); - lifeNr->Number = number; - - auto model = m_World->AddComponent(life); - model->ModelFile = "Models/Sid/Sid.dae"; - - - m_World->CommitEntity(life); -} - -bool dd::Systems::BallSystem::OnLifeLost(const dd::Events::LifeLost &event) -{ - if (!m_GodMode){ - SetLives(Lives() - 1); - } - return true; -} - bool dd::Systems::BallSystem::OnMultiBallLost(const dd::Events::MultiBallLost &event) { SetMultiBalls(MultiBalls() - 1); diff --git a/src/game/Game/LifeSystem.cpp b/src/game/Game/LifeSystem.cpp new file mode 100644 index 0000000..968ed17 --- /dev/null +++ b/src/game/Game/LifeSystem.cpp @@ -0,0 +1,101 @@ +// +// Created by Adniklastrator on 2015-10-13. +// + +#include "PrecompiledHeader.h" +#include "Game/LifeSystem.h" + +void dd::Systems::LifeSystem::Initialize() +{ + EVENT_SUBSCRIBE_MEMBER(m_EPause, &LifeSystem::OnPause); + EVENT_SUBSCRIBE_MEMBER(m_EResume, &LifeSystem::OnResume); + EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, &LifeSystem::OnLifeLost); + + for (int i = 0; i < Lives(); i++) { + CreateLife(i); + } + + m_GodMode = ResourceManager::Load("Config.ini")->GetValue("Cheat.GodMode", false); +} + +void dd::Systems::LifeSystem::Update(double dt) +{ + if (m_Pause) { + return; + } + if (Lives() < 0) { + Events::KrakenAttackEnd ek; + EventBroker->Publish(ek); + + Events::GameOver e; + EventBroker->Publish(e); + + Events::Pause p; + p.Type = "All"; + EventBroker->Publish(p); + + //TODO: Make this not so ugly + SetLives(3); + } +} + +void dd::Systems::LifeSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + if (m_Pause) { + return; + } + + auto templateCheck = m_World->GetComponent(entity); + if (templateCheck != nullptr){ return; } + + if (Lives() != PastLives()) { + auto life = m_World->GetComponent(entity); + if (life != nullptr) { + if (life->Number + 1 == PastLives()) { + m_World->RemoveEntity(entity); + SetPastLives(Lives()); + } + } + } +} + +void dd::Systems::LifeSystem::CreateLife(int number) +{ + auto life = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(life); + transform->Position = glm::vec3(-1.5f + number * 0.15f, -2.f, -5.f); + transform->Scale = glm::vec3(0.1f, 0.1f, 0.1f); + + std::shared_ptr lifeNr = m_World->AddComponent(life); + lifeNr->Number = number; + + auto model = m_World->AddComponent(life); + model->ModelFile = "Models/Sid/Sid.dae"; + + m_World->CommitEntity(life); +} + +bool dd::Systems::LifeSystem::OnLifeLost(const dd::Events::LifeLost &event) +{ + if (!m_GodMode){ + SetLives(Lives() - 1); + } + return true; +} + +bool dd::Systems::LifeSystem::OnPause(const dd::Events::Pause &event) +{ + /*if (event.Type != "LifeSystem" && event.Type != "All") { + return false; + }*/ + m_Pause = true; + return true; +} +bool dd::Systems::LifeSystem::OnResume(const dd::Events::Resume &event) +{ + /*if (event.Type != "LifeSystem" && event.Type != "All") { + return false; + }*/ + m_Pause = false; + return true; +} \ No newline at end of file