diff --git a/Escape-the-Dawn/Camera.cpp b/Escape-the-Dawn/Camera.cpp index 542864d..51977c3 100644 --- a/Escape-the-Dawn/Camera.cpp +++ b/Escape-the-Dawn/Camera.cpp @@ -78,3 +78,21 @@ void Camera::UpdateViewMatrix() { m_ViewMatrix = glm::translate(glm::toMat4(m_Orientation), -m_Position); } + +void Camera::FOV(float val) +{ + m_FOV = val; + UpdateProjectionMatrix(); +} + +void Camera::NearClip(float val) +{ + m_NearClip = val; + UpdateProjectionMatrix(); +} + +void Camera::FarClip(float val) +{ + m_FarClip = val; + UpdateProjectionMatrix(); +} \ No newline at end of file diff --git a/Escape-the-Dawn/Camera.h b/Escape-the-Dawn/Camera.h index a0bfe63..a15a495 100644 --- a/Escape-the-Dawn/Camera.h +++ b/Escape-the-Dawn/Camera.h @@ -1,13 +1,7 @@ #ifndef Camera_h__ #define Camera_h__ -#define GLM_FORCE_RADIANS -#include -#include -#include -#include -#include -#include +#include "GLM.h" class Camera { @@ -38,13 +32,13 @@ public: void ViewMatrix(glm::mat4 val) { m_ViewMatrix = val; } float FOV() const { return m_FOV; } - void FOV(float val) { m_FOV = val; } + void FOV(float val); float NearClip() const { return m_NearClip; } - void NearClip(float val) { m_NearClip = val; } + void NearClip(float val); float FarClip() const { return m_FarClip; } - void FarClip(float val) { m_FarClip = val; } + void FarClip(float val); private: void UpdateProjectionMatrix(); @@ -54,7 +48,6 @@ private: float m_AspectRatio; float m_NearClip; float m_FarClip; - glm::vec3 m_Position; glm::quat m_Orientation; diff --git a/Escape-the-Dawn/Component.h b/Escape-the-Dawn/Component.h index 21d59f6..adaa4f7 100644 --- a/Escape-the-Dawn/Component.h +++ b/Escape-the-Dawn/Component.h @@ -1,6 +1,8 @@ #ifndef Component_h__ #define Component_h__ +#include "GLM.h" + #include "Factory.h" #include "Entity.h" diff --git a/Escape-the-Dawn/Components/Bounds.h b/Escape-the-Dawn/Components/Bounds.h index 3845485..f05c6d8 100644 --- a/Escape-the-Dawn/Components/Bounds.h +++ b/Escape-the-Dawn/Components/Bounds.h @@ -2,7 +2,6 @@ #define Components_Bounds_h__ #include "Component.h" -#include namespace Components { diff --git a/Escape-the-Dawn/Components/Camera.h b/Escape-the-Dawn/Components/Camera.h index 62d6905..2cd379d 100644 --- a/Escape-the-Dawn/Components/Camera.h +++ b/Escape-the-Dawn/Components/Camera.h @@ -8,6 +8,8 @@ namespace Components struct Camera : Component { + Camera() : FOV(glm::radians(45.f)), NearClip(0.1f), FarClip(100.f) { } + float FOV; float NearClip; float FarClip; diff --git a/Escape-the-Dawn/Components/Model.h b/Escape-the-Dawn/Components/Model.h index 11297da..dc58640 100644 --- a/Escape-the-Dawn/Components/Model.h +++ b/Escape-the-Dawn/Components/Model.h @@ -11,8 +11,10 @@ namespace Components struct Model : Component { - const char* ModelFile; + Model() : Visible(true) { } + std::string ModelFile; Color Color; + bool Visible; }; } diff --git a/Escape-the-Dawn/Components/Transform.h b/Escape-the-Dawn/Components/Transform.h index 5d20a67..8d0073f 100644 --- a/Escape-the-Dawn/Components/Transform.h +++ b/Escape-the-Dawn/Components/Transform.h @@ -2,7 +2,6 @@ #define Components_Transform_h__ #include "Component.h" -#include namespace Components { diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 9406458..7fd3220 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -112,6 +112,7 @@ + @@ -146,6 +147,7 @@ + @@ -165,6 +167,7 @@ + diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters index 1f764a8..84ffe0f 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters @@ -49,6 +49,9 @@ + + Systems + @@ -152,6 +155,10 @@ + + Systems + + diff --git a/Escape-the-Dawn/Frame.cpp b/Escape-the-Dawn/Frame.cpp index bf996ed..1c3c753 100644 --- a/Escape-the-Dawn/Frame.cpp +++ b/Escape-the-Dawn/Frame.cpp @@ -3,51 +3,74 @@ GUI::Frame::Frame() { rectangle = new Rectangle(); + texture = nullptr; } int GUI::Frame::GetWidth() { - return rectangle; + return rectangle->Width(); } int GUI::Frame::GetHeight() { - return rectangle.Height(); + return rectangle->Height(); } int GUI::Frame::GetX() { - return rectangle; + return rectangle->X(); } int GUI::Frame::GetY() { - return rectangle.Y(); + return rectangle->Y(); } -void GUI::Frame::SetWidth(int wid) +int GUI::Frame::GetRight() { - rectangle.width(wid); + return rectangle->Right(); } -void GUI::Frame::SetHeight(int hei) +int GUI::Frame::GetLeft() { - rectangle.height(hei); + return rectangle->Left(); } -void GUI::Frame::SetX(int xcord) +int GUI::Frame::GetBottom() { - rectangle.X(xcord) + return rectangle->Bottom(); } -void GUI::Frame::SetY(int ycord) +int GUI::Frame::GetTop() { - rectangle.Y(ycord) + return rectangle->Right(); } +void GUI::Frame::SetWidth(int _width) +{ + rectangle->Width(_width); +} + +void GUI::Frame::SetHeight(int _height) +{ + rectangle->Height(_height); +} + +void GUI::Frame::SetX(int _x) +{ + rectangle->X(_x); +} + +void GUI::Frame::SetY(int _y) +{ + rectangle->Y(_y); +} + + + Rectangle GUI::Frame::AbsouluteRectangle() { - + } bool GUI::Frame::Visible() @@ -85,8 +108,48 @@ void GUI::Frame::Update(double dt) } +void AddChild(GUI::Frame childFrame) +{ + childFrame.Parent = this; + if (!Children.Contains(childFrame)) + Children.Add(childFrame); +} + +/// +/// Remove a child frame from the frame. +/// +/// The child frame to remove. +void RemoveChild(GUI::Frame childFrame) +{ + if (Children.Contains(childFrame)) + Children.Remove(childFrame); +} + +/// +/// Destroys the frame and all its children. +/// +void Destroy() +{ + if (Parent != null) + Parent.RemoveChild(this); + + Manager.Remove(this); +} + +void GUI::Frame::SetTexture(std::string fileName) +{ + //texture == någonTextureIDunno; +} + void GUI::Frame::Draw() { - + if (Visible) + { + if (texture != nullptr) + { + //Drawshit + } + } } + */ \ No newline at end of file diff --git a/Escape-the-Dawn/Frame.h b/Escape-the-Dawn/Frame.h index 2e9a3f4..69dd6d7 100644 --- a/Escape-the-Dawn/Frame.h +++ b/Escape-the-Dawn/Frame.h @@ -1,4 +1,4 @@ -/*#ifndef Frame_h__ +#ifndef Frame_h__ #define Frame_h__ #include @@ -7,7 +7,7 @@ #include "GUIManager.h" #include "Texture.h" #include "Color.h" -//#include "Camera.h" +#include "Camera.h" namespace GUI { @@ -22,14 +22,19 @@ public: int GetHeight(); int GetX(); int GetY(); - void SetWidth(int wid); - void SetHeight(int hei); - void SetX( int xcord); - void SetY(int ycord); + int GetRight(); + int GetLeft(); + int GetBottom(); + int GetTop(); + void SetWidth(int _width); + void SetHeight(int _height); + void SetX( int _x); + void SetY(int _y); + void SetTexture(std::string fileName); Rectangle *rectangle; Rectangle AbsouluteRectangle(); - Texture texture; - Color color; + Texture* texture; + Color* color; bool _Visible; bool Visible(); int layer; @@ -48,4 +53,4 @@ protected: }; } -#endif*/ \ No newline at end of file +#endif \ No newline at end of file diff --git a/Escape-the-Dawn/GLM.h b/Escape-the-Dawn/GLM.h new file mode 100644 index 0000000..db5bcc1 --- /dev/null +++ b/Escape-the-Dawn/GLM.h @@ -0,0 +1,9 @@ +#define GLM_FORCE_RADIANS +#include +#include +#include +#include +#include +#include +#include +#include \ No newline at end of file diff --git a/Escape-the-Dawn/GUIManager.cpp b/Escape-the-Dawn/GUIManager.cpp index 40b50d3..4956638 100644 --- a/Escape-the-Dawn/GUIManager.cpp +++ b/Escape-the-Dawn/GUIManager.cpp @@ -2,7 +2,7 @@ int GUI::GUIManager::Add(Frame frame) { - //Frame.Add(frame); - //return CreateUID(); + Frame.Add(frame); + return CreateUID(); } */ \ No newline at end of file diff --git a/Escape-the-Dawn/GUIManager.h b/Escape-the-Dawn/GUIManager.h index 7b8434a..57b66a7 100644 --- a/Escape-the-Dawn/GUIManager.h +++ b/Escape-the-Dawn/GUIManager.h @@ -1,4 +1,4 @@ -/*#ifndef GUIManager_h__ +#ifndef GUIManager_h__ #define GUIManager_h__ #include "Frame.h" @@ -18,9 +18,8 @@ private: static int uidIndex; }; - } -#endif */ \ No newline at end of file +#endif \ No newline at end of file diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index dbbc64a..5bc99a9 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -5,14 +5,6 @@ void GameWorld::Initialize() { World::Initialize(); - AddSystem("LevelGenerationSystem"); - AddSystem("InputSystem"); - AddSystem("CollisionSystem"); - //AddSystem("ParticleSystem"); - AddSystem("PlayerSystem"); - AddSystem("SoundSystem"); - AddSystem("RenderSystem"); - std::shared_ptr transform; std::shared_ptr model; std::shared_ptr pointLight; @@ -22,11 +14,8 @@ void GameWorld::Initialize() std::shared_ptr soundEmitter; EntityID ent; - - - // Fucking lights - ent = CreateEntity(); + /*ent = CreateEntity(); transform = AddComponent(ent, "Transform"); transform->Position = glm::vec3(0.f, 7.f, 0.f); pointLight = AddComponent(ent, "PointLight"); @@ -61,7 +50,7 @@ void GameWorld::Initialize() pointLight->quadraticAttenuation = 0.f; pointLight->spotExponent = 0.0f; model = AddComponent(ent, "Model"); - model->ModelFile = "Models/sphere.obj"; + model->ModelFile = "Models/sphere.obj";*/ //ground ent = CreateEntity(); @@ -77,7 +66,7 @@ void GameWorld::Initialize() AddComponent(entcamera, "Input"); transform->Position = glm::vec3(0.f, 10.f, 14.f); camera = AddComponent(entcamera, "Camera"); - camera->FOV = 45.f; + camera->FOV = glm::radians(45.f); camera->FarClip = 1000.f; camera->NearClip = 0.01f; transform->Orientation = glm::angleAxis(glm::radians(15.0f),glm::vec3(1,0,0)); @@ -92,13 +81,6 @@ void GameWorld::Initialize() transform->Scale = glm::vec3(1.0f); model = AddComponent(m_Player, "Model"); model->ModelFile = "Models/ship.obj"; - pointLight = AddComponent(m_Player, "PointLight"); - pointLight->Specular = glm::vec3(1.0, 1.0, 1.0); - pointLight->Diffuse = glm::vec3(0.4, 0.4, 1.0); - pointLight->constantAttenuation = 0.f; - pointLight->linearAttenuation = 1.f; - pointLight->quadraticAttenuation = 0.f; - pointLight->spotExponent = 0.0f; AddComponent(m_Player, "Input"); collision = AddComponent(m_Player, "Collision"); collision->Phantom = false; @@ -106,20 +88,43 @@ void GameWorld::Initialize() bounds = AddComponent(m_Player, "Bounds"); bounds->Origin = glm::vec3(0, 0, 2.f); bounds->VolumeVector = glm::vec3(4.f, 0.7f, 1); - soundEmitter = AddComponent(m_Player, "SoundEmitter"); - soundEmitter->Loop = true; - //soundEmitter->MaxDistance = 10; - soundEmitter->ReferenceDistance = 10; - soundEmitter->Gain = 1; - soundEmitter->Pitch = 1; - soundEmitter->Path = "Sounds/hallelujah.wav"; - //GetSystem("SoundSystem")->PlaySound(soundEmitter); - - - - - - + // Player light + auto playerLight = CreateEntity(m_Player); + transform = AddComponent(playerLight, "Transform"); + transform->Position = glm::vec3(1.5f, 0.25f, 2.5f); + pointLight = AddComponent(playerLight, "PointLight"); + pointLight->Specular = glm::vec3(0.1f, 0.1f, 0.1f); + pointLight->Diffuse = glm::vec3(0.45f, 0.65f, 1.f); + pointLight->constantAttenuation = 0.f; + pointLight->linearAttenuation = 0.9f; + pointLight->quadraticAttenuation = 0.f; + pointLight->spotExponent = 0.0f; + bounds = AddComponent(playerLight, "Bounds"); + bounds->VolumeVector = glm::vec3(0.5f); + playerLight = CreateEntity(m_Player); + transform = AddComponent(playerLight, "Transform"); + transform->Position = glm::vec3(-1.5f, 0.25f, 2.5f); + pointLight = AddComponent(playerLight, "PointLight"); + pointLight->Specular = glm::vec3(0.1f, 0.1f, 0.1f); + pointLight->Diffuse = glm::vec3(0.45f, 0.65f, 1.f); + pointLight->constantAttenuation = 0.f; + pointLight->linearAttenuation = 0.9f; + pointLight->quadraticAttenuation = 0.f; + pointLight->spotExponent = 0.0f; + bounds = AddComponent(playerLight, "Bounds"); + bounds->VolumeVector = glm::vec3(0.5f); + playerLight = CreateEntity(m_Player); + transform = AddComponent(playerLight, "Transform"); + transform->Position = glm::vec3(0.0f, 2.0f, 2.5f); + pointLight = AddComponent(playerLight, "PointLight"); + pointLight->Specular = glm::vec3(0.1f, 0.1f, 0.1f); + pointLight->Diffuse = glm::vec3(0.45f, 0.65f, 1.f); + pointLight->constantAttenuation = 0.f; + pointLight->linearAttenuation = 1.0f; + pointLight->quadraticAttenuation = 0.f; + pointLight->spotExponent = 0.0f; + bounds = AddComponent(playerLight, "Bounds"); + bounds->VolumeVector = glm::vec3(0.5f); /*ent = CreateEntity(); transform = AddComponent(ent, "Transform"); @@ -164,5 +169,18 @@ void GameWorld::RegisterSystems() //m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); }); m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); + m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this); }); m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); }); +} + +void GameWorld::AddSystems() +{ + AddSystem("TransformSystem"); + AddSystem("LevelGenerationSystem"); + AddSystem("InputSystem"); + AddSystem("CollisionSystem"); + //AddSystem("ParticleSystem"); + AddSystem("PlayerSystem"); + AddSystem("SoundSystem"); + AddSystem("RenderSystem"); } \ No newline at end of file diff --git a/Escape-the-Dawn/GameWorld.h b/Escape-the-Dawn/GameWorld.h index d2ac780..a192608 100644 --- a/Escape-the-Dawn/GameWorld.h +++ b/Escape-the-Dawn/GameWorld.h @@ -4,6 +4,7 @@ #include "World.h" #include "Renderer.h" +#include "Systems/TransformSystem.h" #include "Systems/CollisionSystem.h" #include "Systems/InputSystem.h" #include "Systems/LevelGenerationSystem.h" @@ -36,9 +37,10 @@ public: void Initialize(); void RegisterSystems() override; + void AddSystems() override; void RegisterComponents() override; - void Update(double dt) ; + void Update(double dt); private: std::shared_ptr m_Renderer; diff --git a/Escape-the-Dawn/Model.h b/Escape-the-Dawn/Model.h index 716d1c2..2888f95 100644 --- a/Escape-the-Dawn/Model.h +++ b/Escape-the-Dawn/Model.h @@ -9,10 +9,7 @@ #include #include "OpenGL.h" -#include -#include -#include -#include +#include "GLM.h" #include #include diff --git a/Escape-the-Dawn/Models/plane.mtl b/Escape-the-Dawn/Models/plane.mtl index 2961836..28df6eb 100644 --- a/Escape-the-Dawn/Models/plane.mtl +++ b/Escape-the-Dawn/Models/plane.mtl @@ -9,4 +9,4 @@ Ks 0.500000 0.500000 0.500000 Ni 1.000000 d 1.000000 illum 2 -map_Kd ObstacleTexture.png +map_Kd planetexture.png diff --git a/Escape-the-Dawn/Models/planetexture.png b/Escape-the-Dawn/Models/planetexture.png new file mode 100644 index 0000000..6766b13 Binary files /dev/null and b/Escape-the-Dawn/Models/planetexture.png differ diff --git a/Escape-the-Dawn/OpenGL.h b/Escape-the-Dawn/OpenGL.h index dec332d..2e34a34 100644 --- a/Escape-the-Dawn/OpenGL.h +++ b/Escape-the-Dawn/OpenGL.h @@ -1,5 +1,4 @@ #include #define GLFW_INCLUDE_GLU #include -#include -#define GLM_FORCE_RADIANS \ No newline at end of file +#include \ No newline at end of file diff --git a/Escape-the-Dawn/Rectangle.h b/Escape-the-Dawn/Rectangle.h index 2f6d0cd..d866955 100644 --- a/Escape-the-Dawn/Rectangle.h +++ b/Escape-the-Dawn/Rectangle.h @@ -22,6 +22,14 @@ public: int Y() const { return y; } void Y(int val) { y = val; } + + int Left() const { return x; } + + int Right() const { return x+width; } + + int Top() const { return y; } + + int Bottom() const { return y+height; } }; Rectangle::Rectangle() diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index 1876317..39e3b2c 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -14,7 +14,7 @@ Renderer::Renderer() #endif m_ShadowMapRes = 2048*2; - m_SunPosition = glm::vec3(0, 0.3f, 10); + m_SunPosition = glm::vec3(0, 1.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); m_SunProjection = glm::ortho(-200, 200, -10, 100, -800, 800); Lights = 0; @@ -237,7 +237,11 @@ void Renderer::DrawScene() { Model* model; glm::mat4 modelMatrix; - std::tie(model, modelMatrix) = tuple; + bool visible; + std::tie(model, modelMatrix, visible) = tuple; + if (!visible) + continue; + MVP = cameraMatrix * modelMatrix; depthMVP = depthCameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); @@ -288,7 +292,8 @@ void Renderer::DrawShadowMap() { Model* model; glm::mat4 modelMatrix; - std::tie(model, modelMatrix) = tuple; + bool visible; + std::tie(model, modelMatrix, visible) = tuple; MVP = depthCamera * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramShadows.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); @@ -317,26 +322,26 @@ void Renderer::DrawDebugShadowMap() void Renderer::DrawModels(ShaderProgram &shader) { - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + /*glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 MVP; for (auto tuple : ModelsToRender) { - Model* model; - glm::mat4 modelMatrix; - std::tie(model, modelMatrix) = tuple; + Model* model; + glm::mat4 modelMatrix; + std::tie(model, modelMatrix) = tuple; - MVP = cameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); - glBindVertexArray(model->VAO); - for (auto texGroup : model->TextureGroups) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texGroup.Texture->texture); - glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); - } + MVP = cameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glBindVertexArray(model->VAO); + for (auto texGroup : model->TextureGroups) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texGroup.Texture->texture); + glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); } + }*/ } void Renderer::DrawText() @@ -349,11 +354,11 @@ void Renderer::AddTextToDraw() //Add to draw shit vector } -void Renderer::AddModelToDraw(std::shared_ptr model, glm::vec3 position, glm::quat orientation, glm::vec3 scale) +void Renderer::AddModelToDraw(std::shared_ptr model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible) { glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); // You can now use ModelMatrix to build the MVP matrix - ModelsToRender.push_back(std::make_tuple(model.get(), modelMatrix)); + ModelsToRender.push_back(std::make_tuple(model.get(), modelMatrix, visible)); } void Renderer::AddPointLightToDraw( diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index 3bb7ff6..f140c4f 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -8,12 +8,7 @@ #include #include "OpenGL.h" -#include -#include -#include -#include -#include -#include +#include "GLM.h" #include "glerror.h" #include "Camera.h" @@ -31,7 +26,7 @@ public: int HEIGHT, WIDTH; - std::list> ModelsToRender; + std::list> ModelsToRender; int Lights; std::vector Light_position; std::vector Light_specular; @@ -48,7 +43,7 @@ public: void Draw(double dt); void DrawText(); - void AddModelToDraw(std::shared_ptr model, glm::vec3 position, glm::quat orientation, glm::vec3 scale); + void AddModelToDraw(std::shared_ptr model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible); void AddTextToDraw(); void AddPointLightToDraw( glm::vec3 _position, diff --git a/Escape-the-Dawn/Shaders/Fragment.glsl b/Escape-the-Dawn/Shaders/Fragment.glsl index a16878d..5e3f603 100644 --- a/Escape-the-Dawn/Shaders/Fragment.glsl +++ b/Escape-the-Dawn/Shaders/Fragment.glsl @@ -23,7 +23,7 @@ in VertexData { vec3 ShadowCoord; } Input; -vec3 scene_ambient = vec3(0.3); +vec3 scene_ambient = vec3(0.98, 0.95, 0.7); out vec4 fragmentColor; @@ -54,7 +54,7 @@ void main() { float bias = 0.0005; vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy); if (shadowMapValue.z < clamp(Input.ShadowCoord.z - bias, 0, 1)) { - visibility = 0.5; + visibility = 0.3; } } diff --git a/Escape-the-Dawn/System.h b/Escape-the-Dawn/System.h index f62eda9..cbe9cb8 100644 --- a/Escape-the-Dawn/System.h +++ b/Escape-the-Dawn/System.h @@ -13,6 +13,8 @@ public: System(World* world) : m_World(world) { } virtual ~System() { } + virtual void Initialize() { } + // Called once per system every tick virtual void Update(double dt) { } // Called once for every entity in the world every tick diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp index ab91656..4bd04bf 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.cpp +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -85,8 +85,8 @@ void Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity) if (aMin.x <= bMax.x && bMin.x <= aMax.x) { if (aMin.y <= bMax.y && bMin.y <= aMax.y) { if (aMin.z <= bMax.z && bMin.z <= aMax.z) { - aCollisionComponent->CollidingEntities.push_back(aEntity); - bCollisionComponent->CollidingEntities.push_back(bEntity); + aCollisionComponent->CollidingEntities.push_back(bEntity); + bCollisionComponent->CollidingEntities.push_back(aEntity); return; } } diff --git a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp index a249739..8df9dd6 100644 --- a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp +++ b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp @@ -8,6 +8,7 @@ Systems::LevelGenerationSystem::LevelGenerationSystem( World* world ) velocity = 0; startx = 0; startyz = glm::vec2(0, -1000); + spawnFrequency = 0.1f; } void Systems::LevelGenerationSystem::SpawnObstacle() @@ -17,18 +18,17 @@ void Systems::LevelGenerationSystem::SpawnObstacle() EntityID ent; ent = m_World->CreateEntity(); - obstacles.push_back(ent); - m_World->SetProperty(ent, "Name", std::string("Obstacle")); - transform = m_World->AddComponent(ent, "Transform"); - bounds = m_World->AddComponent(ent, "Bounds"); - collision = m_World->AddComponent(ent, "Collision"); - model = m_World->AddComponent(ent, "Model"); + + auto transform = m_World->AddComponent(ent, "Transform"); + auto bounds = m_World->AddComponent(ent, "Bounds"); + auto collision = m_World->AddComponent(ent, "Collision"); + auto model = m_World->AddComponent(ent, "Model"); auto sound = m_World->AddComponent(ent, "SoundEmitter"); positionRandom = -500 + startx + (rand() % 1000); transform->Position = glm::vec3(positionRandom, startyz); - //transform->Velocity = glm::vec3(0.f, 10.f, 100.f); + transform->Velocity = glm::vec3(0.f, 10.f, 0); sound->Loop = true; sound->Gain = 1.f; @@ -37,7 +37,8 @@ void Systems::LevelGenerationSystem::SpawnObstacle() if(typeRandom >= 0 && typeRandom < 2) // Mountain stuff :D { - float scale = (float)(rand() % 1000) / 250; + m_World->SetProperty(ent, "Name", std::string("Obstacle")); + float scale = 1.0f + (float)(rand() % 1000) / 250; transform->Scale = glm::vec3(scale); bounds->VolumeVector = glm::vec3(9, 12, 7); bounds->Origin = glm::vec3(1,11,-1); @@ -45,7 +46,8 @@ void Systems::LevelGenerationSystem::SpawnObstacle() } else if(typeRandom >= 2 && typeRandom < 5) // Single Mountain :D { - float scale = (float)(rand() % 1000) / 200; + m_World->SetProperty(ent, "Name", std::string("Obstacle")); + float scale = 1.0f + (float)(rand() % 1000) / 200; transform->Scale = glm::vec3(scale); bounds->VolumeVector = glm::vec3(3.5f, 7.5f, 3.5f); bounds->Origin = glm::vec3(0,7.5f,0); @@ -53,7 +55,8 @@ void Systems::LevelGenerationSystem::SpawnObstacle() } else if(typeRandom >= 5 && typeRandom < 8) { - float scale = (float)(rand() % 1000) / 100; + m_World->SetProperty(ent, "Name", std::string("Obstacle")); + float scale = 1.f + (float)(rand() % 1000) / 100; transform->Scale = glm::vec3(scale); bounds->VolumeVector = glm::vec3(1, 1, 1); bounds->Origin = glm::vec3(0,1,0); @@ -61,20 +64,20 @@ void Systems::LevelGenerationSystem::SpawnObstacle() } if(typeRandom >= 8 && typeRandom < 10) { - float scale = (float)(rand() % 1000) / 100; + m_World->SetProperty(ent, "Name", std::string("PowerUp")); bounds->VolumeVector = glm::vec3(4, 4, 4); bounds->Origin = glm::vec3(0,4,0); - powerUp = m_World->AddComponent(ent, "PowerUp"); - powerUp->Speed = 10.f; + auto powerUp = m_World->AddComponent(ent, "PowerUp"); + powerUp->Speed = 200.f; model->ModelFile = "Models/powerup.obj"; } // Put it below ground level - //transform->Position.y -= bounds->VolumeVector.y * 2.f; + transform->Position.y -= bounds->VolumeVector.y * 2.f; } @@ -82,47 +85,47 @@ void Systems::LevelGenerationSystem::Update( double dt ) { elapsedtime += dt; + spawnFrequency = 10/velocity; - if(elapsedtime > 0.1){ + if(elapsedtime > spawnFrequency){ SpawnObstacle(); elapsedtime = 0; } - std::list removethis; - for(auto ent : obstacles) - { - auto transform = m_World->GetComponent(ent, "Transform"); - - transform->Velocity.z = velocity; - transform->Position += transform->Velocity * (float)dt; - - // Stop raising obstacles when they reach ground level - if (transform->Velocity.y > 0 && transform->Position.y >= 0) { - transform->Position.y = 0; - transform->Velocity.y = 0; - } - - if(transform->Position.z > 800) - { - removethis.push_back(ent); - } - } - - for(auto ent : removethis) - { - m_World->RemoveEntity(ent); - obstacles.remove(ent); - } - removethis.clear(); } void Systems::LevelGenerationSystem::UpdateEntity( double dt, EntityID entity, EntityID parent ) { + if( m_World->GetProperty(entity, "Name") == "Obstacle" || m_World->GetProperty(entity, "Name") == "PowerUp") + { + auto transform = m_World->GetComponent(entity, "Transform"); + transform->Velocity.z = velocity; + transform->Position += transform->Velocity * (float)dt; + + // Stop raising obstacles when they reach ground level + if (transform->Velocity.y > 0 && transform->Position.y >= 0) + { + transform->Position.y = 0; + transform->Velocity.y = 0; + } + + auto model = m_World->GetComponent(entity, "Model"); + if (transform->Position.z > 10 && model->Visible) + { + model->Visible = false; + } + + if(transform->Position.z > 800) { + m_World->RemoveEntity(entity); + } + } + if( m_World->GetProperty(entity, "Name") == "Player") { auto transformComponent = m_World->GetComponent(entity, "Transform"); + startyz = glm::vec2(0, transformComponent->Position.z - 1000); startx = transformComponent->Position.x; velocity = transformComponent->Velocity.z; diff --git a/Escape-the-Dawn/Systems/LevelGenerationSystem.h b/Escape-the-Dawn/Systems/LevelGenerationSystem.h index 864c047..7d0339d 100644 --- a/Escape-the-Dawn/Systems/LevelGenerationSystem.h +++ b/Escape-the-Dawn/Systems/LevelGenerationSystem.h @@ -31,18 +31,9 @@ namespace Systems int positionRandom; glm::vec2 startyz; float startx; - double elapsedtime; - - std::list obstacles; float velocity; - - std::shared_ptr transform; - std::shared_ptr bounds; - std::shared_ptr collision; - std::shared_ptr model; - std::shared_ptr pointLight; - std::shared_ptr powerUp; + float spawnFrequency; }; } diff --git a/Escape-the-Dawn/Systems/PlayerSystem.cpp b/Escape-the-Dawn/Systems/PlayerSystem.cpp index 7dc1959..96cbc8f 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.cpp +++ b/Escape-the-Dawn/Systems/PlayerSystem.cpp @@ -10,11 +10,14 @@ Systems::PlayerSystem::PlayerSystem( World* world ) : System(world) m_CameraOrientation = glm::angleAxis(glm::radians(15.0f),glm::vec3(1,0,0)); freecamEnabled = false; - + m_poweruptimeleft = 0; + m_basespeed = 100.f; + m_maxspeed = 100.f; } void Systems::PlayerSystem::Update(double dt) { + m_poweruptimeleft -= (float)dt; } void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -26,48 +29,74 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa if (input == nullptr) return; + auto collision = m_World->GetComponent(entity, "Collision"); + auto name = m_World->GetProperty(entity, "Name"); - if (name == "Camera" && freecamEnabled) { - glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation); - glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation); - - float speed = m_PlayerSpeed; - if(input->KeyState[GLFW_KEY_LEFT_SHIFT]) { - speed *= 4.0f; - } - if(input->KeyState[GLFW_KEY_LEFT_ALT]) { - speed /= 4.0f; - } - if(input->KeyState[GLFW_KEY_A]) { - transform->Position -= Camera_Right * (float)dt * speed; - } - else if(input->KeyState[GLFW_KEY_D]) { - transform->Position += Camera_Right * (float)dt * speed; - } - if(input->KeyState[GLFW_KEY_W]) { - transform->Position -= Camera_Forward * (float)dt * speed; - } - if(input->KeyState[GLFW_KEY_S]) { - transform->Position += Camera_Forward * (float)dt * speed; - } - if(input->KeyState[GLFW_KEY_SPACE]) { - transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed; - } - if(input->KeyState[GLFW_KEY_LEFT_CONTROL]) { - transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed; - } - - if (input->MouseState[GLFW_MOUSE_BUTTON_LEFT]) { - // TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS - //--------------------------------------------------------------------- - transform->Orientation = glm::angleAxis(input->dY/300.f,glm::vec3(1,0,0)) * transform->Orientation; - - transform->Orientation = transform->Orientation * glm::angleAxis(input->dX/300.f,glm::vec3(0,1,0)); - //--------------------------------------------------------------------- - // TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + if(collision != nullptr) + { + for(auto ent : collision->CollidingEntities) + { + if(m_World->GetProperty(ent, "Name") == "Obstacle") + { + m_World->RemoveEntity(entity); + } } } + if (name == "Camera") { + + if(input->KeyState[GLFW_KEY_F4] && !input->LastKeyState[GLFW_KEY_F4]) { + if(freecamEnabled) + freecamEnabled = false; + else + freecamEnabled = true; + } + + if(freecamEnabled) + { + glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation); + glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation); + + float speed = m_PlayerSpeed; + if(input->KeyState[GLFW_KEY_LEFT_SHIFT]) { + speed *= 4.0f; + } + if(input->KeyState[GLFW_KEY_LEFT_ALT]) { + speed /= 4.0f; + } + if(input->KeyState[GLFW_KEY_A]) { + transform->Position -= Camera_Right * (float)dt * speed; + } + else if(input->KeyState[GLFW_KEY_D]) { + transform->Position += Camera_Right * (float)dt * speed; + } + if(input->KeyState[GLFW_KEY_W]) { + transform->Position -= Camera_Forward * (float)dt * speed; + } + if(input->KeyState[GLFW_KEY_S]) { + transform->Position += Camera_Forward * (float)dt * speed; + } + if(input->KeyState[GLFW_KEY_SPACE]) { + transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed; + } + if(input->KeyState[GLFW_KEY_LEFT_CONTROL]) { + transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed; + } + + if (input->MouseState[GLFW_MOUSE_BUTTON_LEFT]) { + // TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + //--------------------------------------------------------------------- + transform->Orientation = glm::angleAxis(input->dY/300.f,glm::vec3(1,0,0)) * transform->Orientation; + + transform->Orientation = transform->Orientation * glm::angleAxis(input->dX/300.f,glm::vec3(0,1,0)); + //--------------------------------------------------------------------- + // TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + } + } + + } + + if (name == "Player") { auto bounds = m_World->GetComponent(entity, "Bounds"); @@ -79,7 +108,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa glm::vec3 Ship_Forward = glm::vec3(glm::vec4(0, 0, 1, 0)); float TurnSpeed = 1.0f; - glm::vec3 Euler = glm::eulerAngles(transform->Orientation); + glm::vec3 Euler = glm::degrees(glm::eulerAngles(transform->Orientation)); if(input->KeyState[GLFW_KEY_LEFT]) { transform->Position -= Ship_Right * (float)dt * (m_PlayerSpeed + Euler.z); @@ -115,6 +144,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } else { + transform->Position -= Ship_Right * (float)dt * Euler.z; if(Euler.z < 1.5f && Euler.z > -1.5f) transform->Orientation = glm::angleAxis(0,glm::vec3(0,0,1)); else if(Euler.z < 0.f) @@ -130,23 +160,55 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } - if(input->KeyState[GLFW_KEY_F4] && !input->LastKeyState[GLFW_KEY_F4]) { - if(freecamEnabled) - freecamEnabled = false; - else - freecamEnabled = true; + + + //powerup + auto cameraEntity = m_World->GetProperty(entity, "Camera"); + auto cameracamera = m_World->GetComponent(cameraEntity, "Camera"); + cameracamera->FOV = glm::radians(20.f + transform->Velocity.z/3 ); + + auto collisionComponent = m_World->GetComponent(entity, "Collision"); + for(auto ent : collisionComponent->CollidingEntities) + { + std::string name = m_World->GetProperty(ent, "Name"); + if(name == "PowerUp") + { + auto powerupComp = m_World->GetComponent(ent, "PowerUp"); + m_maxspeed = powerupComp->Speed; + m_poweruptimeleft = 5.f; + m_World->RemoveEntity(ent); + + } + } + if(m_poweruptimeleft <= 0) + { + m_maxspeed = m_basespeed; + } + if(transform->Velocity.z < m_maxspeed) + { + transform->Velocity.z += 100.f*dt; + if(transform->Velocity.z >= m_maxspeed) + transform->Velocity.z = m_maxspeed; + } + else if(transform->Velocity.z > m_maxspeed) + { + transform->Velocity.z -= 50.f*dt; + if(transform->Velocity.z <= m_maxspeed) + transform->Velocity.z = m_maxspeed; } + // Update camera if(! freecamEnabled) { - auto cameraEntity = m_World->GetProperty(entity, "Camera"); auto cameraTransform = m_World->GetComponent(cameraEntity, "Transform"); if (cameraTransform) { cameraTransform->Position = transform->Position + m_CameraOffset; cameraTransform->Orientation = m_CameraOrientation; } + + } } } diff --git a/Escape-the-Dawn/Systems/PlayerSystem.h b/Escape-the-Dawn/Systems/PlayerSystem.h index 12ea648..993ee20 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.h +++ b/Escape-the-Dawn/Systems/PlayerSystem.h @@ -9,6 +9,8 @@ #include "Components/Collision.h" #include "Components/Camera.h" #include "Components/Bounds.h" +#include "Components/PowerUp.h" + #include "logging.h" @@ -32,6 +34,11 @@ namespace Systems glm::quat m_CameraOrientation; bool freecamEnabled; + + float m_basespeed; + float m_maxspeed; + + float m_poweruptimeleft; }; } diff --git a/Escape-the-Dawn/Systems/RenderSystem.cpp b/Escape-the-Dawn/Systems/RenderSystem.cpp index 3165a0f..67827bd 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.cpp +++ b/Escape-the-Dawn/Systems/RenderSystem.cpp @@ -25,7 +25,9 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p } auto model = m_CachedModels[modelComponent->ModelFile]; - m_Renderer->AddModelToDraw(model, transformComponent->Position, transformComponent->Orientation, transformComponent->Scale); + glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); + glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity); + m_Renderer->AddModelToDraw(model, position, orientation, transformComponent->Scale, modelComponent->Visible); } // Debug draw bounds @@ -33,7 +35,7 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p auto collision = m_World->GetComponent(entity, "Collision"); auto bounds = m_World->GetComponent(entity, "Bounds"); if (bounds != nullptr) { - glm::vec3 origin = transformComponent->Position + (transformComponent->Scale * bounds->Origin); + glm::vec3 origin = m_TransformSystem->AbsolutePosition(entity) + (transformComponent->Scale * bounds->Origin); glm::vec3 volumeVector = transformComponent->Scale * bounds->VolumeVector; m_Renderer->AddAABBToDraw(origin, volumeVector, (collision != nullptr && collision->CollidingEntities.size() > 0)); } @@ -42,8 +44,9 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p auto pointLightComponent = m_World->GetComponent(entity, "PointLight"); if (pointLightComponent != nullptr) { + glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); m_Renderer->AddPointLightToDraw( - transformComponent->Position, + position, pointLightComponent->Specular, pointLightComponent->Diffuse, pointLightComponent->constantAttenuation, @@ -51,6 +54,7 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p pointLightComponent->quadraticAttenuation, pointLightComponent->spotExponent); } + auto cameraComponent = m_World->GetComponent(entity, "Camera"); if (cameraComponent != nullptr) { @@ -63,6 +67,12 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p } } +void Systems::RenderSystem::Initialize() +{ + m_TransformSystem = m_World->GetSystem("TransformSystem"); +} + + diff --git a/Escape-the-Dawn/Systems/RenderSystem.h b/Escape-the-Dawn/Systems/RenderSystem.h index d2a182f..f9e5ee6 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.h +++ b/Escape-the-Dawn/Systems/RenderSystem.h @@ -4,6 +4,7 @@ #include #include "System.h" +#include "Systems/TransformSystem.h" #include "Model.h" #include "Texture.h" #include "Components/Model.h" @@ -22,6 +23,8 @@ namespace Systems RenderSystem(World* world, std::shared_ptr renderer) : System(world), m_Renderer(renderer){ } + void Initialize() override; + std::unordered_map> m_CachedModels; void OnComponentCreated(std::string type, std:: shared_ptr component) override; @@ -29,7 +32,7 @@ namespace Systems private: std::shared_ptr m_Renderer; - + std::shared_ptr m_TransformSystem; }; diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index 88373d4..07142f1 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -7,7 +7,6 @@ #include #include #include -#include namespace Systems { diff --git a/Escape-the-Dawn/Systems/TransformSystem.cpp b/Escape-the-Dawn/Systems/TransformSystem.cpp new file mode 100644 index 0000000..9cd65fa --- /dev/null +++ b/Escape-the-Dawn/Systems/TransformSystem.cpp @@ -0,0 +1,47 @@ +#include "TransformSystem.h" +#include "World.h" + +//void Systems::TransformSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +//{ +// if (parent == 0) +// return; +// +// auto transform = m_World->GetComponent(entity, "Transform"); +// auto parentTransform = m_World->GetComponent(parent, "Transform"); +// +// transform->Position = parentTransform->Position + transform->RelativePosition; +//} + +glm::vec3 Systems::TransformSystem::AbsolutePosition(EntityID entity) +{ + glm::vec3 absPosition; + glm::quat accumulativeOrientation; + + do + { + auto transform = m_World->GetComponent(entity, "Transform"); + //absPosition += transform->Position; + entity = m_World->GetEntityParent(entity); + auto transform2 = m_World->GetComponent(entity, "Transform"); + if (entity != 0) + absPosition += transform2->Orientation * transform->Position; + else + absPosition += transform->Position; + } while (entity != 0); + + return absPosition * accumulativeOrientation; +} + +glm::quat Systems::TransformSystem::AbsoluteOrientation(EntityID entity) +{ + glm::quat absOrientation; + + do + { + auto transform = m_World->GetComponent(entity, "Transform"); + absOrientation *= transform->Orientation; + entity = m_World->GetEntityParent(entity); + } while (entity != 0); + + return absOrientation; +} \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/TransformSystem.h b/Escape-the-Dawn/Systems/TransformSystem.h new file mode 100644 index 0000000..b0cc2fa --- /dev/null +++ b/Escape-the-Dawn/Systems/TransformSystem.h @@ -0,0 +1,25 @@ +#ifndef TransformSystem_h__ +#define TransformSystem_h__ + +#include "System.h" +#include "Components/Transform.h" + +namespace Systems +{ + + class TransformSystem : public System + { + public: + TransformSystem(World* world) + : System(world) { } + + //void Update(double dt) override; + //void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + + glm::vec3 AbsolutePosition(EntityID entity); + glm::quat AbsoluteOrientation(EntityID entity); + }; + +} + +#endif // TransformSystem_h__ diff --git a/Escape-the-Dawn/World.cpp b/Escape-the-Dawn/World.cpp index 500395e..15bbe40 100644 --- a/Escape-the-Dawn/World.cpp +++ b/Escape-the-Dawn/World.cpp @@ -36,6 +36,8 @@ void World::Update(double dt) system->Update(dt); RecursiveUpdate(system, dt, 0); } + + ProcessEntityRemovals(); } //std::vector GetEntityChildren(EntityID entity); @@ -60,21 +62,29 @@ bool World::ValidEntity(EntityID entity) void World::RemoveEntity(EntityID entity) { - m_EntityParents.erase(entity); - // Remove components - for (auto pair : m_EntityComponents[entity]) { - auto type = pair.first; - auto component = pair.second; - // Trigger events - for (auto pair : m_Systems) { - auto system = pair.second; - system->OnComponentRemoved(type, component.get()); - } - m_ComponentsOfType[type].remove(component); - } - m_EntityComponents.erase(entity); + m_EntitiesToRemove.push_back(entity); +} - RecycleEntityID(entity); +void World::ProcessEntityRemovals() +{ + for (auto entity : m_EntitiesToRemove) { + m_EntityParents.erase(entity); + // Remove components + for (auto pair : m_EntityComponents[entity]) { + auto type = pair.first; + auto component = pair.second; + // Trigger events + for (auto pair : m_Systems) { + auto system = pair.second; + system->OnComponentRemoved(type, component.get()); + } + m_ComponentsOfType[type].remove(component); + } + m_EntityComponents.erase(entity); + + RecycleEntityID(entity); + } + m_EntitiesToRemove.clear(); } EntityID World::CreateEntity(EntityID parent /*= 0*/) @@ -97,19 +107,14 @@ World::World() void World::Initialize() { RegisterSystems(); + AddSystems(); + for (auto system : m_Systems) { + system.second->Initialize(); + } + RegisterComponents(); } -void World::RegisterSystems() -{ - -} - -void World::RegisterComponents() -{ - -} - std::shared_ptr World::AddComponent(EntityID entity, std::string componentType) { return AddComponent(entity, componentType); diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index add6ae3..b481397 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -25,8 +25,10 @@ public: virtual void Initialize(); - virtual void RegisterComponents(); - virtual void RegisterSystems(); + virtual void RegisterSystems() = 0; + virtual void AddSystems() = 0; + virtual void RegisterComponents() = 0; + void AddSystem(std::string systemType); template @@ -85,9 +87,13 @@ protected: std::unordered_map>> m_ComponentsOfType; std::unordered_map>> m_EntityComponents; + std::list m_EntitiesToRemove; + void ProcessEntityRemovals(); + EntityID GenerateEntityID(); void RecycleEntityID(EntityID id); + }; template