Bodies are now removed properly
This commit is contained in:
+10
-39
@@ -237,51 +237,22 @@ 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> rectangleShape = m_World->AddComponent<Components::RectangleShape>(topWall);
|
||||
rectangleShape->Dimensions = glm::vec2(10.f, 0.5f);
|
||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(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(topWall);
|
||||
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);
|
||||
|
||||
|
||||
@@ -72,6 +72,8 @@ void dd::Systems::BallSystem::Update(double dt)
|
||||
|
||||
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) {
|
||||
@@ -128,7 +130,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);
|
||||
@@ -143,7 +146,7 @@ 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()) {
|
||||
@@ -250,10 +253,10 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
|
||||
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);
|
||||
// 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();
|
||||
|
||||
@@ -27,9 +27,8 @@ void dd::Systems::PhysicsSystem::InitializeWater()
|
||||
|
||||
bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||
{
|
||||
b2Body* body = m_EntitiesToBodies[event.Entity];
|
||||
|
||||
if (body == nullptr) {
|
||||
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;
|
||||
}
|
||||
@@ -43,7 +42,7 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||
point.y = event.Point.y;
|
||||
|
||||
Impulse i;
|
||||
i.Body = body;
|
||||
i.Body = it->second;
|
||||
i.Impulse = impulse;
|
||||
i.Point = point;
|
||||
|
||||
@@ -244,21 +243,21 @@ 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;
|
||||
}
|
||||
}*/
|
||||
|
||||
b2Body* body = m_EntitiesToBodies[entity];
|
||||
if (body == nullptr) {
|
||||
auto it = m_EntitiesToBodies.find(entity);
|
||||
if(it == m_EntitiesToBodies.end()) {
|
||||
LOG_ERROR("Trying to remove non-exsisting body, Entity: %i", entity);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INFO("Removing entity %i", entity);
|
||||
it->second->GetWorld()->DestroyBody(it->second);
|
||||
m_BodiesToEntities.erase(it->second);
|
||||
m_EntitiesToBodies.erase(entity);
|
||||
m_BodiesToEntities.erase(body);
|
||||
|
||||
body->GetWorld()->DestroyBody(body);
|
||||
//delete body;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user