diff --git a/assets/Textures/Ball.png b/assets/Textures/Ball.png index f43f7f8..6c82961 100644 Binary files a/assets/Textures/Ball.png and b/assets/Textures/Ball.png differ diff --git a/include/Core/Engine.h b/include/Core/Engine.h old mode 100644 new mode 100755 index 1df8465..3c67ba0 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -81,6 +81,7 @@ public: m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); m_World->SystemFactory.Register( [this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); }); m_World->AddSystem(); @@ -97,22 +98,13 @@ public: //TODO: Remove tobias light-test code. - { - auto t_BrickWall = m_World->CreateEntity(); - auto transform = m_World->AddComponent(t_BrickWall); - transform->Position = glm::vec3(0.f, 0.f, -10.f); - auto sprite = m_World->AddComponent(t_BrickWall); - //TODO: Rename SpriteFile to DiffuseTexture or similar. - sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png"; - sprite->NormalTexture = "Textures/Test/Brick_Normal.png"; - sprite->SpecularTexture = "Textures/Test/Brick_Specular.png"; - } - + /*{ + }*/ { auto ent = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(0.5f, 0.f, -10.f); + transform->Position = glm::vec3(0.5f, 0.f, -9.9f); transform->Scale = glm::vec3(1.f, 1.f, 1.f); std::shared_ptr sprite = m_World->AddComponent(ent); @@ -136,11 +128,33 @@ public: m_EventBroker->Publish(e); } + { + auto t_BrickWall = m_World->CreateEntity(); + auto transform = m_World->AddComponent(t_BrickWall); + transform->Position = glm::vec3(0.f, 0.f, -10.f); + auto sprite = m_World->AddComponent(t_BrickWall); + //TODO: Rename SpriteFile to DiffuseTexture or similar. + sprite->SpriteFile = "Textures/Ball.png"; + sprite->NormalTexture = "Textures/Test/Brick_Normal.png"; + sprite->SpecularTexture = "Textures/Test/Brick_Specular.png"; + } + + { + auto t_BrickWall = m_World->CreateEntity(); + auto transform = m_World->AddComponent(t_BrickWall); + transform->Position = glm::vec3(0.4f, 0.f, -9.5f); + auto sprite = m_World->AddComponent(t_BrickWall); + //TODO: Rename SpriteFile to DiffuseTexture or similar. + sprite->SpriteFile = "Textures/Ball.png"; + sprite->NormalTexture = "Textures/Test/Brick_Normal.png"; + sprite->SpecularTexture = "Textures/Test/Brick_Specular.png"; + } + { auto topWall = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(topWall); - transform->Position = glm::vec3(0.f, 5.f, -10.f); - transform->Scale = glm::vec3(15.f, 0.5f, 1.f); + transform->Position = glm::vec3(0.f, 6.f, -10.f); + transform->Scale = glm::vec3(18.f, 0.5f, 1.f); std::shared_ptr sprite = m_World->AddComponent(topWall); sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; @@ -342,7 +356,7 @@ public: glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position) * glm::toMat4(orientation2D) * glm::scale(absoluteTransform.Scale); - EnqueueSprite(texturediff, texturenorm, texturespec, modelMatrix, spriteComponent->Color); + EnqueueSprite(texturediff, texturenorm, texturespec, modelMatrix, spriteComponent->Color, absoluteTransform.Position.z); } } @@ -365,13 +379,15 @@ public: job.EndIndex = texGroup.EndIndex; job.ModelMatrix = modelMatrix; job.Color = color; + //TODO: This Depth is probably wrong. + job.Depth = (glm::vec4(1,1,1,0) * modelMatrix).z; m_RendererQueue.Deferred.Add(job); } } // TODO: Get this out of engine.h - void EnqueueSprite(Texture* texture, Texture* normalTexture, Texture* specularTexture, glm::mat4 modelMatrix, glm::vec4 color) + void EnqueueSprite(Texture* texture, Texture* normalTexture, Texture* specularTexture, glm::mat4 modelMatrix, glm::vec4 color, float depth) { SpriteJob job; job.TextureID = texture->ResourceID; @@ -380,8 +396,10 @@ public: job.SpecularTexture = *specularTexture; job.ModelMatrix = modelMatrix; job.Color = color; + job.Depth = depth; - m_RendererQueue.Deferred.Add(job); + + m_RendererQueue.Forward.Add(job); } void EnqueuePointLight(glm::vec3 position, glm::vec3 diffuseColor, glm::vec3 specularColor, float radius) diff --git a/include/Core/Renderer.h b/include/Core/Renderer.h index ac84ef7..241366d 100755 --- a/include/Core/Renderer.h +++ b/include/Core/Renderer.h @@ -93,6 +93,8 @@ private: void CreateBuffers(); GLuint CreateQuad(); + static bool DepthSort(const std::shared_ptr &i, const std::shared_ptr &j) { return (i->Depth < j->Depth); } + void DrawDeferred(RenderQueue &objects, RenderQueue &lights); void DrawForward(RenderQueue &objects, RenderQueue &lights); void DrawScene(RenderQueue &objects, ShaderProgram &program); diff --git a/include/Game/CBall.h b/include/Game/CBall.h index d5c652f..e6b96c1 100644 --- a/include/Game/CBall.h +++ b/include/Game/CBall.h @@ -15,7 +15,7 @@ namespace Components struct Ball : Component { - + int number = 0; }; } diff --git a/include/Game/CLife.h b/include/Game/CLife.h new file mode 100644 index 0000000..b5b6a92 --- /dev/null +++ b/include/Game/CLife.h @@ -0,0 +1,26 @@ +// +// Created by Administrator on 2015-09-15. +// + +#ifndef DAYDREAM_CLIFE_H +#define DAYDREAM_CLIFE_H + +#include "Core/Component.h" + +namespace dd +{ + +namespace Components +{ + +struct Life : Component { + + int number = 0; + +}; + +} + +} + +#endif //DAYDREAM_CLIFE_H diff --git a/include/Game/ELifeLost.h b/include/Game/ELifeLost.h new file mode 100644 index 0000000..b77eee1 --- /dev/null +++ b/include/Game/ELifeLost.h @@ -0,0 +1,27 @@ +// +// Created by Administrator on 2015-09-15. +// + +#ifndef DAYDREAM_ELIFELOST_H +#define DAYDREAM_ELIFELOST_H + +#include "Core/EventBroker.h" +#include "Core/Entity.h" + +namespace dd +{ + +namespace Events +{ + +struct LifeLost : Event +{ + EntityID entity; + +}; + +} + +} + +#endif //DAYDREAM_ELIFELOST_H diff --git a/include/Game/LevelSystem.h b/include/Game/LevelSystem.h index 6269974..ad04ffa 100644 --- a/include/Game/LevelSystem.h +++ b/include/Game/LevelSystem.h @@ -9,13 +9,17 @@ #include "Core/CTransform.h" #include "Rendering/CSprite.h" #include "Game/CBrick.h" -#include "Physics/EContact.h" #include "Core/EventBroker.h" #include "Core/World.h" #include "Game/CBall.h" +#include "Game/CLife.h" #include "Game/EStageCleared.h" +#include "Game/ELifeLost.h" #include "Physics/CBoxShape.h" #include "Physics/CPhysics.h" +#include "Physics/CCircleShape.h" +#include "Physics/ESetImpulse.h" +#include "Physics/EContact.h" #include #include #include @@ -47,6 +51,7 @@ public: void Initialize() override; void CreateBasicLevel(int, int, glm::vec2, int); + void CreateLife(int); void SaveLevel(int, int, glm::vec2, int); // Shouldn't be here, but I'm experimenting. void LoadLevel(char[20]); void CreateBrick(int, int, glm::vec2, int, int); @@ -56,12 +61,17 @@ public: void EndLevel(); - void Update(double dt); + void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + int lives = 3; + int pastLives = 3; private: dd::EventRelay m_EContact; + dd::EventRelay m_ELifeLost; bool OnContact(const dd::Events::Contact &event); + bool LifeLost(const dd::Events::LifeLost &event); bool m_Initialized = false; @@ -70,6 +80,8 @@ private: int tLines = 8; glm::vec2 tSpaceBetweenBricks = glm::vec2(2, 0.5); int tSpaceToEdge = 0; + + EntityID entityToRemove = NULL; }; } diff --git a/include/Game/PadSystem.h b/include/Game/PadSystem.h index adce7a0..91a23e7 100644 --- a/include/Game/PadSystem.h +++ b/include/Game/PadSystem.h @@ -7,19 +7,22 @@ #include "Core/InputController.h" #include "Core/System.h" +#include "Core/Component.h" +#include "Core/CTransform.h" #include "Core/EventBroker.h" #include "Core/EKeyDown.h" #include "Core/EKeyUp.h" #include "Input/EBindKey.h" #include "Physics/EContact.h" -#include "Core/CTransform.h" #include "Physics/CBoxShape.h" #include "Physics/CPhysics.h" +#include "Physics/CCircleShape.h" +#include "Physics/ESetImpulse.h" #include "Rendering/CSprite.h" #include "Game/CBall.h" -#include "Core/Component.h" -#include "Physics/ESetImpulse.h" -#include "Physics/CCircleShape.h" +#include "Game/ELifeLost.h" + + namespace dd @@ -37,19 +40,26 @@ public: void Initialize() override; void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; private: dd::EventRelay m_EKeyDown; dd::EventRelay m_EKeyUp; dd::EventRelay m_EContact; + dd::EventRelay m_ELifeLost; bool OnKeyDown(const dd::Events::KeyDown &event); bool OnKeyUp(const dd::Events::KeyUp &event); bool OnContact(const dd::Events::Contact &event); + bool LifeLost(const dd::Events::LifeLost &event); + EntityID removeThisObject = NULL; + float slowdownModifier = 5.f; + float accelerationSpeed = 80.f; + float maxSpeed = 40.f; @@ -59,7 +69,7 @@ private: EntityID ent = 0; Components::Transform* transform; glm::vec3 acceleration = glm::vec3(0.f, 0.f, 0.f); - bool left = false, right = false; + bool left = false, right = false, replaceBall = false; }; class PadSystem::PadSteeringInputController : InputController diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index 57bad34..4b19098 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -80,6 +80,7 @@ private: e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()]; e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()]; + m_PhysicsSystem->EventBroker->Publish(e); } void EndContact(b2Contact* contact) diff --git a/src/game/CMakeLists.txt b/src/game/CMakeLists.txt index 0577ab1..3c79b79 100755 --- a/src/game/CMakeLists.txt +++ b/src/game/CMakeLists.txt @@ -75,8 +75,7 @@ set(SOURCE_FILES ${SOURCE_FILES_Rendering} ${SOURCE_FILES_Transform} ${SOURCE_FILES_Physics} - ${SOURCE_FILES_Game} -) + ${SOURCE_FILES_Game} ../../include/Game/CLife.h ../../include/Game/ELifeLost.h) if(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") diff --git a/src/game/Core/Renderer.cpp b/src/game/Core/Renderer.cpp index 59c08b1..4864bc6 100755 --- a/src/game/Core/Renderer.cpp +++ b/src/game/Core/Renderer.cpp @@ -207,7 +207,9 @@ void dd::Renderer::CreateBuffers() void dd::Renderer::Draw(RenderQueueCollection& rq) { - DrawDeferred(rq.Deferred, rq.Lights); + + //DrawDeferred(rq.Deferred, rq.Lights); + rq.Forward.Jobs.sort(dd::Renderer::DepthSort); DrawForward(rq.Forward, rq.Lights); // Finally: Draw the deferred+forward combined texture to the screen @@ -276,14 +278,20 @@ void dd::Renderer::DrawDeferred(RenderQueue &objects, RenderQueue &lights) void dd::Renderer::DrawForward(RenderQueue &objects, RenderQueue &lights) { + // Forward-render semi-transparent objects on top of the current framebuffer glDisable(GL_CULL_FACE); glCullFace(GL_BACK); glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); + //glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD); glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE); + glBindFramebuffer(GL_FRAMEBUFFER, m_fbDeferred3); + glClearColor(1, 0.9, 0.8f, 1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + m_spForward->Bind(); DrawScene(objects, *m_spForward); } @@ -304,10 +312,13 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program) 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)); + glUniform3fv(glGetUniformLocation(shaderProgramHandle, "LightPosition"), 1, glm::value_ptr(glm::vec3(0.f,0.f,-9.f))); + glUniform3fv(glGetUniformLocation(shaderProgramHandle, "LightSpecular"), 1, glm::value_ptr(glm::vec3(1.f))); + glUniform3fv(glGetUniformLocation(shaderProgramHandle, "LightDiffuse"), 1, glm::value_ptr(glm::vec3(1.f))); + glUniform1f(glGetUniformLocation(shaderProgramHandle, "LightRadius"), 40.0f); 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) { diff --git a/src/game/Core/Shaders/Deferred/3/Fragment.glsl b/src/game/Core/Shaders/Deferred/3/Fragment.glsl index 27e9c62..c08e74d 100755 --- a/src/game/Core/Shaders/Deferred/3/Fragment.glsl +++ b/src/game/Core/Shaders/Deferred/3/Fragment.glsl @@ -38,7 +38,7 @@ void main() //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(1.f, 1.f, 1.f, 0.0) + LightingTexel); - //frag_Diffuse = DiffuseTexel; + //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); } \ No newline at end of file diff --git a/src/game/Core/Shaders/Forward/Fragment.glsl b/src/game/Core/Shaders/Forward/Fragment.glsl index 144062f..dc9d544 100755 --- a/src/game/Core/Shaders/Forward/Fragment.glsl +++ b/src/game/Core/Shaders/Forward/Fragment.glsl @@ -46,7 +46,7 @@ in VertexData vec4 BoneWeights2; } Input; -out vec4 FragmentColor; +out vec4 frag_Diffuse; vec4 phong(vec3 position, vec3 normal, vec3 specular, float specularExponent) { @@ -83,7 +83,8 @@ void main() vec4 specularTexel = texture(SpecularMap, Input.TextureCoord); vec4 diffuseTexel = texture(DiffuseTexture, Input.TextureCoord); - - FragmentColor = phong(Input.Position, normalize(normal), specularTexel.rgb, specularTexel.a); - //FragmentColor = diffuseTexel; + vec4 La = vec4(0.3, 0.3, 0.3, 1.0); + vec4 light = phong(Input.Position, normalize(normal), specularTexel.rgb, specularTexel.a); + //frag_Diffuse = light; + frag_Diffuse = diffuseTexel; } \ No newline at end of file diff --git a/src/game/Core/Shaders/PureForward/Fragment.glsl b/src/game/Core/Shaders/PureForward/Fragment.glsl new file mode 100755 index 0000000..a0ec258 --- /dev/null +++ b/src/game/Core/Shaders/PureForward/Fragment.glsl @@ -0,0 +1,89 @@ +/* + This file is part of Daydream Engine. + Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung + + Daydream Engine is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Daydream Engine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with Daydream Engine. If not, see . +*/ + +#version 440 + +layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D NormalMap; +layout (binding=2) uniform sampler2D SpecularMap; + +uniform mat4 MVP; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform vec3 LightPosition; +uniform float LightRadius; +uniform vec3 LightSpecular; +uniform vec3 LightDiffuse; + +in VertexData +{ + vec3 Position; + vec3 Normal; + vec3 Tangent; + vec3 BiTangent; + vec2 TextureCoord; + vec4 DiffuseColor; + vec4 SpecularColor; + vec4 BoneIndices1; + vec4 BoneIndices2; + vec4 BoneWeights1; + vec4 BoneWeights2; +} Input; + +out vec4 frag_Diffuse; + +vec4 phong(vec3 position, vec3 normal, vec3 specular, float specularExponent) +{ + // Diffuse + vec3 lightPos = vec3(V * vec4(LightPosition, 1.0)); + vec3 distanceToLight = lightPos - position; + vec3 directionToLight = normalize(distanceToLight); + float dotProd = dot(directionToLight, normal); + dotProd = max(dotProd, 0.0); + vec3 Idiffuse = LightDiffuse * dotProd; + + // Specular + //vec3 reflection = reflect(-directionToLight, normal); + vec3 surfaceToViewer = normalize(-position); + vec3 halfWay = normalize(surfaceToViewer + directionToLight); + float dotSpecular = max(dot(halfWay, normal), 0.0); + float specularFactor = pow(dotSpecular, specularExponent); + vec3 Ispecular = specular * LightSpecular * specularFactor; + + //Attenuation + float dist = distance(lightPos, position); + //float attenuation = 1.0 - pow(dist / LightRadius, 2); + float attenuation = pow(max(0.0f, 1.0 - (dist / LightRadius)), 2); + + return vec4((Idiffuse + Ispecular) * attenuation, 1.0); +} + +void main() +{ + vec4 normalTexel = texture(NormalMap, Input.TextureCoord); + mat3 TBN = mat3(Input.Tangent, Input.BiTangent, Input.Normal); + vec3 normal = normalize(vec4(TBN * normalTexel.xyz, 0.0).xyz); + + vec4 specularTexel = texture(SpecularMap, Input.TextureCoord); + + vec4 diffuseTexel = texture(DiffuseTexture, Input.TextureCoord); + + //frag_Diffuse = phong(Input.Position, normalize(normal), specularTexel.rgb, specularTexel.a); + frag_Diffuse = diffuseTexel; +} \ No newline at end of file diff --git a/src/game/Core/Shaders/PureForward/Vertex.glsl b/src/game/Core/Shaders/PureForward/Vertex.glsl new file mode 100755 index 0000000..64a9e26 --- /dev/null +++ b/src/game/Core/Shaders/PureForward/Vertex.glsl @@ -0,0 +1,83 @@ +/* + This file is part of Daydream Engine. + Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung + + Daydream Engine is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Daydream Engine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with Daydream Engine. If not, see . +*/ + +#version 440 + +uniform mat4 MVP; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform mat4 Bones[100]; + +layout (location = 0) in vec3 Position; +layout (location = 1) in vec3 Normal; +layout (location = 2) in vec3 Tangent; +layout (location = 3) in vec3 BiTangent; +layout (location = 4) in vec2 TextureCoord; +layout (location = 5) in vec4 DiffuseColor; +layout (location = 6) in vec4 SpecularColor; +layout (location = 7) in vec4 BoneIndices1; +layout (location = 8) in vec4 BoneIndices2; +layout (location = 9) in vec4 BoneWeights1; +layout (location = 10) in vec4 BoneWeights2; + +out VertexData +{ + vec3 Position; + vec3 Normal; + vec3 Tangent; + vec3 BiTangent; + vec2 TextureCoord; + vec4 DiffuseColor; + vec4 SpecularColor; + vec4 BoneIndices1; + vec4 BoneIndices2; + vec4 BoneWeights1; + vec4 BoneWeights2; +} Output; + +void main() +{ + mat4 boneTransform = mat4(1); + if (length(BoneWeights1 + BoneWeights2) > 0) { + boneTransform = BoneWeights1[0] * Bones[int(BoneIndices1[0])] + + BoneWeights1[1] * Bones[int(BoneIndices1[1])] + + BoneWeights1[2] * Bones[int(BoneIndices1[2])] + + BoneWeights1[3] * Bones[int(BoneIndices1[3])] + + BoneWeights2[0] * Bones[int(BoneIndices2[0])] + + BoneWeights2[1] * Bones[int(BoneIndices2[1])] + + BoneWeights2[2] * Bones[int(BoneIndices2[2])] + + BoneWeights2[3] * Bones[int(BoneIndices2[3])]; + } + + //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; + Output.BiTangent = BiTangent; + Output.TextureCoord = TextureCoord; + Output.DiffuseColor = DiffuseColor; + Output.SpecularColor = SpecularColor; + Output.BoneIndices1 = BoneIndices1; + Output.BoneIndices2 = BoneIndices2; + Output.BoneWeights1 = BoneWeights1; + Output.BoneWeights2 = BoneWeights2; +} \ No newline at end of file diff --git a/src/game/Game/LevelSystem.cpp b/src/game/Game/LevelSystem.cpp index 68dce72..7499d34 100644 --- a/src/game/Game/LevelSystem.cpp +++ b/src/game/Game/LevelSystem.cpp @@ -10,15 +10,76 @@ void dd::Systems::LevelSystem::Initialize() { EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact); + EVENT_SUBSCRIBE_MEMBER(m_ELifeLost, LevelSystem::LifeLost); + + for (int i = 0; i < lives; i++) + { + CreateLife(i); + } return; } -void dd::Systems::LevelSystem::Update(double dt) { +void dd::Systems::LevelSystem::CreateLife(int number) +{ + auto life = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(life); + transform->Position = glm::vec3(-11.f + number, -8.f, -10.f); + transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f); + + std::shared_ptr lifeNr = m_World->AddComponent(life); + lifeNr->number = number; + + std::shared_ptr sprite = m_World->AddComponent(life); + sprite->SpriteFile = "Textures/Ball.png"; + + m_World->CommitEntity(life); +} + +void dd::Systems::LevelSystem::Update(double dt) +{ if (!m_Initialized) { CreateBasicLevel(tRows, tLines, tSpaceBetweenBricks, tSpaceToEdge); m_Initialized = true; } + + if (entityToRemove != NULL) + { + //m_World->RemoveEntity(entityToRemove); + entityToRemove = NULL; + } +} + +void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto ball = m_World->GetComponent(entity); + + if (ball != NULL) + { + auto transformBall = m_World->GetComponent(entity); + if (transformBall->Position.y < -10) + { + if (lives == pastLives) + { + Events::LifeLost e; + e.entity = entity; + EventBroker->Publish(e); + return; + } + } + } + + if (lives != pastLives) + { + auto life = m_World->GetComponent(entity); + if (life != NULL) + { + if (life->number + 1 == pastLives) { + m_World->RemoveEntity(entity); + pastLives = lives; + } + } + } } void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, int spaceToEdge) @@ -55,7 +116,6 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe 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; } @@ -82,8 +142,6 @@ void dd::Systems::LevelSystem::OnEntityRemoved(EntityID entity) 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(entityBrick); if (brick == NULL) @@ -91,49 +149,17 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event) return false; } EntityID entityBall = event.Entity2; - //EntityID entity1 = event.Entity1; - //EntityID entity2 = event.Entity2; - //auto brick = m_World->GetComponent(entity1); - //auto ball = m_World->GetComponent(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(entity2); - if (brick != NULL) - { - std::cout << "Entity2 is a brick!" << std::endl; - entityBrick = entity2; - } + m_World->RemoveEntity(entityBrick); - ball = m_World->GetComponent(entity1); - if (ball != NULL) - { - std::cout << "Entity1 is a ball!" << std::endl; - entityBall = entity1; - } - } + return true; +} - if (entityBrick != NULL && entityBall != NULL) - {*/ - //std::cout << "We're destroying!" << std::endl; - m_World->RemoveEntity(entityBrick); +bool dd::Systems::LevelSystem::LifeLost(const dd::Events::LifeLost &event) +{ + lives--; - return true; - //} - - return false; + return true; } void dd::Systems::LevelSystem::EndLevel() diff --git a/src/game/Game/PadSystem.cpp b/src/game/Game/PadSystem.cpp index a465b8b..0242537 100644 --- a/src/game/Game/PadSystem.cpp +++ b/src/game/Game/PadSystem.cpp @@ -24,10 +24,45 @@ void dd::Systems::PadSystem::Initialize() 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_ELifeLost, PadSystem::LifeLost); return; } +void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto ball = m_World->GetComponent(entity); + if (ball != NULL) { + if (replaceBall == true) { + replaceBall = false; + + auto transformEntity = m_World->GetComponent(entity); + transformEntity->Position = glm::vec3(20, 20, -10); + + //Temporary. Create new ball. + auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(0.5f, 0.f, -10.f);; + transform->Scale = glm::vec3(1.f, 1.f, 1.f); + std::shared_ptr sprite = m_World->AddComponent(ent); + sprite->SpriteFile = "Textures/Ball.png"; + std::shared_ptr circleShape = m_World->AddComponent(ent); + std::shared_ptr cball = m_World->AddComponent(ent); + std::shared_ptr physics = m_World->AddComponent(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); + } + } +} + void dd::Systems::PadSystem::Update(double dt) { if (ent == 0) { @@ -40,25 +75,25 @@ void dd::Systems::PadSystem::Update(double dt) } } - if (transform->Velocity.x < -400.f) + if (transform->Velocity.x < -maxSpeed) { - transform->Velocity.x = -400.f; + transform->Velocity.x = -maxSpeed; } - else if (transform->Velocity.x > 400.f) + else if (transform->Velocity.x > maxSpeed) { - transform->Velocity.x = 400.f; + transform->Velocity.x = maxSpeed; } transform->Position += transform->Velocity * (float)dt; transform->Velocity += acceleration * (float)dt; - transform->Velocity -= transform->Velocity * (0.9f * (float)dt); + transform->Velocity -= transform->Velocity * slowdownModifier * (float)dt; if (left) { - acceleration.x = -20.f; + acceleration.x = -accelerationSpeed; } else if (right) { - acceleration.x = 20.f; + acceleration.x = accelerationSpeed; } else { @@ -85,12 +120,16 @@ bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event) //acceleration.x = -0.01f; left = true; } - else if (val = 262) + else if (val == 262) { //std::cout << "Right!" << std::endl; //acceleration.x = 0.01f; right = true; } + else if (val == 82) + { + replaceBall = true; + } return true; } @@ -109,7 +148,7 @@ bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event) { left = false; } - else if (val = 262) + else if (val == 262) { right = false; } @@ -133,17 +172,32 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event) auto transformBall = m_World->GetComponent(entityBall); auto transformPad = m_World->GetComponent(entityPad); - float movementMultiplier = 2.f; + float movementMultiplier = 4.f; - float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier; - float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f; + float whatX = transformBall->Position.x - transformPad->Position.x; + float movementX; + if (whatX > 0) + { + movementX = movementMultiplier * whatX; + //std::cout << "Right!" << std::endl; + } + else + { + movementX = movementMultiplier * whatX; + //std::cout << "Left!" << std::endl; + } - std::cout << movementX << " " << movementY << std::endl; + //float movementY = 1; + + //float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier; + float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2) + 1; + + //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 transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.1f, -10.f); + transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.01f, -10.f); transform->Scale = glm::vec3(1.f, 1.f, 1.f); std::shared_ptr sprite = m_World->AddComponent(ent); sprite->SpriteFile = "Textures/Ball.png"; @@ -158,10 +212,16 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event) Events::SetImpulse e; e.Entity = ent; e.Impulse = glm::vec2(movementX, movementY); - e.Point = glm::vec2(event.ContactPoint); + e.Point = glm::vec2(transform->Position.x, transform->Position.y); EventBroker->Publish(e); } +bool dd::Systems::PadSystem::LifeLost(const dd::Events::LifeLost &event) +{ + replaceBall = true; + return true; +} + bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event) { std::string command = event.Command; diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index 3d244a7..28ffc37 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -49,6 +49,8 @@ void dd::Systems::PhysicsSystem::Update(double dt) b2Body* body = i.second; auto transformComponent = m_World->GetComponent(entity); + if (! transformComponent) + continue; if (m_World->GetEntityParent(entity) == 0) { @@ -85,7 +87,8 @@ void dd::Systems::PhysicsSystem::Update(double dt) b2Body* body = i.second; auto transformComponent = m_World->GetComponent(entity); - + if (! transformComponent) + continue; if (m_World->GetEntityParent(entity) == 0) { b2Vec2 position = body->GetPosition(); @@ -119,10 +122,13 @@ void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity) { b2Body* body = m_EntitiesToBodies[entity]; - m_EntitiesToBodies.erase(entity); - m_BodiesToEntities.erase(body); + if (body != nullptr) { + m_EntitiesToBodies.erase(entity); + m_BodiesToEntities.erase(body); + + m_PhysicsWorld->DestroyBody(body); + } - m_PhysicsWorld->DestroyBody(body); }