From 807e9c9a9fdfe2ddf6b4f3becd0477e7e5ef19a3 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Wed, 2 Aug 2023 12:40:12 -0400 Subject: [PATCH] Added particle emitter component --- .../ComponentsPanel/ParticleEmitterPanel.h | 79 +++++++++++++++++++ Editor/src/Windows/EditorSelectionPanel.cpp | 1 + Editor/src/Windows/EditorSelectionPanel.h | 5 +- Nuake/src/Resource/Serializable.h | 3 + .../Components/ParticleEmitterComponent.cpp | 28 +++++++ .../Components/ParticleEmitterComponent.h | 28 +++++++ Nuake/src/Scene/Entities/Entity.cpp | 5 +- 7 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 Editor/src/ComponentsPanel/ParticleEmitterPanel.h create mode 100644 Nuake/src/Scene/Components/ParticleEmitterComponent.cpp create mode 100644 Nuake/src/Scene/Components/ParticleEmitterComponent.h diff --git a/Editor/src/ComponentsPanel/ParticleEmitterPanel.h b/Editor/src/ComponentsPanel/ParticleEmitterPanel.h new file mode 100644 index 00000000..7ef448a4 --- /dev/null +++ b/Editor/src/ComponentsPanel/ParticleEmitterPanel.h @@ -0,0 +1,79 @@ +#pragma once +#include "ComponentPanel.h" + +#include +#include +#include +#include + +class ParticleEmitterPanel : ComponentPanel +{ +public: + ParticleEmitterPanel() {} + + void Draw(Nuake::Entity entity) override + { + if (!entity.HasComponent()) + return; + + auto& component = entity.GetComponent(); + BeginComponentTable(PARTICLE EMITTER, Nuake::ParticleEmitterComponent); + { + { + ImGui::Text("Particle Color"); + ImGui::TableNextColumn(); + + ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); + ImGui::ColorEdit3("##lightcolor", &component.ParticleColor.r, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel); + ImGui::PopItemWidth(); + + ImGui::TableNextColumn(); + ComponentTableReset(component.ParticleColor, Nuake::Vector4(1, 1, 1, 1)); + } + ImGui::TableNextColumn(); + { + ImGui::Text("Amount"); + ImGui::TableNextColumn(); + + ImGui::DragFloat("##ParticleAmount", &component.Amount, 0.1f, 0.0f, 500.0f); + ImGui::TableNextColumn(); + ComponentTableReset(component.Amount, 10.0f); + } + ImGui::TableNextColumn(); + { + ImGui::Text("Life"); + ImGui::TableNextColumn(); + + ImGui::DragFloat("##ParticleLife", &component.Life, 0.1f, 0.0f, 100.0f); + ImGui::TableNextColumn(); + ComponentTableReset(component.Life, 5.0f); + } + ImGui::TableNextColumn(); + { + ImGui::Text("Gravity"); + ImGui::TableNextColumn(); + + ImGuiHelper::DrawVec3("Gravity", &component.Gravity); + ImGui::TableNextColumn(); + ComponentTableReset(component.Gravity, Nuake::Vector3(0, -1, 0)); + } + ImGui::TableNextColumn(); + { + ImGui::Text("Gravity Random"); + ImGui::TableNextColumn(); + ImGui::DragFloat("##GravityRandom", &component.GravityRandom, 0.01f, 0.0f, 1.0f); + ImGui::TableNextColumn(); + ComponentTableReset(component.GravityRandom, 0.0f); + } + ImGui::TableNextColumn(); + { + ImGui::Text("Radius"); + ImGui::TableNextColumn(); + ImGui::DragFloat("##ParticleRadius", &component.Radius, 0.01f, 0.0f, 10.0f); + ImGui::TableNextColumn(); + ComponentTableReset(component.Radius, 1.0f); + } + } + EndComponentTable(); + } +}; \ No newline at end of file diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index a5674899..c524ecd6 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -93,6 +93,7 @@ void EditorSelectionPanel::DrawEntity(Nuake::Entity entity) mTransformPanel.Draw(entity); mLightPanel.Draw(entity); mScriptPanel.Draw(entity); + mParticleEmitterPanel.Draw(entity); mSpritePanel.Draw(entity); mMeshPanel.Draw(entity); mQuakeMapPanel.Draw(entity); diff --git a/Editor/src/Windows/EditorSelectionPanel.h b/Editor/src/Windows/EditorSelectionPanel.h index 847406ad..703e59bf 100644 --- a/Editor/src/Windows/EditorSelectionPanel.h +++ b/Editor/src/Windows/EditorSelectionPanel.h @@ -19,9 +19,11 @@ #include "../ComponentsPanel/MeshColliderPanel.h" #include "../ComponentsPanel/CharacterControllerPanel.h" #include "../ComponentsPanel/SpritePanel.h" +#include "../ComponentsPanel/ParticleEmitterPanel.h" -class EditorSelectionPanel { +class EditorSelectionPanel +{ private: TransformPanel mTransformPanel; LightPanel mLightPanel; @@ -37,6 +39,7 @@ private: CylinderColliderPanel mCylinderColliderPanel; SpritePanel mSpritePanel; CharacterControllerPanel mCharacterControllerPanel; + ParticleEmitterPanel mParticleEmitterPanel; Ref currentFile; Ref selectedResource; diff --git a/Nuake/src/Resource/Serializable.h b/Nuake/src/Resource/Serializable.h index ac9f2237..d8ce5d2f 100644 --- a/Nuake/src/Resource/Serializable.h +++ b/Nuake/src/Resource/Serializable.h @@ -19,6 +19,9 @@ using json = nlohmann::json; SERIALIZE_VEC3(v) \ j[#v]["w"] = this->v.w; +#define DESERIALIZE_VEC4(v, p) \ + p = Vector4(v["x"], v["y"], v["z"], v["w"]); + #define DESERIALIZE_VEC3(v, p) \ p = Vector3(v["x"], v["y"], v["z"]); diff --git a/Nuake/src/Scene/Components/ParticleEmitterComponent.cpp b/Nuake/src/Scene/Components/ParticleEmitterComponent.cpp new file mode 100644 index 00000000..3c12015c --- /dev/null +++ b/Nuake/src/Scene/Components/ParticleEmitterComponent.cpp @@ -0,0 +1,28 @@ +#include "src/Scene/Components/ParticleEmitterComponent.h" + +namespace Nuake +{ + json ParticleEmitterComponent::Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VEC4(ParticleColor); + SERIALIZE_VAL(Amount); + SERIALIZE_VAL(Life); + SERIALIZE_VEC3(Gravity); + SERIALIZE_VAL(GravityRandom); + SERIALIZE_VAL(Radius); + END_SERIALIZE(); + } + + bool ParticleEmitterComponent::Deserialize(const std::string& str) + { + BEGIN_DESERIALIZE(); + DESERIALIZE_VEC4(j["ParticleColor"], ParticleColor); + Amount = j["Amount"]; + Life = j["Life"]; + DESERIALIZE_VEC3(j["Gravity"], Gravity); + GravityRandom = j["GravityRandom"]; + Radius = j["Radius"]; + return true; + } +} \ No newline at end of file diff --git a/Nuake/src/Scene/Components/ParticleEmitterComponent.h b/Nuake/src/Scene/Components/ParticleEmitterComponent.h new file mode 100644 index 00000000..7f46ccd2 --- /dev/null +++ b/Nuake/src/Scene/Components/ParticleEmitterComponent.h @@ -0,0 +1,28 @@ +#pragma once +#include "src/Core/Core.h" +#include "src/Resource/Serializable.h" +#include + +namespace Nuake +{ + class ParticleEmitterComponent + { + public: + ParticleEmitterComponent() = default; + ~ParticleEmitterComponent() = default; + + Color ParticleColor; + float Amount; + float Life; + + Vector3 Gravity; + float GravityRandom; + + // For now use a radius, later should use shape. + float Radius; + + public: + json Serialize(); + bool Deserialize(const std::string& str); + }; +} \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Entity.cpp b/Nuake/src/Scene/Entities/Entity.cpp index 87dbca05..706616d0 100644 --- a/Nuake/src/Scene/Entities/Entity.cpp +++ b/Nuake/src/Scene/Entities/Entity.cpp @@ -15,7 +15,7 @@ #include "src/Scene/Components/BoxCollider.h" #include "src/Scene/Components/CapsuleColliderComponent.h" #include "src/Scene/Components/SpriteComponent.h" - +#include "src/Scene/Components/ParticleEmitterComponent.h" namespace Nuake { @@ -39,6 +39,8 @@ namespace Nuake SERIALIZE_OBJECT_REF_LBL("VisibilityComponent", GetComponent()) if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("CameraComponent", GetComponent()) + if (HasComponent()) + SERIALIZE_OBJECT_REF_LBL("ParticleEmitterComponent", GetComponent()) if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("SpriteComponent", GetComponent()) if (HasComponent()) @@ -82,6 +84,7 @@ namespace Nuake } DESERIALIZE_COMPONENT(NameComponent) DESERIALIZE_COMPONENT(ParentComponent) + DESERIALIZE_COMPONENT(ParticleEmitterComponent) DESERIALIZE_COMPONENT(SpriteComponent) DESERIALIZE_COMPONENT(CameraComponent) DESERIALIZE_COMPONENT(QuakeMapComponent)