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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user