More -Weffc++ cleanup, almost done
[supertux.git] / src / object / particlesystem.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/particlesystem.hpp"
18
19 #include <cmath>
20
21 #include "math/random_generator.hpp"
22 #include "supertux/main.hpp"
23 #include "video/drawing_context.hpp"
24
25 ParticleSystem::ParticleSystem(float max_particle_size) :
26   max_particle_size(max_particle_size),
27   z_pos(),
28   particles(),
29   virtual_width(),
30   virtual_height()
31 {
32   virtual_width = SCREEN_WIDTH + max_particle_size * 2;
33   virtual_height = SCREEN_HEIGHT + max_particle_size *2;
34   z_pos = LAYER_BACKGROUND1;
35 }
36
37 ParticleSystem::~ParticleSystem()
38 {
39   std::vector<Particle*>::iterator i;
40   for(i = particles.begin(); i != particles.end(); ++i) {
41     delete *i;
42   }
43 }
44
45 void ParticleSystem::draw(DrawingContext& context)
46 {
47   float scrollx = context.get_translation().x;
48   float scrolly = context.get_translation().y;
49
50   context.push_transform();
51   context.set_translation(Vector(max_particle_size,max_particle_size));
52
53   std::vector<Particle*>::iterator i;
54   for(i = particles.begin(); i != particles.end(); ++i) {
55     Particle* particle = *i;
56
57     // remap x,y coordinates onto screencoordinates
58     Vector pos;
59
60     pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
61     if(pos.x < 0) pos.x += virtual_width;
62
63     pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
64     if(pos.y < 0) pos.y += virtual_height;
65
66     //if(pos.x > virtual_width) pos.x -= virtual_width;
67     //if(pos.y > virtual_height) pos.y -= virtual_height;
68
69     context.draw_surface(particle->texture, pos, z_pos);
70   }
71
72   context.pop_transform();
73 }
74
75 SnowParticleSystem::SnowParticleSystem()
76 {
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");
80
81   virtual_width = SCREEN_WIDTH * 2;
82
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);
88
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;
94
95     particle->texture = snowimages[snowsize];
96
97     particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
98     particle->speed *= 20; // gravity
99
100     particles.push_back(particle);
101   }
102 }
103
104 void
105 SnowParticleSystem::parse(const Reader& reader)
106 {
107   reader.get("z-pos", z_pos);
108 }
109
110 SnowParticleSystem::~SnowParticleSystem()
111 {
112   for(int i=0;i<3;++i)
113     delete snowimages[i];
114 }
115
116 void SnowParticleSystem::update(float elapsed_time)
117 {
118   std::vector<Particle*>::iterator i;
119
120   for(i = particles.begin(); i != particles.end(); ++i) {
121     SnowParticle* particle = (SnowParticle*) *i;
122     float anchor_delta;
123
124     particle->pos.y += particle->speed * elapsed_time;
125     particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
126
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;
131   }
132 }
133
134 //FIXME: Sometimes both ghosts have the same image
135 //       Ghosts don't change their movement pattern - not random
136 GhostParticleSystem::GhostParticleSystem()
137 {
138   ghosts[0] = new Surface("images/objects/particles/ghost0.png");
139   ghosts[1] = new Surface("images/objects/particles/ghost1.png");
140
141   virtual_width = SCREEN_WIDTH * 2;
142
143   // create two ghosts
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);
153   }
154 }
155
156 void
157 GhostParticleSystem::parse(const Reader& reader)
158 {
159   reader.get("z-pos", z_pos);
160 }
161
162 GhostParticleSystem::~GhostParticleSystem()
163 {
164   for(int i=0;i<2;++i)
165     delete ghosts[i];
166 }
167
168 void GhostParticleSystem::update(float elapsed_time)
169 {
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));
178     }
179   }
180 }
181
182 CloudParticleSystem::CloudParticleSystem() :
183   ParticleSystem(128),
184   cloudimage()
185 {
186   cloudimage = new Surface("images/objects/particles/cloud.png");
187
188   virtual_width = 2000.0;
189
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);
197
198     particles.push_back(particle);
199   }
200 }
201
202 void
203 CloudParticleSystem::parse(const Reader& reader)
204 {
205   reader.get("z-pos", z_pos);
206 }
207
208 CloudParticleSystem::~CloudParticleSystem()
209 {
210   delete cloudimage;
211 }
212
213 void CloudParticleSystem::update(float elapsed_time)
214 {
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;
219   }
220 }
221
222 /* EOF */