From ce878ac1db7c0d0104597ad7430264ebea5164b9 Mon Sep 17 00:00:00 2001 From: PlatinumSkink Date: Tue, 15 Sep 2015 13:35:04 +0200 Subject: [PATCH] Lives kind of work, and so does the demo! --- include/Core/Engine.h | 5 ++-- src/game/Game/LevelSystem.cpp | 24 +++++++++------- src/game/Game/PadSystem.cpp | 46 ++++++++++++++++-------------- src/game/Physics/PhysicsSystem.cpp | 14 ++++++--- 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 527d9bb..e500db2 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -81,6 +81,7 @@ public: m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); m_World->SystemFactory.Register( [this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); }); m_World->AddSystem(); @@ -97,7 +98,7 @@ public: //TODO: Remove tobias light-test code. - { + /*{ auto t_BrickWall = m_World->CreateEntity(); auto transform = m_World->AddComponent(t_BrickWall); transform->Position = glm::vec3(0.f, 0.f, -10.f); @@ -106,7 +107,7 @@ public: sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png"; sprite->NormalTexture = "Textures/Test/Brick_Normal.png"; sprite->SpecularTexture = "Textures/Test/Brick_Specular.png"; - } + }*/ { diff --git a/src/game/Game/LevelSystem.cpp b/src/game/Game/LevelSystem.cpp index b8ce807..8243629 100644 --- a/src/game/Game/LevelSystem.cpp +++ b/src/game/Game/LevelSystem.cpp @@ -22,7 +22,7 @@ void dd::Systems::LevelSystem::Initialize() void dd::Systems::LevelSystem::CreateLife(int number) { - /*auto life = m_World->CreateEntity(); + auto life = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(life); transform->Position = glm::vec3(-11.f + number, -8.f, -10.f); transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f); @@ -33,7 +33,7 @@ void dd::Systems::LevelSystem::CreateLife(int number) std::shared_ptr sprite = m_World->AddComponent(life); sprite->SpriteFile = "Textures/Ball.png"; - m_World->CommitEntity(life);*/ + m_World->CommitEntity(life); } void dd::Systems::LevelSystem::Update(double dt) @@ -59,10 +59,13 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID auto transformBall = m_World->GetComponent(entity); if (transformBall->Position.y < -10) { - Events::BallFellOffStage e; - e.entity = entity; - EventBroker->Publish(e); - return; + if (lives == pastLives) + { + Events::BallFellOffStage e; + e.entity = entity; + EventBroker->Publish(e); + return; + } } } @@ -72,9 +75,8 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID if (life != NULL) { if (life->number + 1 == pastLives) { - pastLives--; - - //m_World->RemoveEntity(entity); + m_World->RemoveEntity(entity); + pastLives = lives; } } } @@ -196,8 +198,8 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event) bool dd::Systems::LevelSystem::BallFellOffStage(const dd::Events::BallFellOffStage &event) { - /*std::cout << "Did you get here?" << std::endl; - EntityID entity = event.entity; + std::cout << "Did you get here?" << std::endl; + /*EntityID entity = event.entity; //Temporary. Create new ball. auto ent = m_World->CreateEntity(); diff --git a/src/game/Game/PadSystem.cpp b/src/game/Game/PadSystem.cpp index 1a23985..030c061 100644 --- a/src/game/Game/PadSystem.cpp +++ b/src/game/Game/PadSystem.cpp @@ -32,30 +32,34 @@ void dd::Systems::PadSystem::Initialize() void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { auto ball = m_World->GetComponent(entity); - if (replaceBall == true) - { - replaceBall = false; + if (ball != NULL) { + if (replaceBall == true) { + replaceBall = false; - //Temporary. Create new ball. - auto ent = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(0.5f, 0.f, -10.f);; - transform->Scale = glm::vec3(1.f, 1.f, 1.f); - std::shared_ptr sprite = m_World->AddComponent(ent); - sprite->SpriteFile = "Textures/Ball.png"; - std::shared_ptr circleShape = m_World->AddComponent(ent); - std::shared_ptr cball = m_World->AddComponent(ent); - std::shared_ptr physics = m_World->AddComponent(ent); - physics->Static = false; + auto transformEntity = m_World->GetComponent(entity); + transformEntity->Position = glm::vec3(20, 20, -10); - m_World->RemoveEntity(entity); - m_World->CommitEntity(ent); + //Temporary. Create new ball. + auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(0.5f, 0.f, -10.f);; + transform->Scale = glm::vec3(1.f, 1.f, 1.f); + std::shared_ptr sprite = m_World->AddComponent(ent); + sprite->SpriteFile = "Textures/Ball.png"; + std::shared_ptr circleShape = m_World->AddComponent(ent); + std::shared_ptr cball = m_World->AddComponent(ent); + std::shared_ptr physics = m_World->AddComponent(ent); + physics->Static = false; - Events::SetImpulse e; - e.Entity = ent; - e.Impulse = glm::vec2(0.f, -7.f); - e.Point = glm::vec2(transform->Position.x, transform->Position.y); - EventBroker->Publish(e); + m_World->RemoveEntity(entity); + m_World->CommitEntity(ent); + + Events::SetImpulse e; + e.Entity = ent; + e.Impulse = glm::vec2(0.f, -7.f); + e.Point = glm::vec2(transform->Position.x, transform->Position.y); + EventBroker->Publish(e); + } } } diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index 3d244a7..28ffc37 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -49,6 +49,8 @@ void dd::Systems::PhysicsSystem::Update(double dt) b2Body* body = i.second; auto transformComponent = m_World->GetComponent(entity); + if (! transformComponent) + continue; if (m_World->GetEntityParent(entity) == 0) { @@ -85,7 +87,8 @@ void dd::Systems::PhysicsSystem::Update(double dt) b2Body* body = i.second; auto transformComponent = m_World->GetComponent(entity); - + if (! transformComponent) + continue; if (m_World->GetEntityParent(entity) == 0) { b2Vec2 position = body->GetPosition(); @@ -119,10 +122,13 @@ void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity) { b2Body* body = m_EntitiesToBodies[entity]; - m_EntitiesToBodies.erase(entity); - m_BodiesToEntities.erase(body); + if (body != nullptr) { + m_EntitiesToBodies.erase(entity); + m_BodiesToEntities.erase(body); + + m_PhysicsWorld->DestroyBody(body); + } - m_PhysicsWorld->DestroyBody(body); }