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:
viktorljung
2015-10-06 20:16:57 +02:00
32 changed files with 8624 additions and 693 deletions
+25 -75
View File
@@ -45,6 +45,7 @@
#include "Game/CPad.h"
#include "Game/CBrick.h"
#include "Game/BallSystem.h"
#include "Game/HitLagSystem.h"
#include "Game/Bricks/CPowerUpBrick.h"
#include "Physics/PhysicsSystem.h"
@@ -122,6 +123,9 @@ public:
m_World->SystemFactory.Register<Systems::BallSystem>(
[this]() { return new Systems::BallSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::BallSystem>();
m_World->SystemFactory.Register<Systems::HitLagSystem>(
[this]() { return new Systems::HitLagSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::HitLagSystem>();
m_World->SystemFactory.Register<Systems::PhysicsSystem>(
[this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PhysicsSystem>();
@@ -134,31 +138,6 @@ public:
m_World->ComponentFactory.Register<Components::ParticleEmitter>();
m_World->Initialize();
//OctoBall
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(-0.f, 0.26f, -10.f);
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
transform->Velocity = glm::vec3(0.0f, -10.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);
physics->Static = false;
physics->Category = CollisionLayer::Type::Ball;
physics->Mask = static_cast<CollisionLayer::Type>(CollisionLayer::Pad | CollisionLayer::Brick | CollisionLayer::Wall);
physics->Calculate = true;
//auto plight = m_World->AddComponent<Components::PointLight>(ent);
//plight->Radius = 2.f;
m_World->CommitEntity(ent);
}
//PointLightTest
{
auto t_Light = m_World->CreateEntity();
@@ -258,62 +237,33 @@ public:
//TODO: Why does the ball not collide with these bricks?
//BottomBox
{
auto topWall = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(topWall);
transform->Position = glm::vec3(0.f, -6.f, -9.9f);
transform->Scale = glm::vec3(10.f, 0.5f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
auto BottomWall = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(BottomWall);
transform->Position = glm::vec3(0.f, -6.f, -10.f);
transform->Scale = glm::vec3(20.f, 0.5f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(BottomWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
topWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
physics->Static = true;
m_World->CommitEntity(topWall);
std::shared_ptr<Components::RectangleShape> rectangleShape = m_World->AddComponent<Components::RectangleShape>(BottomWall);
rectangleShape->Dimensions = glm::vec2(20.f, 0.5f);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(BottomWall);
physics->CollisionType = CollisionType::Type::Static;
m_World->CommitEntity(BottomWall);
}
//SideBox
// {
// auto topWall = m_World->CreateEntity();
// std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
// transform->Position = glm::vec3(3.f, -3.0f, -9.9f);
// transform->Scale = glm::vec3(0.5f, 3.f, 1.f);
// std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
// sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
// std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
// topWall);
// std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
// physics->Static = true;
// m_World->CommitEntity(topWall);
// }
// //OtherSideBox
// {
// auto topWall = m_World->CreateEntity();
// std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
// transform->Position = glm::vec3(-4.f, -3.0f, -9.9f);
// transform->Scale = glm::vec3(0.5f, 3.0f, 1.f);
// std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
// sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
// std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
// topWall);
// std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
// physics->Static = true;
// m_World->CommitEntity(topWall);
// }
{
auto topWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(
topWall);
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
transform->Position = glm::vec3(0.f, 6.f, -10.f);
transform->Scale = glm::vec3(20.f, 0.5f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
topWall);
std::shared_ptr<Components::RectangleShape> rectangleShape = m_World->AddComponent<Components::RectangleShape>(topWall);
rectangleShape->Dimensions = glm::vec2(20.f, 0.5f);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
physics->Static = true;
physics->CollisionType = CollisionType::Type::Static;
physics->Category = CollisionLayer::Wall;
physics->Mask = static_cast<CollisionLayer::Type>(CollisionLayer::Ball | CollisionLayer::Brick);
@@ -330,11 +280,11 @@ public:
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(leftWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
leftWall);
std::shared_ptr<Components::RectangleShape> rectangleShape = m_World->AddComponent<Components::RectangleShape>(leftWall);
rectangleShape->Dimensions = glm::vec2(0.5f, 20.f);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(leftWall);
physics->Static = true;
physics->CollisionType = CollisionType::Type::Static;
physics->Category = CollisionLayer::Wall;
physics->Mask = static_cast<CollisionLayer::Type>(CollisionLayer::Ball | CollisionLayer::Brick);
@@ -351,18 +301,18 @@ public:
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(rightWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
rightWall);
std::shared_ptr<Components::RectangleShape> rectangleShape = m_World->AddComponent<Components::RectangleShape>(rightWall);
rectangleShape->Dimensions = glm::vec2(0.5f, 20.f);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(rightWall);
physics->Static = true;
physics->CollisionType = CollisionType::Type::Static;
physics->Category = CollisionLayer::Wall;
physics->Mask = static_cast<CollisionLayer::Type>(CollisionLayer::Ball | CollisionLayer::Brick);
m_World->CommitEntity(rightWall);
}
//EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &Engine::OnGameStart);
m_EGameStart = decltype(m_EGameStart)(std::bind(&Engine::OnGameStart, this, std::placeholders::_1));
m_EventBroker->Subscribe(m_EGameStart);
+20 -2
View File
@@ -8,18 +8,23 @@
#include "Core/CTemplate.h"
#include "Physics/CPhysics.h"
#include "Physics/CCircleShape.h"
#include "Physics/CRectangleShape.h"
#include "Physics/EContact.h"
#include "Rendering/CModel.h"
#include "Rendering/CPointLight.h"
#include "Game/CBall.h"
#include "Game/CPowerUp.h"
#include "Game/CLife.h"
#include "Game/CBrick.h"
#include "Game/ELifeLost.h"
#include "Game/EComboEvent.h"
#include "Game/EResetBall.h"
#include "Game/EMultiBall.h"
#include "Game/EMultiBallLost.h"
#include "Game/EGameOver.h"
#include "Game/EPause.h"
#include "Game/EHitPad.h"
#include "Game/EHitLag.h"
namespace dd
{
@@ -35,8 +40,7 @@ public:
~BallSystem();
EventRelay<BallSystem, Events::Contact> m_Contact;
bool Contact(const Events::Contact &event);
void RegisterComponents(ComponentFactory *cf) override;
@@ -75,6 +79,8 @@ public:
void SetReplaceBall(const bool& replaceBall) { m_ReplaceBall = replaceBall; }
bool MultiBall() const { return m_MultiBall; }
void SetMultiBall(const bool& multiBall) { m_MultiBall = multiBall; }
bool IsPaused() const { return m_Pause; }
void SetPause(const bool& pause) { m_Pause = pause; }
private:
float m_XMovementMultiplier = 2.f;
@@ -85,18 +91,30 @@ private:
int m_PastLives = 3;
bool m_ReplaceBall = false;
bool m_MultiBall = false;
bool m_Pause = false;
EntityID m_LastCollision = 999999;
glm::vec3 m_SavedSpeed;
bool m_InitializePause = false;
EntityID m_Ball;
std::unordered_map<EntityID, std::list<glm::vec2>> m_Contacts;
void ResolveContacts();
dd::EventRelay<BallSystem, dd::Events::LifeLost> m_ELifeLost;
dd::EventRelay<BallSystem, dd::Events::MultiBallLost> m_EMultiBallLost;
dd::EventRelay<BallSystem, dd::Events::ResetBall> m_EResetBall;
dd::EventRelay<BallSystem, dd::Events::MultiBall> m_EMultiBall;
dd::EventRelay<BallSystem, dd::Events::Pause> m_EPause;
EventRelay<BallSystem, Events::Contact> m_Contact;
bool Contact(const Events::Contact &event);
bool OnLifeLost(const dd::Events::LifeLost &event);
bool OnMultiBallLost(const dd::Events::MultiBallLost &event);
bool OnResetBall(const dd::Events::ResetBall &event);
bool OnMultiBall(const dd::Events::MultiBall &event);
bool OnPause(const dd::Events::Pause &event);
};
}
+2
View File
@@ -18,6 +18,8 @@ struct Ball : Component
int Number = 0;
float Speed = 5.f;
int Combo = 0;
bool Paused = false;
glm::vec3 SavedSpeed = glm::vec3(0, 0, 0);
};
}
-2
View File
@@ -15,8 +15,6 @@ namespace Components
struct Brick : Component
{
int Points = 10;
int Hits = 1;
int Score = 50;
bool Removed = false;
};
-3
View File
@@ -17,9 +17,6 @@ namespace Components
struct Pad : Component
{
int Lives;
int Points;
float SlowdownModifier = 5.f;
float AccelerationSpeed = 40.f;
float MaxSpeed = 5.f;
+26
View File
@@ -0,0 +1,26 @@
//
// Created by Administrator on 2015-09-30.
//
#ifndef DAYDREAM_EHITLAG_H
#define DAYDREAM_EHITLAG_H
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
struct HitLag : Event
{
float Time;
std::string Type;
};
}
}
#endif //DAYDREAM_EHITLAG_H
+21
View File
@@ -0,0 +1,21 @@
//
// Created by Administrator on 2015-10-05.
//
#ifndef DAYDREAM_EHITPAD_H
#define DAYDREAM_EHITPAD_H
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
struct HitPad : Event
{
};
}
}
#endif //DAYDREAM_EHITPAD_H
+25
View File
@@ -0,0 +1,25 @@
//
// Created by Adniklastrator on 2015-09-30.
//
#ifndef DAYDREAM_EPAUSE_H
#define DAYDREAM_EPAUSE_H
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
struct Pause : Event
{
std::string Type;
};
}
}
#endif //DAYDREAM_EPAUSE_H
+91
View File
@@ -0,0 +1,91 @@
//
// Created by Adniklastrator on 2015-09-30.
//
#ifndef DAYDREAM_HITLAGSYSTEM_H
#define DAYDREAM_HITLAGSYSTEM_H
#include "Core/System.h"
#include "Core/CTransform.h"
#include "Core/CTemplate.h"
#include "Core/EventBroker.h"
#include "Core/World.h"
#include "Rendering/CSprite.h"
#include "Rendering/CModel.h"
#include "Game/CBrick.h"
#include "Game/CBall.h"
#include "Game/CLife.h"
#include "Game/CPowerUp.h"
#include "Game/EStageCleared.h"
#include "Game/ELifeLost.h"
#include "Game/EResetBall.h"
#include "Game/EScoreEvent.h"
#include "Game/EComboEvent.h"
#include "Game/EMultiBall.h"
#include "Game/EMultiBallLost.h"
#include "Game/EPause.h"
#include "Game/EHitLag.h"
#include "Game/EGameOver.h"
#include "Game/ECreatePowerUp.h"
#include "Game/EPowerUpTaken.h"
#include "Game/Bricks/CPowerUpBrick.h"
#include "Physics/CPhysics.h"
#include "Physics/CCircleShape.h"
#include "Physics/CRectangleShape.h"
#include "Physics/ESetImpulse.h"
#include "Physics/EContact.h"
#include "Sound/CCollisionSound.h"
#include "Game/PadSystem.h"
#include <fstream>
#include <iostream>
namespace dd
{
namespace Systems
{
class HitLagSystem : public System
{
public:
HitLagSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker)
{ }
void Initialize() override;
void Update(double dt) override;
void FlipPause();
bool IsPaused() const { return m_Pause; }
void SetPause(const bool& pause) { m_Pause = pause; }
bool HitLag() const { return m_HitLag; }
void SetHitLag(const bool& hitLag) { m_HitLag = hitLag; }
float& HitLagDuration() { return m_HitLagDuration; }
void SetHitLagDuration(const float& hitLagDuration) { m_HitLagDuration = hitLagDuration; }
float& HitLagTimer() { return m_HitLagTimer; }
void SetHitLagTimer(const float& hitLagTimer) { m_HitLagTimer = hitLagTimer; }
std::string& CurrentType() { return m_CurrentType; }
void SetCurrentType(const std::string& currentType) { m_CurrentType = currentType; }
private:
bool m_Pause;
bool m_HitLag;
float m_HitLagDuration;
float m_HitLagTimer;
std::string m_CurrentType;
dd::EventRelay<HitLagSystem, dd::Events::Pause> m_EPause;
dd::EventRelay<HitLagSystem, dd::Events::HitLag> m_EHitLag;
bool OnPause(const dd::Events::Pause &event);
bool OnHitLag(const dd::Events::HitLag &event);
};
}
}
#endif //DAYDREAM_HITLAGSYSTEM_H
+28 -11
View File
@@ -21,8 +21,10 @@
#include "Game/EResetBall.h"
#include "Game/EScoreEvent.h"
#include "Game/EComboEvent.h"
#include "Game/EHitPad.h"
#include "Game/EMultiBall.h"
#include "Game/EMultiBallLost.h"
#include "Game/EPause.h"
#include "Game/EGameOver.h"
#include "Game/ECreatePowerUp.h"
#include "Game/EPowerUpTaken.h"
@@ -44,15 +46,15 @@ namespace Systems
{
// Me trying things out.
class Level
/*class Level
{
public:
int levelRows;
int levelLines;
int levelSpaceBetweenBricks;
int levelSpaceToEdge;
int bricks[];
};
int levelRows = 6;
int levelLines = 7;
glm::vec2 levelSpaceBetweenBricks = glm::vec2(1, 0.4);
float levelSpaceToEdge = 0.25f;
std::array<int, 42> bricks;
};*/
class LevelSystem : public System
{
@@ -64,10 +66,8 @@ public:
void Initialize() override;
void CreateBasicLevel(int, int, glm::vec2, float);
void SaveLevel(int, int, glm::vec2, int); // Shouldn't be here, but I'm experimenting.
void LoadLevel(char[20]);
void CreateBrick(int, int, glm::vec2, float, int);
void ProcessCollision();
void CreateLevel();
void CreateBrick(int, int, glm::vec2, float, int, int);
void OnEntityRemoved(EntityID entity);
@@ -80,6 +80,8 @@ public:
void SetRestarting(const bool& restarting) { m_Restarting = restarting; }
bool Initialized() const { return m_Initialized; }
void SetInitialized(const bool& initialized) { m_Initialized = initialized; }
bool IsPaused() const { return m_Pause; }
void SetPause(const bool& pause) { m_Pause = pause; }
int& MultiBalls() { return m_MultiBalls; }
void SetMultiBalls(const int& multiBalls) { m_MultiBalls = multiBalls; }
int& PowerUps() { return m_PowerUps; }
@@ -101,8 +103,13 @@ public:
void SetNotResettingTheStage(const float& notResettingTheStage) { m_NotResettingTheStage = notResettingTheStage; }
private:
bool m_Cleared = false;
bool m_Restarting = false;
bool m_Initialized = false;
bool m_Pause = false;
int m_LooseBricks = 0;
int m_CurrentCluster = 2;
int m_CurrentLevel = 1;
int m_MultiBalls = 0;
int m_PowerUps = 0;
int m_Score = 0;
@@ -113,6 +120,10 @@ private:
glm::vec2 m_SpaceBetweenBricks = glm::vec2(1, 0.4);
float m_NotResettingTheStage = 5.f;
EntityID m_BrickTemplate;
std::array<int, 42> m_Bricks;
dd::EventRelay<LevelSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<LevelSystem, dd::Events::ScoreEvent> m_EScoreEvent;
dd::EventRelay<LevelSystem, dd::Events::MultiBall> m_EMultiBall;
@@ -120,6 +131,8 @@ private:
dd::EventRelay<LevelSystem, dd::Events::CreatePowerUp> m_ECreatePowerUp;
dd::EventRelay<LevelSystem, dd::Events::PowerUpTaken> m_EPowerUpTaken;
dd::EventRelay<LevelSystem, dd::Events::StageCleared> m_EStageCleared;
dd::EventRelay<LevelSystem, dd::Events::Pause> m_EPause;
dd::EventRelay<LevelSystem, dd::Events::HitPad> m_EHitPad;
bool OnContact(const dd::Events::Contact &event);
bool OnScoreEvent(const dd::Events::ScoreEvent &event);
@@ -128,6 +141,10 @@ private:
bool OnCreatePowerUp(const dd::Events::CreatePowerUp &event);
bool OnPowerUpTaken(const dd::Events::PowerUpTaken &event);
bool OnStageCleared(const dd::Events::StageCleared &event);
bool OnPause(const dd::Events::Pause &event);
bool OnHitPad(const dd::Events::HitPad &event);
void GetNextLevel();
};
}
+9 -1
View File
@@ -28,6 +28,8 @@
#include "Game/EMultiBall.h"
#include "Game/EPowerUpTaken.h"
#include "Game/EStageCleared.h"
#include "Game/EPause.h"
#include "Game/EHitLag.h"
namespace dd
@@ -63,6 +65,9 @@ public:
Components::Pad* Pad() const { return m_Pad; }
void SetPad(Components::Pad* pad) { m_Pad = pad; }
bool IsPaused() const { return m_Pause; }
void SetPause(const bool& pause) { m_Pause = pause; }
private:
EntityID m_Entity = 0;
glm::vec3 m_Acceleration = glm::vec3(0.f, 0.f, 0.f);
@@ -74,18 +79,21 @@ private:
Components::Transform* m_Transform = nullptr;
Components::Pad* m_Pad = nullptr;
bool m_Pause = false;
dd::EventRelay<PadSystem, dd::Events::KeyDown> m_EKeyDown;
dd::EventRelay<PadSystem, dd::Events::KeyUp> m_EKeyUp;
dd::EventRelay<PadSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<PadSystem, dd::Events::Contact> m_EContactPowerUp;
dd::EventRelay<PadSystem, dd::Events::StageCleared> m_EStageCleared;
dd::EventRelay<PadSystem, dd::Events::Pause> m_EPause;
bool OnKeyDown(const dd::Events::KeyDown &event);
bool OnKeyUp(const dd::Events::KeyUp &event);
bool OnContact(const dd::Events::Contact &event);
bool OnContactPowerUp(const dd::Events::Contact &event);
bool OnStageCleared(const dd::Events::StageCleared &event);
bool OnPause(const dd::Events::Pause &event);
class PadSteeringInputController;
std::array<std::shared_ptr<PadSteeringInputController>, 4> m_PadInputControllers;
+1 -1
View File
@@ -10,7 +10,7 @@ namespace Components
struct CircleShape : public Component
{
// Circles are circles
float Radius = 1.f;
};
}
+23 -8
View File
@@ -18,15 +18,30 @@ enum Type
Wall = 1 << 6
};
}
namespace Components
{
struct Physics: public Component
namespace CollisionType
{
bool Static = true;
bool Calculate = false;
float GravityScale = 0.f;
CollisionLayer::Type Category = CollisionLayer::Type::Other;
enum Type
{
Static = 0,
Kinematic = 1,
Dynamic = 2,
};
}
namespace Components
{
struct Physics: public Component
{
CollisionType::Type CollisionType = CollisionType::Type::Static;
bool Calculate = false;
float GravityScale = 0.f;
CollisionLayer::Type Category = CollisionLayer::Type::Other;
CollisionLayer::Type Mask = static_cast<CollisionLayer::Type>(
CollisionLayer::Pad
| CollisionLayer::Ball
@@ -36,7 +51,7 @@ struct Physics: public Component
| CollisionLayer::Wall
| CollisionLayer::Other
);
};
};
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ namespace Components
struct RectangleShape : public Component
{
glm::vec2 Dimensions = glm::vec2(1.f, 1.f);
};
}
+105 -132
View File
@@ -3,159 +3,132 @@
#include <unordered_map>
#include <Box2D/Box2D.h>
#include "Core/System.h"
#include "Core/World.h"
#include "CRectangleShape.h"
#include "Physics/CPhysics.h"
#include <Box2D/Box2D.h>
#include "Core/CTransform.h"
#include "Transform/TransformSystem.h"
#include "Physics/EContact.h"
#include "Physics/ESetImpulse.h"
#include "Physics/CCircleShape.h"
#include "Core/EventBroker.h"
#include "Game/CPad.h"
#include "Physics/CWaterVolume.h"
#include "Game/CBall.h"
#include "Rendering/CSprite.h"
#include "Physics/CParticle.h"
#include "Physics/CParticleEmitter.h"
#include "Core/CTemplate.h"
#include "Physics/CRectangleShape.h"
#include "Physics/CCircleShape.h"
#include "Physics/CPhysics.h"
#include "Physics/CWaterVolume.h"
#include "Core/CTransform.h"
#include "Transform/TransformSystem.h"
#include "CRectangleShape.h"
#include "Physics/CPhysics.h"
#include "Physics/EContact.h"
#include "Physics/ESetImpulse.h"
#include "Physics/CParticle.h"
#include "Physics/CCircleShape.h"
#include "Physics/CParticleEmitter.h"
#include "Game/CPad.h"
#include "Game/CPad.h"
#include "Game/CBall.h"
#include "Transform/TransformSystem.h"
#include "Rendering/CSprite.h"
#include "Core/CTemplate.h"
#include "Game/EPause.h"
namespace dd
{
namespace Systems
{
class PhysicsSystem : public System
{
friend class ContractListener;
public:
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker) {}
~PhysicsSystem();
EventRelay<PhysicsSystem, Events::SetImpulse> m_SetImpulse;
bool SetImpulse(const Events::SetImpulse &event);
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
// Called once per system every tick
void Update(double dt) override;
// Called once for every entity in the world every tick
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
// Called when components are committed to an entity
void OnEntityCommit(EntityID entity) override;
// Called when an entity is removed
void OnEntityRemoved(EntityID entity) override;
private:
b2Vec2 m_Gravity;
b2World* m_PhysicsWorld;
double m_TimeStep;
double m_Accumulator;
int m_VelocityIterations, m_PositionIterations;
std::unordered_map<EntityID, b2Body*> m_EntitiesToBodies;
std::unordered_map<b2Body*, EntityID> m_BodiesToEntities;
void CreateBody(EntityID entity);
std::vector<b2ParticleSystem*> m_ParticleSystem;
std::vector<b2ParticleGroup*> t_ParticleGroup;
std::vector<std::unordered_map<EntityID, const b2ParticleHandle*>> m_EntitiesToParticleHandle;
std::vector<std::unordered_map<const b2ParticleHandle*, EntityID>> m_ParticleHandleToEntities;
b2ParticleSystem* CreateParticleSystem(float radius, float gravityScale);
void InitializeWater();
void CreateParticleGroup(EntityID entity);
void CreateParticleEmitter(EntityID entity);
void UpdateParticleEmitters(double dt); //TODO: Remove them and particles if needed.
//TODO: Fill struct with info needed.
struct ParticleEmitter
namespace Systems
{
std::vector<b2ParticleSystem*> ParticleSystem;
std::vector<EntityID> Emitter;
std::vector<EntityID> ParticleTemplate;
};
ParticleEmitter m_ParticleEmitters;
//TODO: Struct med listor är bättre än en lista med structs
struct Impulse
{
b2Body* Body;
b2Vec2 Vector;
b2Vec2 Point;
};
std::list<Impulse> m_Impulses;
class ContactListener : public b2ContactListener
{
public:
ContactListener(PhysicsSystem* physicsSystem)
: m_PhysicsSystem(physicsSystem) { }
void BeginContact(b2Contact* contact)
class PhysicsSystem : public System
{
b2WorldManifold worldManifold;
friend class ContractListener;
contact->GetWorldManifold(&worldManifold);
public:
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker) { }
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));
~PhysicsSystem();
m_PhysicsSystem->EventBroker->Publish(e);
}
void EndContact(b2Contact* contact)
{
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnEntityCommit(EntityID entity) override;
void OnEntityRemoved(EntityID entity) override;
}
void 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);
}
}
private:
struct Impulse
{
b2Body* Body;
b2Vec2 Impulse;
b2Vec2 Point;
};
bool m_Pause = false;
}
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
b2Vec2 m_Gravity = b2Vec2(0.f, -9.82f);
b2World* m_PhysicsWorld = nullptr;
float m_TimeStep = 1.f/60.f;
float m_Accumulator = 0.f;
int m_VelocityIterations = 6;
int m_PositionIterations = 2;
}
std::vector<b2ParticleSystem*> m_ParticleSystem;
std::vector<b2ParticleGroup*> t_ParticleGroup;
private:
PhysicsSystem* m_PhysicsSystem;
};
std::vector<std::unordered_map<EntityID, const b2ParticleHandle*>> m_EntitiesToParticleHandle;
std::vector<std::unordered_map<const b2ParticleHandle*, EntityID>> m_ParticleHandleToEntities;
std::unordered_map<EntityID, b2Body*> m_EntitiesToBodies;
std::unordered_map<b2Body*, EntityID> m_BodiesToEntities;
ContactListener* m_ContactListener;
};
}
void CreateBody(EntityID entity);
void SyncEntitiesWithBodies();
void SyncBodiesWithEntities();
void InitializeWater();
void CreateParticleGroup(EntityID entity);
b2ParticleSystem* CreateParticleSystem(float radius, float gravityScale);
void CreateParticleEmitter(EntityID entity);
void UpdateParticleEmitters(double dt); //TODO: Remove them and particles if needed.
EventRelay<PhysicsSystem, Events::SetImpulse> m_SetImpulse;
bool SetImpulse(const Events::SetImpulse &event);
dd::EventRelay<PhysicsSystem, dd::Events::Pause> m_EPause;
bool OnPause(const dd::Events::Pause &event);
//TODO: Fill struct with info needed.
struct ParticleEmitter
{
std::vector<b2ParticleSystem*> ParticleSystem;
std::vector<EntityID> ParticleEmitter;
std::vector<EntityID> ParticleTemplate;
};
ParticleEmitter m_ParticleEmitters;
std::list<Impulse> m_Impulses;
class ContactListener : public b2ContactListener
{
public:
ContactListener(PhysicsSystem* physicsSystem)
: m_PhysicsSystem(physicsSystem) { }
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
private:
PhysicsSystem* m_PhysicsSystem;
};
ContactListener* m_ContactListener;
};
}
}
#endif //DAYDREAM_PHYSICSSYSTEM_H
#endif