#include "PrecompiledHeader.h" #include "Game/BallSystem.h" dd::Systems::BallSystem::~BallSystem() { } void dd::Systems::BallSystem::RegisterComponents(ComponentFactory *cf) { } void dd::Systems::BallSystem::Initialize() { EVENT_SUBSCRIBE_MEMBER(m_Contact, BallSystem::Contact); } void dd::Systems::BallSystem::Update(double dt) { } void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { auto ballComponent = m_World->GetComponent(entity); if (ballComponent != nullptr) { auto transform = m_World->GetComponent(entity); glm::vec2 dir = glm::normalize(glm::vec2(transform->Velocity.x, transform->Velocity.y)); glm::vec2 up = glm::vec2(0.f, 1.f); float angle = glm::acos(glm::dot(dir, up)) * glm::sign(dir.x); transform->Orientation = glm::rotate(glm::quat(), angle, glm::vec3(0.f, 0.f, -1.f)); } } void dd::Systems::BallSystem::OnEntityCommit(EntityID entity) { } void dd::Systems::BallSystem::OnEntityRemoved(EntityID entity) { } bool dd::Systems::BallSystem::Contact(const Events::Contact &event) { Components::Ball* ballComponent1 = m_World->GetComponent(event.Entity1); Components::Ball* ballComponent2 = m_World->GetComponent(event.Entity2); Components::Ball* ballComponent; //Which is the ball? EntityID ballEntity, otherEntitiy; if (ballComponent1 != nullptr) { ballEntity = event.Entity1; otherEntitiy = event.Entity2; ballComponent = ballComponent1; } else if (ballComponent2 != nullptr) { ballEntity = event.Entity2; otherEntitiy = event.Entity1; ballComponent = ballComponent2; } else { return false; //TODO: Add support for power-up collisions } auto ballTransform = m_World->GetComponent(ballEntity); glm::vec2 ballVelocity = glm::vec2(ballTransform->Velocity.x, ballTransform->Velocity.y); if (m_World->GetProperty(otherEntitiy, "Name") == "Pad"){ auto padTransform = m_World->GetComponent(otherEntitiy); float x = ballTransform->Position.x - padTransform->Position.x; float movementMultiplier = 5.0f; float y = glm::cos((abs(x) / ((1.6f) * movementMultiplier)) * glm::pi() / 2.f) + 1.f; ballTransform->Velocity = glm::normalize(glm::vec3(x, y ,0.f)) * ballComponent->Speed; } else { glm::vec2 reflectedVelocity = glm::reflect(ballVelocity, event.Normal); ballTransform->Velocity = glm::vec3(reflectedVelocity, 0.f); } return true; }