Ball hitting bricks destroys bricks! We can PLAY! (kind of)

This commit is contained in:
PlatinumSkink
2015-09-11 10:50:49 +02:00
parent 7e945c85d3
commit c829315598
8 changed files with 151 additions and 44 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

+85 -10
View File
@@ -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<Components::Model>();
// m_World->ComponentFactory.Register<Components::Template>();
m_World->SystemFactory.Register<Systems::LevelSystem>([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::LevelSystem>();
m_World->SystemFactory.Register<Systems::PadSystem>([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PadSystem>();
m_World->ComponentFactory.Register<Components::Sprite>();
m_World->ComponentFactory.Register<Components::RectangleShape>();
m_World->ComponentFactory.Register<Components::Physics>();
m_World->ComponentFactory.Register<Components::Ball>();
m_World->ComponentFactory.Register<Components::Brick>();
m_World->SystemFactory.Register<Systems::PhysicsSystem>(
[this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PhysicsSystem>();
m_World->SystemFactory.Register<Systems::LevelSystem>([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::LevelSystem>();
m_World->SystemFactory.Register<Systems::PadSystem>([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PadSystem>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
m_World->Initialize();
@@ -88,22 +94,78 @@ public:
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
sprite->SpriteFile = "Textures/Ball.png";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(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<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
transform->Position = glm::vec3(-9.f, 5.f, -9.f);
transform->Scale = glm::vec3(10.f, 0.5f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(topWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
physics->Static = true;
m_World->CommitEntity(topWall);
}
{
auto leftWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(leftWall);
transform->Position = glm::vec3(-7.f, 1.f, -9.f);
transform->Scale = glm::vec3(0.5f, 10.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(leftWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(leftWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(leftWall);
physics->Static = true;
m_World->CommitEntity(leftWall);
}
{
auto rightWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(rightWall);
transform->Position = glm::vec3(7.f, 1.f, -9.f);
transform->Scale = glm::vec3(0.5f, 10.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(rightWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(rightWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(rightWall);
physics->Static = true;
m_World->CommitEntity(rightWall);
}
{
/*{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.f, -3.f, -9.f);
@@ -118,6 +180,19 @@ public:
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = true;
m_World->CommitEntity(ent);
}*/
{
auto ent = m_World->CreateEntity();
m_World->SetProperty(ent, "Name", "Pad");
auto ctransform = m_World->AddComponent<Components::Transform>(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<Components::RectangleShape>(ent);
auto physics = m_World->AddComponent<Components::Physics>(ent);
auto csprite = m_World->AddComponent<Components::Sprite>(ent);
csprite->SpriteFile = "Textures/Pad.png";
m_World->CommitEntity(ent);
}
+10 -4
View File
@@ -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 <fstream>
#include <iostream>
#include <intrin.h>
@@ -54,10 +56,14 @@ public:
void EndLevel();
private:
//dd::EventRelay<LevelSystem, dd::Events::Contact> m_EContact;
void Update(double dt);
bool OnContact(/*const dd::Events::Contact &event*/);
private:
dd::EventRelay<LevelSystem, dd::Events::Contact> m_EContact;
bool OnContact(const dd::Events::Contact &event);
bool m_Initialized = false;
int numberOfBricks;
int tRows = 7;
+5 -3
View File
@@ -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<PadSystem, dd::Events::KeyDown> m_EKeyDown;
dd::EventRelay<PadSystem, dd::Events::KeyUp> m_EKeyUp;
@@ -51,9 +54,8 @@ private:
class PadSteeringInputController;
std::array<std::shared_ptr<PadSteeringInputController>, 4> m_PadInputControllers;
EntityID ent;
std::shared_ptr<Components::Transform> transform;
std::shared_ptr<Components::Sprite> sprite;
EntityID ent = 0;
Components::Transform* transform;
glm::vec3 acceleration = glm::vec3(0.f, 0.f, 0.f);
bool left = false, right = false;
};
+3 -3
View File
@@ -7,11 +7,11 @@ namespace dd
namespace Components
{
struct Physics: public Component
{
struct Physics: public Component
{
//idk if anything should be here :)
bool Static = true;
};
};
}
+34 -13
View File
@@ -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<Components::Transform> transform = m_World->AddComponent<Components::Transform>(brick);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(brick);
std::shared_ptr<Components::Brick> cBrick = m_World->AddComponent<Components::Brick>(brick);
std::shared_ptr<Components::RectangleShape> cRec = m_World->AddComponent<Components::RectangleShape>(brick);
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(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<Components::Brick>(entity1);
auto ball = m_World->GetComponent<Components::Ball>(entity2);
if (brick != NULL)
EntityID entityBrick = event.Entity1;
auto brick = m_World->GetComponent<Components::Brick>(entityBrick);
if (brick == NULL)
{
return false;
}
EntityID entityBall = event.Entity2;
//EntityID entity1 = event.Entity1;
//EntityID entity2 = event.Entity2;
//auto brick = m_World->GetComponent<Components::Brick>(entity1);
//auto ball = m_World->GetComponent<Components::Ball>(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<Components::Brick>(entity2);
if (brick != NULL)
{
std::cout << "Entity2 is a brick!" << std::endl;
entityBrick = entity2;
}
ball = m_World->GetComponent<Components::Ball>(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;
}
+10 -7
View File
@@ -20,13 +20,6 @@ void dd::Systems::PadSystem::Initialize()
l.Command = "left";
EventBroker->Publish(l);
ent = m_World->CreateEntity();
transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.f, -5.f, -10.f);
transform->Scale = glm::vec3(3.2, 0.8, 0.);
sprite = m_World->AddComponent<Components::Sprite>(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<std::string>(it->first, "Name") == "Pad") {
ent = it->first;
transform = m_World->GetComponent<Components::Transform>(ent);
break;
}
}
}
if (transform->Velocity.x < -400.f)
{
transform->Velocity.x = -400.f;
+1 -1
View File
@@ -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;