about summary refs log tree commit diff
path: root/src/game/client/components/particles.cpp
blob: 61fcf738025167c9f319bc4d9f67b7424bca4a39 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <base/math.hpp>
#include <engine/client/graphics.h>

#include <game/generated/gc_data.hpp>
#include <game/client/render.hpp>
#include <game/gamecore.hpp>
#include "particles.hpp"

PARTICLES::PARTICLES()
{
	on_reset();
	render_trail.parts = this;
	render_explosions.parts = this;
	render_general.parts = this;
}


void PARTICLES::on_reset()
{
	// reset particles
	for(int i = 0; i < MAX_PARTICLES; i++)
	{
		particles[i].prev_part = i-1;
		particles[i].next_part = i+1;
	}
	
	particles[0].prev_part = 0;
	particles[MAX_PARTICLES-1].next_part = -1;
	first_free = 0;

	for(int i = 0; i < NUM_GROUPS; i++)
		first_part[i] = -1;
}

void PARTICLES::add(int group, PARTICLE *part)
{
	if (first_free == -1)
		return;
		
	// remove from the free list
	int id = first_free;
	first_free = particles[id].next_part;
	particles[first_free].prev_part = -1;
	
	// copy data
	particles[id] = *part;
	
	// insert to the group list
	particles[id].prev_part = -1;
	particles[id].next_part = first_part[group];
	if(first_part[group] != -1)
		particles[first_part[group]].prev_part = id;
	first_part[group] = id;
	
	// set some parameters
	particles[id].life = 0;
}

void PARTICLES::update(float time_passed)
{
	static float friction_fraction = 0;
	friction_fraction += time_passed;

	if(friction_fraction > 2.0f) // safty messure
		friction_fraction = 0;
	
	int friction_count = 0;
	while(friction_fraction > 0.05f)
	{
		friction_count++;
		friction_fraction -= 0.05f;
	}
	
	for(int g = 0; g < NUM_GROUPS; g++)
	{
		int i = first_part[g];
		while(i != -1)
		{
			int next = particles[i].next_part;
			//particles[i].vel += flow_get(particles[i].pos)*time_passed * particles[i].flow_affected;
			particles[i].vel.y += particles[i].gravity*time_passed;
			
			for(int f = 0; f < friction_count; f++) // apply friction
				particles[i].vel *= particles[i].friction;
			
			// move the point
			vec2 vel = particles[i].vel*time_passed;
			move_point(&particles[i].pos, &vel, 0.1f+0.9f*frandom(), NULL);
			particles[i].vel = vel* (1.0f/time_passed);
			
			particles[i].life += time_passed;
			particles[i].rot += time_passed * particles[i].rotspeed;

			// check particle death
			if(particles[i].life > particles[i].life_span)
			{
				// remove it from the group list
				if(particles[i].prev_part != -1)
					particles[particles[i].prev_part].next_part = particles[i].next_part;
				else
					first_part[g] = particles[i].next_part;
					
				if(particles[i].next_part != -1)
					particles[particles[i].next_part].prev_part = particles[i].prev_part;
					
				// insert to the free list
				if(first_free != -1)
					particles[first_free].prev_part = i;
				particles[i].prev_part = -1;
				particles[i].next_part = first_free;
				first_free = i;
			}
			
			i = next;
		}
	}
}

void PARTICLES::on_render()
{
	static int64 lasttime = 0;
	int64 t = time_get();
	update((float)((t-lasttime)/(double)time_freq()));
	lasttime = t;
}

void PARTICLES::render_group(int group)
{
	Graphics()->BlendNormal();
	//gfx_blend_additive();
	Graphics()->TextureSet(data->images[IMAGE_PARTICLES].id);
	Graphics()->QuadsBegin();

	int i = first_part[group];
	while(i != -1)
	{
		RenderTools()->select_sprite(particles[i].spr);
		float a = particles[i].life / particles[i].life_span;
		vec2 p = particles[i].pos;
		float size = mix(particles[i].start_size, particles[i].end_size, a);

		Graphics()->QuadsSetRotation(particles[i].rot);

		Graphics()->SetColor(
			particles[i].color.r,
			particles[i].color.g,
			particles[i].color.b,
			particles[i].color.a); // pow(a, 0.75f) * 

		Graphics()->QuadsDraw(p.x, p.y, size, size);
		
		i = particles[i].next_part;
	}
	Graphics()->QuadsEnd();
	Graphics()->BlendNormal();
}