20130313
[gdash.git] / src / cave / particle.cpp
blobaa3b99a8070fdc7dca75348cbcbccb4d7a4d419e
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <glib.h>
19 #include "cave/particle.hpp"
21 ParticleSet::ParticleSet(int count, float size, float opacity, float p0x, float p0y, float dp0x, float dp0y, float v0x, float v0y, float dvx, float dvy, const GdColor &color)
22 : color(color)
23 , life(1000)
24 , is_new(true)
25 , size(size)
26 , opacity(opacity)
27 , particles(count) {
28 for (size_t i = 0; i < particles.size(); ++i) {
29 Particle &p = particles[i];
30 p.px = p0x + g_random_double_range(-dp0x, dp0x);
31 p.py = p0y + g_random_double_range(-dp0y, dp0y);
32 p.vx = v0x + g_random_double_range(-dvx, dvx);
33 p.vy = v0y + g_random_double_range(-dvy, dvy);
38 void ParticleSet::move(int dt_ms) {
39 float dt = dt_ms/1000.0;
41 for (size_t i = 0; i < particles.size(); ++i) {
42 Particle &p = particles[i];
43 p.px += p.vx * dt;
44 p.py += p.vy * dt;
47 life -= dt_ms;
51 void ParticleSet::normalize(double factor) {
52 if (!is_new)
53 return;
54 is_new = false;
56 size *= factor;
57 for (size_t i = 0; i < particles.size(); ++i) {
58 Particle &p = particles[i];
59 p.px *= factor;
60 p.py *= factor;
61 p.vx *= factor;
62 p.vy *= factor;