-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParticleComponent.h
88 lines (71 loc) · 2.41 KB
/
ParticleComponent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Defines both the Particle component and graphical representation Particles class.
*
* Author: Skylar Payne
* Date: 02/21/2014
* File: ParticleComponent.h
**/
#pragma once
#include "IRenderComponent.h"
#include "SFML/Graphics.hpp"
#include "Logger.h"
class Particles : public sf::Drawable, public sf::Transformable
{
friend class ParticleComponent;
friend class ParticleSystem;
private:
sf::VertexArray _vertices;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.texture = nullptr;
target.draw(_vertices, states);
}
public:
Particles(unsigned int count) : _vertices(sf::Points, count) { }
};
struct ParticleInfo
{
sf::Vector2f velocity;
sf::Time life;
};
class ParticleComponent : public IRenderComponent
{
friend class ParticleSystem;
private:
std::vector<ParticleInfo> _particleInfo;
Particles _particles;
sf::Time _lifeTime;
public:
ParticleComponent(unsigned int count = 0, unsigned int z = 0, sf::Vector2f offset = sf::Vector2f(0.f, 0.f)) :
IRenderComponent("Particle", z, offset), _particleInfo(count), _particles(count), _lifeTime(sf::seconds(3)) { }
void resizeParticles(unsigned int count) {
_particles._vertices.resize(count);
_particleInfo.resize(count);
}
void resetParticle(unsigned int index) {
float angle = (rand() % 360) * 3.14f / 180.f;
float speed = (rand() % 50) + 50.0f;
_particleInfo[index].velocity = sf::Vector2f(std::cos(angle) * speed, std::sin(angle) * speed);
_particleInfo[index].life = sf::milliseconds((rand() % 2000) + 1000);
_particles._vertices[index].position = _particles.getPosition();
}
void rLoad(lua_State *L) override
{
lua_pushstring(L, "count");
lua_gettable(L, -2);
this->resizeParticles(lua_tonumber(L, -1));
lua_pop(L, 1);
lua_pushstring(L, "lifetime");
lua_gettable(L, -2);
_lifeTime = sf::seconds(lua_tonumber(L, -1));
lua_pop(L, 1);
lua_getglobal(L, "x");
lua_getglobal(L, "y");
_particles.setPosition(lua_tonumber(L, -2) + _offset.x, lua_tonumber(L, -1) + _offset.y);
lua_pop(L, 2);
}
sf::Drawable const& GetDrawable() const { return _particles; }
void SetPosition(sf::Vector2f const& pos) {
_particles.setPosition(sf::Vector2f(pos.x + 10, pos.y + 10));
}
};