cc02ffc1bb50a291c31e27f6bbc4c255355f272a
[supertux.git] / src / object / snow_particle_system.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/snow_particle_system.hpp"
18
19 #include <math.h>
20
21 #include "math/random_generator.hpp"
22 #include "supertux/globals.hpp"
23 #include "video/drawing_context.hpp"
24
25 SnowParticleSystem::SnowParticleSystem()
26 {
27   snowimages[0] = Surface::create("images/objects/particles/snow2.png");
28   snowimages[1] = Surface::create("images/objects/particles/snow1.png");
29   snowimages[2] = Surface::create("images/objects/particles/snow0.png");
30
31   virtual_width = SCREEN_WIDTH * 2;
32
33   // create some random snowflakes
34   size_t snowflakecount = size_t(virtual_width/10.0);
35   for(size_t i=0; i<snowflakecount; ++i) {
36     SnowParticle* particle = new SnowParticle;
37     int snowsize = systemRandom.rand(3);
38
39     particle->pos.x = systemRandom.randf(virtual_width);
40     particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
41     particle->anchorx = particle->pos.x + (systemRandom.randf(-0.5, 0.5) * 16);
42     particle->drift_speed = systemRandom.randf(-0.5, 0.5) * 0.3;
43     particle->wobble = 0.0;
44
45     particle->texture = snowimages[snowsize];
46
47     particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
48     particle->speed *= 20; // gravity
49
50     particles.push_back(particle);
51   }
52 }
53
54 void
55 SnowParticleSystem::parse(const Reader& reader)
56 {
57   reader.get("z-pos", z_pos);
58 }
59
60 SnowParticleSystem::~SnowParticleSystem()
61 {
62 }
63
64 void SnowParticleSystem::update(float elapsed_time)
65 {
66   std::vector<Particle*>::iterator i;
67
68   for(i = particles.begin(); i != particles.end(); ++i) {
69     SnowParticle* particle = (SnowParticle*) *i;
70     float anchor_delta;
71
72     particle->pos.y += particle->speed * elapsed_time;
73     particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
74
75     anchor_delta = (particle->anchorx - particle->pos.x);
76     particle->wobble += (4 * anchor_delta * 0.05) + systemRandom.randf(-0.5, 0.5);
77     particle->wobble *= 0.99f;
78     particle->anchorx += particle->drift_speed * elapsed_time;
79   }
80 }
81
82 /* EOF */