Merge branch 'Physics'
Conflicts: include/Core/Engine.h include/Physics/CPhysics.h include/Physics/PhysicsSystem.h src/game/Game/BallSystem.cpp src/game/Physics/PhysicsSystem.cpp
This commit is contained in:
+148
-50
@@ -18,26 +18,42 @@ void dd::Systems::BallSystem::Initialize()
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBallLost, &BallSystem::OnMultiBallLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EResetBall, &BallSystem::OnResetBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, &BallSystem::OnMultiBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPause, &BallSystem::OnPause);
|
||||
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(-0.f, 50.f, -10.f);
|
||||
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
|
||||
transform->Velocity = glm::vec3(0.0f, 0.f, 0.f);
|
||||
auto model = m_World->AddComponent<Components::Model>(ent);
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
|
||||
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
|
||||
ball->Speed = 5.f;
|
||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
|
||||
std::shared_ptr<Components::Template> ballTemplate = m_World->AddComponent<Components::Template>(ent);
|
||||
physics->Static = false;
|
||||
physics->Category = CollisionLayer::Type::Ball;
|
||||
physics->Mask = static_cast<CollisionLayer::Type>(CollisionLayer::Pad | CollisionLayer::Brick | CollisionLayer::Wall);
|
||||
physics->Calculate = true;
|
||||
m_World->CommitEntity(ent);
|
||||
//OctoBall
|
||||
{
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(-0.f, 50.26f, -10.f);
|
||||
transform->Scale = glm::vec3(0.2f, 0.2f, 0.2f);
|
||||
transform->Velocity = glm::vec3(0.f, 0.f, 0.f);
|
||||
auto model = m_World->AddComponent<Components::Model>(ent);
|
||||
model->ModelFile = "Models/Test/Ball/Sid.obj";
|
||||
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
|
||||
circleShape->Radius = 0.2f;
|
||||
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
|
||||
ball->Speed = 5.f;
|
||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
|
||||
std::shared_ptr<Components::Template> ballTemplate = m_World->AddComponent<Components::Template>(ent);
|
||||
physics->CollisionType = CollisionType::Type::Dynamic;
|
||||
physics->Category = CollisionLayer::Type::Ball;
|
||||
physics->Mask = static_cast<CollisionLayer::Type>(CollisionLayer::Type::Pad | CollisionLayer::Type::Brick | CollisionLayer::Type::Wall);
|
||||
physics->Calculate = true;
|
||||
|
||||
SetBall(ent);
|
||||
//auto plight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
//plight->Radius = 2.f;
|
||||
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
SetBall(ent);
|
||||
|
||||
auto ent2 = CreateBall();
|
||||
auto transform2 = m_World->GetComponent<Components::Transform>(ent2);
|
||||
transform2->Position = glm::vec3(-0.f, 0.26f, -10.f);
|
||||
transform2->Velocity = glm::vec3(0.0f, -10.f, 0.f);
|
||||
|
||||
m_World->CommitEntity(ent2);
|
||||
}
|
||||
|
||||
for (int i = 0; i < Lives(); i++) {
|
||||
CreateLife(i);
|
||||
@@ -52,15 +68,41 @@ void dd::Systems::BallSystem::Update(double dt)
|
||||
Events::GameOver e;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
ResolveContacts();
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
//TODO: When the ball collides with 2 bricks on the same frame it is reflected and then reflected again, make it reflect only once like a list or something?
|
||||
|
||||
auto ballComponent = m_World->GetComponent<Components::Ball>(entity);
|
||||
if (IsPaused()) {
|
||||
if (ballComponent != nullptr) {
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
|
||||
if (!ballComponent->Paused) {
|
||||
ballComponent->Paused = true;
|
||||
ballComponent->SavedSpeed = transform->Velocity;
|
||||
transform->Velocity = glm::vec3(0, 0, 0);
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
if (ballComponent != nullptr) {
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
|
||||
if (ballComponent->Paused) {
|
||||
transform->Velocity = ballComponent->SavedSpeed;
|
||||
ballComponent->Paused = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto templateCheck = m_World->GetComponent<Components::Template>(entity);
|
||||
if (templateCheck != nullptr){ return; }
|
||||
auto ballComponent = m_World->GetComponent<Components::Ball>(entity);
|
||||
|
||||
if (ballComponent != nullptr) {
|
||||
if (ReplaceBall() == true) {
|
||||
if (ReplaceBall()) {
|
||||
SetReplaceBall(false);
|
||||
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
@@ -78,10 +120,6 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
||||
}
|
||||
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);
|
||||
@@ -93,7 +131,8 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
||||
EventBroker->Publish(e);
|
||||
return;
|
||||
}
|
||||
} else if (transformBall->Position.x > EdgeX()) {
|
||||
} //Removing this made the wall collisions work again
|
||||
/*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);
|
||||
@@ -108,15 +147,13 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
||||
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());
|
||||
}
|
||||
@@ -132,6 +169,20 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
||||
}
|
||||
}
|
||||
|
||||
bool dd::Systems::BallSystem::OnPause(const dd::Events::Pause &event)
|
||||
{
|
||||
if (event.Type != "BallSystem" && event.Type != "All") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsPaused()) {
|
||||
SetPause(false);
|
||||
} else {
|
||||
SetPause(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::OnEntityCommit(EntityID entity)
|
||||
{
|
||||
|
||||
@@ -178,12 +229,36 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
|
||||
//TODO: Add support for power-up collisions
|
||||
}
|
||||
|
||||
auto brick = m_World->GetComponent<Components::Brick>(otherEntitiy);
|
||||
if (brick != nullptr) {
|
||||
auto transform = m_World->GetComponent<Components::Transform>(otherEntitiy);
|
||||
auto physics = m_World->GetComponent<Components::Physics>(otherEntitiy);
|
||||
auto rectangle = m_World->GetComponent<Components::RectangleShape>(otherEntitiy);
|
||||
int hey = 5;
|
||||
}
|
||||
|
||||
/*if (otherEntitiy == m_LastCollision) {
|
||||
|
||||
if (brick != nullptr) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
m_LastCollision = otherEntitiy;
|
||||
}*/
|
||||
|
||||
//if this is a brick thats dead do not collide :)
|
||||
|
||||
auto ballTransform = m_World->GetComponent<Components::Transform>(ballEntity);
|
||||
glm::vec2 ballVelocity = glm::vec2(ballTransform->Velocity.x, ballTransform->Velocity.y);
|
||||
|
||||
if (m_World->GetProperty<std::string>(otherEntitiy, "Name") == "Pad"){
|
||||
Events::HitPad e;
|
||||
EventBroker->Publish(e);
|
||||
// Events::HitLag el;
|
||||
// el.Time = 0.1f;
|
||||
//el.Type = "All";
|
||||
// EventBroker->Publish(el);
|
||||
|
||||
auto padTransform = m_World->GetComponent<Components::Transform>(otherEntitiy);
|
||||
float x = (ballTransform->Position.x - padTransform->Position.x) * XMovementMultiplier();
|
||||
|
||||
@@ -198,39 +273,61 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
|
||||
//std::cout << "Combo: " << ballComponent->Combo << std::endl;
|
||||
}
|
||||
else {
|
||||
glm::vec2 reflectedVelocity = glm::reflect(ballVelocity, event.Normal);
|
||||
ballTransform->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
auto it = m_Contacts.find(ballEntity);
|
||||
if (it == m_Contacts.end()) {
|
||||
std::list<glm::vec2> normalList;
|
||||
normalList.push_back(event.Normal);
|
||||
m_Contacts[ballEntity] = normalList;
|
||||
} else {
|
||||
std::list<glm::vec2> normalList = it->second;
|
||||
normalList.push_back(event.Normal);
|
||||
m_Contacts[ballEntity] = normalList;
|
||||
}
|
||||
|
||||
//glm::vec2 reflectedVelocity = glm::reflect(ballVelocity, event.Normal);
|
||||
//ballTransform->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void dd::Systems::BallSystem::ResolveContacts()
|
||||
{
|
||||
|
||||
for (auto c : m_Contacts) {
|
||||
int count = 0;
|
||||
EntityID entity = c.first;
|
||||
std::list<glm::vec2> contactNormals = c.second;
|
||||
glm::vec2 finalNormal = glm::vec2(0.f);
|
||||
for (auto it = contactNormals.begin(); it != contactNormals.end(); it++) {
|
||||
finalNormal = finalNormal + (*it);
|
||||
count++;
|
||||
}
|
||||
finalNormal = glm::normalize(finalNormal);
|
||||
|
||||
if (count > 1) {
|
||||
LOG_INFO("MultiCollision %i", count);
|
||||
}
|
||||
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
glm::vec2 velocity = glm::vec2(transform->Velocity.x, transform->Velocity.y);
|
||||
glm::vec2 reflectedVelocity = glm::reflect(velocity, finalNormal);
|
||||
transform->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
|
||||
}
|
||||
|
||||
|
||||
m_Contacts.clear();
|
||||
}
|
||||
|
||||
EntityID dd::Systems::BallSystem::CreateBall()
|
||||
{
|
||||
auto ent = m_World->CloneEntity(Ball());
|
||||
|
||||
m_World->RemoveComponent<Components::Template>(ent);
|
||||
|
||||
/* 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;
|
||||
|
||||
auto plight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
plight->Radius = 2.f;*/
|
||||
|
||||
m_World->CommitEntity(ent);
|
||||
//m_World->CommitEntity(ent);
|
||||
|
||||
return ent;
|
||||
}
|
||||
@@ -297,3 +394,4 @@ bool dd::Systems::BallSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// Created by Adniklastrator on 2015-09-30.
|
||||
//
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Game/HitLagSystem.h"
|
||||
|
||||
void dd::Systems::HitLagSystem::Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPause, &HitLagSystem::OnPause);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EHitLag, &HitLagSystem::OnHitLag);
|
||||
}
|
||||
|
||||
void dd::Systems::HitLagSystem::Update(double dt)
|
||||
{
|
||||
if (IsPaused()) {
|
||||
if (HitLag()) {
|
||||
SetHitLagTimer(HitLagTimer() += dt);
|
||||
std::cout << HitLagTimer() << std::endl;
|
||||
if (HitLagDuration() < HitLagTimer()) {
|
||||
SetHitLagTimer(0);
|
||||
SetHitLag(false);
|
||||
FlipPause();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::HitLagSystem::FlipPause()
|
||||
{
|
||||
Events::Pause e;
|
||||
e.Type = CurrentType();
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
bool dd::Systems::HitLagSystem::OnPause(const dd::Events::Pause &event)
|
||||
{
|
||||
if (IsPaused()) {
|
||||
SetPause(false);
|
||||
} else {
|
||||
SetPause(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::HitLagSystem::OnHitLag(const dd::Events::HitLag &event)
|
||||
{
|
||||
if (HitLag()) {
|
||||
FlipPause();
|
||||
}
|
||||
SetHitLag(true);
|
||||
SetCurrentType(event.Type);
|
||||
FlipPause();
|
||||
SetHitLagDuration(event.Time);
|
||||
return true;
|
||||
}
|
||||
+217
-82
@@ -16,6 +16,30 @@ void dd::Systems::LevelSystem::Initialize()
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBallLost, &LevelSystem::OnMultiBallLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPowerUpTaken, &LevelSystem::OnPowerUpTaken);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, &LevelSystem::OnStageCleared);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPause, &LevelSystem::OnPause);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EHitPad, &LevelSystem::OnHitPad);
|
||||
|
||||
m_BrickTemplate = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(m_BrickTemplate);
|
||||
auto model = m_World->AddComponent<Components::Model>(m_BrickTemplate);
|
||||
std::shared_ptr<Components::Brick> cBrick = m_World->AddComponent<Components::Brick>(m_BrickTemplate);
|
||||
std::shared_ptr<Components::RectangleShape> cRec = m_World->AddComponent<Components::RectangleShape>(m_BrickTemplate);
|
||||
cRec->Dimensions = glm::vec2(0.9f, 0.35f);
|
||||
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(m_BrickTemplate);
|
||||
std::shared_ptr<Components::Template> cTemplate = m_World->AddComponent<Components::Template>(m_BrickTemplate);
|
||||
cPhys->CollisionType = CollisionType::Type::Dynamic;
|
||||
cPhys->GravityScale = 0.f;
|
||||
cPhys->Category = CollisionLayer::Type::Brick;
|
||||
cPhys->Mask = CollisionLayer::Type::Ball;
|
||||
|
||||
model->ModelFile = "Models/Brick/TurquoiseBrick.obj";
|
||||
transform->Position = glm::vec3(50, 50, -10);
|
||||
|
||||
//sound
|
||||
auto collisionSound = m_World->AddComponent<Components::CollisionSound>(m_BrickTemplate);
|
||||
collisionSound->FilePath = "Sounds/Brick/shortbrickbreak.wav";
|
||||
|
||||
m_World->CommitEntity(m_BrickTemplate);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -23,54 +47,74 @@ void dd::Systems::LevelSystem::Initialize()
|
||||
void dd::Systems::LevelSystem::Update(double dt)
|
||||
{
|
||||
if (!m_Initialized) {
|
||||
CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
//CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
GetNextLevel();
|
||||
CreateLevel();
|
||||
m_Initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
if (IsPaused()) {
|
||||
return;
|
||||
}
|
||||
auto templateCheck = m_World->GetComponent<Components::Template>(entity);
|
||||
if (templateCheck != nullptr){ return; }
|
||||
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
SetNotResettingTheStage(NotResettingTheStage() + 0.01 * dt);
|
||||
auto brick = m_World->GetComponent<Components::Brick>(entity);
|
||||
if (brick != nullptr) {
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
//Removes bricks that falls out of the stage.
|
||||
if (transform->Position.y < -10) {
|
||||
m_LooseBricks--;
|
||||
std::cout << m_LooseBricks << std::endl;
|
||||
m_World->RemoveEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
if (NumberOfBricks() <= 0 && Restarting() == false) {
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
|
||||
if (NumberOfBricks() <= 0 && m_LooseBricks <= 0 && !Restarting()) {
|
||||
if (MultiBalls() <= 0 && PowerUps() <= 0) {
|
||||
if (NotResettingTheStage() > 5) {
|
||||
SetRestarting(true);
|
||||
Events::ResetBall e;
|
||||
EventBroker->Publish(e);
|
||||
Events::ScoreEvent es;
|
||||
es.Score = 500;
|
||||
EventBroker->Publish(es);
|
||||
Events::StageCleared ec;
|
||||
EventBroker->Publish(ec);
|
||||
SetNotResettingTheStage(0);
|
||||
//CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
}
|
||||
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);
|
||||
} 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);
|
||||
} else {
|
||||
if (brick != nullptr) {
|
||||
m_LooseBricks--;
|
||||
std::cout << m_LooseBricks << std::endl;
|
||||
m_World->RemoveEntity(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnPause(const dd::Events::Pause &event)
|
||||
{
|
||||
if (event.Type != "LevelSystem" && event.Type != "All") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsPaused()) {
|
||||
SetPause(false);
|
||||
} else {
|
||||
SetPause(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, float spaceToEdge)
|
||||
{
|
||||
SetNumberOfBricks(rows * lines);
|
||||
@@ -79,7 +123,7 @@ void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 s
|
||||
for (int i = 0; i < rows; i++) {
|
||||
num--;
|
||||
for (int j = 0; j < Lines(); j++) {
|
||||
CreateBrick(i, j, spacesBetweenBricks, spaceToEdge, num);
|
||||
CreateBrick(i, j, spacesBetweenBricks, spaceToEdge, num, 1);
|
||||
}
|
||||
if (num == 1)
|
||||
num = Lines();
|
||||
@@ -90,60 +134,55 @@ void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 s
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBetweenBricks, float spaceToEdge, int num)
|
||||
void dd::Systems::LevelSystem::CreateLevel()
|
||||
{
|
||||
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::cout << "Loading level: " << m_CurrentLevel << std::endl;
|
||||
SetNumberOfBricks(Rows() * Lines());
|
||||
int num = Lines();
|
||||
int getter = 0;
|
||||
|
||||
/*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 == 5 && row == 2)) {
|
||||
for (int i = 0; i < Rows(); i++) {
|
||||
num--;
|
||||
for (int j = 0; j < Lines(); j++) {
|
||||
CreateBrick(i, j, SpaceBetweenBricks(), SpaceToEdge(), num, m_Bricks[getter]);
|
||||
getter++;
|
||||
}
|
||||
if (num == 1)
|
||||
num = Lines();
|
||||
}
|
||||
m_LooseBricks = NumberOfBricks();
|
||||
SetRestarting(false);
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBetweenBricks, float spaceToEdge, int num, int typeInt)
|
||||
{
|
||||
if (typeInt == 0) {
|
||||
SetNumberOfBricks(NumberOfBricks() - 1);
|
||||
return;
|
||||
}
|
||||
auto brick = m_World->CloneEntity(m_BrickTemplate);
|
||||
m_World->RemoveComponent<Components::Template>(brick);
|
||||
auto transform = m_World->GetComponent<Components::Transform>(brick);
|
||||
auto cBrick = m_World->GetComponent<Components::Brick>(brick);
|
||||
auto cPhys = m_World->GetComponent<Components::Physics>(brick);
|
||||
|
||||
if (typeInt == 2) {
|
||||
std::shared_ptr<Components::PowerUpBrick> cPow = m_World->AddComponent<Components::PowerUpBrick>(brick);
|
||||
auto model = m_World->GetComponent<Components::Model>(brick);
|
||||
model->ModelFile = "Models/Brick/IceBrick.obj";
|
||||
}
|
||||
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)
|
||||
@@ -186,7 +225,9 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
if (ball != nullptr && Restarting() == false) {
|
||||
if (!Restarting() && !brick->Removed) {
|
||||
brick->Removed = true;
|
||||
// Hit brick with ball code.
|
||||
ball->Combo += 1;
|
||||
Events::ComboEvent ec;
|
||||
ec.Combo = ball->Combo;
|
||||
@@ -195,12 +236,6 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
|
||||
|
||||
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 = static_cast<CollisionLayer::Type>(CollisionLayer::Water | CollisionLayer::Wall);
|
||||
@@ -221,17 +256,14 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
|
||||
|
||||
|
||||
SetNumberOfBricks(NumberOfBricks() - 1);
|
||||
if (NumberOfBricks() <= 0) {
|
||||
//SetMultiBalls(MultiBalls() + 1);
|
||||
}
|
||||
Events::ScoreEvent es;
|
||||
es.Score = brick->Score * ball->Combo;
|
||||
EventBroker->Publish(es);
|
||||
|
||||
std::cout << "Combo: " << ball->Combo << std::endl;
|
||||
//std::cout << "Combo: " << ball->Combo << std::endl;
|
||||
//std::cout << NumberOfBricks() << std::endl;
|
||||
//std::cout << "Brick Score: " << brick->Score << std::endl;
|
||||
std::cout << "Score: " << Score() << std::endl;
|
||||
//std::cout << "Score: " << Score() << std::endl;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -247,14 +279,12 @@ bool dd::Systems::LevelSystem::OnScoreEvent(const dd::Events::ScoreEvent &event)
|
||||
bool dd::Systems::LevelSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
SetMultiBalls(MultiBalls()+2);
|
||||
std::cout << MultiBalls() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnMultiBallLost(const dd::Events::MultiBallLost &event)
|
||||
{
|
||||
SetMultiBalls(MultiBalls()-1);
|
||||
std::cout << MultiBalls() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -266,13 +296,12 @@ bool dd::Systems::LevelSystem::OnCreatePowerUp(const dd::Events::CreatePowerUp &
|
||||
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->CollisionType = CollisionType::Type::Dynamic;
|
||||
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);
|
||||
@@ -292,13 +321,119 @@ bool dd::Systems::LevelSystem::OnPowerUpTaken(const dd::Events::PowerUpTaken &ev
|
||||
|
||||
bool dd::Systems::LevelSystem::OnStageCleared(const dd::Events::StageCleared &event)
|
||||
{
|
||||
if (!m_Cleared) {
|
||||
m_Cleared = true;
|
||||
SetRestarting(true);
|
||||
Events::ResetBall e;
|
||||
EventBroker->Publish(e);
|
||||
Events::ScoreEvent es;
|
||||
es.Score = 500;
|
||||
EventBroker->Publish(es);
|
||||
m_CurrentLevel++;
|
||||
if (m_CurrentLevel < 6) {
|
||||
GetNextLevel();
|
||||
CreateLevel();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::EndLevel()
|
||||
void dd::Systems::LevelSystem::GetNextLevel()
|
||||
{
|
||||
Events::StageCleared e;
|
||||
EventBroker->Publish(e);
|
||||
return;
|
||||
std::array<int, 42> level;
|
||||
if (m_CurrentCluster == 1) {
|
||||
if (m_CurrentLevel == 1) {
|
||||
level =
|
||||
{0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 1, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0};
|
||||
} else if (m_CurrentLevel == 2) {
|
||||
level =
|
||||
{1, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0};
|
||||
} else if (m_CurrentLevel == 3) {
|
||||
level =
|
||||
{0, 0, 0, 0, 0, 0, 1,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0};
|
||||
} else if (m_CurrentLevel == 4) {
|
||||
level =
|
||||
{0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0, 0, 0};
|
||||
} else if (m_CurrentLevel == 5) {
|
||||
level =
|
||||
{0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 1};
|
||||
}
|
||||
} else if (m_CurrentCluster == 2) {
|
||||
if (m_CurrentLevel == 1) {
|
||||
level =
|
||||
{1, 1, 0, 1, 0, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1,
|
||||
1, 2, 1, 2, 1, 2, 1,
|
||||
1, 1, 0, 1, 0, 1, 1,
|
||||
1, 0, 0, 1, 0, 0, 1};
|
||||
} else if (m_CurrentLevel == 2) {
|
||||
level =
|
||||
{1, 0, 0, 0, 0, 0, 1,
|
||||
0, 1, 0, 0, 0, 1, 0,
|
||||
0, 0, 2, 2, 2, 0, 0,
|
||||
0, 0, 0, 1, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0};
|
||||
} else if (m_CurrentLevel == 3) {
|
||||
level =
|
||||
{2, 0, 0, 0, 0, 0, 2,
|
||||
0, 1, 0, 0, 0, 1, 0,
|
||||
1, 2, 1, 1, 1, 2, 1,
|
||||
1, 0, 1, 1, 1, 0, 1,
|
||||
1, 0, 1, 1, 1, 0, 1,
|
||||
1, 0, 1, 1, 1, 0, 1};
|
||||
} else if (m_CurrentLevel == 4) {
|
||||
level =
|
||||
{1, 2, 1, 1, 1, 2, 1,
|
||||
1, 0, 1, 1, 1, 0, 1,
|
||||
1, 2, 1, 1, 1, 2, 1,
|
||||
1, 0, 1, 1, 1, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 1,
|
||||
0, 1, 1, 0, 1, 1, 0};
|
||||
} else if (m_CurrentLevel == 5) {
|
||||
level =
|
||||
{1, 0, 1, 0, 1, 0, 1,
|
||||
0, 1, 0, 1, 0, 1, 0,
|
||||
2, 0, 1, 0, 1, 0, 2,
|
||||
0, 1, 0, 2, 0, 1, 0,
|
||||
1, 0, 1, 0, 1, 0, 1,
|
||||
0, 1, 0, 1, 0, 1, 0};
|
||||
}
|
||||
} else if (m_CurrentCluster == 3) {
|
||||
|
||||
}
|
||||
|
||||
std::copy(std::begin(level), std::end(level), std::begin(m_Bricks));
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnHitPad(const dd::Events::HitPad &event)
|
||||
{
|
||||
m_Cleared = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -27,16 +27,17 @@ void dd::Systems::PadSystem::Initialize()
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, &PadSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContactPowerUp, &PadSystem::OnContactPowerUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, &PadSystem::OnStageCleared);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPause, &PadSystem::OnPause);
|
||||
|
||||
{
|
||||
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, -3.5f, -10.f);
|
||||
ctransform->Scale = glm::vec3(1.0f, 1.0f, 1.f);
|
||||
auto rectangle = m_World->AddComponent<Components::RectangleShape>(ent);
|
||||
auto rectangleShape = m_World->AddComponent<Components::RectangleShape>(ent);
|
||||
rectangleShape->Dimensions = glm::vec2(1.f, 0.5f);
|
||||
auto physics = m_World->AddComponent<Components::Physics>(ent);
|
||||
physics->Static = false;
|
||||
physics->CollisionType = CollisionType::Type::Dynamic;
|
||||
physics->Category = CollisionLayer::Type::Pad;
|
||||
physics->Mask = static_cast<CollisionLayer::Type>(CollisionLayer::Ball | CollisionLayer::PowerUp);
|
||||
physics->Calculate = true;
|
||||
@@ -58,6 +59,9 @@ void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID p
|
||||
|
||||
void dd::Systems::PadSystem::Update(double dt)
|
||||
{
|
||||
if (IsPaused()) {
|
||||
return;
|
||||
}
|
||||
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") {
|
||||
@@ -105,6 +109,20 @@ void dd::Systems::PadSystem::Update(double dt)
|
||||
return;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnPause(const dd::Events::Pause &event)
|
||||
{
|
||||
if (event.Type != "PadSystem" && event.Type != "All") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsPaused()) {
|
||||
SetPause(false);
|
||||
} else {
|
||||
SetPause(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
{
|
||||
int val = event.KeyCode;
|
||||
@@ -127,6 +145,15 @@ bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
Events::MultiBall e;
|
||||
e.padTransform = Transform();
|
||||
EventBroker->Publish(e);
|
||||
} else if (val == GLFW_KEY_P) {
|
||||
Events::Pause e;
|
||||
e.Type = "All";
|
||||
EventBroker->Publish(e);
|
||||
} else if (val == GLFW_KEY_H) {
|
||||
Events::HitLag e;
|
||||
e.Time = 0.2;
|
||||
e.Type = "All";
|
||||
EventBroker->Publish(e);
|
||||
} else if (val == GLFW_KEY_D) {
|
||||
return false;
|
||||
}
|
||||
@@ -226,9 +253,6 @@ bool dd::Systems::PadSystem::OnContactPowerUp(const dd::Events::Contact &event)
|
||||
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";
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Physics/PhysicsSystem.h"
|
||||
|
||||
void dd::Systems::PhysicsSystem::ContactListener::BeginContact(b2Contact* contact)
|
||||
{
|
||||
b2WorldManifold worldManifold;
|
||||
|
||||
contact->GetWorldManifold(&worldManifold);
|
||||
|
||||
Events::Contact e;
|
||||
e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
|
||||
e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
|
||||
e.Normal = glm::normalize(glm::vec2(contact->GetManifold()->localNormal.x, contact->GetManifold()->localNormal.y));
|
||||
e.SignificantNormal = glm::normalize((glm::abs(e.Normal.x) > glm::abs(e.Normal.y)) ? glm::vec2(e.Normal.x, 0) : glm::vec2(0, e.Normal.y));
|
||||
|
||||
m_PhysicsSystem->EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::ContactListener::EndContact(b2Contact* contact)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::ContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
|
||||
{
|
||||
EntityID entityA = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
|
||||
EntityID entityB = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
|
||||
|
||||
auto physicsComponentA = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityA);
|
||||
auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityB);
|
||||
|
||||
if (physicsComponentA != nullptr && physicsComponentB != nullptr) {
|
||||
if (physicsComponentA->Calculate || physicsComponentB->Calculate) {
|
||||
// Turn of collisions
|
||||
contact->SetEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::ContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
+188
-136
@@ -2,29 +2,20 @@
|
||||
#include "Physics/PhysicsSystem.h"
|
||||
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
|
||||
{
|
||||
cf->Register<Components::CircleShape>();
|
||||
//TODO: REGISTER PHYSICS COMPONENTS HERE
|
||||
}
|
||||
|
||||
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.0/60.0;
|
||||
m_VelocityIterations = 6;
|
||||
m_PositionIterations = 2;
|
||||
m_Accumulator = 0.0;
|
||||
|
||||
m_PhysicsWorld->SetContactListener(m_ContactListener);
|
||||
|
||||
InitializeWater();
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, &PhysicsSystem::SetImpulse);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, PhysicsSystem::SetImpulse);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPause, &PhysicsSystem::OnPause);
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::InitializeWater()
|
||||
@@ -36,7 +27,11 @@ void dd::Systems::PhysicsSystem::InitializeWater()
|
||||
|
||||
bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||
{
|
||||
b2Body* body = m_EntitiesToBodies[event.Entity];
|
||||
auto it = m_EntitiesToBodies.find(event.Entity);
|
||||
if(it == m_EntitiesToBodies.end()) {
|
||||
LOG_ERROR("Entity: %i, Tried to set an impulse on a body that does not exsist", event.Entity);
|
||||
return false;
|
||||
}
|
||||
|
||||
b2Vec2 impulse;
|
||||
impulse.x = event.Impulse.x;
|
||||
@@ -46,118 +41,143 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||
point.x = event.Point.x;
|
||||
point.y = event.Point.y;
|
||||
|
||||
|
||||
Impulse i;
|
||||
i.Body = body;
|
||||
i.Vector = impulse;
|
||||
i.Body = it->second;
|
||||
i.Impulse = impulse;
|
||||
i.Point = point;
|
||||
|
||||
m_Impulses.push_back(i);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::Update(double dt) {
|
||||
void dd::Systems::PhysicsSystem::SyncEntitiesWithBodies()
|
||||
{
|
||||
|
||||
m_Accumulator += dt;
|
||||
while (m_Accumulator >= m_TimeStep) {
|
||||
for (auto i : m_EntitiesToBodies) {
|
||||
EntityID entity = i.first;
|
||||
b2Body* body = i.second;
|
||||
|
||||
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);
|
||||
|
||||
b2Filter filter;
|
||||
filter.categoryBits = physicsComponent->Category;
|
||||
filter.maskBits = physicsComponent->Mask;
|
||||
body->GetFixtureList()->SetFilterData(filter);
|
||||
}
|
||||
if (body == nullptr) {
|
||||
LOG_ERROR("This body should not exist, please fix this");
|
||||
continue;
|
||||
}
|
||||
|
||||
UpdateParticleEmitters(dt);
|
||||
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 (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);
|
||||
|
||||
b2Filter filter;
|
||||
filter.categoryBits = physicsComponent->Category;
|
||||
filter.maskBits = physicsComponent->Mask;
|
||||
body->GetFixtureList()->SetFilterData(filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::SyncBodiesWithEntities()
|
||||
{
|
||||
for (auto i : m_BodiesToEntities) {
|
||||
b2Body* body = i.first;
|
||||
EntityID entity = i.second;
|
||||
|
||||
if (body == nullptr) {
|
||||
LOG_ERROR("This body should not exist, please fix this");
|
||||
continue;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
auto parent = m_World->GetEntityParent(entity);
|
||||
if (parent == 0) {
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
|
||||
if (physicsComponent->Calculate) { //TODO: REPLACE THIS WITH PARTICLE COLLISION FILTERS
|
||||
transformComponent->Position.x = transformComponent->Position.x + (transformComponent->Velocity.x * m_TimeStep);
|
||||
transformComponent->Position.y = transformComponent->Position.y + (transformComponent->Velocity.y * m_TimeStep);
|
||||
} else {
|
||||
b2Vec2 position = body->GetPosition();
|
||||
transformComponent->Position.x = position.x;
|
||||
transformComponent->Position.y = position.y;
|
||||
|
||||
float angle = body->GetAngle();
|
||||
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, angle));
|
||||
|
||||
b2Vec2 velocity = body->GetLinearVelocity();
|
||||
transformComponent->Velocity.x = velocity.x;
|
||||
transformComponent->Velocity.y = velocity.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
{
|
||||
if (m_Pause) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_Accumulator += dt;
|
||||
|
||||
while(m_Accumulator >= m_TimeStep)
|
||||
{
|
||||
|
||||
UpdateParticleEmitters(dt);
|
||||
SyncEntitiesWithBodies();
|
||||
|
||||
//Apply Impulses Must be done after SyncEntitiesWithBodies
|
||||
for (auto i : m_Impulses) {
|
||||
i.Body->ApplyLinearImpulse(i.Vector, i.Point, true);
|
||||
i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
|
||||
}
|
||||
m_Impulses.clear();
|
||||
|
||||
|
||||
//Update the PhysicsWorld
|
||||
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
|
||||
|
||||
SyncBodiesWithEntities();
|
||||
|
||||
for (auto i : m_EntitiesToBodies) {
|
||||
EntityID entity = i.first;
|
||||
b2Body *body = i.second;
|
||||
|
||||
if (body == nullptr) {
|
||||
LOG_ERROR("This body should not exist");
|
||||
continue;
|
||||
}
|
||||
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (!transformComponent)
|
||||
continue;
|
||||
auto parent = m_World->GetEntityParent(entity);
|
||||
if (parent == 0) {
|
||||
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
if (physicsComponent->Calculate) { //TODO: REPLACE THIS WITH PARTICLE COLLISION FILTERS
|
||||
transformComponent->Position.x =
|
||||
transformComponent->Position.x + (transformComponent->Velocity.x * m_TimeStep);
|
||||
transformComponent->Position.y =
|
||||
transformComponent->Position.y + (transformComponent->Velocity.y * m_TimeStep);
|
||||
}
|
||||
else {
|
||||
b2Vec2 position = body->GetPosition();
|
||||
transformComponent->Position.x = position.x;
|
||||
transformComponent->Position.y = position.y;
|
||||
|
||||
float angle = body->GetAngle();
|
||||
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, angle));
|
||||
|
||||
b2Vec2 velocity = body->GetLinearVelocity();
|
||||
transformComponent->Velocity.x = velocity.x;
|
||||
transformComponent->Velocity.y = velocity.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for ( int e = 0; e < m_EntitiesToParticleHandle.size(); e++) {
|
||||
b2Vec2 *positionBuffer = m_ParticleSystem[e]->GetPositionBuffer();
|
||||
for (auto i : m_EntitiesToParticleHandle[e]){
|
||||
for (auto i : m_EntitiesToParticleHandle[e]) {
|
||||
EntityID entity = i.first;
|
||||
const b2ParticleHandle *particleH = i.second;
|
||||
b2ParticleHandle *particleH = i.second;
|
||||
|
||||
b2Vec2 positionB2 = positionBuffer[particleH->GetIndex()];
|
||||
glm::vec2 position = glm::vec2(positionB2.x, positionB2.y);
|
||||
|
||||
EntityID entityParent = m_World->GetEntityParent(entity);
|
||||
glm::vec3 parentTransform = glm::vec3(0.f);
|
||||
if(entityParent) {
|
||||
if (entityParent) {
|
||||
auto tp = m_World->GetComponent<Components::Transform>(entityParent);
|
||||
parentTransform = tp->Position;
|
||||
}
|
||||
@@ -172,12 +192,25 @@ void dd::Systems::PhysicsSystem::Update(double dt) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool dd::Systems::PhysicsSystem::OnPause(const dd::Events::Pause &event)
|
||||
{
|
||||
if (event.Type != "PhysicsSystem" && event.Type != "All") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_Pause) {
|
||||
m_Pause = false;
|
||||
} else {
|
||||
m_Pause = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
||||
{
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
@@ -194,7 +227,12 @@ void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
||||
}
|
||||
|
||||
if (physicsComponent) {
|
||||
CreateBody(entity);
|
||||
auto it = m_EntitiesToBodies.find(entity);
|
||||
if(it == m_EntitiesToBodies.end()) {
|
||||
CreateBody(entity);
|
||||
} else {
|
||||
LOG_ERROR("Tried to commit a body that already exists");
|
||||
}
|
||||
} else if (waterComponent) {
|
||||
CreateParticleGroup(entity);
|
||||
} else if (particleEmitterComponent) {
|
||||
@@ -205,22 +243,25 @@ void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
||||
|
||||
void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity)
|
||||
{
|
||||
auto physics = m_World->GetComponent<Components::Physics>(entity);
|
||||
/* auto physics = m_World->GetComponent<Components::Physics>(entity);
|
||||
if (physics == nullptr) {
|
||||
return;
|
||||
}*/
|
||||
|
||||
auto it = m_EntitiesToBodies.find(entity);
|
||||
if(it == m_EntitiesToBodies.end()) {
|
||||
LOG_ERROR("Trying to remove non-exsisting body, Entity: %i", entity);
|
||||
return;
|
||||
}
|
||||
|
||||
b2Body* body = m_EntitiesToBodies[entity];
|
||||
|
||||
if (body != nullptr) {
|
||||
m_EntitiesToBodies.erase(entity);
|
||||
m_BodiesToEntities.erase(body);
|
||||
|
||||
m_PhysicsWorld->DestroyBody(body);
|
||||
}
|
||||
|
||||
//LOG_INFO("Removing entity %i", entity);
|
||||
it->second->GetWorld()->DestroyBody(it->second);
|
||||
m_BodiesToEntities.erase(it->second);
|
||||
m_EntitiesToBodies.erase(entity);
|
||||
//delete body;
|
||||
}
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
{
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
@@ -229,7 +270,6 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if(!transformComponent) {
|
||||
LOG_ERROR("No TransformComponent in CreateBody");
|
||||
@@ -242,34 +282,28 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
bodyDef.position.Set(absoluteTransform.Position.x, absoluteTransform.Position.y);
|
||||
bodyDef.angle = -glm::eulerAngles(absoluteTransform.Orientation).z;
|
||||
|
||||
if (physicsComponent->Static) {
|
||||
if (physicsComponent->CollisionType == CollisionType::Type::Static) {
|
||||
bodyDef.type = b2_staticBody;
|
||||
} else {
|
||||
} else if (physicsComponent->CollisionType == CollisionType::Type::Dynamic) {
|
||||
bodyDef.type = b2_dynamicBody;
|
||||
} else if (physicsComponent->CollisionType == CollisionType::Type::Kinematic) {
|
||||
bodyDef.type = b2_dynamicBody;
|
||||
|
||||
}
|
||||
|
||||
b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef);
|
||||
|
||||
b2Shape* pShape;
|
||||
|
||||
auto boxComponent = m_World->GetComponent<Components::RectangleShape>(entity);
|
||||
if (boxComponent) {
|
||||
auto rectangleComponent = m_World->GetComponent<Components::RectangleShape>(entity);
|
||||
if (rectangleComponent) {
|
||||
b2PolygonShape* bShape = new b2PolygonShape();
|
||||
bShape->SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2);
|
||||
bShape->SetAsBox(rectangleComponent->Dimensions.x/2.f, rectangleComponent->Dimensions.y/2.f);
|
||||
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;
|
||||
|
||||
pShape->m_radius = circleComponent->Radius;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,26 +313,26 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
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 = 10.f;
|
||||
fixtureDef.restitution = 1.0f;
|
||||
fixtureDef.friction = 0.0f;
|
||||
body->CreateFixture(&fixtureDef);
|
||||
|
||||
}
|
||||
fixtureDef.shape = pShape;
|
||||
fixtureDef.density = 10.f;
|
||||
fixtureDef.restitution = 1.0f;
|
||||
fixtureDef.friction = 0.0f;
|
||||
body->CreateFixture(&fixtureDef);
|
||||
|
||||
|
||||
delete pShape;
|
||||
|
||||
/* if(physicsComponent->Static) {
|
||||
body->SetType(b2BodyType::b2_staticBody);
|
||||
} else if (! physicsComponent->Static) {
|
||||
body->SetType(b2BodyType::b2_dynamicBody);
|
||||
}*/
|
||||
|
||||
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)
|
||||
{
|
||||
auto transform = m_World->GetComponent<Components::Transform>(e);
|
||||
@@ -337,7 +371,7 @@ void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e)
|
||||
void dd::Systems::PhysicsSystem::UpdateParticleEmitters(double dt)
|
||||
{
|
||||
for (int i = 0; i < m_ParticleEmitters.ParticleSystem.size(); i++) {
|
||||
auto e = m_ParticleEmitters.Emitter[i];
|
||||
auto e = m_ParticleEmitters.ParticleEmitter[i];
|
||||
auto pt = m_ParticleEmitters.ParticleTemplate[i];
|
||||
auto ps = m_ParticleEmitters.ParticleSystem[i];
|
||||
|
||||
@@ -407,7 +441,7 @@ void dd::Systems::PhysicsSystem::CreateParticleEmitter(EntityID entity)
|
||||
}
|
||||
//TODO: Skicka med fler flaggor till particlesystemet;
|
||||
m_ParticleEmitters.ParticleSystem.push_back(CreateParticleSystem(particle->Radius, 0.f));
|
||||
m_ParticleEmitters.Emitter.push_back(entity);
|
||||
m_ParticleEmitters.ParticleEmitter.push_back(entity);
|
||||
m_ParticleEmitters.ParticleTemplate.push_back(childEntity);
|
||||
}
|
||||
|
||||
@@ -428,10 +462,28 @@ b2ParticleSystem* dd::Systems::PhysicsSystem::CreateParticleSystem(float radius,
|
||||
return m_ParticleSystem.back();
|
||||
}
|
||||
|
||||
|
||||
|
||||
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
||||
{
|
||||
for (auto i = m_BodiesToEntities.begin(); i != m_BodiesToEntities.end(); i++) {
|
||||
b2Body* body = i->first;
|
||||
body->GetWorld()->DestroyBody(body);
|
||||
//delete body;
|
||||
}
|
||||
m_BodiesToEntities.clear();
|
||||
m_EntitiesToBodies.clear();
|
||||
m_Impulses.clear();
|
||||
|
||||
//TODO:REMOVE PARTICLE SYSTEMS
|
||||
|
||||
if (m_ContactListener != nullptr) {
|
||||
delete m_ContactListener;
|
||||
m_ContactListener = nullptr;
|
||||
}
|
||||
|
||||
if (m_PhysicsWorld != nullptr) {
|
||||
delete m_PhysicsWorld;
|
||||
m_PhysicsWorld = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,183 +0,0 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Sound/SoundSystem.h"
|
||||
|
||||
dd::Systems::SoundSystem::~SoundSystem()
|
||||
{
|
||||
alcCloseDevice(m_Device);
|
||||
};
|
||||
|
||||
void dd::Systems::SoundSystem::Initialize()
|
||||
{
|
||||
//initialize OpenAL
|
||||
m_Device = alcOpenDevice(NULL);
|
||||
ALCcontext* context;
|
||||
|
||||
if (m_Device) {
|
||||
context = alcCreateContext(m_Device, NULL);
|
||||
alcMakeContextCurrent(context);
|
||||
}
|
||||
else {
|
||||
LOG_ERROR("OpenAL failed to initialize.");
|
||||
}
|
||||
alGetError();
|
||||
|
||||
//Subscribe to events
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySound);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMasterVolume, &SoundSystem::OnMasterVolume);
|
||||
|
||||
<<<<<<< HEAD
|
||||
//Todo: Move this
|
||||
{
|
||||
dd::Events::PlaySound e;
|
||||
e.FilePath = "Sounds/BGM/under-the-sea-instrumental.wav";
|
||||
e.IsAmbient = true;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
{
|
||||
dd::Events::PlaySound e;
|
||||
e.FilePath = "Sounds/BGM/water-flowing.wav";
|
||||
e.Gain = 0.3f;
|
||||
e.IsAmbient = true;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
=======
|
||||
|
||||
|
||||
>>>>>>> origin/master
|
||||
}
|
||||
|
||||
void dd::Systems::SoundSystem::Update(double dt)
|
||||
{
|
||||
//Clean up none-active sources
|
||||
//Only used for SFX's. BGM's are handled on stop sound.
|
||||
std::vector<ALuint> deleteList;
|
||||
for (auto& item : m_SFXSourcesToBuffers) {
|
||||
ALint sourceState;
|
||||
alGetSourcei(item.first, AL_SOURCE_STATE, &sourceState);
|
||||
if (sourceState == AL_STOPPED) {
|
||||
deleteList.push_back(item.first);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < deleteList.size(); i++) {
|
||||
alDeleteSources(1, &deleteList[i]);
|
||||
//alDeleteBuffers(1, &m_SourcesToBuffers[deleteList[i]]);
|
||||
m_SFXSourcesToBuffers.erase(deleteList[i]);
|
||||
}
|
||||
}
|
||||
|
||||
ALuint dd::Systems::SoundSystem::CreateSource()
|
||||
{
|
||||
ALuint source;
|
||||
alGenSources((ALuint)1, &source);
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event)
|
||||
{
|
||||
//Loading and binding sound buffer to source
|
||||
Sound *sound = ResourceManager::Load<Sound>(event.FilePath);
|
||||
if (sound == nullptr) {
|
||||
return false;
|
||||
}
|
||||
ALuint source = CreateSource();
|
||||
ALuint buffer = sound->Buffer();
|
||||
alSourcei(source, AL_BUFFER, buffer);
|
||||
|
||||
//Sound settings
|
||||
float relativeVolume = 1.f;
|
||||
if (event.IsAmbient) {
|
||||
alSourcei(source, AL_LOOPING, AL_TRUE);
|
||||
relativeVolume = m_BGMMasterVolume;
|
||||
m_BGMSourcesToBuffers[source] = sound;
|
||||
|
||||
}
|
||||
else if (!event.IsAmbient)
|
||||
{
|
||||
m_SFXSourcesToBuffers[source] = sound;
|
||||
alSourcei(source, AL_LOOPING, AL_FALSE);
|
||||
relativeVolume = m_SFXMasterVolume;
|
||||
}
|
||||
|
||||
alSourcef(source, AL_GAIN, (event.Gain * relativeVolume));
|
||||
alSourcef(source, AL_PITCH, event.Pitch);
|
||||
|
||||
|
||||
//Play
|
||||
alSourcePlay(source);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::SoundSystem::OnStopSound(const dd::Events::StopSound &event)
|
||||
{
|
||||
ALuint itemToDeleted;
|
||||
for (auto& item : m_BGMSourcesToBuffers)
|
||||
{
|
||||
if (item.second->Path() == event.FilePath) {
|
||||
itemToDeleted = item.first;
|
||||
break;
|
||||
}
|
||||
}
|
||||
alSourceStop(itemToDeleted);
|
||||
alDeleteSources(1, &itemToDeleted);
|
||||
m_BGMSourcesToBuffers.erase(itemToDeleted);
|
||||
|
||||
//Should not happen because SFX's should be very short.
|
||||
for (auto item : m_SFXSourcesToBuffers)
|
||||
{
|
||||
if (item.second->Path() == event.FilePath) {
|
||||
alSourceStop(item.first);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool dd::Systems::SoundSystem::OnMasterVolume(const dd::Events::MasterVolume &event)
|
||||
{
|
||||
//TODO: Make the Gain depend on the value given when stored.
|
||||
//TODO: .. now it ONLY uses the master Gain
|
||||
if (event.IsAmbient) {
|
||||
m_BGMMasterVolume = event.Gain;
|
||||
for (auto& item : m_BGMSourcesToBuffers)
|
||||
{
|
||||
alSourcef(item.first, AL_GAIN, event.Gain);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if (!event.IsAmbient) {
|
||||
m_SFXMasterVolume = event.Gain;
|
||||
for (auto& item : m_SFXSourcesToBuffers)
|
||||
{
|
||||
alSourcef(item.first, AL_GAIN, event.Gain);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event)
|
||||
{
|
||||
//Check which entity has the collisionSound component.
|
||||
auto collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity1);
|
||||
if (collisionSound == nullptr) {
|
||||
collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity2);
|
||||
if (collisionSound == nullptr) {
|
||||
//None of the entities had a collisionSound component.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Send play-sound event
|
||||
dd::Events::PlaySound e;
|
||||
e.FilePath = collisionSound->FilePath;
|
||||
e.IsAmbient = false;
|
||||
EventBroker->Publish(e);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "Core/World.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Physics/PhysicsSystem.h"
|
||||
#include "Transform/TransformSystem.h"
|
||||
|
||||
#include "Physics/CPhysics.h"
|
||||
#include "Physics/CRectangleShape.h"
|
||||
|
||||
|
||||
|
||||
|
||||
using namespace dd;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(RemovePhysicsSystemTest)
|
||||
{
|
||||
// Setup world
|
||||
std::shared_ptr<EventBroker> eventBroker = std::make_shared<EventBroker>();
|
||||
World world(eventBroker);
|
||||
|
||||
world.SystemFactory.Register<Systems::TransformSystem>(
|
||||
[this]() { return new Systems::TransformSystem(&world, eventBroker); });
|
||||
world.AddSystem<Systems::TransformSystem>();
|
||||
|
||||
world.SystemFactory.Register<Systems::PhysicsSystem>(
|
||||
[this]() { return new Systems::PhysicsSystem(&world, eventBroker); });
|
||||
world.AddSystem<Systems::PhysicsSystem>();
|
||||
|
||||
world.ComponentFactory.Register<Components::RectangleShape>();
|
||||
world.ComponentFactory.Register<Components::Physics>();
|
||||
|
||||
//Create entity
|
||||
EntityID ent = world.CreateEntity();
|
||||
|
||||
auto transform = world.AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(20.f, 0.f, -10.f);
|
||||
transform->Velocity = glm::vec3(1.f, 3.f, 0.f);
|
||||
transform->Scale = glm::vec3(2.f, 5.f, 1.f);
|
||||
|
||||
auto physics = world.AddComponent<Components::Physics>(ent);
|
||||
physics->Static = false;
|
||||
physics->Calculate = true;
|
||||
physics->Category = CollisionLayer::Type::Ball;
|
||||
physics->Mask = CollisionLayer::Type::Brick | CollisionLayer::Type::Pad | CollisionLayer::Wall;
|
||||
physics->GravityScale = 0.f;
|
||||
|
||||
auto rectangle = world.AddComponent<Components::RectangleShape>(ent);
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user