Merge remote-tracking branch 'origin/master' into GLrenderer

This commit is contained in:
tleety
2015-09-16 14:42:38 +02:00
12 changed files with 247 additions and 78 deletions
+5 -2
View File
@@ -78,6 +78,7 @@ public:
m_World->ComponentFactory.Register<Components::Ball>();
m_World->ComponentFactory.Register<Components::Brick>();
m_World->ComponentFactory.Register<Components::Pad>();
m_World->ComponentFactory.Register<Components::Life>();
m_World->SystemFactory.Register<Systems::PhysicsSystem>(
[this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PhysicsSystem>();
@@ -94,6 +95,8 @@ public:
//TODO: Remove tobias light-test code.
/*{
}*/
//OctoBall
{
@@ -156,8 +159,8 @@ public:
{
auto topWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
transform->Position = glm::vec3(0.f, 5.f, -10.f);
transform->Scale = glm::vec3(15.f, 0.5f, 1.f);
transform->Position = glm::vec3(0.f, 6.f, -10.f);
transform->Scale = glm::vec3(18.f, 0.5f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
+1 -1
View File
@@ -15,7 +15,7 @@ namespace Components
struct Ball : Component
{
int number = 0;
};
}
+26
View File
@@ -0,0 +1,26 @@
//
// Created by Administrator on 2015-09-15.
//
#ifndef DAYDREAM_CLIFE_H
#define DAYDREAM_CLIFE_H
#include "Core/Component.h"
namespace dd
{
namespace Components
{
struct Life : Component {
int number = 0;
};
}
}
#endif //DAYDREAM_CLIFE_H
+27
View File
@@ -0,0 +1,27 @@
//
// Created by Administrator on 2015-09-15.
//
#ifndef DAYDREAM_ELIFELOST_H
#define DAYDREAM_ELIFELOST_H
#include "Core/EventBroker.h"
#include "Core/Entity.h"
namespace dd
{
namespace Events
{
struct LifeLost : Event
{
EntityID entity;
};
}
}
#endif //DAYDREAM_ELIFELOST_H
+14 -2
View File
@@ -10,13 +10,17 @@
#include "Rendering/CSprite.h"
#include "Rendering/CModel.h"
#include "Game/CBrick.h"
#include "Physics/EContact.h"
#include "Core/EventBroker.h"
#include "Core/World.h"
#include "Game/CBall.h"
#include "Game/CLife.h"
#include "Game/EStageCleared.h"
#include "Game/ELifeLost.h"
#include "Physics/CBoxShape.h"
#include "Physics/CPhysics.h"
#include "Physics/CCircleShape.h"
#include "Physics/ESetImpulse.h"
#include "Physics/EContact.h"
#include <fstream>
#include <iostream>
#include <intrin.h>
@@ -48,6 +52,7 @@ public:
void Initialize() override;
void CreateBasicLevel(int, int, glm::vec2, int);
void CreateLife(int);
void SaveLevel(int, int, glm::vec2, int); // Shouldn't be here, but I'm experimenting.
void LoadLevel(char[20]);
void CreateBrick(int, int, glm::vec2, int, int);
@@ -57,12 +62,17 @@ public:
void EndLevel();
void Update(double dt);
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
int lives = 3;
int pastLives = 3;
private:
dd::EventRelay<LevelSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<LevelSystem, dd::Events::LifeLost> m_ELifeLost;
bool OnContact(const dd::Events::Contact &event);
bool LifeLost(const dd::Events::LifeLost &event);
bool m_Initialized = false;
@@ -71,6 +81,8 @@ private:
int tLines = 8;
glm::vec2 tSpaceBetweenBricks = glm::vec2(2, 0.5);
int tSpaceToEdge = 0;
EntityID entityToRemove = NULL;
};
}
+15 -5
View File
@@ -7,20 +7,23 @@
#include "Core/InputController.h"
#include "Core/System.h"
#include "Core/Component.h"
#include "Core/CTransform.h"
#include "Core/EventBroker.h"
#include "Core/EKeyDown.h"
#include "Core/EKeyUp.h"
#include "Input/EBindKey.h"
#include "Physics/EContact.h"
#include "Core/CTransform.h"
#include "Physics/CBoxShape.h"
#include "Physics/CPhysics.h"
#include "Physics/CCircleShape.h"
#include "Physics/ESetImpulse.h"
#include "Rendering/CSprite.h"
#include "Rendering/CModel.h"
#include "Game/CBall.h"
#include "Core/Component.h"
#include "Physics/ESetImpulse.h"
#include "Physics/CCircleShape.h"
#include "Game/ELifeLost.h"
namespace dd
@@ -38,19 +41,26 @@ public:
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
dd::EventRelay<PadSystem, dd::Events::KeyDown> m_EKeyDown;
dd::EventRelay<PadSystem, dd::Events::KeyUp> m_EKeyUp;
dd::EventRelay<PadSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<PadSystem, dd::Events::LifeLost> m_ELifeLost;
bool OnKeyDown(const dd::Events::KeyDown &event);
bool OnKeyUp(const dd::Events::KeyUp &event);
bool OnContact(const dd::Events::Contact &event);
bool LifeLost(const dd::Events::LifeLost &event);
EntityID removeThisObject = NULL;
float slowdownModifier = 5.f;
float accelerationSpeed = 80.f;
float maxSpeed = 40.f;
@@ -60,7 +70,7 @@ private:
EntityID ent = 0;
Components::Transform* transform;
glm::vec3 acceleration = glm::vec3(0.f, 0.f, 0.f);
bool left = false, right = false;
bool left = false, right = false, replaceBall = false;
};
class PadSystem::PadSteeringInputController : InputController<PadSystem>
+1
View File
@@ -80,6 +80,7 @@ private:
e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
m_PhysicsSystem->EventBroker->Publish(e);
}
void EndContact(b2Contact* contact)
+1 -2
View File
@@ -75,8 +75,7 @@ set(SOURCE_FILES
${SOURCE_FILES_Rendering}
${SOURCE_FILES_Transform}
${SOURCE_FILES_Physics}
${SOURCE_FILES_Game}
)
${SOURCE_FILES_Game} ../../include/Game/CLife.h ../../include/Game/ELifeLost.h)
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
+69 -43
View File
@@ -10,15 +10,76 @@
void dd::Systems::LevelSystem::Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact);
EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, LevelSystem::LifeLost);
for (int i = 0; i < lives; i++)
{
CreateLife(i);
}
return;
}
void dd::Systems::LevelSystem::Update(double dt) {
void dd::Systems::LevelSystem::CreateLife(int number)
{
auto life = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(life);
transform->Position = glm::vec3(-11.f + number, -8.f, -10.f);
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
std::shared_ptr<Components::Life> lifeNr = m_World->AddComponent<Components::Life>(life);
lifeNr->number = number;
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(life);
sprite->SpriteFile = "Textures/Ball.png";
m_World->CommitEntity(life);
}
void dd::Systems::LevelSystem::Update(double dt)
{
if (!m_Initialized) {
CreateBasicLevel(tRows, tLines, tSpaceBetweenBricks, tSpaceToEdge);
m_Initialized = true;
}
if (entityToRemove != NULL)
{
//m_World->RemoveEntity(entityToRemove);
entityToRemove = NULL;
}
}
void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto ball = m_World->GetComponent<Components::Ball>(entity);
if (ball != NULL)
{
auto transformBall = m_World->GetComponent<Components::Transform>(entity);
if (transformBall->Position.y < -10)
{
if (lives == pastLives)
{
Events::LifeLost e;
e.entity = entity;
EventBroker->Publish(e);
return;
}
}
}
if (lives != pastLives)
{
auto life = m_World->GetComponent<Components::Life>(entity);
if (life != NULL)
{
if (life->number + 1 == pastLives) {
m_World->RemoveEntity(entity);
pastLives = lives;
}
}
}
}
void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, int spaceToEdge)
@@ -57,7 +118,6 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe
float y = spaceToEdge + row * spacesBetweenBricks.y;
transform->Scale = glm::vec3(1.6, 0.2, 0.5);
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;
}
@@ -84,8 +144,6 @@ void dd::Systems::LevelSystem::OnEntityRemoved(EntityID entity)
bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
{
/*event.Entity1 - Vi vet inte vilken som.
event.Entity2 - */
EntityID entityBrick = event.Entity1;
auto brick = m_World->GetComponent<Components::Brick>(entityBrick);
if (brick == NULL)
@@ -93,49 +151,17 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
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;
}
}
else
{
brick = m_World->GetComponent<Components::Brick>(entity2);
if (brick != NULL)
{
std::cout << "Entity2 is a brick!" << std::endl;
entityBrick = entity2;
}
m_World->RemoveEntity(entityBrick);
ball = m_World->GetComponent<Components::Ball>(entity1);
if (ball != NULL)
{
std::cout << "Entity1 is a ball!" << std::endl;
entityBall = entity1;
}
}
return true;
}
if (entityBrick != NULL && entityBall != NULL)
{*/
//std::cout << "We're destroying!" << std::endl;
m_World->RemoveEntity(entityBrick);
bool dd::Systems::LevelSystem::LifeLost(const dd::Events::LifeLost &event)
{
lives--;
return true;
//}
return false;
return true;
}
void dd::Systems::LevelSystem::EndLevel()
+75 -15
View File
@@ -25,10 +25,45 @@ 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);
EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, PadSystem::LifeLost);
return;
}
void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto ball = m_World->GetComponent<Components::Ball>(entity);
if (ball != NULL) {
if (replaceBall == true) {
replaceBall = false;
auto transformEntity = m_World->GetComponent<Components::Transform>(entity);
transformEntity->Position = glm::vec3(20, 20, -10);
//Temporary. Create new ball.
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
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/Ball.png";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
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);
}
}
}
void dd::Systems::PadSystem::Update(double dt)
{
if (ent == 0) {
@@ -41,25 +76,25 @@ void dd::Systems::PadSystem::Update(double dt)
}
}
if (transform->Velocity.x < -400.f)
if (transform->Velocity.x < -maxSpeed)
{
transform->Velocity.x = -400.f;
transform->Velocity.x = -maxSpeed;
}
else if (transform->Velocity.x > 400.f)
else if (transform->Velocity.x > maxSpeed)
{
transform->Velocity.x = 400.f;
transform->Velocity.x = maxSpeed;
}
transform->Position += transform->Velocity * (float)dt;
transform->Velocity += acceleration * (float)dt;
transform->Velocity -= transform->Velocity * (0.9f * (float)dt);
transform->Velocity -= transform->Velocity * slowdownModifier * (float)dt;
if (left)
{
acceleration.x = -20.f;
acceleration.x = -accelerationSpeed;
}
else if (right)
{
acceleration.x = 20.f;
acceleration.x = accelerationSpeed;
}
else
{
@@ -86,12 +121,16 @@ bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
//acceleration.x = -0.01f;
left = true;
}
else if (val = 262)
else if (val == 262)
{
//std::cout << "Right!" << std::endl;
//acceleration.x = 0.01f;
right = true;
}
else if (val == 82)
{
replaceBall = true;
}
return true;
}
@@ -110,7 +149,7 @@ bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event)
{
left = false;
}
else if (val = 262)
else if (val == 262)
{
right = false;
}
@@ -134,17 +173,32 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
auto transformBall = m_World->GetComponent<Components::Transform>(entityBall);
auto transformPad = m_World->GetComponent<Components::Transform>(entityPad);
float movementMultiplier = 2.f;
float movementMultiplier = 4.f;
float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f;
float whatX = transformBall->Position.x - transformPad->Position.x;
float movementX;
if (whatX > 0)
{
movementX = movementMultiplier * whatX;
//std::cout << "Right!" << std::endl;
}
else
{
movementX = movementMultiplier * whatX;
//std::cout << "Left!" << std::endl;
}
std::cout << movementX << " " << movementY << std::endl;
//float movementY = 1;
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2) + 1;
//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<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.1f, -10.f);
transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.01f, -10.f);
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
auto model = m_World->AddComponent<Components::Model>(ent);
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
@@ -160,10 +214,16 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
Events::SetImpulse e;
e.Entity = ent;
e.Impulse = glm::vec2(movementX, movementY);
e.Point = glm::vec2(event.ContactPoint);
e.Point = glm::vec2(transform->Position.x, transform->Position.y);
EventBroker->Publish(e);
}
bool dd::Systems::PadSystem::LifeLost(const dd::Events::LifeLost &event)
{
replaceBall = true;
return true;
}
bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event)
{
std::string command = event.Command;
+10 -4
View File
@@ -49,6 +49,8 @@ void dd::Systems::PhysicsSystem::Update(double dt)
b2Body* body = i.second;
auto transformComponent = m_World->GetComponent<Components::Transform>(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<Components::Transform>(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);
}
+3 -4
View File
@@ -1,5 +1,5 @@
#ifndef FILENAME_H__
#define FILENAME_H__
#ifndef NAMESPACE_FILENAME_H__
#define NAMESPACE_FILENAME_H__
namespace dd
{
@@ -62,8 +62,7 @@ private:
// Private member function
bool privateMemberFunction()
{
switch (m_PrivateMember1)
{
switch (m_PrivateMember1) {
case 1:
m_PrivateMember2 = new ClassName(2);
break;