1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_CLIENT_COMPONENTS_PARTICLES_H
4#define GAME_CLIENT_COMPONENTS_PARTICLES_H
5#include <base/color.h>
6#include <base/vmath.h>
7#include <game/client/component.h>
8
9// particles
10struct CParticle
11{
12 void SetDefault()
13 {
14 m_Pos = vec2(0, 0);
15 m_Vel = vec2(0, 0);
16 m_LifeSpan = 0;
17 m_StartSize = 32;
18 m_EndSize = 32;
19 m_UseAlphaFading = false;
20 m_StartAlpha = 1;
21 m_EndAlpha = 1;
22 m_Rot = 0;
23 m_Rotspeed = 0;
24 m_Gravity = 0;
25 m_Friction = 0;
26 m_FlowAffected = 1.0f;
27 m_Color = ColorRGBA(1, 1, 1, 1);
28 m_Collides = true;
29 }
30
31 vec2 m_Pos;
32 vec2 m_Vel;
33
34 int m_Spr;
35
36 float m_FlowAffected;
37
38 float m_LifeSpan;
39
40 float m_StartSize;
41 float m_EndSize;
42
43 bool m_UseAlphaFading;
44 float m_StartAlpha;
45 float m_EndAlpha;
46
47 float m_Rot;
48 float m_Rotspeed;
49
50 float m_Gravity;
51 float m_Friction;
52
53 ColorRGBA m_Color;
54
55 bool m_Collides;
56
57 // set by the particle system
58 float m_Life;
59 int m_PrevPart;
60 int m_NextPart;
61};
62
63class CParticles : public CComponent
64{
65 friend class CGameClient;
66
67public:
68 enum
69 {
70 GROUP_PROJECTILE_TRAIL = 0,
71 GROUP_EXPLOSIONS,
72 GROUP_EXTRA,
73 GROUP_GENERAL,
74 NUM_GROUPS
75 };
76
77 CParticles();
78 virtual int Sizeof() const override { return sizeof(*this); }
79
80 void Add(int Group, CParticle *pPart, float TimePassed = 0.f);
81
82 virtual void OnReset() override;
83 virtual void OnRender() override;
84 virtual void OnInit() override;
85
86private:
87 int m_ParticleQuadContainerIndex;
88 int m_ExtraParticleQuadContainerIndex;
89
90 enum
91 {
92 MAX_PARTICLES = 1024 * 8,
93 };
94
95 CParticle m_aParticles[MAX_PARTICLES];
96 int m_FirstFree;
97 int m_aFirstPart[NUM_GROUPS];
98
99 void RenderGroup(int Group);
100 void Update(float TimePassed);
101
102 template<int TGROUP>
103 class CRenderGroup : public CComponent
104 {
105 public:
106 CParticles *m_pParts;
107 virtual int Sizeof() const override { return sizeof(*this); }
108 virtual void OnRender() override { m_pParts->RenderGroup(Group: TGROUP); }
109 };
110
111 CRenderGroup<GROUP_PROJECTILE_TRAIL> m_RenderTrail;
112 CRenderGroup<GROUP_EXPLOSIONS> m_RenderExplosions;
113 CRenderGroup<GROUP_EXTRA> m_RenderExtra;
114 CRenderGroup<GROUP_GENERAL> m_RenderGeneral;
115
116 bool ParticleIsVisibleOnScreen(const vec2 &CurPos, float CurSize);
117};
118#endif
119