Transferred Ball-stuff into BallSystem.
This commit is contained in:
@@ -95,7 +95,7 @@ set(SOURCE_FILES
|
||||
${SOURCE_FILES_Game}
|
||||
${SOURCE_FILES_Sound}
|
||||
${SOURCE_FILES_GUI}
|
||||
)
|
||||
../../include/Game/EMultiBallLost.h)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
||||
|
||||
@@ -14,16 +14,89 @@ void dd::Systems::BallSystem::Initialize()
|
||||
{
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_Contact, BallSystem::Contact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, BallSystem::OnLifeLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBallLost, BallSystem::OnMultiBallLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EResetBall, &BallSystem::OnResetBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, &BallSystem::OnMultiBall);
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::Update(double dt)
|
||||
{
|
||||
|
||||
if (Lives() < 0)
|
||||
{
|
||||
SetLives(3);
|
||||
Events::GameOver e;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto ballComponent = m_World->GetComponent<Components::Ball>(entity);
|
||||
if (ballComponent != nullptr) {
|
||||
if (ReplaceBall() == true) {
|
||||
SetReplaceBall(false);
|
||||
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
transform->Position = glm::vec3(0.0f, 0.26f, -10.f);
|
||||
transform->Velocity = glm::vec3(0.0f, -ballComponent->Speed, 0.f);
|
||||
}
|
||||
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (glm::abs(transformBall->Velocity.y) < 2) {
|
||||
if (transformBall->Velocity.y > 0) {
|
||||
transformBall->Velocity.y = 2;
|
||||
} else {
|
||||
transformBall->Velocity.y = -2;
|
||||
}
|
||||
}
|
||||
if (transformBall->Position.y < -EdgeY() - 4) {
|
||||
if (MultiBalls() != 0) {
|
||||
// m_World->RemoveComponent<Components::Ball>(entity);
|
||||
// m_World->RemoveComponent<Components::Transform>(entity);
|
||||
// m_World->RemoveComponent<Components::CircleShape>(entity);
|
||||
// m_World->RemoveComponent<Components::Physics>(entity);
|
||||
m_World->RemoveEntity(entity);
|
||||
Events::MultiBallLost e;
|
||||
EventBroker->Publish(e);
|
||||
} else if (Lives() == PastLives()) {
|
||||
Events::ResetBall be;
|
||||
EventBroker->Publish(be);
|
||||
Events::LifeLost e;
|
||||
e.Entity = entity;
|
||||
EventBroker->Publish(e);
|
||||
return;
|
||||
}
|
||||
} else if (transformBall->Position.x > EdgeX()) {
|
||||
if (transformBall->Velocity.x > 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(1, 0));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
} else if (transformBall->Position.x < -EdgeX()) {
|
||||
if (transformBall->Velocity.x < 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(-1, 0));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
} else if (transformBall->Position.y > EdgeY()) {
|
||||
if (transformBall->Velocity.y > 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(0, 1));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Lives() != PastLives()) {
|
||||
/*auto life = m_World->GetComponent<Components::Life>(entity);
|
||||
if (life != nullptr) {
|
||||
if (life->Number + 1 == PastLives()) {
|
||||
/*m_World->RemoveComponent<Components::Life>(entity);
|
||||
// m_World->RemoveComponent<Components::Transform>(entity);
|
||||
m_World->RemoveEntity(entity);*/
|
||||
SetPastLives(Lives());
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
if (ballComponent != nullptr) {
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
glm::vec2 dir = glm::normalize(glm::vec2(transform->Velocity.x, transform->Velocity.y));
|
||||
@@ -99,3 +172,72 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
EntityID dd::Systems::BallSystem::CreateBall()
|
||||
{
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(0.5f, 0.26f, -10.f);
|
||||
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
|
||||
auto model = m_World->AddComponent<Components::Model>(ent);
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
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;
|
||||
physics->Category = CollisionLayer::Type::Ball;
|
||||
physics->Mask = CollisionLayer::Type::Pad | CollisionLayer::Type::Brick | CollisionLayer::Type::Wall;
|
||||
physics->Calculate = true;
|
||||
cball->Speed = 5.f;
|
||||
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
return ent;
|
||||
}
|
||||
|
||||
bool dd::Systems::BallSystem::OnLifeLost(const dd::Events::LifeLost &event)
|
||||
{
|
||||
SetLives(Lives() - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::BallSystem::OnMultiBallLost(const dd::Events::MultiBallLost &event)
|
||||
{
|
||||
SetMultiBalls(MultiBalls()-1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::BallSystem::OnResetBall(const dd::Events::ResetBall &event)
|
||||
{
|
||||
SetReplaceBall(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::BallSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
auto ent1 = CreateBall();
|
||||
auto ent2 = CreateBall();
|
||||
auto transform1 = m_World->GetComponent<Components::Transform>(ent1);
|
||||
auto transform2 = m_World->GetComponent<Components::Transform>(ent2);
|
||||
auto ball1 = m_World->GetComponent<Components::Ball>(ent1);
|
||||
auto ball2 = m_World->GetComponent<Components::Ball>(ent2);
|
||||
auto padTransform = event.padTransform;
|
||||
float x1 = padTransform->Position.x - 2, x2 = padTransform->Position.x + 2;
|
||||
if (x1 < -3.1) {
|
||||
x1 = 3;
|
||||
}
|
||||
if (x2 > 3.1) {
|
||||
x2 = -3;
|
||||
}
|
||||
transform1->Position = glm::vec3(x1, -5.5, -10);
|
||||
transform2->Position = glm::vec3(x2, -5.5, -10);
|
||||
|
||||
transform1->Velocity = glm::normalize(glm::vec3(5, 5 ,0.f)) * ball1->Speed;
|
||||
transform2->Velocity = glm::normalize(glm::vec3(-5, 5 ,0.f)) * ball2->Speed;
|
||||
|
||||
SetMultiBalls(MultiBalls() + 2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -10,16 +10,16 @@
|
||||
void dd::Systems::LevelSystem::Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, LevelSystem::OnLifeLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EScoreEvent, LevelSystem::OnScoreEvent);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, LevelSystem::OnMultiBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBallLost, LevelSystem::OnMultiBallLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ECreatePowerUp, LevelSystem::OnCreatePowerUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPowerUpTaken, LevelSystem::OnPowerUpTaken);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, LevelSystem::OnStageCleared);
|
||||
|
||||
for (int i = 0; i < Lives(); i++) {
|
||||
/*for (int i = 0; i < Lives(); i++) {
|
||||
CreateLife(i);
|
||||
}
|
||||
}*/
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -47,13 +47,6 @@ void dd::Systems::LevelSystem::Update(double dt)
|
||||
CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
m_Initialized = true;
|
||||
}
|
||||
|
||||
if (Lives() < 0)
|
||||
{
|
||||
SetLives(3);
|
||||
Events::GameOver e;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
@@ -61,64 +54,10 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
SetNotResettingTheStage(NotResettingTheStage() + 0.01 * dt);
|
||||
|
||||
if (ball != nullptr) {
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (glm::abs(transformBall->Velocity.y) < 2) {
|
||||
if (transformBall->Velocity.y > 0) {
|
||||
transformBall->Velocity.y = 2;
|
||||
} else {
|
||||
transformBall->Velocity.y = -2;
|
||||
}
|
||||
}
|
||||
if (transformBall->Position.y < -EdgeY() - 4) {
|
||||
if (MultiBalls() != 0) {
|
||||
SetMultiBalls(MultiBalls() - 1);
|
||||
// m_World->RemoveComponent<Components::Ball>(entity);
|
||||
// m_World->RemoveComponent<Components::Transform>(entity);
|
||||
// m_World->RemoveComponent<Components::CircleShape>(entity);
|
||||
// m_World->RemoveComponent<Components::Physics>(entity);
|
||||
m_World->RemoveEntity(entity);
|
||||
} else if (Lives() == PastLives()) {
|
||||
Events::ResetBall be;
|
||||
EventBroker->Publish(be);
|
||||
Events::LifeLost e;
|
||||
e.Entity = entity;
|
||||
EventBroker->Publish(e);
|
||||
return;
|
||||
}
|
||||
} else if (transformBall->Position.x > EdgeX()) {
|
||||
if (transformBall->Velocity.x > 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(1, 0));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
} else if (transformBall->Position.x < -EdgeX()) {
|
||||
if (transformBall->Velocity.x < 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(-1, 0));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
} else if (transformBall->Position.y > EdgeY()) {
|
||||
if (transformBall->Velocity.y > 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(0, 1));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Lives() != PastLives()) {
|
||||
/*auto life = m_World->GetComponent<Components::Life>(entity);
|
||||
if (life != nullptr) {
|
||||
if (life->Number + 1 == PastLives()) {
|
||||
/*m_World->RemoveComponent<Components::Life>(entity);
|
||||
// m_World->RemoveComponent<Components::Transform>(entity);
|
||||
m_World->RemoveEntity(entity);*/
|
||||
SetPastLives(Lives());
|
||||
// }
|
||||
//}
|
||||
}
|
||||
if (NumberOfBricks() <= 0) {
|
||||
/*SetRestarting(true);
|
||||
if (NumberOfBricks() <= 0 && Restarting() == false) {
|
||||
if (MultiBalls() <= 0 && PowerUps() <= 0) {
|
||||
if (NotResettingTheStage() > 5) {
|
||||
SetRestarting(true);
|
||||
Events::ResetBall e;
|
||||
EventBroker->Publish(e);
|
||||
Events::ScoreEvent es;
|
||||
@@ -127,11 +66,12 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
||||
Events::StageCleared ec;
|
||||
EventBroker->Publish(ec);
|
||||
SetNotResettingTheStage(0);
|
||||
CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
//CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
}
|
||||
} else {
|
||||
if (ball != nullptr && MultiBalls() > 0) {
|
||||
SetMultiBalls(MultiBalls() - 1);
|
||||
Events::MultiBallLost e;
|
||||
EventBroker->Publish(e);
|
||||
m_World->RemoveComponent<Components::Ball>(entity);
|
||||
m_World->RemoveComponent<Components::Transform>(entity);
|
||||
m_World->RemoveComponent<Components::CircleShape>(entity);
|
||||
@@ -147,7 +87,7 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
||||
m_World->RemoveEntity(entity);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,13 +249,6 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnLifeLost(const dd::Events::LifeLost &event)
|
||||
{
|
||||
SetLives(Lives() - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnScoreEvent(const dd::Events::ScoreEvent &event)
|
||||
{
|
||||
SetScore(Score() += event.Score);
|
||||
@@ -326,6 +259,13 @@ bool dd::Systems::LevelSystem::OnScoreEvent(const dd::Events::ScoreEvent &event)
|
||||
bool dd::Systems::LevelSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
SetMultiBalls(MultiBalls()+2);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnMultiBallLost(const dd::Events::MultiBallLost &event)
|
||||
{
|
||||
SetMultiBalls(MultiBalls()-1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnCreatePowerUp(const dd::Events::CreatePowerUp &event)
|
||||
|
||||
@@ -1,372 +0,0 @@
|
||||
//
|
||||
// Created by Adniklastrator on 2015-09-09.
|
||||
//
|
||||
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Game/LevelSystem.h"
|
||||
|
||||
|
||||
void dd::Systems::LevelSystem::Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, LevelSystem::OnLifeLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EScoreEvent, LevelSystem::OnScoreEvent);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, LevelSystem::OnMultiBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ECreatePowerUp, LevelSystem::OnCreatePowerUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPowerUpTaken, LevelSystem::OnPowerUpTaken);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, LevelSystem::OnStageCleared);
|
||||
|
||||
for (int i = 0; i < Lives(); i++) {
|
||||
CreateLife(i);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
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(-1.5f + number * 0.15f, -2.f, -5.f);
|
||||
transform->Scale = glm::vec3(0.1f, 0.1f, 0.1f);
|
||||
|
||||
std::shared_ptr<Components::Life> lifeNr = m_World->AddComponent<Components::Life>(life);
|
||||
lifeNr->Number = number;
|
||||
|
||||
auto model = m_World->AddComponent<Components::Model>(life);
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
|
||||
|
||||
m_World->CommitEntity(life);
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::Update(double dt)
|
||||
{
|
||||
if (!m_Initialized) {
|
||||
CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
m_Initialized = true;
|
||||
}
|
||||
|
||||
if (Lives() < 0)
|
||||
{
|
||||
SetLives(3);
|
||||
Events::GameOver e;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
|
||||
if (ball != nullptr) {
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (glm::abs(transformBall->Velocity.y) < 2) {
|
||||
if (transformBall->Velocity.y > 0) {
|
||||
transformBall->Velocity.y = 2;
|
||||
} else {
|
||||
transformBall->Velocity.y = -2;
|
||||
}
|
||||
}
|
||||
if (transformBall->Position.y < -EdgeY() - 4) {
|
||||
if (MultiBalls() != 0) {
|
||||
SetMultiBalls(MultiBalls() - 1);
|
||||
// m_World->RemoveComponent<Components::Ball>(entity);
|
||||
// m_World->RemoveComponent<Components::Transform>(entity);
|
||||
// m_World->RemoveComponent<Components::CircleShape>(entity);
|
||||
// m_World->RemoveComponent<Components::Physics>(entity);
|
||||
m_World->RemoveEntity(entity);
|
||||
} else if (Lives() == PastLives()) {
|
||||
Events::ResetBall be;
|
||||
EventBroker->Publish(be);
|
||||
Events::LifeLost e;
|
||||
e.Entity = entity;
|
||||
EventBroker->Publish(e);
|
||||
return;
|
||||
}
|
||||
} else if (transformBall->Position.x > EdgeX()) {
|
||||
if (transformBall->Velocity.x > 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(1, 0));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
} else if (transformBall->Position.x < -EdgeX()) {
|
||||
if (transformBall->Velocity.x < 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(-1, 0));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
} else if (transformBall->Position.y > EdgeY()) {
|
||||
if (transformBall->Velocity.y > 0) {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(0, 1));
|
||||
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Lives() != PastLives()) {
|
||||
auto life = m_World->GetComponent<Components::Life>(entity);
|
||||
if (life != nullptr) {
|
||||
if (life->Number + 1 == PastLives()) {
|
||||
// m_World->RemoveComponent<Components::Life>(entity);
|
||||
// m_World->RemoveComponent<Components::Transform>(entity);
|
||||
m_World->RemoveEntity(entity);
|
||||
SetPastLives(Lives());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (NumberOfBricks() <= 0) {
|
||||
/*SetRestarting(true);
|
||||
if (MultiBalls() <= 0 && PowerUps() <= 0) {
|
||||
Events::ResetBall e;
|
||||
EventBroker->Publish(e);
|
||||
Events::ScoreEvent es;
|
||||
es.Score = 500;
|
||||
EventBroker->Publish(es);
|
||||
Events::StageCleared ec;
|
||||
EventBroker->Publish(ec);
|
||||
CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
} else {
|
||||
if (ball != nullptr && MultiBalls() > 0) {
|
||||
SetMultiBalls(MultiBalls() - 1);
|
||||
m_World->RemoveComponent<Components::Ball>(entity);
|
||||
m_World->RemoveComponent<Components::Transform>(entity);
|
||||
m_World->RemoveComponent<Components::CircleShape>(entity);
|
||||
m_World->RemoveComponent<Components::Physics>(entity);
|
||||
m_World->RemoveEntity(entity);
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entity);
|
||||
} else {
|
||||
auto powerUp = m_World->GetComponent<Components::PowerUp>(entity);
|
||||
if (powerUp != nullptr) {
|
||||
SetPowerUps(PowerUps() - 1);
|
||||
m_World->RemoveComponent<Components::PowerUp>(entity);
|
||||
m_World->RemoveComponent<Components::Transform>(entity);
|
||||
m_World->RemoveEntity(entity);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, float spaceToEdge)
|
||||
{
|
||||
SetNumberOfBricks(rows * lines);
|
||||
std::cout << "You're only doing this once, right?" << std::endl;
|
||||
int num = 0;
|
||||
for (int i = 0; i < rows; i++) {
|
||||
num++;
|
||||
for (int j = 0; j < Lines(); j++) {
|
||||
CreateBrick(i, j, spacesBetweenBricks, spaceToEdge, num);
|
||||
}
|
||||
if (num == 7)
|
||||
num = 0;
|
||||
}
|
||||
|
||||
SetNumberOfBricks(rows * lines);
|
||||
SetRestarting(false);
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBetweenBricks, float spaceToEdge, int num)
|
||||
{
|
||||
auto brick = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(brick);
|
||||
auto model = m_World->AddComponent<Components::Model>(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);
|
||||
cPhys->Static = false;
|
||||
cPhys->GravityScale = 0.f;
|
||||
cPhys->Category = CollisionLayer::Type::Brick;
|
||||
cPhys->Mask = CollisionLayer::Type::Ball;
|
||||
|
||||
std::string fileName = "Textures/Bricks/";
|
||||
fileName.append(std::to_string(num));
|
||||
fileName.append(".png");
|
||||
//sprite->SpriteFile = fileName;
|
||||
model->ModelFile = "Models/Test/Brick/Brick.obj";
|
||||
if ((line == 1 && row == 4) || (line == 3 && row == 2) || (line == 6 && row == 6)) {
|
||||
std::shared_ptr<Components::PowerUpBrick> cPow = m_World->AddComponent<Components::PowerUpBrick>(brick);
|
||||
}
|
||||
float x = line * spacesBetweenBricks.x;
|
||||
float y = row * spacesBetweenBricks.y;
|
||||
transform->Scale = glm::vec3(0.8, 0.2, 0.2);
|
||||
transform->Position = glm::vec3(x - 3, 5 - spaceToEdge - y , -10.f);
|
||||
cBrick->Score = 10 * num;
|
||||
|
||||
//sound
|
||||
auto collisionSound = m_World->AddComponent<Components::CollisionSound>(brick);
|
||||
collisionSound->filePath = "Sounds/Brick/shortbrickbreak.wav";
|
||||
|
||||
m_World->CommitEntity(brick);
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::ProcessCollision()
|
||||
{
|
||||
// Like, go into brick component and check what it does upon collision. Maybe not done here?
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::OnEntityRemoved(EntityID entity)
|
||||
{
|
||||
auto brick = m_World->GetComponent<Components::Brick>(entity);
|
||||
if (brick != nullptr) {
|
||||
SetNumberOfBricks(NumberOfBricks() - 1);
|
||||
Events::ScoreEvent es;
|
||||
es.Score = brick->Score;
|
||||
EventBroker->Publish(es);
|
||||
/*if (numberOfBricks < 1) {
|
||||
EndLevel();
|
||||
}*/
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
|
||||
{
|
||||
EntityID entityBrick;
|
||||
EntityID entityBall;
|
||||
auto ball = m_World->GetComponent<Components::Ball>(event.Entity2);
|
||||
auto brick = m_World->GetComponent<Components::Brick>(event.Entity1);
|
||||
if (brick != nullptr) {
|
||||
entityBrick = event.Entity1;
|
||||
} else {
|
||||
brick = m_World->GetComponent<Components::Brick>(event.Entity2);
|
||||
if (brick != nullptr) {
|
||||
entityBrick = event.Entity2;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (ball != nullptr) {
|
||||
entityBall = event.Entity2;
|
||||
} else {
|
||||
ball = m_World->GetComponent<Components::Ball>(event.Entity1);
|
||||
if (ball != nullptr) {
|
||||
entityBall = event.Entity1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
brick = m_World->GetComponent<Components::Brick>(entityBrick);
|
||||
if (brick == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto power = m_World->GetComponent<Components::PowerUpBrick>(entityBrick);
|
||||
if (power != nullptr) {
|
||||
Events::CreatePowerUp e;
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entityBrick);
|
||||
e.Position = transform->Position;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
if (ball != nullptr && Restarting() == false) {
|
||||
auto ballTransform = m_World->GetComponent<Components::Transform>(entityBall);
|
||||
|
||||
// m_World->RemoveComponent<Components::Brick>(entityBrick);
|
||||
// m_World->RemoveComponent<Components::Transform>(entityBrick);
|
||||
// m_World->RemoveComponent<Components::RectangleShape>(entityBrick);
|
||||
// m_World->RemoveComponent<Components::Physics>(entityBrick);
|
||||
// m_World->RemoveEntity(entityBrick);
|
||||
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entityBrick);
|
||||
physicsComponent->GravityScale = 1.f;
|
||||
physicsComponent->Mask = CollisionLayer::Type::Water | CollisionLayer::Type::Wall;
|
||||
|
||||
auto transformComponentBrick = m_World->GetComponent<Components::Transform>(entityBrick);
|
||||
auto transformComponentBall = m_World->GetComponent<Components::Transform>(entityBall);
|
||||
|
||||
Events::SetImpulse e;
|
||||
e.Entity = entityBrick;
|
||||
|
||||
glm::vec2 point = glm::vec2(transformComponentBrick->Position.x + ((transformComponentBrick->Position.x - transformComponentBall->Position.x)/4), transformComponentBrick->Position.y + ((transformComponentBrick->Position.y - transformComponentBall->Position.y )/4));
|
||||
|
||||
e.Impulse = glm::normalize(point)/2.f;
|
||||
e.Point = point;
|
||||
EventBroker->Publish(e);
|
||||
//TODO: Bricks dont collide with walls D=
|
||||
|
||||
SetNumberOfBricks(NumberOfBricks() - 1);
|
||||
if (NumberOfBricks() <= 0) {
|
||||
SetMultiBalls(MultiBalls() + 1);
|
||||
}
|
||||
Events::ScoreEvent es;
|
||||
es.Score = brick->Score;
|
||||
EventBroker->Publish(es);
|
||||
|
||||
<<<<<<< HEAD
|
||||
// std::cout << NumberOfBricks() << std::endl;
|
||||
=======
|
||||
//std::cout << NumberOfBricks() << std::endl;
|
||||
>>>>>>> origin/master
|
||||
//std::cout << "Score: " << Score() << std::endl;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnLifeLost(const dd::Events::LifeLost &event)
|
||||
{
|
||||
SetLives(Lives() - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnScoreEvent(const dd::Events::ScoreEvent &event)
|
||||
{
|
||||
SetScore(Score() += event.Score);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
SetMultiBalls(MultiBalls()+2);
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnCreatePowerUp(const dd::Events::CreatePowerUp &event)
|
||||
{
|
||||
SetPowerUps(PowerUps() + 1);
|
||||
auto powerUp = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(powerUp);
|
||||
auto model = m_World->AddComponent<Components::Model>(powerUp);
|
||||
std::shared_ptr<Components::CircleShape> cRec = m_World->AddComponent<Components::CircleShape>(powerUp);
|
||||
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(powerUp);
|
||||
cPhys->Static = false;
|
||||
cPhys->Category = CollisionLayer::Type::PowerUp;
|
||||
cPhys->Mask = CollisionLayer::Type::Pad;
|
||||
|
||||
std::shared_ptr<Components::PowerUp> cPow = m_World->AddComponent<Components::PowerUp>(powerUp);
|
||||
|
||||
|
||||
transform->Position = event.Position;
|
||||
transform->Scale = glm::vec3(0.2, 0.2, 0.2);
|
||||
transform->Velocity = glm::vec3(0, -4, 0);
|
||||
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
|
||||
m_World->CommitEntity(powerUp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnPowerUpTaken(const dd::Events::PowerUpTaken &event)
|
||||
{
|
||||
SetPowerUps(PowerUps() - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnStageCleared(const dd::Events::StageCleared &event)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::EndLevel()
|
||||
{
|
||||
Events::StageCleared e;
|
||||
EventBroker->Publish(e);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -26,23 +26,12 @@ void dd::Systems::PadSystem::Initialize()
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &PadSystem::OnKeyUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, &PadSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContactPowerUp, &PadSystem::OnContactPowerUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EResetBall, &PadSystem::OnResetBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, &PadSystem::OnMultiBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, &PadSystem::OnStageCleared);
|
||||
}
|
||||
|
||||
void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
if (ball != nullptr) {
|
||||
if (ReplaceBall() == true) {
|
||||
SetReplaceBall(false);
|
||||
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
transform->Position = glm::vec3(0.0f, 0.26f, -10.f);
|
||||
transform->Velocity = glm::vec3(0.0f, -ball->Speed, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::PadSystem::Update(double dt)
|
||||
@@ -72,7 +61,6 @@ void dd::Systems::PadSystem::Update(double dt)
|
||||
transform->Velocity += acceleration * (float)dt;
|
||||
transform->Velocity -= transform->Velocity * pad->SlowdownModifier * (float)dt;
|
||||
|
||||
|
||||
if (Left()) {
|
||||
acceleration.x = -pad->AccelerationSpeed;
|
||||
} else if (Right()) {
|
||||
@@ -85,40 +73,9 @@ void dd::Systems::PadSystem::Update(double dt)
|
||||
SetPad(pad);
|
||||
SetAcceleration(acceleration);
|
||||
|
||||
if (MultiBall() == true) {
|
||||
SetMultiBall(false);
|
||||
|
||||
Events::MultiBall e;
|
||||
e.padTransform = transform;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
EntityID dd::Systems::PadSystem::CreateBall()
|
||||
{
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(0.5f, 0.26f, -10.f);
|
||||
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
|
||||
auto model = m_World->AddComponent<Components::Model>(ent);
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
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;
|
||||
physics->Category = CollisionLayer::Type::Ball;
|
||||
physics->Mask = CollisionLayer::Type::Pad | CollisionLayer::Type::Brick | CollisionLayer::Type::Wall;
|
||||
physics->Calculate = true;
|
||||
cball->Speed = 5.f;
|
||||
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
return ent;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
{
|
||||
int val = event.KeyCode;
|
||||
@@ -135,9 +92,12 @@ bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
//acceleration.x = 0.01f;
|
||||
SetRight(true);
|
||||
} else if (val == GLFW_KEY_R) {
|
||||
SetReplaceBall(true);
|
||||
Events::ResetBall e;
|
||||
EventBroker->Publish(e);
|
||||
} else if (val == GLFW_KEY_M) {
|
||||
SetMultiBall(true);
|
||||
Events::MultiBall e;
|
||||
e.padTransform = Transform();
|
||||
EventBroker->Publish(e);
|
||||
} else if (val == GLFW_KEY_D) {
|
||||
return false;
|
||||
}
|
||||
@@ -251,37 +211,6 @@ bool dd::Systems::PadSystem::OnContactPowerUp(const dd::Events::Contact &event)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnResetBall(const dd::Events::ResetBall &event)
|
||||
{
|
||||
SetReplaceBall(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
auto ent1 = CreateBall();
|
||||
auto ent2 = CreateBall();
|
||||
auto transform1 = m_World->GetComponent<Components::Transform>(ent1);
|
||||
auto transform2 = m_World->GetComponent<Components::Transform>(ent2);
|
||||
auto ball1 = m_World->GetComponent<Components::Ball>(ent1);
|
||||
auto ball2 = m_World->GetComponent<Components::Ball>(ent2);
|
||||
auto padTransform = event.padTransform;
|
||||
float x1 = padTransform->Position.x - 2, x2 = padTransform->Position.x + 2;
|
||||
if (x1 < -3.1) {
|
||||
x1 = 3;
|
||||
}
|
||||
if (x2 > 3.1) {
|
||||
x2 = -3;
|
||||
}
|
||||
transform1->Position = glm::vec3(x1, -5.5, -10);
|
||||
transform2->Position = glm::vec3(x2, -5.5, -10);
|
||||
|
||||
transform1->Velocity = glm::normalize(glm::vec3(5, 5 ,0.f)) * ball1->Speed;
|
||||
transform2->Velocity = glm::normalize(glm::vec3(-5, 5 ,0.f)) * ball2->Speed;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnStageCleared(const dd::Events::StageCleared &event)
|
||||
{
|
||||
//auto entity = CreateBall();
|
||||
|
||||
@@ -1,313 +0,0 @@
|
||||
//
|
||||
// Created by Adniklastrator on 2015-09-10.
|
||||
//
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Game/PadSystem.h"
|
||||
#include "Core/World.h"
|
||||
#include <iostream>
|
||||
#include <Game/CPad.h>
|
||||
#include <Rendering/CPointLight.h>
|
||||
|
||||
|
||||
void dd::Systems::PadSystem::Initialize()
|
||||
{
|
||||
Events::BindKey r;
|
||||
r.KeyCode = GLFW_KEY_RIGHT;
|
||||
r.Command = "right";
|
||||
EventBroker->Publish(r);
|
||||
|
||||
Events::BindKey l;
|
||||
l.KeyCode = GLFW_KEY_LEFT;
|
||||
l.Command = "left";
|
||||
EventBroker->Publish(l);
|
||||
|
||||
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_EContactPowerUp, &PadSystem::OnContactPowerUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EResetBall, &PadSystem::OnResetBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, &PadSystem::OnMultiBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, &PadSystem::OnStageCleared);
|
||||
}
|
||||
|
||||
void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
if (ball != nullptr) {
|
||||
if (ReplaceBall() == true) {
|
||||
SetReplaceBall(false);
|
||||
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
transform->Position = glm::vec3(0.0f, 0.26f, -10.f);
|
||||
transform->Velocity = glm::vec3(0.0f, -ball->Speed, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::PadSystem::Update(double dt)
|
||||
{
|
||||
if (Entity() == 0) {
|
||||
for (auto it = m_World->GetEntities()->begin(); it != m_World->GetEntities()->end(); it++) {
|
||||
if (m_World->GetProperty<std::string>(it->first, "Name") == "Pad") {
|
||||
SetEntity(it->first);
|
||||
SetTransform(m_World->GetComponent<Components::Transform>(Entity()));
|
||||
SetPad(m_World->GetComponent<Components::Pad>(Entity()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto transform = Transform();
|
||||
auto pad = Pad();
|
||||
auto acceleration = Acceleration();
|
||||
|
||||
if (transform->Velocity.x < -pad->MaxSpeed) {
|
||||
transform->Velocity.x = -pad->MaxSpeed;
|
||||
}
|
||||
else if (transform->Velocity.x > pad->MaxSpeed) {
|
||||
transform->Velocity.x = pad->MaxSpeed;
|
||||
}
|
||||
transform->Position += transform->Velocity * (float)dt;
|
||||
transform->Velocity += acceleration * (float)dt;
|
||||
transform->Velocity -= transform->Velocity * pad->SlowdownModifier * (float)dt;
|
||||
|
||||
|
||||
if (Left()) {
|
||||
acceleration.x = -pad->AccelerationSpeed;
|
||||
<<<<<<< HEAD
|
||||
}
|
||||
else if (Right()) {
|
||||
=======
|
||||
} else if (Right()) {
|
||||
>>>>>>> 72413dc0a93bd3a2f6ab9fa7e19bbc3b846232db
|
||||
acceleration.x = pad->AccelerationSpeed;
|
||||
} else {
|
||||
acceleration.x = 0.f;
|
||||
}
|
||||
|
||||
SetTransform(transform);
|
||||
SetPad(pad);
|
||||
SetAcceleration(acceleration);
|
||||
|
||||
if (MultiBall() == true) {
|
||||
SetMultiBall(false);
|
||||
|
||||
Events::MultiBall e;
|
||||
e.padTransform = transform;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
EntityID dd::Systems::PadSystem::CreateBall()
|
||||
{
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(0.5f, 0.26f, -10.f);
|
||||
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
|
||||
auto model = m_World->AddComponent<Components::Model>(ent);
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
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;
|
||||
|
||||
cball->Speed = 10;
|
||||
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
return ent;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
{
|
||||
int val = event.KeyCode;
|
||||
if (val == GLFW_KEY_UP) {
|
||||
//std::cout << "Up!" << std::endl;
|
||||
} else if (val == GLFW_KEY_DOWN) {
|
||||
//std::cout << "Down!" << std::endl;
|
||||
} else if (val == GLFW_KEY_LEFT) {
|
||||
//std::cout << "Left!" << std::endl;
|
||||
//acceleration.x = -0.01f;
|
||||
SetLeft(true);
|
||||
} else if (val == GLFW_KEY_RIGHT) {
|
||||
//std::cout << "Right!" << std::endl;
|
||||
//acceleration.x = 0.01f;
|
||||
SetRight(true);
|
||||
} else if (val == GLFW_KEY_R) {
|
||||
SetReplaceBall(true);
|
||||
} else if (val == GLFW_KEY_M) {
|
||||
SetMultiBall(true);
|
||||
} else if (val == GLFW_KEY_D) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event)
|
||||
{
|
||||
int val = event.KeyCode;
|
||||
if (val == GLFW_KEY_UP) {
|
||||
|
||||
} else if (val == GLFW_KEY_DOWN) {
|
||||
|
||||
} else if (val == GLFW_KEY_LEFT) {
|
||||
SetLeft(false);
|
||||
} else if (val == GLFW_KEY_RIGHT) {
|
||||
SetRight(false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
{
|
||||
/*
|
||||
EntityID entityBall = event.Entity2;
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entityBall);
|
||||
if (ball == NULL) {
|
||||
return false;
|
||||
}
|
||||
EntityID entityPad = event.Entity1;
|
||||
auto pad = m_World->GetComponent<Components::Pad>(entityPad);
|
||||
if (pad == NULL) {
|
||||
return false;
|
||||
}
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entityBall);
|
||||
auto transformPad = m_World->GetComponent<Components::Transform>(entityPad);
|
||||
|
||||
float movementMultiplier = 0.5f;
|
||||
|
||||
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
//float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f;
|
||||
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 movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
<<<<<<< HEAD
|
||||
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2) + 1;
|
||||
=======
|
||||
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2)+ 0.2;
|
||||
>>>>>>> 72413dc0a93bd3a2f6ab9fa7e19bbc3b846232db
|
||||
|
||||
//std::cout << movementX << " " << movementY << std::endl;
|
||||
|
||||
float len = glm::length<float>(transformBall->Velocity);
|
||||
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
|
||||
|
||||
transformBall->Velocity += glm::vec3(transformPad->Velocity.x, 0, 0);
|
||||
transformBall->Velocity = glm::normalize(transformBall->Velocity) * len;
|
||||
|
||||
|
||||
//transform->Velocity = glm::vec3(movementX, movementY, 0.f);
|
||||
*/
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnContactPowerUp(const dd::Events::Contact &event)
|
||||
{
|
||||
EntityID entityPower;
|
||||
EntityID entityPad;
|
||||
auto powerUp = m_World->GetComponent<Components::PowerUp>(event.Entity1);
|
||||
auto pad = m_World->GetComponent<Components::Pad>(event.Entity2);
|
||||
if (powerUp != nullptr) {
|
||||
entityPower = event.Entity1;
|
||||
} else {
|
||||
powerUp = m_World->GetComponent<Components::PowerUp>(event.Entity2);
|
||||
if (powerUp != nullptr) {
|
||||
entityPower = event.Entity2;
|
||||
}
|
||||
}
|
||||
if (pad != nullptr) {
|
||||
entityPad = event.Entity2;
|
||||
} else {
|
||||
pad = m_World->GetComponent<Components::Pad>(event.Entity1);
|
||||
if (pad != nullptr) {
|
||||
entityPad = event.Entity1;
|
||||
}
|
||||
}
|
||||
|
||||
if (entityPower == NULL || entityPad == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pad = m_World->GetComponent<Components::Pad>(entityPad);
|
||||
if (pad == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_World->RemoveComponent<Components::PowerUp>(entityPower);
|
||||
m_World->RemoveComponent<Components::CircleShape>(entityPower);
|
||||
m_World->RemoveComponent<Components::Physics>(entityPower);
|
||||
m_World->RemoveEntity(entityPower);
|
||||
Events::PowerUpTaken ep;
|
||||
ep.Name = "Something";
|
||||
EventBroker->Publish(ep);
|
||||
|
||||
Events::MultiBall e;
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entityPad);
|
||||
e.padTransform = transform;
|
||||
EventBroker->Publish(e);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnResetBall(const dd::Events::ResetBall &event)
|
||||
{
|
||||
SetReplaceBall(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
auto ent1 = CreateBall();
|
||||
auto ent2 = CreateBall();
|
||||
auto transform1 = m_World->GetComponent<Components::Transform>(ent1);
|
||||
auto transform2 = m_World->GetComponent<Components::Transform>(ent2);
|
||||
auto ball1 = m_World->GetComponent<Components::Ball>(ent1);
|
||||
auto ball2 = m_World->GetComponent<Components::Ball>(ent2);
|
||||
auto padTransform = event.padTransform;
|
||||
float x1 = padTransform->Position.x - 2, x2 = padTransform->Position.x + 2;
|
||||
if (x1 < -3.1) {
|
||||
x1 = 3;
|
||||
}
|
||||
if (x2 > 3.1) {
|
||||
x2 = -3;
|
||||
}
|
||||
transform1->Position = glm::vec3(x1, -5.5, -10);
|
||||
transform2->Position = glm::vec3(x2, -5.5, -10);
|
||||
|
||||
transform1->Velocity = glm::normalize(glm::vec3(5, 5 ,0.f)) * ball1->Speed;
|
||||
transform2->Velocity = glm::normalize(glm::vec3(-5, 5 ,0.f)) * ball2->Speed;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnStageCleared(const dd::Events::StageCleared &event)
|
||||
{
|
||||
auto entity = CreateBall();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event)
|
||||
{
|
||||
std::string command = event.Command;
|
||||
|
||||
std::cout << "Command!" << std::endl;
|
||||
|
||||
if (command == "right") {
|
||||
std::cout << "Right!" << std::endl;
|
||||
} else if (command == "left") {
|
||||
std::cout << "Left!" << std::endl;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,363 +0,0 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Physics/PhysicsSystem.h"
|
||||
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
|
||||
{
|
||||
cf->Register<Components::CircleShape>();
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::Initialize()
|
||||
{
|
||||
m_ContactListener = new ContactListener(this);
|
||||
|
||||
m_Gravity = b2Vec2(0.f, -9.82f);
|
||||
m_PhysicsWorld = new b2World(m_Gravity);
|
||||
|
||||
m_TimeStep = 1.f/60.f;
|
||||
m_VelocityIterations = 6;
|
||||
m_PositionIterations = 2;
|
||||
m_Accumulator = 0.f;
|
||||
|
||||
m_PhysicsWorld->SetContactListener(m_ContactListener);
|
||||
|
||||
InitializeWater();
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, PhysicsSystem::SetImpulse);
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::InitializeWater()
|
||||
{
|
||||
b2ParticleSystemDef m_ParticleSystemDef;
|
||||
m_ParticleSystemDef.radius = 0.13f;
|
||||
|
||||
m_ParticleSystem = m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef);
|
||||
}
|
||||
|
||||
bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||
{
|
||||
b2Body* body = m_EntitiesToBodies[event.Entity];
|
||||
|
||||
b2Vec2 impulse;
|
||||
impulse.x = event.Impulse.x;
|
||||
impulse.y = event.Impulse.y;
|
||||
|
||||
b2Vec2 point;
|
||||
point.x = event.Point.x;
|
||||
point.y = event.Point.y;
|
||||
|
||||
|
||||
Impulse i;
|
||||
i.Body = body;
|
||||
i.Impulse = impulse;
|
||||
i.Point = point;
|
||||
|
||||
m_Impulses.push_back(i);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
{
|
||||
|
||||
m_Accumulator += dt;
|
||||
while(m_Accumulator >= m_TimeStep)
|
||||
{
|
||||
|
||||
for (auto i : m_EntitiesToBodies) {
|
||||
EntityID entity = i.first;
|
||||
b2Body* body = i.second;
|
||||
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (! transformComponent) {
|
||||
continue;
|
||||
LOG_ERROR("RigidBody with no TransformComponent");
|
||||
}
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
if (! physicsComponent) {
|
||||
continue;
|
||||
LOG_ERROR("RigidBody with no PhysicsComponent");
|
||||
}
|
||||
|
||||
|
||||
if (body == nullptr) {
|
||||
LOG_ERROR("This body should not exist");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m_World->GetEntityParent(entity) == 0) { //TODO: Make this work with childs too
|
||||
b2Vec2 position;
|
||||
position.x = transformComponent->Position.x;
|
||||
position.y = transformComponent->Position.y;
|
||||
float angle = -glm::eulerAngles(transformComponent->Orientation).z;
|
||||
body->SetTransform(position, angle);
|
||||
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
|
||||
|
||||
body->SetGravityScale(physicsComponent->GravityScale);
|
||||
|
||||
<<<<<<< HEAD
|
||||
|
||||
b2Filter filter;
|
||||
filter.categoryBits = physicsComponent->Category;
|
||||
filter.maskBits = physicsComponent->Mask;
|
||||
body->GetFixtureList()->SetFilterData(filter);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i : m_Impulses) {
|
||||
i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
|
||||
=======
|
||||
>>>>>>> origin/master
|
||||
}
|
||||
m_Impulses.clear();
|
||||
|
||||
|
||||
<<<<<<< HEAD
|
||||
|
||||
|
||||
=======
|
||||
for (auto i : m_Impulses) {
|
||||
i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
|
||||
}
|
||||
m_Impulses.clear();
|
||||
m_Accumulator += dt;
|
||||
while(m_Accumulator >= m_TimeStep)
|
||||
{
|
||||
>>>>>>> origin/master
|
||||
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
|
||||
m_Accumulator -= dt;
|
||||
|
||||
<<<<<<< HEAD
|
||||
|
||||
=======
|
||||
for (auto i : m_EntitiesToBodies) {
|
||||
EntityID entity = i.first;
|
||||
b2Body* body = i.second;
|
||||
>>>>>>> origin/master
|
||||
|
||||
for (auto i : m_EntitiesToBodies) {
|
||||
EntityID entity = i.first;
|
||||
b2Body* body = i.second;
|
||||
|
||||
if (body == nullptr) {
|
||||
LOG_ERROR("This body should not exist");
|
||||
continue;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (! transformComponent)
|
||||
continue;
|
||||
if (m_World->GetEntityParent(entity) == 0) {
|
||||
b2Vec2 position = body->GetPosition();
|
||||
transformComponent->Position.x = position.x;
|
||||
transformComponent->Position.y = position.y;
|
||||
=======
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (! transformComponent)
|
||||
continue;
|
||||
auto parent = m_World->GetEntityParent(entity);
|
||||
if (parent == 0) {
|
||||
b2Vec2 position = body->GetPosition();
|
||||
transformComponent->Position.x = position.x;
|
||||
transformComponent->Position.y = position.y;
|
||||
>>>>>>> origin/master
|
||||
|
||||
float angle = body->GetAngle();
|
||||
|
||||
|
||||
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle));
|
||||
|
||||
<<<<<<< HEAD
|
||||
b2Vec2 velocity = body->GetLinearVelocity();
|
||||
transformComponent->Velocity.x = velocity.x;
|
||||
transformComponent->Velocity.y = velocity.y;
|
||||
|
||||
}
|
||||
=======
|
||||
>>>>>>> origin/master
|
||||
}
|
||||
}
|
||||
|
||||
b2Vec2* positionBuffer = m_ParticleSystem->GetPositionBuffer();
|
||||
for (auto i : m_EntitiesToParticleHandle) {
|
||||
EntityID entity = i.first;
|
||||
b2ParticleHandle* particleH = i.second;
|
||||
|
||||
b2Vec2 positionB2 = positionBuffer[particleH->GetIndex()];
|
||||
glm::vec2 position = glm::vec2(positionB2.x, positionB2.y);
|
||||
|
||||
|
||||
EntityID entityParent = m_World->GetEntityParent(entity);
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
auto transformParent = m_World->GetComponent<Components::Transform>(entityParent);
|
||||
|
||||
transform->Position = glm::vec3(position.x, position.y, -10) - transformParent->Position;
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
||||
{
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
auto waterComponent = m_World->GetComponent<Components::WaterVolume>(entity);
|
||||
|
||||
if (physicsComponent && waterComponent) {
|
||||
LOG_ERROR("Entity has both water and physics component, this is illegal. The police has been alerted.");
|
||||
return;
|
||||
}
|
||||
if (physicsComponent) {
|
||||
CreateBody(entity);
|
||||
} else if (waterComponent) {
|
||||
CreateParticleGroup(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity)
|
||||
{
|
||||
auto physics = m_World->GetComponent<Components::Physics>(entity);
|
||||
if (physics == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
b2Body* body = m_EntitiesToBodies[entity];
|
||||
|
||||
if (body != nullptr) {
|
||||
m_EntitiesToBodies.erase(entity);
|
||||
m_BodiesToEntities.erase(body);
|
||||
|
||||
m_PhysicsWorld->DestroyBody(body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
{
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
if(!physicsComponent){
|
||||
LOG_ERROR("No PhysicsComponent in CreateBody");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if(!transformComponent) {
|
||||
LOG_ERROR("No TransformComponent in CreateBody");
|
||||
return;
|
||||
}
|
||||
|
||||
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(entity);
|
||||
|
||||
b2BodyDef bodyDef;
|
||||
bodyDef.position.Set(absoluteTransform.Position.x, absoluteTransform.Position.y);
|
||||
bodyDef.angle = -glm::eulerAngles(absoluteTransform.Orientation).z;
|
||||
|
||||
if (physicsComponent->Static) {
|
||||
bodyDef.type = b2_staticBody;
|
||||
} else {
|
||||
bodyDef.type = b2_dynamicBody;
|
||||
|
||||
}
|
||||
|
||||
b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef);
|
||||
|
||||
b2Shape* pShape;
|
||||
|
||||
auto boxComponent = m_World->GetComponent<Components::RectangleShape>(entity);
|
||||
if (boxComponent) {
|
||||
b2PolygonShape* bShape = new b2PolygonShape();
|
||||
bShape->SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2);
|
||||
pShape = bShape;
|
||||
} else {
|
||||
auto circleComponent = m_World->GetComponent<Components::CircleShape>(entity);
|
||||
if (circleComponent) {
|
||||
pShape = new b2CircleShape();
|
||||
pShape->m_radius = absoluteTransform.Scale.x;
|
||||
|
||||
|
||||
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/2;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
b2FixtureDef fixtureDef;
|
||||
fixtureDef.shape = pShape;
|
||||
fixtureDef.filter.categoryBits = physicsComponent->Category;
|
||||
fixtureDef.filter.maskBits = physicsComponent->Mask;
|
||||
|
||||
|
||||
if(physicsComponent->Static) {
|
||||
body->CreateFixture(&fixtureDef); //Density kanske ska vara 0 på statiska kroppar
|
||||
}
|
||||
else {
|
||||
fixtureDef.shape = pShape;
|
||||
fixtureDef.density = 1.f;
|
||||
fixtureDef.restitution = 1.0f;
|
||||
fixtureDef.friction = 0.0f;
|
||||
body->CreateFixture(&fixtureDef);
|
||||
|
||||
}
|
||||
|
||||
delete pShape;
|
||||
|
||||
|
||||
body->SetGravityScale(physicsComponent->GravityScale);
|
||||
m_EntitiesToBodies.insert(std::make_pair(entity, body));
|
||||
m_BodiesToEntities.insert(std::make_pair(body, entity));
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e)
|
||||
{
|
||||
//TODO: Lägg alla pd i en lista
|
||||
auto transform = m_World->GetComponent<Components::Transform>(e);
|
||||
if (!transform) {
|
||||
LOG_ERROR("No Transform component in CreateParticleGroup");
|
||||
return;
|
||||
}
|
||||
|
||||
b2ParticleGroupDef pd;
|
||||
b2PolygonShape shape;
|
||||
|
||||
shape.SetAsBox(transform->Scale.x/2.f, transform->Scale.y/2.f);
|
||||
pd.shape = &shape;
|
||||
pd.flags = b2_tensileParticle;
|
||||
pd.position.Set(transform->Position.x, transform->Position.y);
|
||||
//TODO: PUT IN LIST
|
||||
t_watergroup = m_ParticleSystem->CreateParticleGroup(pd);
|
||||
b2Vec2* t_ParticlePositions = m_ParticleSystem->GetPositionBuffer();
|
||||
for(int i = 0; i < m_ParticleSystem->GetParticleCount(); i++){
|
||||
{
|
||||
auto t_waterparticle = m_World->CreateEntity(e);
|
||||
auto transformChild = m_World->AddComponent<Components::Transform>(t_waterparticle);
|
||||
//auto sprite = m_World->AddComponent<Components::Sprite>(t_waterparticle);
|
||||
|
||||
transformChild->Position = glm::vec3(t_ParticlePositions[i].x - transform->Position.x, t_ParticlePositions[i].y - transform->Position.y, -9.5f);
|
||||
transformChild->Scale = glm::vec3(m_ParticleSystem->GetRadius())/transform->Scale;
|
||||
//sprite->SpriteFile = "Textures/Ball.png";
|
||||
m_World->CommitEntity(t_waterparticle);
|
||||
|
||||
m_EntitiesToParticleHandle.insert(std::make_pair(t_waterparticle, m_ParticleSystem->GetParticleHandleFromIndex(i)));
|
||||
m_ParticleHandleToEntities.insert(std::make_pair( m_ParticleSystem->GetParticleHandleFromIndex(i), t_waterparticle));
|
||||
}
|
||||
|
||||
}
|
||||
LOG_INFO("ParticleCount: %i", m_ParticleSystem->GetParticleCount());
|
||||
}
|
||||
|
||||
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
||||
{
|
||||
if (m_ContactListener != nullptr) {
|
||||
delete m_ContactListener;
|
||||
m_ContactListener = nullptr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user