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
8#include <game/client/component.h>
9
10// particles
11struct CParticle
12{
13 void SetDefault()
14 {
15 m_Pos = vec2(0, 0);
16 m_Vel = vec2(0, 0);
17 m_LifeSpan = 0;
18 m_StartSize = 32;
19 m_EndSize = 32;
20 m_UseAlphaFading = false;
21 m_StartAlpha = 1;
22 m_EndAlpha = 1;
23 m_Rot = 0;
24 m_Rotspeed = 0;
25 m_Gravity = 0;
26 m_Friction = 0;
27 m_FlowAffected = 1.0f;
28 m_Color = ColorRGBA(1, 1, 1, 1);
29 m_Collides = true;
30 }
31
32 vec2 m_Pos;
33 vec2 m_Vel;
34
35 int m_Spr;
36
37 float m_FlowAffected;
38
39 float m_LifeSpan;
40
41 float m_StartSize;
42 float m_EndSize;
43
44 bool m_UseAlphaFading;
45 float m_StartAlpha;
46 float m_EndAlpha;
47
48 float m_Rot;
49 float m_Rotspeed;
50
51 float m_Gravity;
52 float m_Friction;
53
54 ColorRGBA m_Color;
55
56 bool m_Collides;
57
58 // set by the particle system
59 float m_Life;
60 int m_PrevPart;
61 int m_NextPart;
62};
63
64class CParticles : public CComponent
65{
66 friend class CGameClient;
67
68public:
69 enum
70 {
71 GROUP_PROJECTILE_TRAIL = 0,
72 GROUP_TRAIL_EXTRA,
73 GROUP_EXPLOSIONS,
74 GROUP_EXTRA,
75 GROUP_GENERAL,
76 NUM_GROUPS
77 };
78
79 CParticles();
80 int Sizeof() const override { return sizeof(*this); }
81
82 void Add(int Group, CParticle *pPart, float TimePassed = 0.f);
83
84 void OnReset() override;
85 void OnRender() override;
86 void OnInit() override;
87
88private:
89 int m_ParticleQuadContainerIndex;
90 int m_ExtraParticleQuadContainerIndex;
91
92 enum
93 {
94 MAX_PARTICLES = 1024 * 8,
95 };
96
97 CParticle m_aParticles[MAX_PARTICLES];
98 int m_FirstFree;
99 int m_aFirstPart[NUM_GROUPS];
100
101 float m_FrictionFraction = 0.0f;
102 int64_t m_LastRenderTime = 0;
103
104 void RenderGroup(int Group);
105 void Update(float TimePassed);
106
107 template<int TGROUP>
108 class CRenderGroup : public CComponent
109 {
110 public:
111 CParticles *m_pParts;
112 int Sizeof() const override { return sizeof(*this); }
113 void OnRender() override { m_pParts->RenderGroup(Group: TGROUP); }
114 };
115
116 // behind players
117 CRenderGroup<GROUP_PROJECTILE_TRAIL> m_RenderTrail;
118 CRenderGroup<GROUP_TRAIL_EXTRA> m_RenderTrailExtra;
119 // in front of players
120 CRenderGroup<GROUP_EXPLOSIONS> m_RenderExplosions;
121 CRenderGroup<GROUP_EXTRA> m_RenderExtra;
122 CRenderGroup<GROUP_GENERAL> m_RenderGeneral;
123
124 bool ParticleIsVisibleOnScreen(const vec2 &CurPos, float CurSize);
125};
126#endif
127