66 lines
1.5 KiB
C++
66 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include "ZLMath.h"
|
|
#include "Renderer.h"
|
|
#include "TextureManager.h"
|
|
#include <vector>
|
|
#include <chrono>
|
|
|
|
namespace ZL {
|
|
|
|
struct SparkParticle {
|
|
Vector3f position;
|
|
Vector3f velocity;
|
|
float scale;
|
|
float lifeTime;
|
|
float maxLifeTime;
|
|
bool active;
|
|
int emitterIndex;
|
|
|
|
SparkParticle() : position({ 0,0,0 }), velocity({ 0,0,0 }), scale(1.0f),
|
|
lifeTime(0), maxLifeTime(1000.0f), active(false), emitterIndex(0) {
|
|
}
|
|
};
|
|
|
|
class SparkEmitter {
|
|
private:
|
|
std::vector<SparkParticle> particles;
|
|
std::vector<Vector3f> emissionPoints;
|
|
std::chrono::steady_clock::time_point lastEmissionTime;
|
|
float emissionRate;
|
|
bool isActive;
|
|
|
|
std::vector<Vector3f> drawPositions;
|
|
std::vector<Vector2f> drawTexCoords;
|
|
bool drawDataDirty;
|
|
VertexRenderStruct sparkQuad;
|
|
std::shared_ptr<Texture> texture;
|
|
|
|
int maxParticles;
|
|
|
|
void prepareDrawData();
|
|
|
|
public:
|
|
SparkEmitter();
|
|
SparkEmitter(const std::vector<Vector3f>& positions, float rate = 100.0f);
|
|
SparkEmitter(const std::vector<Vector3f>& positions,
|
|
std::shared_ptr<Texture> tex,
|
|
float rate = 100.0f);
|
|
|
|
void setEmissionPoints(const std::vector<Vector3f>& positions);
|
|
void setTexture(std::shared_ptr<Texture> tex);
|
|
|
|
void update(float deltaTimeMs);
|
|
void emit();
|
|
|
|
void draw(Renderer& renderer, float zoom, int screenWidth, int screenHeight);
|
|
|
|
const std::vector<SparkParticle>& getParticles() const;
|
|
size_t getActiveParticleCount() const;
|
|
|
|
private:
|
|
void initParticle(SparkParticle& particle, int emitterIndex);
|
|
Vector3f getRandomVelocity(int emitterIndex);
|
|
};
|
|
|
|
} // namespace ZL
|