From c63ec035a707051ee4d66e5c17d7449032c8af93 Mon Sep 17 00:00:00 2001 From: PlatinumSkink Date: Wed, 30 Sep 2015 16:42:35 +0200 Subject: [PATCH] HitLagSystem implemented. --- include/Core/Engine.h | 4 ++ include/Game/BallSystem.h | 13 +---- include/Game/HitLagSystem.h | 83 ++++++++++++++++++++++++++++++ include/Game/LevelSystem.h | 2 +- include/Game/PadSystem.h | 2 +- include/Physics/PhysicsSystem.h | 13 +---- src/game/Game/BallSystem.cpp | 34 +----------- src/game/Game/HitLagSystem.cpp | 48 +++++++++++++++++ src/game/Game/LevelSystem.cpp | 4 +- src/game/Game/PadSystem.cpp | 8 +-- src/game/Physics/PhysicsSystem.cpp | 26 +--------- 11 files changed, 147 insertions(+), 90 deletions(-) create mode 100644 include/Game/HitLagSystem.h create mode 100644 src/game/Game/HitLagSystem.cpp diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 38142a7..c07919b 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -45,6 +45,7 @@ #include "Game/CPad.h" #include "Game/CBrick.h" #include "Game/BallSystem.h" +#include "Game/HitLagSystem.h" #include "Game/Bricks/CPowerUpBrick.h" #include "Physics/PhysicsSystem.h" @@ -120,6 +121,9 @@ public: m_World->SystemFactory.Register( [this]() { return new Systems::BallSystem(m_World.get(), m_EventBroker); }); m_World->AddSystem(); + m_World->SystemFactory.Register( + [this]() { return new Systems::HitLagSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); m_World->SystemFactory.Register( [this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); }); m_World->AddSystem(); diff --git a/include/Game/BallSystem.h b/include/Game/BallSystem.h index c0e94fe..f56a08a 100644 --- a/include/Game/BallSystem.h +++ b/include/Game/BallSystem.h @@ -21,7 +21,6 @@ #include "Game/EMultiBallLost.h" #include "Game/EGameOver.h" #include "Game/EPause.h" -#include "Game/EHitLag.h" namespace dd { @@ -77,10 +76,8 @@ public: void SetReplaceBall(const bool& replaceBall) { m_ReplaceBall = replaceBall; } bool MultiBall() const { return m_MultiBall; } void SetMultiBall(const bool& multiBall) { m_MultiBall = multiBall; } - bool Pause() const { return m_Pause; } + bool IsPaused() const { return m_Pause; } void SetPause(const bool& pause) { m_Pause = pause; } - bool HitLag() const { return m_HitLag; } - void SetHitLag(const bool& hitLag) { m_HitLag = hitLag; } private: float m_XMovementMultiplier = 2.f; @@ -93,12 +90,6 @@ private: bool m_MultiBall = false; bool m_Pause = false; - bool m_HitLag = false; - float m_Time; - float m_SlowdownTime; - float m_HitLagTimer = 0; - float m_HitLagPlacement = 0; - EntityID m_Ball; dd::EventRelay m_ELifeLost; @@ -106,14 +97,12 @@ private: dd::EventRelay m_EResetBall; dd::EventRelay m_EMultiBall; dd::EventRelay m_EPause; - dd::EventRelay m_EHitLag; 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); bool OnPause(const dd::Events::Pause &event); - bool OnHitLag(const dd::Events::HitLag &event); }; } diff --git a/include/Game/HitLagSystem.h b/include/Game/HitLagSystem.h new file mode 100644 index 0000000..8367dd6 --- /dev/null +++ b/include/Game/HitLagSystem.h @@ -0,0 +1,83 @@ +// +// Created by Adniklastrator on 2015-09-30. +// + +#ifndef DAYDREAM_HITLAGSYSTEM_H +#define DAYDREAM_HITLAGSYSTEM_H + + +#include "Core/System.h" +#include "Core/CTransform.h" +#include "Core/CTemplate.h" +#include "Core/EventBroker.h" +#include "Core/World.h" +#include "Rendering/CSprite.h" +#include "Rendering/CModel.h" +#include "Game/CBrick.h" +#include "Game/CBall.h" +#include "Game/CLife.h" +#include "Game/CPowerUp.h" +#include "Game/EStageCleared.h" +#include "Game/ELifeLost.h" +#include "Game/EResetBall.h" +#include "Game/EScoreEvent.h" +#include "Game/EComboEvent.h" +#include "Game/EMultiBall.h" +#include "Game/EMultiBallLost.h" +#include "Game/EPause.h" +#include "Game/EHitLag.h" +#include "Game/EGameOver.h" +#include "Game/ECreatePowerUp.h" +#include "Game/EPowerUpTaken.h" +#include "Game/Bricks/CPowerUpBrick.h" +#include "Physics/CPhysics.h" +#include "Physics/CCircleShape.h" +#include "Physics/CRectangleShape.h" +#include "Physics/ESetImpulse.h" +#include "Physics/EContact.h" +#include "Sound/CCollisionSound.h" +#include "Game/PadSystem.h" +#include +#include + + +namespace dd +{ + +namespace Systems +{ + +class HitLagSystem : public System +{ +public: + HitLagSystem(World* world, std::shared_ptr eventBroker) + : System(world, eventBroker) + { } + + void Initialize() override; + + void Update(double dt) override; + + bool IsPaused() const { return m_Pause; } + void SetPause(const bool& pause) { m_Pause = pause; } + bool HitLag() const { return m_HitLag; } + void SetHitLag(const bool& hitLag) { m_HitLag = hitLag; } +private: + bool m_Pause; + bool m_HitLag; + float m_HitLagDuration; + float m_HitLagTimer; + + dd::EventRelay m_EPause; + dd::EventRelay m_EHitLag; + + bool OnPause(const dd::Events::Pause &event); + bool OnHitLag(const dd::Events::HitLag &event); + +}; + +} + +} + +#endif //DAYDREAM_HITLAGSYSTEM_H diff --git a/include/Game/LevelSystem.h b/include/Game/LevelSystem.h index f8b4603..67131c6 100755 --- a/include/Game/LevelSystem.h +++ b/include/Game/LevelSystem.h @@ -81,7 +81,7 @@ public: void SetRestarting(const bool& restarting) { m_Restarting = restarting; } bool Initialized() const { return m_Initialized; } void SetInitialized(const bool& initialized) { m_Initialized = initialized; } - bool Pause() const { return m_Pause; } + bool IsPaused() const { return m_Pause; } void SetPause(const bool& pause) { m_Pause = pause; } int& MultiBalls() { return m_MultiBalls; } void SetMultiBalls(const int& multiBalls) { m_MultiBalls = multiBalls; } diff --git a/include/Game/PadSystem.h b/include/Game/PadSystem.h index 9328972..ef76104 100755 --- a/include/Game/PadSystem.h +++ b/include/Game/PadSystem.h @@ -65,7 +65,7 @@ public: Components::Pad* Pad() const { return m_Pad; } void SetPad(Components::Pad* pad) { m_Pad = pad; } - bool Pause() const { return m_Pause; } + bool IsPaused() const { return m_Pause; } void SetPause(const bool& pause) { m_Pause = pause; } private: diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index 4f9a8ef..c970eb0 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -14,7 +14,6 @@ #include "Physics/EContact.h" #include "Physics/ESetImpulse.h" #include "Game/EPause.h" -#include "Game/EHitLag.h" #include "Game/CPad.h" #include "Game/CBall.h" #include "Transform/TransformSystem.h" @@ -56,10 +55,8 @@ public: // Called when an entity is removed void OnEntityRemoved(EntityID entity) override; - bool Pause() const { return m_Pause; } + bool IsPaused() const { return m_Pause; } void SetPause(const bool& pause) { m_Pause = pause; } - bool HitLag() const { return m_HitLag; } - void SetHitLag(const bool& hitLag) { m_HitLag = hitLag; } private: b2Vec2 m_Gravity; @@ -68,16 +65,8 @@ private: double m_Accumulator; bool m_Pause = false; - bool m_HitLag = false; - float m_Time; - float m_SlowdownTime; - float m_HitLagTimer = 0; - float m_HitLagPlacement = 0; - dd::EventRelay m_EPause; - dd::EventRelay m_EHitLag; bool OnPause(const dd::Events::Pause &event); - bool OnHitLag(const dd::Events::HitLag &event); int m_VelocityIterations, m_PositionIterations; diff --git a/src/game/Game/BallSystem.cpp b/src/game/Game/BallSystem.cpp index d7d264d..84e103e 100644 --- a/src/game/Game/BallSystem.cpp +++ b/src/game/Game/BallSystem.cpp @@ -19,7 +19,6 @@ void dd::Systems::BallSystem::Initialize() EVENT_SUBSCRIBE_MEMBER(m_EResetBall, &BallSystem::OnResetBall); EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, &BallSystem::OnMultiBall); EVENT_SUBSCRIBE_MEMBER(m_EPause, &BallSystem::OnPause); - EVENT_SUBSCRIBE_MEMBER(m_EHitLag, &BallSystem::OnHitLag); //OctoBall { @@ -68,27 +67,11 @@ void dd::Systems::BallSystem::Update(double dt) Events::GameOver e; EventBroker->Publish(e); } - - /* if (HitLag()) { - m_HitLagTimer += 0.1 * dt; - if (Pause()) { - if (m_HitLagTimer > 1) { - SetPause(false); - m_HitLagTimer = 0; - } - } else { - if (m_HitLagTimer > m_HitLagPlacement) { - SetPause(true); - m_HitLagPlacement *= 0.9; - m_HitLagTimer = 0; - } - } - }*/ } void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - if (Pause()) { + if (IsPaused()) { return; } @@ -170,7 +153,7 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID bool dd::Systems::BallSystem::OnPause(const dd::Events::Pause &event) { - if (Pause()) { + if (IsPaused()) { SetPause(false); } else { SetPause(true); @@ -178,19 +161,6 @@ bool dd::Systems::BallSystem::OnPause(const dd::Events::Pause &event) return true; } -bool dd::Systems::BallSystem::OnHitLag(const dd::Events::HitLag &event) -{ - if (HitLag()) { - SetHitLag(false); - } else { - SetHitLag(true); - m_Time = event.Time; - m_SlowdownTime = event.SlowdownTime; - m_HitLagTimer = m_SlowdownTime; - } - return true; -} - void dd::Systems::BallSystem::OnEntityCommit(EntityID entity) { diff --git a/src/game/Game/HitLagSystem.cpp b/src/game/Game/HitLagSystem.cpp new file mode 100644 index 0000000..92deb9a --- /dev/null +++ b/src/game/Game/HitLagSystem.cpp @@ -0,0 +1,48 @@ +// +// Created by Adniklastrator on 2015-09-30. +// + +#include "PrecompiledHeader.h" +#include "Game/HitLagSystem.h" + +void dd::Systems::HitLagSystem::Initialize() +{ + EVENT_SUBSCRIBE_MEMBER(m_EPause, &HitLagSystem::OnPause); + EVENT_SUBSCRIBE_MEMBER(m_EHitLag, &HitLagSystem::OnHitLag); +} + +void dd::Systems::HitLagSystem::Update(double dt) +{ + if (IsPaused()) { + if (HitLag()) { + m_HitLagTimer += dt; + std::cout << m_HitLagTimer << std::endl; + if (m_HitLagDuration < m_HitLagTimer) { + m_HitLagTimer = 0; + SetHitLag(false); + Events::Pause e; + EventBroker->Publish(e); + } + } + return; + } +} + +bool dd::Systems::HitLagSystem::OnPause(const dd::Events::Pause &event) +{ + if (IsPaused()) { + SetPause(false); + } else { + SetPause(true); + } + return true; +} + +bool dd::Systems::HitLagSystem::OnHitLag(const dd::Events::HitLag &event) +{ + SetHitLag(true); + Events::Pause e; + EventBroker->Publish(e); + m_HitLagDuration = event.Time; + return true; +} \ No newline at end of file diff --git a/src/game/Game/LevelSystem.cpp b/src/game/Game/LevelSystem.cpp index 4696491..c85132a 100755 --- a/src/game/Game/LevelSystem.cpp +++ b/src/game/Game/LevelSystem.cpp @@ -31,7 +31,7 @@ void dd::Systems::LevelSystem::Update(double dt) void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - if (Pause()) { + if (IsPaused()) { return; } auto templateCheck = m_World->GetComponent(entity); @@ -77,7 +77,7 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID bool dd::Systems::LevelSystem::OnPause(const dd::Events::Pause &event) { - if (Pause()) { + if (IsPaused()) { SetPause(false); } else { SetPause(true); diff --git a/src/game/Game/PadSystem.cpp b/src/game/Game/PadSystem.cpp index 887d6a2..becb0f6 100755 --- a/src/game/Game/PadSystem.cpp +++ b/src/game/Game/PadSystem.cpp @@ -59,7 +59,7 @@ void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID p void dd::Systems::PadSystem::Update(double dt) { - if (Pause()) { + if (IsPaused()) { return; } if (Entity() == 0) { @@ -111,7 +111,7 @@ void dd::Systems::PadSystem::Update(double dt) bool dd::Systems::PadSystem::OnPause(const dd::Events::Pause &event) { - if (Pause()) { + if (IsPaused()) { SetPause(false); } else { SetPause(true); @@ -143,14 +143,10 @@ bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event) EventBroker->Publish(e); } else if (val == GLFW_KEY_P) { Events::Pause e; - e.Time = 0; - e.SlowdownTime = 0; - e.Type = "All"; EventBroker->Publish(e); } else if (val == GLFW_KEY_H) { Events::HitLag e; e.Time = 0.3; - e.SlowdownTime = 10; e.Type = "All"; EventBroker->Publish(e); } else if (val == GLFW_KEY_D) { diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index f8a2d4e..b0bd283 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -26,7 +26,6 @@ void dd::Systems::PhysicsSystem::Initialize() EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, &PhysicsSystem::SetImpulse); EVENT_SUBSCRIBE_MEMBER(m_EPause, &PhysicsSystem::OnPause); - EVENT_SUBSCRIBE_MEMBER(m_EHitLag, &PhysicsSystem::OnHitLag); } void dd::Systems::PhysicsSystem::InitializeWater() @@ -62,17 +61,7 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event) void dd::Systems::PhysicsSystem::Update(double dt) { - if (Pause()) { - if (HitLag()) { - m_HitLagTimer += dt; - std::cout << m_HitLagTimer << std::endl; - if (m_HitLagPlacement < m_HitLagTimer) { - m_HitLagTimer = 0; - SetHitLag(false); - Events::Pause e; - EventBroker->Publish(e); - } - } + if (IsPaused()) { return; } @@ -190,7 +179,7 @@ void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, Entity bool dd::Systems::PhysicsSystem::OnPause(const dd::Events::Pause &event) { - if (Pause()) { + if (IsPaused()) { SetPause(false); } else { SetPause(true); @@ -198,17 +187,6 @@ bool dd::Systems::PhysicsSystem::OnPause(const dd::Events::Pause &event) return true; } -bool dd::Systems::PhysicsSystem::OnHitLag(const dd::Events::HitLag &event) -{ - SetHitLag(true); - Events::Pause e; - EventBroker->Publish(e); - m_HitLagPlacement = event.Time + 10; - m_SlowdownTime = event.SlowdownTime; - m_HitLagTimer = m_SlowdownTime; - return true; -} - void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity) { auto physicsComponent = m_World->GetComponent(entity);