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)
28 virtual_width = SCREEN_WIDTH + max_particle_size * 2;
29 virtual_height = SCREEN_HEIGHT + max_particle_size *2;
30 z_pos = LAYER_BACKGROUND1;
33 ParticleSystem::~ParticleSystem()
35 std::vector<Particle*>::iterator i;
36 for(i = particles.begin(); i != particles.end(); ++i) {
41 void ParticleSystem::draw(DrawingContext& context)
43 float scrollx = context.get_translation().x;
44 float scrolly = context.get_translation().y;
46 context.push_transform();
47 context.set_translation(Vector(max_particle_size,max_particle_size));
49 std::vector<Particle*>::iterator i;
50 for(i = particles.begin(); i != particles.end(); ++i) {
51 Particle* particle = *i;
53 // remap x,y coordinates onto screencoordinates
56 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
57 if(pos.x < 0) pos.x += virtual_width;
59 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
60 if(pos.y < 0) pos.y += virtual_height;
62 //if(pos.x > virtual_width) pos.x -= virtual_width;
63 //if(pos.y > virtual_height) pos.y -= virtual_height;
65 context.draw_surface(particle->texture, pos, z_pos);
68 context.pop_transform();
71 SnowParticleSystem::SnowParticleSystem()
73 snowimages[0] = new Surface("images/objects/particles/snow2.png");
74 snowimages[1] = new Surface("images/objects/particles/snow1.png");
75 snowimages[2] = new Surface("images/objects/particles/snow0.png");
77 virtual_width = SCREEN_WIDTH * 2;
79 // create some random snowflakes
80 size_t snowflakecount = size_t(virtual_width/10.0);
81 for(size_t i=0; i<snowflakecount; ++i) {
82 SnowParticle* particle = new SnowParticle;
83 int snowsize = systemRandom.rand(3);
85 particle->pos.x = systemRandom.randf(virtual_width);
86 particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
87 particle->anchorx = particle->pos.x + (systemRandom.randf(-0.5, 0.5) * 16);
88 particle->drift_speed = systemRandom.randf(-0.5, 0.5) * 0.3;
89 particle->wobble = 0.0;
91 particle->texture = snowimages[snowsize];
93 particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
94 particle->speed *= 20; // gravity
96 particles.push_back(particle);
101 SnowParticleSystem::parse(const Reader& reader)
103 reader.get("z-pos", z_pos);
106 SnowParticleSystem::~SnowParticleSystem()
109 delete snowimages[i];
112 void SnowParticleSystem::update(float elapsed_time)
114 std::vector<Particle*>::iterator i;
116 for(i = particles.begin(); i != particles.end(); ++i) {
117 SnowParticle* particle = (SnowParticle*) *i;
120 particle->pos.y += particle->speed * elapsed_time;
121 particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
123 anchor_delta = (particle->anchorx - particle->pos.x);
124 particle->wobble += (4 * anchor_delta * 0.05) + systemRandom.randf(-0.5, 0.5);
125 particle->wobble *= 0.99f;
126 particle->anchorx += particle->drift_speed * elapsed_time;
130 //FIXME: Sometimes both ghosts have the same image
131 // Ghosts don't change their movement pattern - not random
132 GhostParticleSystem::GhostParticleSystem()
134 ghosts[0] = new Surface("images/objects/particles/ghost0.png");
135 ghosts[1] = new Surface("images/objects/particles/ghost1.png");
137 virtual_width = SCREEN_WIDTH * 2;
140 size_t ghostcount = 2;
141 for(size_t i=0; i<ghostcount; ++i) {
142 GhostParticle* particle = new GhostParticle;
143 particle->pos.x = systemRandom.randf(virtual_width);
144 particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
145 int size = systemRandom.rand(2);
146 particle->texture = ghosts[size];
147 particle->speed = systemRandom.randf(std::max(50, (size * 10)), 180 + (size * 10));
148 particles.push_back(particle);
153 GhostParticleSystem::parse(const Reader& reader)
155 reader.get("z-pos", z_pos);
158 GhostParticleSystem::~GhostParticleSystem()
164 void GhostParticleSystem::update(float elapsed_time)
166 std::vector<Particle*>::iterator i;
167 for(i = particles.begin(); i != particles.end(); ++i) {
168 GhostParticle* particle = (GhostParticle*) *i;
169 particle->pos.y -= particle->speed * elapsed_time;
170 particle->pos.x -= particle->speed * elapsed_time;
171 if(particle->pos.y > SCREEN_HEIGHT) {
172 particle->pos.y = fmodf(particle->pos.y , virtual_height);
173 particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
178 CloudParticleSystem::CloudParticleSystem()
179 : ParticleSystem(128)
181 cloudimage = new Surface("images/objects/particles/cloud.png");
183 virtual_width = 2000.0;
185 // create some random clouds
186 for(size_t i=0; i<15; ++i) {
187 CloudParticle* particle = new CloudParticle;
188 particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
189 particle->pos.y = systemRandom.rand(static_cast<int>(virtual_height));
190 particle->texture = cloudimage;
191 particle->speed = -systemRandom.randf(25.0, 54.0);
193 particles.push_back(particle);
198 CloudParticleSystem::parse(const Reader& reader)
200 reader.get("z-pos", z_pos);
203 CloudParticleSystem::~CloudParticleSystem()
208 void CloudParticleSystem::update(float elapsed_time)
210 std::vector<Particle*>::iterator i;
211 for(i = particles.begin(); i != particles.end(); ++i) {
212 CloudParticle* particle = (CloudParticle*) *i;
213 particle->pos.x += particle->speed * elapsed_time;