diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2024-12-12 15:32:07 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2024-12-12 15:32:07 +0100 |
commit | afd81007153f05c8f8b42bcf08a8cdf8ce13a98c (patch) | |
tree | 1c6eec51833d34c06118b1c76f19f035b526d6c6 /src | |
parent | 3de0a0f12d9d91e120f92975bda6f9915e5b8fcb (diff) |
updated particle emitter
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 1 | ||||
-rw-r--r-- | src/crepe/api/ParticleEmitter.h | 7 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.cpp | 16 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 2 | ||||
-rw-r--r-- | src/example/game.cpp | 21 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 23 |
6 files changed, 39 insertions, 31 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index b5e5ff7..7a78019 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -65,6 +65,7 @@ void LoopManager::fixed_update() { this->get_system<InputSystem>().update(); this->event_manager.dispatch_events(); this->get_system<ScriptSystem>().update(); + this->get_system<ParticleSystem>().update(); this->get_system<AISystem>().update(); this->get_system<PhysicsSystem>().update(); this->get_system<CollisionSystem>().update(); diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 48c7625..cdea69a 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -5,6 +5,7 @@ #include "Component.h" #include "Particle.h" +#include "system/ParticleSystem.h" #include "types.h" namespace crepe { @@ -52,8 +53,6 @@ public: const unsigned int max_particles = 256; //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored) float emission_rate = 1; - //! Saves time left over from last update event. - float spawn_accumulator = 0; //! min speed of the particles float min_speed = 1; //! min speed of the particles @@ -85,6 +84,10 @@ public: public: //! Configuration data for particle emission settings. Data data; +private: + //! Saves time left over from last update event. + friend ParticleSystem; + float spawn_accumulator = 0; }; } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 3befb03..941e502 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -1,3 +1,4 @@ +#include <chrono> #include <cmath> #include <cstdlib> #include <ctime> @@ -5,6 +6,7 @@ #include "../api/ParticleEmitter.h" #include "../api/Transform.h" #include "../manager/ComponentManager.h" +#include "../manager/LoopTimerManager.h" #include "ParticleSystem.h" @@ -12,10 +14,12 @@ using namespace crepe; void ParticleSystem::update() { // Get all emitters - - ComponentManager & mgr = this->mediator.component_manager; + const Mediator & mediator = this->mediator; + LoopTimerManager & loop_timer = mediator.loop_timer; + ComponentManager & mgr = mediator.component_manager; + float dt = std::chrono::duration<float>(loop_timer.get_scaled_fixed_delta_time()).count(); + RefVector<ParticleEmitter> emitters = mgr.get_components_by_type<ParticleEmitter>(); - double dt = LoopTimer::get_instance().get_fixed_delta_time(); for (ParticleEmitter & emitter : emitters) { // Get transform linked to emitter @@ -23,10 +27,10 @@ void ParticleSystem::update() { = mgr.get_components_by_id<Transform>(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - emitter.data.spawn_accumulator = emitter.data.emission_rate * dt; - while (emitter.data.spawn_accumulator >= 1.0) { + emitter.spawn_accumulator = emitter.data.emission_rate * dt; + while (emitter.spawn_accumulator >= 1.0) { this->emit_particle(emitter, transform); - emitter.data.spawn_accumulator -= 1.0; + emitter.spawn_accumulator -= 1.0; } // Update all particles diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index afd9548..cc633e8 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -83,7 +83,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { - if (&em.data.sprite != &sprite) continue; + if (&em.sprite != &sprite) continue; rendering_particles = true; if (!em.active) continue; diff --git a/src/example/game.cpp b/src/example/game.cpp index 5361f3a..279648e 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -1,4 +1,5 @@ #include "api/CircleCollider.h" +#include "api/ParticleEmitter.h" #include "api/Scene.h" #include "manager/ComponentManager.h" #include "manager/Mediator.h" @@ -258,6 +259,26 @@ public: }) .active = false; + Asset img5{"asset/texture/square.png"}; + + + + GameObject particle = mgr.new_object( + "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); + auto & particle_image = particle.add_component<Sprite>(img5, Sprite::Data{.size = {5, 5},}); + auto & test = particle.add_component<ParticleEmitter>(particle_image,ParticleEmitter::Data{ + .position = {0, 0}, + .max_particles = 256, + .emission_rate = 50, + .min_speed = 10, + .max_speed = 20, + .min_angle = -20, + .max_angle = 20, + .begin_lifespan = 0, + .end_lifespan = 5, + } + ); + } string get_name() const { return "scene1"; } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 13e625f..2b5c041 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -17,28 +17,7 @@ using namespace crepe; using namespace std; - -/* - auto & test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 10, - .emission_rate = 0.1, - .min_speed = 6, - .max_speed = 20, - .min_angle = -20, - .max_angle = 20, - .begin_lifespan = 0, - .end_lifespan = 60, - .force_over_time = vec2{0, 0}, - .boundary{ - .width = 1000, - .height = 1000, - .offset = vec2{0, 0}, - .reset_on_exit = false, - }, - .sprite = test_sprite, - }); - */ + class TestScene : public Scene { public: |