Merge master
This commit is contained in:
+16
-3
@@ -9,6 +9,10 @@ find_package(ZLIB REQUIRED)
|
||||
find_package(PNG REQUIRED)
|
||||
find_package(BGFX REQUIRED)
|
||||
find_package(liquidfun REQUIRED)
|
||||
# Because FindOpenAL is retarded
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${CMAKE_SOURCE_DIR}/deps/include/AL")
|
||||
find_package(OpenAL REQUIRED)
|
||||
|
||||
# GLM
|
||||
if(UNIX)
|
||||
find_package(X11 REQUIRED)
|
||||
@@ -25,6 +29,7 @@ include_directories(
|
||||
${PNG_INCLUDE_DIRS}
|
||||
${X11_INCLUDE_DIRS}
|
||||
${BGFX_INCLUDE_DIRS}
|
||||
${OPENAL_INCLUDE_DIR}
|
||||
${liquidfun_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
@@ -68,6 +73,12 @@ file(GLOB SOURCE_FILES_Game
|
||||
)
|
||||
source_group(Game FILES ${SOURCE_FILES_Game})
|
||||
|
||||
file(GLOB SOURCE_FILES_Sound
|
||||
"${INCLUDE_PATH}/Sound/*.h"
|
||||
"Sound/*.cpp"
|
||||
)
|
||||
source_group(Sound FILES ${SOURCE_FILES_Sound})
|
||||
|
||||
set(SOURCE_FILES
|
||||
${SOURCE_FILES_Core}
|
||||
${SOURCE_FILES_Core_Util}
|
||||
@@ -75,14 +86,14 @@ set(SOURCE_FILES
|
||||
${SOURCE_FILES_Rendering}
|
||||
${SOURCE_FILES_Transform}
|
||||
${SOURCE_FILES_Physics}
|
||||
${SOURCE_FILES_Game})
|
||||
${SOURCE_FILES_Game}
|
||||
${SOURCE_FILES_Sound} ../../include/Game/EGameOver.h ../../include/Game/Bricks/CPowerUpBrick.h ../../include/Game/CPowerUp.h ../../include/Game/ECreatePowerUp.h ../../include/Game/EPowerUpTaken.h)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
||||
set(STATIC_LIBRARY_FLAGS "${STATIC_LIBRARY_FLAGS}")
|
||||
endif()
|
||||
|
||||
add_definitions(-DBGFX_CONFIG_RENDERER_OPENGL=33)
|
||||
|
||||
add_library(game ${SOURCE_FILES})
|
||||
target_link_libraries(game
|
||||
${OPENGL_LIBRARIES}
|
||||
@@ -93,6 +104,7 @@ target_link_libraries(game
|
||||
${PNG_LIBRARIES}
|
||||
${X11_LIBRARIES}
|
||||
${BGFX_LIBRARIES}
|
||||
${OPENAL_LIBRARY}
|
||||
${liquidfun_LIBRARIES}
|
||||
)
|
||||
|
||||
@@ -109,6 +121,7 @@ target_link_libraries(breakout
|
||||
${PNG_LIBRARIES}
|
||||
${X11_LIBRARIES}
|
||||
${BGFX_LIBRARIES}
|
||||
${OPENAL_LIBRARY}
|
||||
${liquidfun_LIBRARIES}
|
||||
)
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Core/World.h"
|
||||
|
||||
//TODO Delete this please
|
||||
|
||||
void dd::World::RecycleEntityID(EntityID id)
|
||||
{
|
||||
m_RecycledEntityIDs.push(id);
|
||||
|
||||
@@ -23,7 +23,14 @@ void dd::Systems::BallSystem::Update(double dt)
|
||||
|
||||
void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
|
||||
auto ballComponent = m_World->GetComponent<Components::Ball>(entity);
|
||||
if (ballComponent != nullptr) {
|
||||
auto transform = m_World->GetComponent<Components::Transform>(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<float>(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)
|
||||
@@ -38,35 +45,55 @@ void dd::Systems::BallSystem::OnEntityRemoved(EntityID entity)
|
||||
|
||||
bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
|
||||
{
|
||||
auto ballComponent1 = m_World->GetComponent<Components::Ball>(event.Entity1);
|
||||
auto ballComponent2 = m_World->GetComponent<Components::Ball>(event.Entity2);
|
||||
Components::PowerUp* power1 = m_World->GetComponent<Components::PowerUp>(event.Entity1);
|
||||
Components::PowerUp* power2 = m_World->GetComponent<Components::PowerUp>(event.Entity2);
|
||||
|
||||
if (power1 != nullptr || power2 != nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Components::Ball* ballComponent1 = m_World->GetComponent<Components::Ball>(event.Entity1);
|
||||
Components::Ball* ballComponent2 = m_World->GetComponent<Components::Ball>(event.Entity2);
|
||||
Components::Ball* ballComponent;
|
||||
|
||||
if (ballComponent1 != nullptr && ballComponent2 != nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//Which is the ball?
|
||||
EntityID ballEntity, otherEntitiy;
|
||||
glm::vec2 normal;
|
||||
|
||||
if (ballComponent1 != nullptr) {
|
||||
ballEntity = event.Entity1;
|
||||
otherEntitiy = event.Entity2;
|
||||
|
||||
//Make normal point from the other entity to the ball
|
||||
normal = event.Normal * -1.f;
|
||||
ballComponent = ballComponent1;
|
||||
}
|
||||
else if (ballComponent2 != nullptr) {
|
||||
ballEntity = event.Entity2;
|
||||
otherEntitiy = event.Entity1;
|
||||
|
||||
//Make normal point from the other entity to the ball
|
||||
normal = event.Normal * 1.f;
|
||||
ballComponent = ballComponent2;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
//TODO: Add support for power-up collisions
|
||||
}
|
||||
|
||||
auto ballTransform = m_World->GetComponent<Components::Transform>(ballEntity);
|
||||
auto otherTransform = m_World->GetComponent<Components::Transform>(otherEntitiy);
|
||||
|
||||
glm::vec2 ballVelocity = glm::vec2(ballTransform->Velocity.x, ballTransform->Velocity.y);
|
||||
glm::vec2 reflectedVelocity = ballVelocity - 2.f * glm::dot(ballVelocity, normal)*normal;
|
||||
ballTransform->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
|
||||
if (m_World->GetProperty<std::string>(otherEntitiy, "Name") == "Pad"){
|
||||
auto padTransform = m_World->GetComponent<Components::Transform>(otherEntitiy);
|
||||
float x = ballTransform->Position.x - padTransform->Position.x;
|
||||
|
||||
float movementMultiplier = 5.0f;
|
||||
float y = glm::cos((abs(x) / (1.6f)) * glm::pi<float>() / 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;
|
||||
}
|
||||
|
||||
+202
-44
@@ -10,8 +10,12 @@
|
||||
void dd::Systems::LevelSystem::Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, LevelSystem::LifeLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EScoreEvent, LevelSystem::ScoreEvent);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, LevelSystem::OnLifeLost);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EScoreEvent, LevelSystem::OnScoreEvent);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, LevelSystem::OnMultiBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ECreatePowerUp, LevelSystem::OnCreatePowerUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPowerUpTaken, LevelSystem::OnPowerUpTaken);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, LevelSystem::OnStageCleared);
|
||||
|
||||
for (int i = 0; i < Lives(); i++) {
|
||||
CreateLife(i);
|
||||
@@ -24,8 +28,8 @@ void dd::Systems::LevelSystem::CreateLife(int number)
|
||||
{
|
||||
auto life = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(life);
|
||||
transform->Position = glm::vec3(-4.f + number * 0.5f, -2.f, -5.f);
|
||||
transform->Scale = glm::vec3(0.25f, 0.25f, 0.25f);
|
||||
transform->Position = glm::vec3(-1.5f + number * 0.15f, -2.f, -5.f);
|
||||
transform->Scale = glm::vec3(0.1f, 0.1f, 0.1f);
|
||||
|
||||
std::shared_ptr<Components::Life> lifeNr = m_World->AddComponent<Components::Life>(life);
|
||||
lifeNr->Number = number;
|
||||
@@ -33,8 +37,6 @@ void dd::Systems::LevelSystem::CreateLife(int number)
|
||||
auto model = m_World->AddComponent<Components::Model>(life);
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
|
||||
/*std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(life);
|
||||
sprite->SpriteFile = "Textures/Ball.png";*/
|
||||
|
||||
m_World->CommitEntity(life);
|
||||
}
|
||||
@@ -45,41 +47,111 @@ void dd::Systems::LevelSystem::Update(double dt)
|
||||
CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
m_Initialized = true;
|
||||
}
|
||||
|
||||
if (Lives() < 0)
|
||||
{
|
||||
SetLives(3);
|
||||
Events::GameOver e;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
|
||||
if (ball != NULL) {
|
||||
if (ball != nullptr) {
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (transformBall->Position.y < -10) {
|
||||
if (Lives() == PastLives()) {
|
||||
if (glm::abs(transformBall->Velocity.y) < 2) {
|
||||
if (transformBall->Velocity.y > 0) {
|
||||
transformBall->Velocity.y = 2;
|
||||
} else {
|
||||
transformBall->Velocity.y = -2;
|
||||
}
|
||||
}
|
||||
if (transformBall->Position.y < -EdgeY() - 4) {
|
||||
if (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 if (Lives() == PastLives()) {
|
||||
Events::ResetBall be;
|
||||
EventBroker->Publish(be);
|
||||
Events::LifeLost e;
|
||||
e.Entity = entity;
|
||||
EventBroker->Publish(e);
|
||||
Events::ResetBall be;
|
||||
EventBroker->Publish(be);
|
||||
return;
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
} else if (transformBall->Position.y > EdgeY()) {
|
||||
if (transformBall->Velocity.y > 0) {
|
||||
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 != NULL) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (NumberOfBricks() <= 0) {
|
||||
/*SetRestarting(true);
|
||||
if (MultiBalls() <= 0 && PowerUps() <= 0) {
|
||||
Events::ResetBall e;
|
||||
EventBroker->Publish(e);
|
||||
Events::ScoreEvent es;
|
||||
es.Score = 500;
|
||||
EventBroker->Publish(es);
|
||||
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);
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(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);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, int spaceToEdge)
|
||||
void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, float spaceToEdge)
|
||||
{
|
||||
int num = 0;
|
||||
SetNumberOfBricks(rows * lines);
|
||||
std::cout << "You're only doing this once, right?" << std::endl;
|
||||
int num = 0;
|
||||
for (int i = 0; i < rows; i++) {
|
||||
num++;
|
||||
for (int j = 0; j < Lines(); j++) {
|
||||
@@ -89,10 +161,12 @@ void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 s
|
||||
num = 0;
|
||||
}
|
||||
|
||||
SetNumberOfBricks(rows * lines);
|
||||
SetRestarting(false);
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBetweenBricks, int spaceToEdge, int num)
|
||||
void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBetweenBricks, float spaceToEdge, int num)
|
||||
{
|
||||
auto brick = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(brick);
|
||||
@@ -106,11 +180,19 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe
|
||||
fileName.append(".png");
|
||||
//sprite->SpriteFile = fileName;
|
||||
model->ModelFile = "Models/Test/Brick/Brick.obj";
|
||||
float x = spaceToEdge + line * spacesBetweenBricks.x;
|
||||
float y = spaceToEdge + row * spacesBetweenBricks.y;
|
||||
transform->Scale = glm::vec3(1.6, 0.35, 0.5);
|
||||
transform->Position = glm::vec3(x - 7, y + 1, -10.f);
|
||||
if ((line == 1 && row == 4) || (line == 3 && row == 2) || (line == 6 && row == 6)) {
|
||||
std::shared_ptr<Components::PowerUpBrick> cPow = m_World->AddComponent<Components::PowerUpBrick>(brick);
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -123,59 +205,135 @@ void dd::Systems::LevelSystem::ProcessCollision()
|
||||
|
||||
void dd::Systems::LevelSystem::OnEntityRemoved(EntityID entity)
|
||||
{
|
||||
/*auto brick = m_World->GetComponent<Components::Brick>(entity);
|
||||
if (brick != NULL) {
|
||||
numberOfBricks--;
|
||||
if (numberOfBricks < 1) {
|
||||
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)
|
||||
{
|
||||
EntityID entityBrick = event.Entity1;
|
||||
auto brick = m_World->GetComponent<Components::Brick>(entityBrick);
|
||||
if (brick == NULL) {
|
||||
EntityID entityBrick;
|
||||
EntityID entityBall;
|
||||
auto ball = m_World->GetComponent<Components::Ball>(event.Entity2);
|
||||
auto brick = m_World->GetComponent<Components::Brick>(event.Entity1);
|
||||
if (brick != nullptr) {
|
||||
entityBrick = event.Entity1;
|
||||
} else {
|
||||
brick = m_World->GetComponent<Components::Brick>(event.Entity2);
|
||||
if (brick != nullptr) {
|
||||
entityBrick = event.Entity2;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (ball != nullptr) {
|
||||
entityBall = event.Entity2;
|
||||
} else {
|
||||
ball = m_World->GetComponent<Components::Ball>(event.Entity1);
|
||||
if (ball != nullptr) {
|
||||
entityBall = event.Entity1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
brick = m_World->GetComponent<Components::Brick>(entityBrick);
|
||||
if (brick == nullptr) {
|
||||
return false;
|
||||
}
|
||||
EntityID entityBall = event.Entity2;
|
||||
|
||||
m_World->RemoveEntity(entityBrick);
|
||||
|
||||
SetNumberOfBricks(NumberOfBricks() - 1);
|
||||
if (NumberOfBricks() == 0) {
|
||||
Events::ScoreEvent es;
|
||||
es.Score = 500;
|
||||
EventBroker->Publish(es);
|
||||
Events::ResetBall e;
|
||||
auto power = m_World->GetComponent<Components::PowerUpBrick>(entityBrick);
|
||||
if (power != nullptr) {
|
||||
Events::CreatePowerUp e;
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entityBrick);
|
||||
e.Position = transform->Position;
|
||||
EventBroker->Publish(e);
|
||||
CreateBasicLevel(Rows(), Lines(), SpaceBetweenBricks(), SpaceToEdge());
|
||||
}
|
||||
Events::ScoreEvent es;
|
||||
es.Score = brick->Score;
|
||||
EventBroker->Publish(es);
|
||||
|
||||
std::cout << "Score: " << Score() << std::endl;
|
||||
if (ball != nullptr && Restarting() == false) {
|
||||
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);
|
||||
|
||||
SetNumberOfBricks(NumberOfBricks() - 1);
|
||||
if (NumberOfBricks() <= 0) {
|
||||
SetMultiBalls(MultiBalls() + 1);
|
||||
}
|
||||
Events::ScoreEvent es;
|
||||
es.Score = brick->Score;
|
||||
EventBroker->Publish(es);
|
||||
|
||||
std::cout << NumberOfBricks() << std::endl;
|
||||
//std::cout << "Score: " << Score() << std::endl;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::LifeLost(const dd::Events::LifeLost &event)
|
||||
bool dd::Systems::LevelSystem::OnLifeLost(const dd::Events::LifeLost &event)
|
||||
{
|
||||
SetLives(Lives() - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::ScoreEvent(const dd::Events::ScoreEvent &event)
|
||||
bool dd::Systems::LevelSystem::OnScoreEvent(const dd::Events::ScoreEvent &event)
|
||||
{
|
||||
SetScore(Score() += event.Score);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
SetMultiBalls(MultiBalls()+2);
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnCreatePowerUp(const dd::Events::CreatePowerUp &event)
|
||||
{
|
||||
SetPowerUps(PowerUps() + 1);
|
||||
auto powerUp = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(powerUp);
|
||||
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);
|
||||
std::shared_ptr<Components::PowerUp> cPow = m_World->AddComponent<Components::PowerUp>(powerUp);
|
||||
cPhys->Static = false;
|
||||
|
||||
transform->Position = event.Position;
|
||||
transform->Scale = glm::vec3(0.2, 0.2, 0.2);
|
||||
transform->Velocity = glm::vec3(0, -4, 0);
|
||||
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
|
||||
m_World->CommitEntity(powerUp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnPowerUpTaken(const dd::Events::PowerUpTaken &event)
|
||||
{
|
||||
SetPowerUps(PowerUps() - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnStageCleared(const dd::Events::StageCleared &event)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::EndLevel()
|
||||
{
|
||||
Events::StageCleared e;
|
||||
|
||||
+128
-51
@@ -22,56 +22,25 @@ void dd::Systems::PadSystem::Initialize()
|
||||
l.Command = "left";
|
||||
EventBroker->Publish(l);
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, PadSystem::OnKeyDown);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, PadSystem::OnKeyUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, PadSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EResetBall, PadSystem::ResetBall);
|
||||
|
||||
return;
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &PadSystem::OnKeyDown);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &PadSystem::OnKeyUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, &PadSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContactPowerUp, &PadSystem::OnContactPowerUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EResetBall, &PadSystem::OnResetBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, &PadSystem::OnMultiBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, &PadSystem::OnStageCleared);
|
||||
}
|
||||
|
||||
void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
if (ball != NULL) {
|
||||
if (ball != nullptr) {
|
||||
if (ReplaceBall() == true) {
|
||||
SetReplaceBall(false);
|
||||
|
||||
auto transformEntity = m_World->GetComponent<Components::Transform>(entity);
|
||||
transformEntity->Position = glm::vec3(20, 20, -10);
|
||||
|
||||
//Temporary. Create new ball.
|
||||
/*auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(0.5f, 0.f, -10.f);
|
||||
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
|
||||
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
|
||||
sprite->SpriteFile = "Textures/Ball.png";
|
||||
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;*/
|
||||
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(0.5f, 0.f, -10.f);
|
||||
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
|
||||
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;
|
||||
|
||||
m_World->RemoveEntity(entity);
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
Events::SetImpulse e;
|
||||
e.Entity = ent;
|
||||
e.Impulse = glm::vec2(0.f, -7.f);
|
||||
e.Point = glm::vec2(transform->Position.x, transform->Position.y);
|
||||
EventBroker->Publish(e);
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
transform->Position = glm::vec3(0.0f, 0.26f, -10.f);
|
||||
transform->Velocity = glm::vec3(0.0f, -ball->Speed, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,11 +75,9 @@ void dd::Systems::PadSystem::Update(double dt)
|
||||
|
||||
if (Left()) {
|
||||
acceleration.x = -pad->AccelerationSpeed;
|
||||
}
|
||||
else if (Right()) {
|
||||
} else if (Right()) {
|
||||
acceleration.x = pad->AccelerationSpeed;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
acceleration.x = 0.f;
|
||||
}
|
||||
|
||||
@@ -118,9 +85,38 @@ void dd::Systems::PadSystem::Update(double dt)
|
||||
SetPad(pad);
|
||||
SetAcceleration(acceleration);
|
||||
|
||||
if (MultiBall() == true) {
|
||||
SetMultiBall(false);
|
||||
|
||||
Events::MultiBall e;
|
||||
e.padTransform = transform;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
EntityID dd::Systems::PadSystem::CreateBall()
|
||||
{
|
||||
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;
|
||||
|
||||
cball->Speed = 10;
|
||||
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
return ent;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
{
|
||||
int val = event.KeyCode;
|
||||
@@ -138,6 +134,10 @@ bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
SetRight(true);
|
||||
} else if (val == GLFW_KEY_R) {
|
||||
SetReplaceBall(true);
|
||||
} else if (val == GLFW_KEY_M) {
|
||||
SetMultiBall(true);
|
||||
} else if (val == GLFW_KEY_D) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -173,7 +173,7 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entityBall);
|
||||
auto transformPad = m_World->GetComponent<Components::Transform>(entityPad);
|
||||
|
||||
float movementMultiplier = 4.f;
|
||||
float movementMultiplier = 0.5f;
|
||||
|
||||
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
//float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f;
|
||||
@@ -188,13 +188,12 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
// std::cout << movementX << " " << movementY << std::endl;
|
||||
|
||||
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2) + 1;
|
||||
|
||||
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2)+ 0.2;
|
||||
//std::cout << movementX << " " << movementY << std::endl;
|
||||
|
||||
float len = glm::length<float>(transformBall->Velocity);
|
||||
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
|
||||
|
||||
transformBall->Velocity += glm::vec3(transformPad->Velocity.x, 0, 0);
|
||||
transformBall->Velocity = glm::normalize(transformBall->Velocity) * len;
|
||||
|
||||
@@ -203,12 +202,90 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
*/
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::ResetBall(const dd::Events::ResetBall &event)
|
||||
bool dd::Systems::PadSystem::OnContactPowerUp(const dd::Events::Contact &event)
|
||||
{
|
||||
EntityID entityPower;
|
||||
EntityID entityPad;
|
||||
auto powerUp = m_World->GetComponent<Components::PowerUp>(event.Entity1);
|
||||
auto pad = m_World->GetComponent<Components::Pad>(event.Entity2);
|
||||
if (powerUp != nullptr) {
|
||||
entityPower = event.Entity1;
|
||||
} else {
|
||||
powerUp = m_World->GetComponent<Components::PowerUp>(event.Entity2);
|
||||
if (powerUp != nullptr) {
|
||||
entityPower = event.Entity2;
|
||||
}
|
||||
}
|
||||
if (pad != nullptr) {
|
||||
entityPad = event.Entity2;
|
||||
} else {
|
||||
pad = m_World->GetComponent<Components::Pad>(event.Entity1);
|
||||
if (pad != nullptr) {
|
||||
entityPad = event.Entity1;
|
||||
}
|
||||
}
|
||||
|
||||
if (entityPower == NULL || entityPad == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pad = m_World->GetComponent<Components::Pad>(entityPad);
|
||||
if (pad == nullptr) {
|
||||
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";
|
||||
EventBroker->Publish(ep);
|
||||
|
||||
Events::MultiBall e;
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entityPad);
|
||||
e.padTransform = transform;
|
||||
EventBroker->Publish(e);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnResetBall(const dd::Events::ResetBall &event)
|
||||
{
|
||||
SetReplaceBall(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
auto ent1 = CreateBall();
|
||||
auto ent2 = CreateBall();
|
||||
auto transform1 = m_World->GetComponent<Components::Transform>(ent1);
|
||||
auto transform2 = m_World->GetComponent<Components::Transform>(ent2);
|
||||
auto ball1 = m_World->GetComponent<Components::Ball>(ent1);
|
||||
auto ball2 = m_World->GetComponent<Components::Ball>(ent2);
|
||||
auto padTransform = event.padTransform;
|
||||
float x1 = padTransform->Position.x - 2, x2 = padTransform->Position.x + 2;
|
||||
if (x1 < -3.1) {
|
||||
x1 = 3;
|
||||
}
|
||||
if (x2 > 3.1) {
|
||||
x2 = -3;
|
||||
}
|
||||
transform1->Position = glm::vec3(x1, -5.5, -10);
|
||||
transform2->Position = glm::vec3(x2, -5.5, -10);
|
||||
|
||||
transform1->Velocity = glm::normalize(glm::vec3(5, 5 ,0.f)) * ball1->Speed;
|
||||
transform2->Velocity = glm::normalize(glm::vec3(-5, 5 ,0.f)) * ball2->Speed;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnStageCleared(const dd::Events::StageCleared &event)
|
||||
{
|
||||
auto entity = CreateBall();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event)
|
||||
{
|
||||
std::string command = event.Command;
|
||||
|
||||
@@ -22,56 +22,25 @@ void dd::Systems::PadSystem::Initialize()
|
||||
l.Command = "left";
|
||||
EventBroker->Publish(l);
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, PadSystem::OnKeyDown);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, PadSystem::OnKeyUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, PadSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EResetBall, PadSystem::ResetBall);
|
||||
|
||||
return;
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &PadSystem::OnKeyDown);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &PadSystem::OnKeyUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, &PadSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContactPowerUp, &PadSystem::OnContactPowerUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EResetBall, &PadSystem::OnResetBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMultiBall, &PadSystem::OnMultiBall);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, &PadSystem::OnStageCleared);
|
||||
}
|
||||
|
||||
void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entity);
|
||||
if (ball != NULL) {
|
||||
if (ball != nullptr) {
|
||||
if (ReplaceBall() == true) {
|
||||
SetReplaceBall(false);
|
||||
|
||||
auto transformEntity = m_World->GetComponent<Components::Transform>(entity);
|
||||
transformEntity->Position = glm::vec3(20, 20, -10);
|
||||
|
||||
//Temporary. Create new ball.
|
||||
/*auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(0.5f, 0.f, -10.f);
|
||||
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
|
||||
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
|
||||
sprite->SpriteFile = "Textures/Ball.png";
|
||||
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;*/
|
||||
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(0.5f, 0.f, -10.f);
|
||||
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
|
||||
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;
|
||||
|
||||
m_World->RemoveEntity(entity);
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
Events::SetImpulse e;
|
||||
e.Entity = ent;
|
||||
e.Impulse = glm::vec2(0.f, -7.f);
|
||||
e.Point = glm::vec2(transform->Position.x, transform->Position.y);
|
||||
EventBroker->Publish(e);
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
transform->Position = glm::vec3(0.0f, 0.26f, -10.f);
|
||||
transform->Velocity = glm::vec3(0.0f, -ball->Speed, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,27 +69,20 @@ void dd::Systems::PadSystem::Update(double dt)
|
||||
transform->Velocity.x = pad->MaxSpeed;
|
||||
}
|
||||
transform->Position += transform->Velocity * (float)dt;
|
||||
<<<<<<< HEAD
|
||||
transform->Velocity += acceleration * (float)dt;
|
||||
transform->Velocity += acceleration * (float)dt;
|
||||
transform->Velocity -= transform->Velocity * pad->SlowdownModifier * (float)dt;
|
||||
|
||||
|
||||
if (Left()) {
|
||||
acceleration.x = -pad->AccelerationSpeed;
|
||||
=======
|
||||
transform->Velocity += acceleration * (float)dt;
|
||||
transform->Velocity -= transform->Velocity * (0.9f * (float)dt);
|
||||
|
||||
|
||||
|
||||
if (left)
|
||||
{
|
||||
acceleration.x = -20.f;
|
||||
>>>>>>> origin/physics
|
||||
<<<<<<< HEAD
|
||||
}
|
||||
else if (Right()) {
|
||||
=======
|
||||
} else if (Right()) {
|
||||
>>>>>>> 72413dc0a93bd3a2f6ab9fa7e19bbc3b846232db
|
||||
acceleration.x = pad->AccelerationSpeed;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
acceleration.x = 0.f;
|
||||
}
|
||||
|
||||
@@ -128,9 +90,38 @@ void dd::Systems::PadSystem::Update(double dt)
|
||||
SetPad(pad);
|
||||
SetAcceleration(acceleration);
|
||||
|
||||
if (MultiBall() == true) {
|
||||
SetMultiBall(false);
|
||||
|
||||
Events::MultiBall e;
|
||||
e.padTransform = transform;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
EntityID dd::Systems::PadSystem::CreateBall()
|
||||
{
|
||||
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;
|
||||
|
||||
cball->Speed = 10;
|
||||
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
return ent;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
{
|
||||
int val = event.KeyCode;
|
||||
@@ -148,6 +139,10 @@ bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
SetRight(true);
|
||||
} else if (val == GLFW_KEY_R) {
|
||||
SetReplaceBall(true);
|
||||
} else if (val == GLFW_KEY_M) {
|
||||
SetMultiBall(true);
|
||||
} else if (val == GLFW_KEY_D) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -183,10 +178,10 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entityBall);
|
||||
auto transformPad = m_World->GetComponent<Components::Transform>(entityPad);
|
||||
|
||||
float movementMultiplier = 4.f;
|
||||
float movementMultiplier = 0.5f;
|
||||
|
||||
float whatX = transformBall->Position.x - transformPad->Position.x;
|
||||
float movementX;
|
||||
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
//float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f;
|
||||
if (whatX > 0) {
|
||||
movementX = movementMultiplier * whatX;
|
||||
//std::cout << "Right!" << std::endl;
|
||||
@@ -195,41 +190,20 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
//std::cout << "Left!" << std::endl;
|
||||
}
|
||||
|
||||
//float movementY = 1;
|
||||
// std::cout << movementX << " " << movementY << std::endl;
|
||||
|
||||
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
<<<<<<< HEAD
|
||||
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2) + 1;
|
||||
=======
|
||||
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2)+ 0.2;
|
||||
>>>>>>> 72413dc0a93bd3a2f6ab9fa7e19bbc3b846232db
|
||||
|
||||
//std::cout << movementX << " " << movementY << std::endl;
|
||||
|
||||
//Temporary solution. Add a new ball and delete the old one.
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.01f, -10.f);
|
||||
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
|
||||
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;
|
||||
|
||||
m_World->RemoveEntity(entityBall);
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
Events::SetImpulse e;
|
||||
e.Entity = ent;
|
||||
e.Impulse = glm::vec2(movementX, movementY);
|
||||
e.Point = glm::vec2(transform->Position.x, transform->Position.y);
|
||||
EventBroker->Publish(e);
|
||||
=======
|
||||
//float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f;
|
||||
|
||||
// std::cout << movementX << " " << movementY << std::endl;
|
||||
|
||||
float len = glm::length<float>(transformBall->Velocity);
|
||||
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
|
||||
|
||||
transformBall->Velocity += glm::vec3(transformPad->Velocity.x, 0, 0);
|
||||
transformBall->Velocity = glm::normalize(transformBall->Velocity) * len;
|
||||
@@ -237,15 +211,92 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
|
||||
//transform->Velocity = glm::vec3(movementX, movementY, 0.f);
|
||||
*/
|
||||
>>>>>>> origin/physics
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::ResetBall(const dd::Events::ResetBall &event)
|
||||
bool dd::Systems::PadSystem::OnContactPowerUp(const dd::Events::Contact &event)
|
||||
{
|
||||
EntityID entityPower;
|
||||
EntityID entityPad;
|
||||
auto powerUp = m_World->GetComponent<Components::PowerUp>(event.Entity1);
|
||||
auto pad = m_World->GetComponent<Components::Pad>(event.Entity2);
|
||||
if (powerUp != nullptr) {
|
||||
entityPower = event.Entity1;
|
||||
} else {
|
||||
powerUp = m_World->GetComponent<Components::PowerUp>(event.Entity2);
|
||||
if (powerUp != nullptr) {
|
||||
entityPower = event.Entity2;
|
||||
}
|
||||
}
|
||||
if (pad != nullptr) {
|
||||
entityPad = event.Entity2;
|
||||
} else {
|
||||
pad = m_World->GetComponent<Components::Pad>(event.Entity1);
|
||||
if (pad != nullptr) {
|
||||
entityPad = event.Entity1;
|
||||
}
|
||||
}
|
||||
|
||||
if (entityPower == NULL || entityPad == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pad = m_World->GetComponent<Components::Pad>(entityPad);
|
||||
if (pad == nullptr) {
|
||||
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";
|
||||
EventBroker->Publish(ep);
|
||||
|
||||
Events::MultiBall e;
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entityPad);
|
||||
e.padTransform = transform;
|
||||
EventBroker->Publish(e);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnResetBall(const dd::Events::ResetBall &event)
|
||||
{
|
||||
SetReplaceBall(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnMultiBall(const dd::Events::MultiBall &event)
|
||||
{
|
||||
auto ent1 = CreateBall();
|
||||
auto ent2 = CreateBall();
|
||||
auto transform1 = m_World->GetComponent<Components::Transform>(ent1);
|
||||
auto transform2 = m_World->GetComponent<Components::Transform>(ent2);
|
||||
auto ball1 = m_World->GetComponent<Components::Ball>(ent1);
|
||||
auto ball2 = m_World->GetComponent<Components::Ball>(ent2);
|
||||
auto padTransform = event.padTransform;
|
||||
float x1 = padTransform->Position.x - 2, x2 = padTransform->Position.x + 2;
|
||||
if (x1 < -3.1) {
|
||||
x1 = 3;
|
||||
}
|
||||
if (x2 > 3.1) {
|
||||
x2 = -3;
|
||||
}
|
||||
transform1->Position = glm::vec3(x1, -5.5, -10);
|
||||
transform2->Position = glm::vec3(x2, -5.5, -10);
|
||||
|
||||
transform1->Velocity = glm::normalize(glm::vec3(5, 5 ,0.f)) * ball1->Speed;
|
||||
transform2->Velocity = glm::normalize(glm::vec3(-5, 5 ,0.f)) * ball2->Speed;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnStageCleared(const dd::Events::StageCleared &event)
|
||||
{
|
||||
auto entity = CreateBall();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event)
|
||||
{
|
||||
std::string command = event.Command;
|
||||
|
||||
@@ -47,7 +47,13 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||
point.x = event.Point.x;
|
||||
point.y = event.Point.y;
|
||||
|
||||
body->ApplyLinearImpulse(impulse, point, true);
|
||||
|
||||
Impulse i;
|
||||
i.Body = body;
|
||||
i.Impulse = impulse;
|
||||
i.Point = point;
|
||||
|
||||
m_Impulses.push_back(i);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -67,7 +73,7 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m_World->GetEntityParent(entity) == 0) {
|
||||
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;
|
||||
@@ -79,6 +85,10 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i : m_Impulses) {
|
||||
i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
|
||||
}
|
||||
m_Impulses.clear();
|
||||
m_Accumulator += dt;
|
||||
while(m_Accumulator >= m_TimeStep)
|
||||
{
|
||||
@@ -106,7 +116,7 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
|
||||
float angle = body->GetAngle();
|
||||
|
||||
//TODO: CHECK IF THIS IS CORRECT
|
||||
|
||||
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle));
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ void dd::Systems::PhysicsSystem::Initialize()
|
||||
{
|
||||
m_ContactListener = new ContactListener(this);
|
||||
|
||||
m_Gravity = b2Vec2(0.f, 0.f);
|
||||
m_Gravity = b2Vec2(0.f, -9.82f);
|
||||
m_PhysicsWorld = new b2World(m_Gravity);
|
||||
|
||||
m_TimeStep = 1.f/60.f;
|
||||
@@ -22,11 +22,16 @@ void dd::Systems::PhysicsSystem::Initialize()
|
||||
|
||||
m_PhysicsWorld->SetContactListener(m_ContactListener);
|
||||
|
||||
InitializeWater();
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, PhysicsSystem::SetImpulse);
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::InitializeWater()
|
||||
{
|
||||
b2ParticleSystemDef m_ParticleSystemDef;
|
||||
m_ParticleSystemDef.radius = 0.1f;
|
||||
|
||||
m_ParticleSystem = m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef);
|
||||
}
|
||||
|
||||
@@ -42,7 +47,13 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||
point.x = event.Point.x;
|
||||
point.y = event.Point.y;
|
||||
|
||||
body->ApplyLinearImpulse(impulse, point, true);
|
||||
|
||||
Impulse i;
|
||||
i.Body = body;
|
||||
i.Impulse = impulse;
|
||||
i.Point = point;
|
||||
|
||||
m_Impulses.push_back(i);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -62,7 +73,11 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
continue;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
if (m_World->GetEntityParent(entity) == 0) {
|
||||
=======
|
||||
if (m_World->GetEntityParent(entity) == 0) { //TODO: Make this work with childs too
|
||||
>>>>>>> 72413dc0a93bd3a2f6ab9fa7e19bbc3b846232db
|
||||
b2Vec2 position;
|
||||
position.x = transformComponent->Position.x;
|
||||
position.y = transformComponent->Position.y;
|
||||
@@ -70,17 +85,25 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
float angle = -glm::eulerAngles(transformComponent->Orientation).z;
|
||||
|
||||
body->SetTransform(position, angle);
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
|
||||
|
||||
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
|
||||
|
||||
|
||||
>>>>>>> origin/physics
|
||||
}
|
||||
}
|
||||
|
||||
=======
|
||||
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i : m_Impulses) {
|
||||
i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
|
||||
}
|
||||
m_Impulses.clear();
|
||||
|
||||
|
||||
>>>>>>> 72413dc0a93bd3a2f6ab9fa7e19bbc3b846232db
|
||||
m_Accumulator += dt;
|
||||
while(m_Accumulator >= m_TimeStep)
|
||||
{
|
||||
@@ -88,10 +111,6 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
m_Accumulator -= dt;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
|
||||
>>>>>>> origin/physics
|
||||
for (auto i : m_EntitiesToBodies) {
|
||||
EntityID entity = i.first;
|
||||
b2Body* body = i.second;
|
||||
@@ -101,21 +120,22 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
continue;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (! transformComponent)
|
||||
continue;
|
||||
|
||||
<<<<<<< HEAD
|
||||
auto parent = m_World->GetEntityParent(entity);
|
||||
if (parent == 0) {
|
||||
=======
|
||||
>>>>>>> origin/physics
|
||||
if (m_World->GetEntityParent(entity) == 0) {
|
||||
>>>>>>> 72413dc0a93bd3a2f6ab9fa7e19bbc3b846232db
|
||||
b2Vec2 position = body->GetPosition();
|
||||
transformComponent->Position.x = position.x;
|
||||
transformComponent->Position.y = position.y;
|
||||
|
||||
float angle = body->GetAngle();
|
||||
|
||||
//TODO: CHECK IF THIS IS CORRECT
|
||||
|
||||
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle));
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
@@ -123,10 +143,26 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
b2Vec2 velocity = body->GetLinearVelocity();
|
||||
transformComponent->Velocity.x = velocity.x;
|
||||
transformComponent->Velocity.y = velocity.y;
|
||||
>>>>>>> 72413dc0a93bd3a2f6ab9fa7e19bbc3b846232db
|
||||
|
||||
>>>>>>> origin/physics
|
||||
}
|
||||
}
|
||||
|
||||
b2Vec2* positionBuffer = m_ParticleSystem->GetPositionBuffer();
|
||||
for (auto i : m_EntitiesToParticleHandle) {
|
||||
EntityID entity = i.first;
|
||||
b2ParticleHandle* particleH = i.second;
|
||||
|
||||
b2Vec2 positionB2 = positionBuffer[particleH->GetIndex()];
|
||||
glm::vec2 position = glm::vec2(positionB2.x, positionB2.y);
|
||||
|
||||
|
||||
EntityID entityParent = m_World->GetEntityParent(entity);
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
auto transformParent = m_World->GetComponent<Components::Transform>(entityParent);
|
||||
|
||||
transform->Position = glm::vec3(position.x, position.y, -10) - transformParent->Position;
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
@@ -139,10 +175,13 @@ void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
auto waterComponent = m_World->GetComponent<Components::WaterVolume>(entity);
|
||||
|
||||
if (physicsComponent && waterComponent) {
|
||||
LOG_ERROR("Entity has both water and physics component, this is illegal. The police has been alerted.");
|
||||
return;
|
||||
}
|
||||
if (physicsComponent) {
|
||||
CreateBody(entity);
|
||||
}
|
||||
if (waterComponent) {
|
||||
} else if (waterComponent) {
|
||||
CreateParticleGroup(entity);
|
||||
}
|
||||
}
|
||||
@@ -200,7 +239,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
auto boxComponent = m_World->GetComponent<Components::RectangleShape>(entity);
|
||||
if (boxComponent) {
|
||||
b2PolygonShape* bShape = new b2PolygonShape();
|
||||
bShape->SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2); //TODO: THIS SUCKS DUDE 4?!?!?!?
|
||||
bShape->SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2);
|
||||
pShape = bShape;
|
||||
} else {
|
||||
auto circleComponent = m_World->GetComponent<Components::CircleShape>(entity);
|
||||
@@ -212,7 +251,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
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; //TODO: THIS ALSO SUCKS 4 WTH
|
||||
pShape->m_radius = absoluteTransform.Scale.x/2;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -245,15 +284,37 @@ void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e)
|
||||
LOG_ERROR("No Transform component in CreateParticleGroup");
|
||||
return;
|
||||
}
|
||||
LOG_INFO("------ CREATING PARTICLE GROUP");
|
||||
|
||||
b2ParticleGroupDef pd;
|
||||
b2PolygonShape shape;
|
||||
shape.SetAsBox(transform->Scale.x, transform->Scale.y);
|
||||
|
||||
shape.SetAsBox(transform->Scale.x/2.f, transform->Scale.y/2.f);
|
||||
pd.shape = &shape;
|
||||
pd.flags = b2_elasticParticle;
|
||||
pd.angle = -0.5f;
|
||||
pd.angularVelocity = 2.0f;
|
||||
pd.flags = b2_tensileParticle;
|
||||
pd.linearVelocity = b2Vec2(0, 5.f);
|
||||
pd.position.Set(transform->Position.x, transform->Position.y);
|
||||
//TODO: PUT IN LIST
|
||||
t_watergroup = m_ParticleSystem->CreateParticleGroup(pd);
|
||||
b2Vec2* t_ParticlePositions = m_ParticleSystem->GetPositionBuffer();
|
||||
for(int i = 0; i < m_ParticleSystem->GetParticleCount(); i++){
|
||||
{
|
||||
auto t_waterparticle = m_World->CreateEntity(e);
|
||||
auto transformChild = m_World->AddComponent<Components::Transform>(t_waterparticle);
|
||||
//auto sprite = m_World->AddComponent<Components::Sprite>(t_waterparticle);
|
||||
|
||||
|
||||
|
||||
transformChild->Position = glm::vec3(t_ParticlePositions[i].x - transform->Position.x, t_ParticlePositions[i].y - transform->Position.y, -9.5f);
|
||||
transformChild->Scale = glm::vec3(m_ParticleSystem->GetRadius())/transform->Scale;
|
||||
//sprite->SpriteFile = "Textures/Ball.png";
|
||||
m_World->CommitEntity(t_waterparticle);
|
||||
|
||||
m_EntitiesToParticleHandle.insert(std::make_pair(t_waterparticle, m_ParticleSystem->GetParticleHandleFromIndex(i)));
|
||||
m_ParticleHandleToEntities.insert(std::make_pair( m_ParticleSystem->GetParticleHandleFromIndex(i), t_waterparticle));
|
||||
}
|
||||
|
||||
}
|
||||
LOG_INFO("ParticleCount: %i", m_ParticleSystem->GetParticleCount());
|
||||
}
|
||||
|
||||
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
#include "Sound/Sound.h"
|
||||
|
||||
dd::Sound::Sound(std::string path)
|
||||
{
|
||||
m_Buffer = LoadFile(path);
|
||||
m_Path = path;
|
||||
}
|
||||
|
||||
ALuint dd::Sound::LoadFile(std::string path)
|
||||
{
|
||||
if (m_BufferCache.find(path) != m_BufferCache.end()) {
|
||||
return m_BufferCache[path];
|
||||
}
|
||||
|
||||
//Open file
|
||||
FILE *fp = fopen(path.c_str(), "rb");
|
||||
if (!fp) {
|
||||
LOG_ERROR("Failed to open file %s, no such file exists", path.c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
//CHECK FOR VALID WAVE-FILE
|
||||
fread(m_Type, sizeof(char), 4, fp);
|
||||
if(m_Type[0] != 'R' || m_Type[1] != 'I' || m_Type[2] != 'F' || m_Type[3] != 'F') {
|
||||
LOG_ERROR("ERROR: No RIFF in WAVE-file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
fread(&m_Size, sizeof(unsigned long), 1, fp);
|
||||
fread(m_Type, sizeof(char), 4, fp);
|
||||
if (m_Type[0] != 'W' || m_Type[1] != 'A' || m_Type[2] != 'V' || m_Type[3]!= 'E') {
|
||||
LOG_ERROR("ERROR: Not WAVE-file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
fread(m_Type, sizeof(char), 4, fp);
|
||||
if (m_Type[0] != 'f' || m_Type[1] != 'm' || m_Type[2] != 't' || m_Type[3] != ' ') {
|
||||
LOG_ERROR("ERROR: No fmt in WAVE-file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//READ THE DATA FROM WAVE-FILE
|
||||
fread(&m_ChunkSize, sizeof(unsigned long), 1, fp);
|
||||
fread(&m_FormatType, sizeof(short), 1, fp);
|
||||
fread(&m_Channels, sizeof(short), 1, fp);
|
||||
fread(&m_SampleRate, sizeof(unsigned long), 1, fp);
|
||||
fread(&m_AvgBytesPerSec, sizeof(unsigned long), 1, fp);
|
||||
fread(&m_BytesPerSample, sizeof(short), 1, fp);
|
||||
fread(&m_BitsPerSample, sizeof(short), 1, fp);
|
||||
|
||||
fread(m_Type, sizeof(char), 4, fp);
|
||||
if (m_Type[0] !='d' || m_Type[1] != 'a' || m_Type[2] != 't' || m_Type[3] != 'a') {
|
||||
LOG_ERROR("ERROR: WAVE-file Missing data");
|
||||
return 0;
|
||||
}
|
||||
|
||||
fread(&m_DataSize, sizeof(unsigned long), 1, fp);
|
||||
|
||||
unsigned char* buf = new unsigned char[m_DataSize];
|
||||
fread(buf, sizeof(unsigned char), m_DataSize, fp);
|
||||
fclose(fp);
|
||||
|
||||
// Create buffer
|
||||
ALuint format = 0;
|
||||
if (m_BitsPerSample == 8) {
|
||||
if (m_Channels == 1) {
|
||||
format = AL_FORMAT_MONO8;
|
||||
}
|
||||
else if (m_Channels == 2) {
|
||||
format = AL_FORMAT_STEREO8;
|
||||
}
|
||||
}
|
||||
if (m_BitsPerSample == 16) {
|
||||
if (m_Channels == 1) {
|
||||
format = AL_FORMAT_MONO16;
|
||||
}
|
||||
else if (m_Channels == 2) {
|
||||
format = AL_FORMAT_STEREO16;
|
||||
}
|
||||
}
|
||||
|
||||
ALuint buffer;
|
||||
alGenBuffers(1, &buffer);
|
||||
alBufferData(buffer, format, buf, m_DataSize, m_SampleRate);
|
||||
delete[] buf;
|
||||
|
||||
m_BufferCache[path] = buffer;
|
||||
return buffer;
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
#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();
|
||||
|
||||
const ALfloat pos[3] = {0, 0, 0};
|
||||
alListenerfv(AL_POSITION, pos);
|
||||
|
||||
//Subscribe to events
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySound);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound);
|
||||
|
||||
//Todo: Move this
|
||||
{
|
||||
dd::Events::PlaySound e;
|
||||
e.path = "Sounds/BGM/soft-guitar.wav";
|
||||
e.loop = true;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
{
|
||||
dd::Events::PlaySound e;
|
||||
e.path = "Sounds/BGM/water-flowing.wav";
|
||||
e.volume = 0.3f;
|
||||
e.loop = true;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::SoundSystem::Update(double dt)
|
||||
{
|
||||
//LOG_INFO("Sources : %i", m_SourcesToBuffers.size());
|
||||
|
||||
std::vector<ALuint> deleteList;
|
||||
for (auto item : m_SourcesToBuffers) {
|
||||
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_SourcesToBuffers.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.path);
|
||||
if (sound == nullptr) {
|
||||
return false;
|
||||
}
|
||||
ALuint source = CreateSource();
|
||||
m_SourcesToBuffers[source] = sound;
|
||||
ALuint buffer = sound->Buffer();
|
||||
alSourcei(source, AL_BUFFER, buffer);
|
||||
|
||||
//Sound settings
|
||||
alSourcef(source, AL_GAIN, event.volume);
|
||||
alSourcef(source, AL_PITCH, event.pitch);
|
||||
if (event.loop) {
|
||||
alSourcei(source, AL_LOOPING, AL_TRUE);
|
||||
}
|
||||
else if (!event.loop)
|
||||
{
|
||||
alSourcei(source, AL_LOOPING, AL_FALSE);
|
||||
}
|
||||
|
||||
|
||||
//Play
|
||||
alSourcePlay(source);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::SoundSystem::OnStopSound(const dd::Events::StopSound &event)
|
||||
{
|
||||
for (auto item : m_SourcesToBuffers) {
|
||||
if (item.second->Path() == event.path) {
|
||||
alSourceStop(item.first);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
dd::Events::StopSound e;
|
||||
e.path = "Sounds/BGM/soft-guitar.wav";
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
//Send play-sound event
|
||||
dd::Events::PlaySound e;
|
||||
e.path = collisionSound->filePath;
|
||||
EventBroker->Publish(e);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user