2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "object/snow_particle_system.hpp"
21 #include "math/random_generator.hpp"
22 #include "supertux/globals.hpp"
23 #include "video/drawing_context.hpp"
25 SnowParticleSystem::SnowParticleSystem()
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");
31 virtual_width = SCREEN_WIDTH * 2;
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);
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;
45 particle->texture = snowimages[snowsize];
47 particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
48 particle->speed *= 20; // gravity
50 particles.push_back(particle);
55 SnowParticleSystem::parse(const Reader& reader)
57 reader.get("z-pos", z_pos);
60 SnowParticleSystem::~SnowParticleSystem()
64 void SnowParticleSystem::update(float elapsed_time)
66 std::vector<Particle*>::iterator i;
68 for(i = particles.begin(); i != particles.end(); ++i) {
69 SnowParticle* particle = (SnowParticle*) *i;
72 particle->pos.y += particle->speed * elapsed_time;
73 particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
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;