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/particlesystem.hpp"
21 #include "math/random_generator.hpp"
22 #include "supertux/main.hpp"
23 #include "video/drawing_context.hpp"
25 ParticleSystem::ParticleSystem(float max_particle_size) :
26 max_particle_size(max_particle_size),
32 virtual_width = SCREEN_WIDTH + max_particle_size * 2;
33 virtual_height = SCREEN_HEIGHT + max_particle_size *2;
34 z_pos = LAYER_BACKGROUND1;
37 ParticleSystem::~ParticleSystem()
39 std::vector<Particle*>::iterator i;
40 for(i = particles.begin(); i != particles.end(); ++i) {
45 void ParticleSystem::draw(DrawingContext& context)
47 float scrollx = context.get_translation().x;
48 float scrolly = context.get_translation().y;
50 context.push_transform();
51 context.set_translation(Vector(max_particle_size,max_particle_size));
53 std::vector<Particle*>::iterator i;
54 for(i = particles.begin(); i != particles.end(); ++i) {
55 Particle* particle = *i;
57 // remap x,y coordinates onto screencoordinates
60 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
61 if(pos.x < 0) pos.x += virtual_width;
63 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
64 if(pos.y < 0) pos.y += virtual_height;
66 //if(pos.x > virtual_width) pos.x -= virtual_width;
67 //if(pos.y > virtual_height) pos.y -= virtual_height;
69 context.draw_surface(particle->texture, pos, z_pos);
72 context.pop_transform();
75 SnowParticleSystem::SnowParticleSystem()
77 snowimages[0] = new Surface("images/objects/particles/snow2.png");
78 snowimages[1] = new Surface("images/objects/particles/snow1.png");
79 snowimages[2] = new Surface("images/objects/particles/snow0.png");
81 virtual_width = SCREEN_WIDTH * 2;
83 // create some random snowflakes
84 size_t snowflakecount = size_t(virtual_width/10.0);
85 for(size_t i=0; i<snowflakecount; ++i) {
86 SnowParticle* particle = new SnowParticle;
87 int snowsize = systemRandom.rand(3);
89 particle->pos.x = systemRandom.randf(virtual_width);
90 particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
91 particle->anchorx = particle->pos.x + (systemRandom.randf(-0.5, 0.5) * 16);
92 particle->drift_speed = systemRandom.randf(-0.5, 0.5) * 0.3;
93 particle->wobble = 0.0;
95 particle->texture = snowimages[snowsize];
97 particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
98 particle->speed *= 20; // gravity
100 particles.push_back(particle);
105 SnowParticleSystem::parse(const Reader& reader)
107 reader.get("z-pos", z_pos);
110 SnowParticleSystem::~SnowParticleSystem()
113 delete snowimages[i];
116 void SnowParticleSystem::update(float elapsed_time)
118 std::vector<Particle*>::iterator i;
120 for(i = particles.begin(); i != particles.end(); ++i) {
121 SnowParticle* particle = (SnowParticle*) *i;
124 particle->pos.y += particle->speed * elapsed_time;
125 particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
127 anchor_delta = (particle->anchorx - particle->pos.x);
128 particle->wobble += (4 * anchor_delta * 0.05) + systemRandom.randf(-0.5, 0.5);
129 particle->wobble *= 0.99f;
130 particle->anchorx += particle->drift_speed * elapsed_time;
134 //FIXME: Sometimes both ghosts have the same image
135 // Ghosts don't change their movement pattern - not random
136 GhostParticleSystem::GhostParticleSystem()
138 ghosts[0] = new Surface("images/objects/particles/ghost0.png");
139 ghosts[1] = new Surface("images/objects/particles/ghost1.png");
141 virtual_width = SCREEN_WIDTH * 2;
144 size_t ghostcount = 2;
145 for(size_t i=0; i<ghostcount; ++i) {
146 GhostParticle* particle = new GhostParticle;
147 particle->pos.x = systemRandom.randf(virtual_width);
148 particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
149 int size = systemRandom.rand(2);
150 particle->texture = ghosts[size];
151 particle->speed = systemRandom.randf(std::max(50, (size * 10)), 180 + (size * 10));
152 particles.push_back(particle);
157 GhostParticleSystem::parse(const Reader& reader)
159 reader.get("z-pos", z_pos);
162 GhostParticleSystem::~GhostParticleSystem()
168 void GhostParticleSystem::update(float elapsed_time)
170 std::vector<Particle*>::iterator i;
171 for(i = particles.begin(); i != particles.end(); ++i) {
172 GhostParticle* particle = (GhostParticle*) *i;
173 particle->pos.y -= particle->speed * elapsed_time;
174 particle->pos.x -= particle->speed * elapsed_time;
175 if(particle->pos.y > SCREEN_HEIGHT) {
176 particle->pos.y = fmodf(particle->pos.y , virtual_height);
177 particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
182 CloudParticleSystem::CloudParticleSystem() :
186 cloudimage = new Surface("images/objects/particles/cloud.png");
188 virtual_width = 2000.0;
190 // create some random clouds
191 for(size_t i=0; i<15; ++i) {
192 CloudParticle* particle = new CloudParticle;
193 particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
194 particle->pos.y = systemRandom.rand(static_cast<int>(virtual_height));
195 particle->texture = cloudimage;
196 particle->speed = -systemRandom.randf(25.0, 54.0);
198 particles.push_back(particle);
203 CloudParticleSystem::parse(const Reader& reader)
205 reader.get("z-pos", z_pos);
208 CloudParticleSystem::~CloudParticleSystem()
213 void CloudParticleSystem::update(float elapsed_time)
215 std::vector<Particle*>::iterator i;
216 for(i = particles.begin(); i != particles.end(); ++i) {
217 CloudParticle* particle = (CloudParticle*) *i;
218 particle->pos.x += particle->speed * elapsed_time;