Merge remote-tracking branch 'origin/master' into Sound
Conflicts: include/Core/Engine.h src/game/CMakeLists.txt
This commit is contained in:
+13
-3
@@ -8,7 +8,7 @@ find_package(assimp REQUIRED)
|
||||
find_package(ZLIB REQUIRED)
|
||||
find_package(PNG REQUIRED)
|
||||
find_package(BGFX REQUIRED)
|
||||
|
||||
find_package(liquidfun REQUIRED)
|
||||
find_package(OpenAL REQUIRED)
|
||||
|
||||
# GLM
|
||||
@@ -26,8 +26,9 @@ include_directories(
|
||||
${assimp_INCLUDE_DIRS}
|
||||
${PNG_INCLUDE_DIRS}
|
||||
${X11_INCLUDE_DIRS}
|
||||
${BGFX_INCLUDE_DIRS}
|
||||
${BGFX_INCLUDE_DIRS}
|
||||
${OPENAL_INCLUDE_DIR}
|
||||
${liquidfun_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
file(GLOB SOURCE_FILES_Core
|
||||
@@ -58,6 +59,12 @@ file(GLOB SOURCE_FILES_Transform
|
||||
)
|
||||
source_group(Transform FILES ${SOURCE_FILES_Transform})
|
||||
|
||||
file(GLOB SOURCE_FILES_Physics
|
||||
"${INCLUDE_PATH}/Physics/*.h"
|
||||
"Physics/*.cpp"
|
||||
)
|
||||
source_group(Physics FILES ${SOURCE_FILES_Physics})
|
||||
|
||||
file(GLOB SOURCE_FILES_Game
|
||||
"${INCLUDE_PATH}/Game/*.h"
|
||||
"Game/*.cpp"
|
||||
@@ -70,6 +77,7 @@ set(SOURCE_FILES
|
||||
${SOURCE_FILES_Input}
|
||||
${SOURCE_FILES_Rendering}
|
||||
${SOURCE_FILES_Transform}
|
||||
${SOURCE_FILES_Physics}
|
||||
${SOURCE_FILES_Game}
|
||||
)
|
||||
|
||||
@@ -87,8 +95,9 @@ target_link_libraries(game
|
||||
${assimp_LIBRARIES}
|
||||
${PNG_LIBRARIES}
|
||||
${X11_LIBRARIES}
|
||||
${BGFX_LIBRARIES}
|
||||
${BGFX_LIBRARIES}
|
||||
${OPENAL_LIBRARY}
|
||||
${liquidfun_LIBRARIES}
|
||||
)
|
||||
|
||||
add_executable(breakout
|
||||
@@ -105,5 +114,6 @@ target_link_libraries(breakout
|
||||
${X11_LIBRARIES}
|
||||
${BGFX_LIBRARIES}
|
||||
${OPENAL_LIBRARY}
|
||||
${liquidfun_LIBRARIES}
|
||||
)
|
||||
|
||||
|
||||
Regular → Executable
+11
-3
@@ -77,12 +77,20 @@ void dd::Camera::SetOrientation(glm::quat val)
|
||||
|
||||
void dd::Camera::UpdateProjectionMatrix()
|
||||
{
|
||||
m_ProjectionMatrix = glm::perspective(
|
||||
m_FOV,
|
||||
m_AspectRatio,
|
||||
m_ProjectionMatrix = glm::ortho(
|
||||
-16.f,
|
||||
16.f,
|
||||
-9.f,
|
||||
9.f,
|
||||
m_NearClip,
|
||||
m_FarClip
|
||||
);
|
||||
// m_ProjectionMatrix = glm::perspective(
|
||||
// m_FOV,
|
||||
// m_AspectRatio,
|
||||
// m_NearClip,
|
||||
// m_FarClip
|
||||
// );
|
||||
}
|
||||
|
||||
void dd::Camera::UpdateViewMatrix()
|
||||
|
||||
Regular → Executable
+33
-8
@@ -98,21 +98,20 @@ void dd::Renderer::LoadShaders()
|
||||
/*
|
||||
Forward rendering
|
||||
*/
|
||||
|
||||
m_spForward = ResourceManager::Load<ShaderProgram>("Shaders/Forward/");
|
||||
m_spForward->Link();
|
||||
|
||||
/*
|
||||
Screen draw
|
||||
*/
|
||||
|
||||
m_spScreen = ResourceManager::Load<ShaderProgram>("Shaders/Screen/");
|
||||
m_spScreen->Link();
|
||||
}
|
||||
|
||||
void dd::Renderer::CreateBuffers()
|
||||
{
|
||||
m_UnitQuad = CreateQuad();
|
||||
m_ScreenQuad = CreateQuad();
|
||||
m_UnitQuad = ResourceManager::Load<Model>("Models/Core/UnitQuad.obj");
|
||||
m_UnitSphere = ResourceManager::Load<Model>("Models/Core/UnitSphere.obj");
|
||||
|
||||
glGenRenderbuffers(1, &m_rbDepthBuffer);
|
||||
@@ -222,7 +221,7 @@ void dd::Renderer::Draw(RenderQueueCollection& rq)
|
||||
m_spScreen->Bind();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, m_CurrentScreenBuffer);
|
||||
glBindVertexArray(m_UnitQuad);
|
||||
glBindVertexArray(m_ScreenQuad);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
||||
glfwSwapBuffers(m_Window);
|
||||
@@ -266,12 +265,12 @@ void dd::Renderer::DrawDeferred(RenderQueue &objects, RenderQueue &lights)
|
||||
glClearColor(0, 0, 0, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
m_spDeferred3->Bind();
|
||||
glUniform3fv(glGetUniformLocation(*m_spDeferred3, "La"), 1, glm::value_ptr(glm::vec3(0.3f)));
|
||||
glUniform3fv(glGetUniformLocation(*m_spDeferred3, "La"), 1, glm::value_ptr(glm::vec3(0.5f)));
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, m_GDiffuse);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, m_tLighting);
|
||||
glBindVertexArray(m_UnitQuad);
|
||||
glBindVertexArray(m_ScreenQuad);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
}
|
||||
|
||||
@@ -308,6 +307,7 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program)
|
||||
|
||||
glUniform1f(glGetUniformLocation(shaderProgramHandle, "MaterialShininess"), modelJob->Shininess);
|
||||
|
||||
//TODO: Make sure that a normal/specular is loaded in.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture);
|
||||
if (modelJob->NormalTexture != 0) {
|
||||
@@ -322,6 +322,31 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program)
|
||||
glBindVertexArray(modelJob->VAO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->ElementBuffer);
|
||||
glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
auto spriteJob = std::dynamic_pointer_cast<SpriteJob>(job);
|
||||
if (spriteJob)
|
||||
{
|
||||
glm::mat4 modelMatrix = spriteJob->ModelMatrix;
|
||||
MVP = PV * modelMatrix;
|
||||
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
|
||||
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
|
||||
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, spriteJob->DiffuseTexture);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, spriteJob->NormalTexture);
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
glBindTexture(GL_TEXTURE_2D, spriteJob->SpecularTexture);
|
||||
|
||||
glBindVertexArray(m_UnitQuad->VAO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_UnitQuad->ElementBuffer);
|
||||
glDrawElementsBaseVertex(GL_TRIANGLES, m_UnitQuad->m_Indices.size(), GL_UNSIGNED_INT, 0, 0);
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -396,9 +421,9 @@ GLuint dd::Renderer::CreateQuad()
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(4);
|
||||
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
Regular → Executable
+1
-1
@@ -65,7 +65,7 @@ void main()
|
||||
+ BoneWeights2[3] * Bones[int(BoneIndices2[3])];
|
||||
}
|
||||
|
||||
gl_Position = MVP * boneTransform * vec4(Position, 1.0);
|
||||
gl_Position = MVP * vec4(Position, 1.0);
|
||||
|
||||
Output.Position = (V * M * boneTransform * vec4(Position, 1.0)).xyz;
|
||||
Output.Normal = (inverse(transpose(V * M)) * boneTransform * vec4(Normal, 0.0)).xyz;
|
||||
|
||||
Regular → Executable
+3
-1
@@ -37,6 +37,8 @@ void main()
|
||||
vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord);
|
||||
|
||||
//frag_Diffuse = DiffuseTexel * (vec4(La, 0.0) * (vec4(LightingTexel.rgb, 0.0) + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0)));
|
||||
frag_Diffuse = DiffuseTexel * (vec4(La, 0.0) + LightingTexel);
|
||||
//frag_Diffuse = DiffuseTexel * (vec4(La, 0.0) + LightingTexel);
|
||||
frag_Diffuse = DiffuseTexel * (vec4(1.f, 1.f, 1.f, 0.0) + LightingTexel);
|
||||
//frag_Diffuse = DiffuseTexel;
|
||||
//frag_Diffuse = vec4(LightingTexel.r, LightingTexel.g, LightingTexel.b, 1.0);
|
||||
}
|
||||
Regular → Executable
+2
-2
@@ -84,6 +84,6 @@ void main()
|
||||
|
||||
vec4 diffuseTexel = texture(DiffuseTexture, Input.TextureCoord);
|
||||
|
||||
//FragmentColor = phong(Input.Position, normalize(normal), specularTexel.rgb, specularTexel.a);
|
||||
FragmentColor = diffuseTexel;
|
||||
FragmentColor = phong(Input.Position, normalize(normal), specularTexel.rgb, specularTexel.a);
|
||||
//FragmentColor = diffuseTexel;
|
||||
}
|
||||
Regular → Executable
+3
-1
@@ -65,8 +65,10 @@ void main()
|
||||
+ BoneWeights2[3] * Bones[int(BoneIndices2[3])];
|
||||
}
|
||||
|
||||
gl_Position = MVP * boneTransform * vec4(Position, 1.0);
|
||||
//gl_Position = MVP * boneTransform * vec4(Position, 1.0);
|
||||
gl_Position = MVP * vec4(Position, 1.0);
|
||||
|
||||
//TODO: Make sure that boneTransform works here.
|
||||
Output.Position = (V * M * boneTransform * vec4(Position, 1.0)).xyz;
|
||||
Output.Normal = (inverse(transpose(V * M)) * boneTransform * vec4(Normal, 0.0)).xyz;
|
||||
Output.Tangent = Tangent;
|
||||
|
||||
@@ -24,7 +24,7 @@ dd::Texture::Texture(std::string path)
|
||||
std::unique_ptr<Image> image = std::make_unique<PNG>(path);
|
||||
|
||||
if (image->Width == 0 && image->Height == 0 || image->Format == Image::ImageFormat::Unknown) {
|
||||
image = std::make_unique<PNG>("Textures/ErrorTexture.png");
|
||||
image = std::make_unique<PNG>("Textures/Core/ErrorTexture.png");
|
||||
if (image->Width == 0 && image->Height == 0 || image->Format == Image::ImageFormat::Unknown) {
|
||||
LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed.");
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
//
|
||||
// Created by Adniklastrator on 2015-09-09.
|
||||
//
|
||||
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Game/LevelSystem.h"
|
||||
|
||||
|
||||
void dd::Systems::LevelSystem::Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::Update(double dt) {
|
||||
if (!m_Initialized) {
|
||||
CreateBasicLevel(tRows, tLines, tSpaceBetweenBricks, tSpaceToEdge);
|
||||
m_Initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, int spaceToEdge)
|
||||
{
|
||||
int num = 0;
|
||||
numberOfBricks = rows * lines;
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
num++;
|
||||
for (int j = 0; j < lines; j++)
|
||||
{
|
||||
CreateBrick(i, j, spacesBetweenBricks, spaceToEdge, num);
|
||||
}
|
||||
if (num == 7)
|
||||
num = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBetweenBricks, int spaceToEdge, int num)
|
||||
{
|
||||
auto brick = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(brick);
|
||||
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(brick);
|
||||
std::shared_ptr<Components::Brick> cBrick = m_World->AddComponent<Components::Brick>(brick);
|
||||
std::shared_ptr<Components::RectangleShape> cRec = m_World->AddComponent<Components::RectangleShape>(brick);
|
||||
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(brick);
|
||||
std::string fileName = "Textures/Bricks/";
|
||||
fileName.append(std::to_string(num));
|
||||
fileName.append(".png");
|
||||
sprite->SpriteFile = fileName;
|
||||
float x = spaceToEdge + line * spacesBetweenBricks.x;
|
||||
float y = spaceToEdge + row * spacesBetweenBricks.y;
|
||||
transform->Scale = glm::vec3(1.6, 0.4, 0.);
|
||||
transform->Position = glm::vec3(x - 7, y + 1, -10.f);
|
||||
//sprite->Color = glm::vec4(1.f, 1.f, 1.f, 1.f);
|
||||
m_World->CommitEntity(brick);
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::ProcessCollision()
|
||||
{
|
||||
// Like, go into brick component and check what it does upon collision. Maybe not done here?
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::OnEntityRemoved(EntityID entity)
|
||||
{
|
||||
auto brick = m_World->GetComponent<Components::Brick>(entity);
|
||||
if (brick != NULL)
|
||||
{
|
||||
numberOfBricks--;
|
||||
if (numberOfBricks < 1)
|
||||
{
|
||||
EndLevel();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
|
||||
{
|
||||
/*event.Entity1 - Vi vet inte vilken som.
|
||||
event.Entity2 - */
|
||||
EntityID entityBrick = event.Entity1;
|
||||
auto brick = m_World->GetComponent<Components::Brick>(entityBrick);
|
||||
if (brick == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
EntityID entityBall = event.Entity2;
|
||||
//EntityID entity1 = event.Entity1;
|
||||
//EntityID entity2 = event.Entity2;
|
||||
|
||||
//auto brick = m_World->GetComponent<Components::Brick>(entity1);
|
||||
//auto ball = m_World->GetComponent<Components::Ball>(entity2);
|
||||
//std::cout << brick << std::endl;
|
||||
//std::cout << ball << std::endl;
|
||||
/*if (brick != NULL)
|
||||
{
|
||||
std::cout << "Entity1 is a brick!" << std::endl;
|
||||
entityBrick = entity1;
|
||||
if (ball != NULL)
|
||||
{
|
||||
std::cout << "Entity2 is a ball!" << std::endl;
|
||||
entityBall = entity2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
brick = m_World->GetComponent<Components::Brick>(entity2);
|
||||
if (brick != NULL)
|
||||
{
|
||||
std::cout << "Entity2 is a brick!" << std::endl;
|
||||
entityBrick = entity2;
|
||||
}
|
||||
|
||||
ball = m_World->GetComponent<Components::Ball>(entity1);
|
||||
if (ball != NULL)
|
||||
{
|
||||
std::cout << "Entity1 is a ball!" << std::endl;
|
||||
entityBall = entity1;
|
||||
}
|
||||
}
|
||||
|
||||
if (entityBrick != NULL && entityBall != NULL)
|
||||
{*/
|
||||
//std::cout << "We're destroying!" << std::endl;
|
||||
m_World->RemoveEntity(entityBrick);
|
||||
|
||||
return true;
|
||||
//}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void dd::Systems::LevelSystem::EndLevel()
|
||||
{
|
||||
Events::StageCleared e;
|
||||
EventBroker->Publish(e);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
//
|
||||
// Created by Adniklastrator on 2015-09-10.
|
||||
//
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Game/PadSystem.h"
|
||||
#include "Core/World.h"
|
||||
#include <iostream>
|
||||
#include <Game/CPad.h>
|
||||
|
||||
|
||||
void dd::Systems::PadSystem::Initialize()
|
||||
{
|
||||
Events::BindKey r;
|
||||
r.KeyCode = GLFW_KEY_RIGHT;
|
||||
r.Command = "right";
|
||||
EventBroker->Publish(r);
|
||||
|
||||
Events::BindKey l;
|
||||
l.KeyCode = GLFW_KEY_LEFT;
|
||||
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);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void dd::Systems::PadSystem::Update(double dt)
|
||||
{
|
||||
if (ent == 0) {
|
||||
for (auto it = m_World->GetEntities()->begin(); it != m_World->GetEntities()->end(); it++) {
|
||||
if (m_World->GetProperty<std::string>(it->first, "Name") == "Pad") {
|
||||
ent = it->first;
|
||||
transform = m_World->GetComponent<Components::Transform>(ent);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (transform->Velocity.x < -400.f)
|
||||
{
|
||||
transform->Velocity.x = -400.f;
|
||||
}
|
||||
else if (transform->Velocity.x > 400.f)
|
||||
{
|
||||
transform->Velocity.x = 400.f;
|
||||
}
|
||||
transform->Position += transform->Velocity * (float)dt;
|
||||
transform->Velocity += acceleration * (float)dt;
|
||||
transform->Velocity -= transform->Velocity * (0.9f * (float)dt);
|
||||
|
||||
if (left)
|
||||
{
|
||||
acceleration.x = -20.f;
|
||||
}
|
||||
else if (right)
|
||||
{
|
||||
acceleration.x = 20.f;
|
||||
}
|
||||
else
|
||||
{
|
||||
acceleration.x = 0.f;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
|
||||
{
|
||||
int val = event.KeyCode;
|
||||
if (val == 265)
|
||||
{
|
||||
//std::cout << "Up!" << std::endl;
|
||||
}
|
||||
else if (val == 264)
|
||||
{
|
||||
//std::cout << "Down!" << std::endl;
|
||||
}
|
||||
else if (val == 263)
|
||||
{
|
||||
//std::cout << "Left!" << std::endl;
|
||||
//acceleration.x = -0.01f;
|
||||
left = true;
|
||||
}
|
||||
else if (val = 262)
|
||||
{
|
||||
//std::cout << "Right!" << std::endl;
|
||||
//acceleration.x = 0.01f;
|
||||
right = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event)
|
||||
{
|
||||
int val = event.KeyCode;
|
||||
if (val == 265)
|
||||
{
|
||||
|
||||
}
|
||||
else if (val == 264)
|
||||
{
|
||||
|
||||
}
|
||||
else if (val == 263)
|
||||
{
|
||||
left = false;
|
||||
}
|
||||
else if (val = 262)
|
||||
{
|
||||
right = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
{
|
||||
EntityID entityBall = event.Entity2;
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entityBall);
|
||||
if (ball == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
EntityID entityPad = event.Entity1;
|
||||
auto pad = m_World->GetComponent<Components::Pad>(entityPad);
|
||||
if (pad == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto transformBall = m_World->GetComponent<Components::Transform>(entityBall);
|
||||
auto transformPad = m_World->GetComponent<Components::Transform>(entityPad);
|
||||
|
||||
float movementMultiplier = 2.f;
|
||||
|
||||
float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f;
|
||||
|
||||
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.1f, -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;
|
||||
|
||||
m_World->RemoveEntity(entityBall);
|
||||
m_World->CommitEntity(ent);
|
||||
|
||||
Events::SetImpulse e;
|
||||
e.Entity = ent;
|
||||
e.Impulse = glm::vec2(movementX, movementY);
|
||||
e.Point = glm::vec2(event.ContactPoint);
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event)
|
||||
{
|
||||
std::string command = event.Command;
|
||||
|
||||
std::cout << "Command!" << std::endl;
|
||||
|
||||
if (command == "right")
|
||||
{
|
||||
std::cout << "Right!" << std::endl;
|
||||
}
|
||||
else if (command == "left")
|
||||
{
|
||||
std::cout << "Left!" << std::endl;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Physics/PhysicsSystem.h"
|
||||
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
|
||||
{
|
||||
cf->Register<Components::CircleShape>();
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::Initialize()
|
||||
{
|
||||
m_ContactListener = new ContactListener(this);
|
||||
|
||||
m_Gravity = b2Vec2(0.f, 0.f);
|
||||
m_PhysicsWorld = new b2World(m_Gravity);
|
||||
|
||||
m_TimeStep = 1.f/60.f;
|
||||
m_VelocityIterations = 8;
|
||||
m_PositionIterations = 6;
|
||||
|
||||
|
||||
m_PhysicsWorld->SetContactListener(m_ContactListener);
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, PhysicsSystem::SetImpulse);
|
||||
}
|
||||
|
||||
bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||
{
|
||||
b2Body* body = m_EntitiesToBodies[event.Entity];
|
||||
|
||||
b2Vec2 impulse;
|
||||
impulse.x = event.Impulse.x;
|
||||
impulse.y = event.Impulse.y;
|
||||
|
||||
b2Vec2 point;
|
||||
point.x = event.Point.x;
|
||||
point.y = event.Point.y;
|
||||
|
||||
body->ApplyLinearImpulse(impulse, point, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
{
|
||||
for (auto i : m_EntitiesToBodies) {
|
||||
EntityID entity = i.first;
|
||||
b2Body* body = i.second;
|
||||
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
|
||||
|
||||
if (m_World->GetEntityParent(entity) == 0) {
|
||||
b2Vec2 position;
|
||||
position.x = transformComponent->Position.x;
|
||||
position.y = transformComponent->Position.y;
|
||||
|
||||
float angle = -glm::eulerAngles(transformComponent->Orientation).z;
|
||||
|
||||
body->SetTransform(position, angle);
|
||||
}
|
||||
}
|
||||
|
||||
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
|
||||
|
||||
for (auto i : m_EntitiesToBodies) {
|
||||
EntityID entity = i.first;
|
||||
b2Body* body = i.second;
|
||||
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
|
||||
|
||||
if (m_World->GetEntityParent(entity) == 0) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
||||
{
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
if(physicsComponent) {
|
||||
CreateBody(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity)
|
||||
{
|
||||
b2Body* body = m_EntitiesToBodies[entity];
|
||||
|
||||
m_EntitiesToBodies.erase(entity);
|
||||
m_BodiesToEntities.erase(body);
|
||||
|
||||
m_PhysicsWorld->DestroyBody(body);
|
||||
}
|
||||
|
||||
|
||||
void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
{
|
||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||
if(!physicsComponent){
|
||||
LOG_ERROR("No PhysicsComponent in CreateBody");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if(!transformComponent) {
|
||||
LOG_ERROR("No TransformComponent in CreateBody");
|
||||
return;
|
||||
}
|
||||
|
||||
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(entity);
|
||||
|
||||
b2BodyDef bodyDef;
|
||||
bodyDef.position.Set(absoluteTransform.Position.x, absoluteTransform.Position.y);
|
||||
|
||||
|
||||
bodyDef.angle = -glm::eulerAngles(absoluteTransform.Orientation).z;
|
||||
|
||||
if (physicsComponent->Static) {
|
||||
bodyDef.type = b2_staticBody;
|
||||
} else {
|
||||
bodyDef.type = b2_dynamicBody;
|
||||
}
|
||||
|
||||
|
||||
b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef);
|
||||
|
||||
b2Shape* pShape;
|
||||
|
||||
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?!?!?!?
|
||||
pShape = bShape;
|
||||
} else {
|
||||
auto circleComponent = m_World->GetComponent<Components::CircleShape>(entity);
|
||||
if (circleComponent) {
|
||||
pShape = new b2CircleShape();
|
||||
pShape->m_radius = absoluteTransform.Scale.x;
|
||||
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if(physicsComponent->Static) {
|
||||
body->CreateFixture(pShape, 0); //Density kanske ska vara 0 på statiska kroppar
|
||||
}
|
||||
else {
|
||||
//TODO: FIX THIS SHIT INTO COMPONENTS
|
||||
b2FixtureDef fixtureDef;
|
||||
fixtureDef.shape = pShape;
|
||||
fixtureDef.density = 1.f;
|
||||
fixtureDef.restitution = 1.0f;
|
||||
fixtureDef.friction = 0.3f;
|
||||
body->CreateFixture(&fixtureDef);
|
||||
|
||||
}
|
||||
|
||||
delete pShape;
|
||||
|
||||
m_EntitiesToBodies.insert(std::make_pair(entity, body));
|
||||
m_BodiesToEntities.insert(std::make_pair(body, entity));
|
||||
}
|
||||
|
||||
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
||||
{
|
||||
if (m_ContactListener != nullptr) {
|
||||
delete m_ContactListener;
|
||||
m_ContactListener = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,12 @@
|
||||
#include "Transform/TransformSystem.h"
|
||||
#include "Core/World.h"
|
||||
|
||||
void dd::Systems::TransformSystem::Update(double dt)
|
||||
{
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
glm::vec3 dd::Systems::TransformSystem::AbsolutePosition(EntityID entity)
|
||||
{
|
||||
glm::vec3 absPosition;
|
||||
|
||||
Reference in New Issue
Block a user