From bda864ac118ed35da789c7dd17282ce8f8aa1a1a Mon Sep 17 00:00:00 2001 From: PlatinumSkink Date: Fri, 11 Sep 2015 15:07:00 +0200 Subject: [PATCH] Breakout Standard Screen Meaning bricks, ball, pad, walls bounces, things. May or may not work as intended. --- include/Core/Engine.h | 13 +++++---- include/Game/CBall.h | 8 +++--- include/Game/CPad.h | 4 +-- include/Game/PadSystem.h | 8 ++++-- include/Physics/PhysicsSystem.h | 2 -- src/game/Game/PadSystem.cpp | 45 +++++++++++++++++++++++++++++- src/game/Physics/PhysicsSystem.cpp | 10 +++---- 7 files changed, 67 insertions(+), 23 deletions(-) diff --git a/include/Core/Engine.h b/include/Core/Engine.h index e427523..503bd5b 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -39,6 +39,7 @@ #include "Game/PadSystem.h" #include "Game/CBall.h" #include "Game/CBrick.h" +#include "Game/CPad.h" #include "Physics/PhysicsSystem.h" #include "Physics/CPhysics.h" @@ -78,6 +79,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(); @@ -110,7 +112,7 @@ public: Events::SetImpulse e; e.Entity = ent; - e.Impulse = glm::vec2(0.f, -1.f); + e.Impulse = glm::vec2(0.f, -7.f); e.Point = glm::vec2(0.5f, 0.f); m_EventBroker->Publish(e); } @@ -118,8 +120,8 @@ public: { 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); + transform->Position = glm::vec3(0.f, 5.f, -10.f); + transform->Scale = glm::vec3(15.f, 0.5f, 1.f); std::shared_ptr sprite = m_World->AddComponent(topWall); sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; @@ -134,7 +136,7 @@ public: { auto leftWall = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(leftWall); - transform->Position = glm::vec3(-7.f, 1.f, -9.f); + transform->Position = glm::vec3(-9.f, 1.f, -10.f); transform->Scale = glm::vec3(0.5f, 10.f, 1.f); std::shared_ptr sprite = m_World->AddComponent(leftWall); @@ -150,7 +152,7 @@ public: { auto rightWall = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(rightWall); - transform->Position = glm::vec3(7.f, 1.f, -9.f); + transform->Position = glm::vec3(9.f, 1.f, -10.f); transform->Scale = glm::vec3(0.5f, 10.f, 1.f); std::shared_ptr sprite = m_World->AddComponent(rightWall); @@ -192,6 +194,7 @@ public: auto rectangle = m_World->AddComponent(ent); auto physics = m_World->AddComponent(ent); auto csprite = m_World->AddComponent(ent); + auto pad = m_World->AddComponent(ent); csprite->SpriteFile = "Textures/Pad.png"; m_World->CommitEntity(ent); } diff --git a/include/Game/CBall.h b/include/Game/CBall.h index ecddc4f..d5c652f 100644 --- a/include/Game/CBall.h +++ b/include/Game/CBall.h @@ -1,9 +1,9 @@ // -// Created by Administrator on 2015-09-10. +// Created by Adniklastrator on 2015-09-10. // -#ifndef DAYDREAM_CPAD_H -#define DAYDREAM_CPAD_H +#ifndef DAYDREAM_CBALL_H +#define DAYDREAM_CBALL_H #include "Core/Component.h" @@ -22,4 +22,4 @@ struct Ball : Component } -#endif //DAYDREAM_CPAD_H +#endif //DAYDREAM_CBALL_H diff --git a/include/Game/CPad.h b/include/Game/CPad.h index dac06c3..fef614f 100644 --- a/include/Game/CPad.h +++ b/include/Game/CPad.h @@ -1,5 +1,5 @@ // -// Created by Administrator on 2015-09-10. +// Created by Adniklastrator on 2015-09-10. // #ifndef DAYDREAM_CPAD_H @@ -17,8 +17,6 @@ namespace Components struct Pad : Component { - //std::shared_ptr eventBroker; - //EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, CPad::OnKeyDown); int Lives; int Points; }; diff --git a/include/Game/PadSystem.h b/include/Game/PadSystem.h index fc5e2ba..adce7a0 100644 --- a/include/Game/PadSystem.h +++ b/include/Game/PadSystem.h @@ -11,13 +11,15 @@ #include "Core/EKeyDown.h" #include "Core/EKeyUp.h" #include "Input/EBindKey.h" -//#include "Core/EContact.h" +#include "Physics/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" +#include "Physics/ESetImpulse.h" +#include "Physics/CCircleShape.h" namespace dd @@ -39,13 +41,13 @@ public: private: dd::EventRelay m_EKeyDown; dd::EventRelay m_EKeyUp; - //dd::EventRelay m_EContact; + dd::EventRelay m_EContact; bool OnKeyDown(const dd::Events::KeyDown &event); bool OnKeyUp(const dd::Events::KeyUp &event); - bool OnContact(/*const dd::Events::Contact &event*/); + bool OnContact(const dd::Events::Contact &event); diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index 6754a1a..265e511 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -78,8 +78,6 @@ private: e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()]; e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()]; - std::cout << e.Entity1 << " " << e.Entity2 << std::endl; - m_PhysicsSystem->EventBroker->Publish(e); } void EndContact(b2Contact* contact) diff --git a/src/game/Game/PadSystem.cpp b/src/game/Game/PadSystem.cpp index caffb1b..a465b8b 100644 --- a/src/game/Game/PadSystem.cpp +++ b/src/game/Game/PadSystem.cpp @@ -6,6 +6,7 @@ #include "Game/PadSystem.h" #include "Core/World.h" #include +#include void dd::Systems::PadSystem::Initialize() @@ -22,6 +23,7 @@ void dd::Systems::PadSystem::Initialize() EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, PadSystem::OnKeyDown); EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, PadSystem::OnKeyUp); + EVENT_SUBSCRIBE_MEMBER(m_EContact, PadSystem::OnContact); return; } @@ -114,9 +116,50 @@ bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event) return true; } -bool dd::Systems::PadSystem::OnContact(/*const dd::Events::Contact &event,*/) +bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event) { + EntityID entityBall = event.Entity2; + auto ball = m_World->GetComponent(entityBall); + if (ball == NULL) + { + return false; + } + EntityID entityPad = event.Entity1; + auto pad = m_World->GetComponent(entityPad); + if (pad == NULL) + { + return false; + } + auto transformBall = m_World->GetComponent(entityBall); + auto transformPad = m_World->GetComponent(entityPad); + float movementMultiplier = 2.f; + + float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier; + float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f; + + std::cout << movementX << " " << movementY << std::endl; + + //Temporary solution. Add a new ball and delete the old one. + auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.1f, -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; + + m_World->RemoveEntity(entityBall); + m_World->CommitEntity(ent); + + Events::SetImpulse e; + e.Entity = ent; + e.Impulse = glm::vec2(movementX, movementY); + e.Point = glm::vec2(event.ContactPoint); + EventBroker->Publish(e); } bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event) diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index 14c1039..5f8ec8d 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -16,8 +16,8 @@ void dd::Systems::PhysicsSystem::Initialize() m_PhysicsWorld = new b2World(m_Gravity); m_TimeStep = 1.f/60.f; - m_VelocityIterations = 6; - m_PositionIterations = 2; + m_VelocityIterations = 8; + m_PositionIterations = 6; m_PhysicsWorld->SetContactListener(m_ContactListener); @@ -147,7 +147,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) auto boxComponent = m_World->GetComponent(entity); if (boxComponent) { b2PolygonShape* bShape = new b2PolygonShape(); - bShape->SetAsBox(absoluteTransform.Scale.x/4, absoluteTransform.Scale.y/4); //TODO: THIS SUCKS DUDE 4?!?!?!? + bShape->SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2); //TODO: THIS SUCKS DUDE 4?!?!?!? pShape = bShape; } else { auto circleComponent = m_World->GetComponent(entity); @@ -159,7 +159,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) if (absoluteTransform.Scale.x != absoluteTransform.Scale.y && absoluteTransform.Scale.y != absoluteTransform.Scale.z) { LOG_WARNING("Circles has to be of uniform scale."); } - pShape->m_radius = absoluteTransform.Scale.x/4; //TODO: THIS ALSO SUCKS 4 WTH + pShape->m_radius = absoluteTransform.Scale.x/2; //TODO: THIS ALSO SUCKS 4 WTH } } @@ -172,7 +172,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) b2FixtureDef fixtureDef; fixtureDef.shape = pShape; fixtureDef.density = 1.f; - fixtureDef.restitution = 0.0f; + fixtureDef.restitution = 1.0f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef);