Skip to content

Commit

Permalink
feat (./ParticleSystem)
Browse files Browse the repository at this point in the history
  • Loading branch information
PharaEthan committed Jan 8, 2024
1 parent 75663bf commit 2778cb4
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 5 deletions.
1 change: 1 addition & 0 deletions GameEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(SOURCES_GAME_ENGINE
src/Exodia/Scene/System/Script/ScriptSystem.cpp
src/Exodia/Scene/System/Physics/GravitySystem.cpp
src/Exodia/Scene/System/Physics/MovingSystem.cpp
src/Exodia/Scene/System/Particle/ParticleSystem.cpp
src/Exodia/Scene/EventSubscriber/EventHover.cpp
src/Exodia/Scene/GameObject/GameObject.cpp
src/Exodia/Scene/Prefabs/Prefabs.cpp
Expand Down
4 changes: 4 additions & 0 deletions GameEngine/src/Exodia/Project/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ namespace Exodia {
return new ComponentContainer<MusicComponent>(data);
});

RegisterComponent("ParticleComponent", [](Buffer data) -> IComponentContainer * {
return new ComponentContainer<ParticleComponent>(data);
});

// -- Registering the script factories -- //

// ...
Expand Down
2 changes: 1 addition & 1 deletion GameEngine/src/Exodia/Scene/Components/Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Exodia {
virtual void Serialize(UNUSED(YAML::Emitter &out)){};
virtual void Deserialize(UNUSED(const YAML::Node &node)){};

virtual void DrawComponent(){};
virtual void DrawComponent() {};

virtual Buffer SerializeData() { return Buffer(this, sizeof(*this)); }
};
Expand Down
1 change: 1 addition & 0 deletions GameEngine/src/Exodia/Scene/Components/Components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
#include "Scene/Components/DefaultComponents/PrefabComponent.hpp"
#include "Scene/Components/DefaultComponents/RigidBody2DComponent.hpp"
#include "Scene/Components/DefaultComponents/SoundComponent.hpp"
#include "Scene/Components/DefaultComponents/ParticleComponent.hpp"

#endif /* !COMPONENTS_HPP_ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
** EPITECH PROJECT, 2024
** R-Type
** File description:
** ParticleComponent
*/

#ifndef PARTICLECOMPONENT_HPP_
#define PARTICLECOMPONENT_HPP_

// Exodia ECS includes
#include "Scene/Components/Component.hpp"

// External includes
#include <glm/glm.hpp>

namespace Exodia {

struct ParticleComponent : public Component {
float LifeTime;
float LifeRemaining;
glm::vec4 ColorBegin;
glm::vec4 ColorEnd;
float SizeBegin;
float SizeEnd;

ParticleComponent(ParticleComponent const &) = default;
ParticleComponent() = default;
ParticleComponent(float lifeTime, const glm::vec4 &colorBegin, const glm::vec4 &colorEnd, float sizeBegin, float sizeEnd) : LifeTime(lifeTime), LifeRemaining(lifeTime), ColorBegin(colorBegin), ColorEnd(colorEnd), SizeBegin(sizeBegin), SizeEnd(sizeEnd) {};
};
};

#endif /* !PARTICLECOMPONENT_HPP_ */
1 change: 1 addition & 0 deletions GameEngine/src/Exodia/Scene/SceneHeaders.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "Exodia/Scene/System/Script/ScriptSystem.hpp"
#include "Exodia/Scene/System/Physics/GravitySystem.hpp"
#include "Exodia/Scene/System/Physics/MovingSystem.hpp"
#include "Exodia/Scene/System/Particle/ParticleSystem.hpp"

// -- Event Subscribers ----------------------------------------------------

Expand Down
40 changes: 40 additions & 0 deletions GameEngine/src/Exodia/Scene/System/Particle/ParticleSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
** EPITECH PROJECT, 2024
** R-Type
** File description:
** ParticleSystem
*/

#include "ParticleSystem.hpp"
#include <glm/gtx/compatibility.hpp>

namespace Exodia {

/////////////
// Methods //
/////////////

void ParticleSystem::Update(World *world, Timestep ts)
{
world->ForEach<TransformComponent, RigidBody2DComponent, ParticleComponent, SpriteRendererComponent>([&](Entity *entity, auto transform, auto rigidBody, auto particle, auto sprite) {
auto &tc = transform.Get();
auto &rb = rigidBody.Get();
auto &pc = particle.Get();
auto &sc = sprite.Get();

if (pc.LifeRemaining <= 0.0f) {
world->DestroyEntity(entity);

return;
}
float life = pc.LifeRemaining / pc.LifeTime;
float size = glm::lerp(pc.SizeEnd, pc.SizeBegin, life);
glm::vec4 color = glm::lerp(pc.ColorEnd, pc.ColorBegin, life);

pc.LifeRemaining -= ts;
sc.Color.a = color.a * life;
tc.Rotation.z += 0.01f * ts;
tc.Scale = glm::vec3(size);
});
}
};
30 changes: 30 additions & 0 deletions GameEngine/src/Exodia/Scene/System/Particle/ParticleSystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
** EPITECH PROJECT, 2024
** R-Type
** File description:
** ParticleSystem
*/

#ifndef PARTICLESYSTEM_HPP_
#define PARTICLESYSTEM_HPP_

// Exodia ECS Interface includes
#include "Exodia-ECS.hpp"

// Exodia Scene includes
#include "Scene/Components/Components.hpp"

namespace Exodia {

class ParticleSystem : public EntitySystem {

/////////////
// Methods //
/////////////
public:

virtual void Update(World *world, Timestep ts) override;
};
};

#endif /* !PARTICLESYSTEM_HPP_ */
2 changes: 1 addition & 1 deletion GameEngine/src/Exodia/Scene/System/Script/ScriptSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define SCRIPTSYSTEM_HPP_

// Exodia ECS Interface includes
#include "Interface/EntitySystem.hpp"
#include "Exodia-ECS.hpp"

// Exodia ECS Components includes
#include "Scene/Components/Components.hpp"
Expand Down
12 changes: 11 additions & 1 deletion SandBox/Ethan/Assets/Scene/Menu/Menu.exodia
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,14 @@ Entities:
Color: [1, 1, 1, 1]
Font: 45121874124124
Kerning: 0
LineSpacing: 0
LineSpacing: 0

- Entity: 4512011102411212
TagComponent:
Tag: Particle
TransformComponent:
Translation: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
ScriptComponent:
Name: ParticleController
17 changes: 15 additions & 2 deletions SandBox/Ethan/Client/src/GameScene/MenuScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace RType {
// -- Scene System -- //
_Scene->RegisterSystem(new AnimationSystem());
_Scene->RegisterSystem(new FadeSystem());
_Scene->RegisterSystem(new ParticleSystem());
_Scene->RegisterSystem(new MovingSystem());

// -- Resizing the viewport -- //
_Scene->OnViewportResize(Application::Get().GetWindow().GetWidth(), Application::Get().GetWindow().GetHeight());
Expand Down Expand Up @@ -70,7 +72,7 @@ namespace RType {
}

bool MenuScene::OnKeyPressedEvent(KeyPressedEvent &event) {
GameObject menuHandler = _Scene->GetEntityByName("MenuHandler");
/*GameObject menuHandler = _Scene->GetEntityByName("MenuHandler");
if (menuHandler.GetEntity()) {
auto &script = menuHandler.GetComponent<ScriptComponent>();
Expand All @@ -81,6 +83,17 @@ namespace RType {
return true;
}
}
return false;
return false;*/

_Scene->GetWorldPtr()->ForEach<ScriptComponent>([&](Entity *entity, auto script) {
auto &sc = script.Get(); // sc = Script Component

if (sc.Instance != nullptr)
sc.Instance->OnKeyPressed(event.GetKeyCode());

(void)entity;
});

return true;
}
}; // namespace RType
2 changes: 2 additions & 0 deletions SandBox/Ethan/Client/src/Layer/GameLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// R-Type Scripts
#include "Scripts/Intro.hpp"
#include "Scripts/MenuHandler.hpp"
#include "Scripts/Particle.hpp"

// R-Type Settings
#include "Settings/Config.hpp"
Expand Down Expand Up @@ -74,6 +75,7 @@ namespace RType {
// -- Register Client scripts -- //
project->RegisterScript("Intro", []() -> ScriptableEntity * { return new Intro(); });
project->RegisterScript("MenuHandler", []() -> ScriptableEntity * { return new MenuHandler(); });
project->RegisterScript("ParticleController", []() -> ScriptableEntity * { return new ParticleController(); });
}

///////////////////////
Expand Down
147 changes: 147 additions & 0 deletions SandBox/Ethan/Client/src/Scripts/Particle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
** EPITECH PROJECT, 2024
** R-Type
** File description:
** Particle
*/

#ifndef PARTICLE_HPP_
#define PARTICLE_HPP_

// Exodia includes
#include "Exodia.hpp"

namespace RType {

using namespace Exodia;

class ParticleController : public ScriptableEntity {

////////////////
// Structures //
////////////////
public:

struct ParticleProps {
glm::vec2 Position;
glm::vec2 Velocity;
glm::vec2 VelocityVariation;
glm::vec4 ColorBegin;
glm::vec4 ColorEnd;
float SizeBegin;
float SizeEnd;
float SizeVariation;
float LifeTime = 1.0f;

int NumParticles = 1;
};

/////////////
// Methods //
/////////////
public:

void OnCreate() override
{
// Smoke Particles
_SmokeParticle.Position = { 0.0f, 0.0f };
_SmokeParticle.Velocity = { -2.0f, 0.0f };
_SmokeParticle.VelocityVariation = { 4.0f, 2.0f };
_SmokeParticle.SizeBegin = 0.35f;
_SmokeParticle.SizeEnd = 0.0f;
_SmokeParticle.SizeVariation = 0.15f;
_SmokeParticle.ColorBegin = { 0.8f, 0.8f, 0.8f, 1.0f };
_SmokeParticle.ColorEnd = { 0.6f, 0.6f, 0.6f, 1.0f };
_SmokeParticle.LifeTime = 4.0f;

// Engine Particles
_EngineParticle.Position = { 0.0f, 0.0f };
_EngineParticle.Velocity = { -2.0f, 0.0f };
_EngineParticle.VelocityVariation = { 3.0f, 1.0f };
_EngineParticle.SizeBegin = 0.5f;
_EngineParticle.SizeEnd = 0.0f;
_EngineParticle.SizeVariation = 0.3f;
_EngineParticle.ColorBegin = { 254 / 255.0f, 109 / 255.0f, 41 / 255.0f, 1.0f };
_EngineParticle.ColorEnd = { 254 / 255.0f, 212 / 255.0f, 123 / 255.0f, 1.0f };
_EngineParticle.LifeTime = 1.0f;
_EngineParticle.NumParticles = 999;
}

void OnUpdate(Timestep ts) override
{
_Time += ts;

if (_Time > _SmokeNextEmitTime) {
Emit(_SmokeParticle);

_SmokeNextEmitTime += _SmokeEmitInterval;
}
}

void OnKeyPressed(int keycode) override
{
if (keycode == Key::SPACE) {
glm::vec2 mousePos = Input::GetMousePosition();

_EngineParticle.Position = mousePos;

Emit(_EngineParticle);
}
}

private:

void Emit(ParticleProps &props)
{
std::mt19937 engine;
std::uniform_real_distribution<float> distribution(0.0f, 1.0f);

for (int i = 0; i < props.NumParticles; i++) {
GameObject particle = HandleEntity.GetScene()->CreateNewEntity("Particle #" + std::to_string(i));

TransformComponent &tc = particle.GetComponent<TransformComponent>();

tc.Translation.x = props.Position.x + Input::GetMousePosition().x;
tc.Translation.y = props.Position.y + Input::GetMousePosition().y;
tc.Rotation.z = (float)distribution(engine);
tc.Scale = glm::vec3(props.SizeBegin + props.SizeVariation * ((float)distribution(engine) - 0.5f));

SpriteRendererComponent &src = particle.AddComponent<SpriteRendererComponent>();

src.Color = props.ColorBegin;

ParticleComponent &pc = particle.AddComponent<ParticleComponent>();

pc.LifeTime = props.LifeTime;
pc.LifeRemaining = props.LifeTime;
pc.ColorBegin = props.ColorBegin;
pc.ColorEnd = props.ColorEnd;
pc.SizeBegin = props.SizeBegin + props.SizeVariation * ((float)distribution(engine) - 0.5f);
pc.SizeEnd = props.SizeEnd;

RigidBody2DComponent &rbc = particle.AddComponent<RigidBody2DComponent>();

rbc.Type = RigidBody2DComponent::BodyType::Dynamic;
rbc.Velocity = props.Velocity;
rbc.Velocity.x += props.VelocityVariation.x * ((float)distribution(engine) - 0.5f);
rbc.Velocity.y += props.VelocityVariation.y * ((float)distribution(engine) - 0.5f);
rbc.GravityScale = 0.0f;
rbc.Mass = 0.0f;
}
}

///////////////
// Atributes //
///////////////
private:

ParticleProps _SmokeParticle;
ParticleProps _EngineParticle;

float _Time;
float _SmokeEmitInterval;
float _SmokeNextEmitTime;
};
};

#endif /* !PARTICLE_HPP_ */

0 comments on commit 2778cb4

Please sign in to comment.