diff --git a/assets/Textures/Ball.png b/assets/Textures/Ball.png new file mode 100644 index 0000000..f43f7f8 Binary files /dev/null and b/assets/Textures/Ball.png differ diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 4fee7b6..c112785 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -30,18 +30,20 @@ //TODO: Remove includes that are only here for the temporary draw solution. #include "World.h" #include "CTransform.h" +#include "Core/EventBroker.h" #include "Rendering/CModel.h" #include "Rendering/CSprite.h" #include "CTemplate.h" #include "Transform/TransformSystem.h" #include "Game/LevelSystem.h" #include "Game/PadSystem.h" +#include "Game/CBall.h" +#include "Game/CBrick.h" #include "Physics/PhysicsSystem.h" #include "Physics/CPhysics.h" - - #include "Physics/CBoxShape.h" +#include "Physics/ESetImpulse.h" namespace dd { @@ -69,18 +71,22 @@ public: // m_World->ComponentFactory.Register(); // m_World->ComponentFactory.Register(); - m_World->SystemFactory.Register([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); }); - m_World->AddSystem(); - m_World->SystemFactory.Register([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); }); - m_World->AddSystem(); + m_World->ComponentFactory.Register(); 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(); + m_World->SystemFactory.Register([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + m_World->SystemFactory.Register([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); m_World->Initialize(); @@ -88,22 +94,78 @@ public: { auto ent = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(0.f, 0.f, -10.f); - + 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/Core/ErrorTexture.png"; + sprite->SpriteFile = "Textures/Ball.png"; std::shared_ptr circleShape = m_World->AddComponent(ent); + std::shared_ptr ball = m_World->AddComponent(ent); std::shared_ptr physics = m_World->AddComponent(ent); physics->Static = false; m_World->CommitEntity(ent); + + Events::SetImpulse e; + e.Entity = ent; + e.Impulse = glm::vec2(0.f, -1.f); + e.Point = glm::vec2(0.5f, 0.f); + m_EventBroker->Publish(e); } + { + auto topWall = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(topWall); + transform->Position = glm::vec3(-9.f, 5.f, -9.f); + transform->Scale = glm::vec3(10.f, 0.5f, 1.f); - { + std::shared_ptr sprite = m_World->AddComponent(topWall); + sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + + std::shared_ptr boxShape = m_World->AddComponent(topWall); + + std::shared_ptr physics = m_World->AddComponent(topWall); + physics->Static = true; + + m_World->CommitEntity(topWall); + } + { + auto leftWall = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(leftWall); + transform->Position = glm::vec3(-7.f, 1.f, -9.f); + transform->Scale = glm::vec3(0.5f, 10.f, 1.f); + + std::shared_ptr sprite = m_World->AddComponent(leftWall); + sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + + std::shared_ptr boxShape = m_World->AddComponent(leftWall); + + std::shared_ptr physics = m_World->AddComponent(leftWall); + physics->Static = true; + + m_World->CommitEntity(leftWall); + } + { + auto rightWall = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(rightWall); + transform->Position = glm::vec3(7.f, 1.f, -9.f); + transform->Scale = glm::vec3(0.5f, 10.f, 1.f); + + std::shared_ptr sprite = m_World->AddComponent(rightWall); + sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + + std::shared_ptr boxShape = m_World->AddComponent(rightWall); + + std::shared_ptr physics = m_World->AddComponent(rightWall); + physics->Static = true; + + m_World->CommitEntity(rightWall); + } + + + /*{ auto ent = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(ent); transform->Position = glm::vec3(0.f, -3.f, -9.f); @@ -119,7 +181,20 @@ public: physics->Static = true; m_World->CommitEntity(ent); - } + }*/ + + { + auto ent = m_World->CreateEntity(); + m_World->SetProperty(ent, "Name", "Pad"); + auto ctransform = m_World->AddComponent(ent); + ctransform->Position = glm::vec3(0.f, -5.f, -10.f); + ctransform->Scale = glm::vec3(3.2, 0.8, 0.); + auto rectangle = m_World->AddComponent(ent); + auto physics = m_World->AddComponent(ent); + auto csprite = m_World->AddComponent(ent); + csprite->SpriteFile = "Textures/Pad.png"; + m_World->CommitEntity(ent); + } m_LastTime = glfwGetTime(); } diff --git a/include/Game/LevelSystem.h b/include/Game/LevelSystem.h index 5efa79b..6269974 100644 --- a/include/Game/LevelSystem.h +++ b/include/Game/LevelSystem.h @@ -9,11 +9,13 @@ #include "Core/CTransform.h" #include "Rendering/CSprite.h" #include "Game/CBrick.h" -//#include "Core/EContact.h" +#include "Physics/EContact.h" #include "Core/EventBroker.h" #include "Core/World.h" #include "Game/CBall.h" #include "Game/EStageCleared.h" +#include "Physics/CBoxShape.h" +#include "Physics/CPhysics.h" #include #include #include @@ -54,10 +56,14 @@ public: void EndLevel(); -private: - //dd::EventRelay m_EContact; + void Update(double dt); - bool OnContact(/*const dd::Events::Contact &event*/); +private: + dd::EventRelay m_EContact; + + bool OnContact(const dd::Events::Contact &event); + + bool m_Initialized = false; int numberOfBricks; int tRows = 7; diff --git a/include/Game/PadSystem.h b/include/Game/PadSystem.h index e2a62a1..fc5e2ba 100644 --- a/include/Game/PadSystem.h +++ b/include/Game/PadSystem.h @@ -13,6 +13,8 @@ #include "Input/EBindKey.h" //#include "Core/EContact.h" #include "Core/CTransform.h" +#include "Physics/CBoxShape.h" +#include "Physics/CPhysics.h" #include "Rendering/CSprite.h" #include "Game/CBall.h" #include "Core/Component.h" @@ -33,6 +35,7 @@ public: void Initialize() override; void Update(double dt) override; + private: dd::EventRelay m_EKeyDown; dd::EventRelay m_EKeyUp; @@ -51,9 +54,8 @@ private: class PadSteeringInputController; std::array, 4> m_PadInputControllers; - EntityID ent; - std::shared_ptr transform; - std::shared_ptr sprite; + EntityID ent = 0; + Components::Transform* transform; glm::vec3 acceleration = glm::vec3(0.f, 0.f, 0.f); bool left = false, right = false; }; diff --git a/include/Physics/CPhysics.h b/include/Physics/CPhysics.h index d6c3b21..8d1f975 100644 --- a/include/Physics/CPhysics.h +++ b/include/Physics/CPhysics.h @@ -7,11 +7,11 @@ namespace dd namespace Components { - struct Physics: public Component - { - //idk if anything should be here :) - bool Static = true; - }; +struct Physics: public Component +{ + //idk if anything should be here :) + bool Static = true; +}; } diff --git a/src/game/Game/LevelSystem.cpp b/src/game/Game/LevelSystem.cpp index d414d66..68dce72 100644 --- a/src/game/Game/LevelSystem.cpp +++ b/src/game/Game/LevelSystem.cpp @@ -9,11 +9,18 @@ void dd::Systems::LevelSystem::Initialize() { - CreateBasicLevel(tRows, tLines, tSpaceBetweenBricks, tSpaceToEdge); + EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact); return; } +void dd::Systems::LevelSystem::Update(double dt) { + if (!m_Initialized) { + CreateBasicLevel(tRows, tLines, tSpaceBetweenBricks, tSpaceToEdge); + m_Initialized = true; + } +} + void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, int spaceToEdge) { int num = 0; @@ -38,6 +45,8 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe std::shared_ptr transform = m_World->AddComponent(brick); std::shared_ptr sprite = m_World->AddComponent(brick); std::shared_ptr cBrick = m_World->AddComponent(brick); + std::shared_ptr cRec = m_World->AddComponent(brick); + std::shared_ptr cPhys = m_World->AddComponent(brick); std::string fileName = "Textures/Bricks/"; fileName.append(std::to_string(num)); fileName.append(".png"); @@ -47,6 +56,7 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe transform->Scale = glm::vec3(1.6, 0.4, 0.); transform->Position = glm::vec3(x - 7, y + 1, -10.f); //sprite->Color = glm::vec4(1.f, 1.f, 1.f, 1.f); + m_World->CommitEntity(brick); return; } @@ -70,23 +80,31 @@ void dd::Systems::LevelSystem::OnEntityRemoved(EntityID entity) return; } -bool dd::Systems::LevelSystem::OnContact(/*const dd::Events::Contact &event,*/) +bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event) { /*event.Entity1 - Vi vet inte vilken som. event.Entity2 - */ - std::string event = "I'm just a placeholder :("; - EntityID entityBrick; - EntityID entityBall; - EntityID entity1; - EntityID entity2; - - auto brick = m_World->GetComponent(entity1); - auto ball = m_World->GetComponent(entity2); - if (brick != NULL) + EntityID entityBrick = event.Entity1; + auto brick = m_World->GetComponent(entityBrick); + if (brick == NULL) { + return false; + } + EntityID entityBall = event.Entity2; + //EntityID entity1 = event.Entity1; + //EntityID entity2 = event.Entity2; + + //auto brick = m_World->GetComponent(entity1); + //auto ball = m_World->GetComponent(entity2); + //std::cout << brick << std::endl; + //std::cout << ball << std::endl; + /*if (brick != NULL) + { + std::cout << "Entity1 is a brick!" << std::endl; entityBrick = entity1; if (ball != NULL) { + std::cout << "Entity2 is a ball!" << std::endl; entityBall = entity2; } } @@ -95,22 +113,25 @@ bool dd::Systems::LevelSystem::OnContact(/*const dd::Events::Contact &event,*/) brick = m_World->GetComponent(entity2); if (brick != NULL) { + std::cout << "Entity2 is a brick!" << std::endl; entityBrick = entity2; } ball = m_World->GetComponent(entity1); if (ball != NULL) { + std::cout << "Entity1 is a ball!" << std::endl; entityBall = entity1; } } if (entityBrick != NULL && entityBall != NULL) - { + {*/ + //std::cout << "We're destroying!" << std::endl; m_World->RemoveEntity(entityBrick); return true; - } + //} return false; } diff --git a/src/game/Game/PadSystem.cpp b/src/game/Game/PadSystem.cpp index d903f4e..caffb1b 100644 --- a/src/game/Game/PadSystem.cpp +++ b/src/game/Game/PadSystem.cpp @@ -20,13 +20,6 @@ void dd::Systems::PadSystem::Initialize() l.Command = "left"; EventBroker->Publish(l); - ent = m_World->CreateEntity(); - transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(0.f, -5.f, -10.f); - transform->Scale = glm::vec3(3.2, 0.8, 0.); - sprite = m_World->AddComponent(ent); - sprite->SpriteFile = "Textures/Pad.png"; - EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, PadSystem::OnKeyDown); EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, PadSystem::OnKeyUp); @@ -35,6 +28,16 @@ void dd::Systems::PadSystem::Initialize() void dd::Systems::PadSystem::Update(double dt) { + if (ent == 0) { + for (auto it = m_World->GetEntities()->begin(); it != m_World->GetEntities()->end(); it++) { + if (m_World->GetProperty(it->first, "Name") == "Pad") { + ent = it->first; + transform = m_World->GetComponent(ent); + break; + } + } + } + if (transform->Velocity.x < -400.f) { transform->Velocity.x = -400.f; diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index bd0b6cc..03a3645 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -12,7 +12,7 @@ void dd::Systems::PhysicsSystem::Initialize() { m_ContactListener = new ContactListener(this); - m_Gravity = b2Vec2(0.f, -9.82f); + m_Gravity = b2Vec2(0.f, 0.f); m_PhysicsWorld = new b2World(m_Gravity); m_TimeStep = 1.f/60.f;