Remove particles when they get off the screen.
[supertux.git] / src / gameobjs.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21
22 #include <algorithm>
23 #include <iostream>
24 #include <cmath>
25
26 #include "app/globals.h"
27 #include "tile.h"
28 #include "tile_manager.h"
29 #include "gameloop.h"
30 #include "gameobjs.h"
31 #include "special/sprite_manager.h"
32 #include "resources.h"
33 #include "sector.h"
34 #include "tilemap.h"
35 #include "video/drawing_context.h"
36 #include "camera.h"
37
38 BouncyDistro::BouncyDistro(const Vector& pos)
39   : position(pos)
40 {
41   ym = -2;
42 }
43
44 void
45 BouncyDistro::action(float elapsed_time)
46 {
47   position.y += ym * elapsed_time;
48
49   ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
50   if(ym >= 0)
51     remove_me();
52 }
53
54 void
55 BouncyDistro::draw(DrawingContext& context)
56 {
57   context.draw_surface(img_distro[0], position, LAYER_OBJECTS);
58 }
59
60
61 BrokenBrick::BrokenBrick(Tile* ntile,const Vector& pos, const Vector& nmovement)
62   : tile(ntile), position(pos), movement(nmovement)
63 {
64   timer.start(200);
65 }
66
67 void
68 BrokenBrick::action(float elapsed_time)
69 {
70   position += movement * elapsed_time;
71
72   if (!timer.check())
73     remove_me();
74 }
75
76 void
77 BrokenBrick::draw(DrawingContext& context)
78 {
79   if (tile->images.size() > 0)
80     context.draw_surface_part(tile->images[0],
81         Vector(rand() % 16, rand() % 16),
82         Vector(16, 16),
83         position, LAYER_OBJECTS + 1);
84 }
85
86 BouncyBrick::BouncyBrick(const Vector& pos)
87   : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED), 
88     shape(Sector::current()->solids->get_tile_id_at(pos))
89
90   shape.hidden = true;
91 }
92
93 void
94 BouncyBrick::action(float elapsed_time)
95 {
96   offset += offset_m * elapsed_time;
97
98   /* Go back down? */
99   if (offset < -BOUNCY_BRICK_MAX_OFFSET)
100     offset_m = BOUNCY_BRICK_SPEED;
101
102   /* Stop bouncing? */
103   if (offset >= 0)
104     {
105       shape.hidden = false;
106       remove_me();
107     }
108 }
109
110 void
111 BouncyBrick::draw(DrawingContext& context)
112 {
113   TileManager::instance()->
114     draw_tile(context, shape.id, position + Vector(0, offset), LAYER_TILES+1);
115 }
116
117 FloatingScore::FloatingScore(const Vector& pos, int score)
118   : position(pos)
119 {
120   timer.start(1000);
121   snprintf(str, 10, "%d", score);
122   position.x -= strlen(str) * 8;
123 }
124
125 void
126 FloatingScore::action(float elapsed_time)
127 {
128   position.y -= 2 * elapsed_time;
129
130   if(!timer.check())
131     remove_me();
132 }
133
134 void
135 FloatingScore::draw(DrawingContext& context)
136 {
137   context.draw_text(gold_text, str, position, LEFT_ALLIGN, LAYER_OBJECTS);
138 }
139
140 /* Trampoline */
141
142 Sprite *img_trampoline;
143
144 Trampoline::Trampoline(LispReader& reader)
145 {
146   reader.read_float("x", base.x);
147   reader.read_float("y", base.y); 
148   base.width = 32;
149   base.height = 32;
150   power = 7.5;
151   reader.read_float("power", power);
152
153   frame = 0;
154   mode = M_NORMAL;
155   physic.reset();
156 }
157
158 Trampoline::Trampoline(float x, float y)
159 {
160   base.x = x;
161   base.y = y;
162   base.width = 32;
163   base.height = 32;
164   power = 7.5;
165
166   frame = 0;
167   mode = M_NORMAL;
168   physic.reset();
169 }
170
171 void
172 Trampoline::write(LispWriter& writer)
173 {
174   writer.start_list("trampoline");
175
176   writer.write_float("x", base.x);
177   writer.write_float("y", base.y);
178   writer.write_float("power", power);
179
180   writer.end_list("trampoline");
181 }
182
183 void
184 Trampoline::draw(DrawingContext& context)
185 {
186   img_trampoline->set_frame(frame);
187   img_trampoline->draw(context, base, LAYER_OBJECTS);
188   frame = 0;
189 }
190
191 void
192 Trampoline::action(float frame_ratio)
193 {
194   // TODO: Remove if we're too far off the screen
195
196   // Falling
197   if (mode != M_HELD)
198   {
199     if (issolid(base.x + base.width/2, base.y + base.height))
200     {
201       base.y = int((base.y + base.height)/32) * 32 - base.height;
202
203       physic.enable_gravity(false);
204       physic.set_velocity_y(0.0f);
205
206       physic.set_velocity_x(0);
207     }
208     else
209     {
210       physic.enable_gravity(true);
211     }
212   }
213   else // Player is carrying us around
214   {
215     /* FIXME: The trampoline object shouldn't know about pplayer objects. */
216     /* If we're holding the iceblock */
217     Player& tux = *Sector::current()->player;
218     Direction dir = tux.dir;
219
220     if(dir == RIGHT)
221     {
222       base.x = tux.base.x + 16;
223       base.y = tux.base.y + tux.base.height/1.5 - base.height;
224     }
225     else /* facing left */
226     {
227       base.x = tux.base.x - 16;
228       base.y = tux.base.y + tux.base.height/1.5 - base.height;
229     }
230
231     if(collision_object_map(base))
232     {
233       base.x = tux.base.x;
234       base.y = tux.base.y + tux.base.height/1.5 - base.height;
235     }
236   }
237
238   physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
239   collision_swept_object_map(&old_base, &base);
240 }
241
242 void
243 Trampoline::collision(const MovingObject&, int)
244 {
245   // comes later
246 }
247
248 void
249 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
250 {
251   Player* pplayer_c = NULL;
252   switch (c_object)
253   {
254     case CO_PLAYER:
255       pplayer_c = (Player*) p_c_object;
256
257       if (type == COLLISION_NORMAL)
258       {
259         // Pick up if HELD (done in Player)
260       }
261
262       else if (type == COLLISION_SQUISH)
263       {
264         int squish_amount = (32 - (int)pplayer_c->base.y % 32);
265
266         if (squish_amount < 24)
267           frame = 3;
268         else if (squish_amount < 28)
269           frame = 2;
270         else if (squish_amount < 30)
271           frame = 1;
272         else
273           frame = 0;
274
275         if (squish_amount < 20) {
276           pplayer_c->physic.set_velocity_y(power);
277           pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
278         }
279         else if (pplayer_c->physic.get_velocity_y() < 0)
280           pplayer_c->physic.set_velocity_y(-squish_amount/32);
281       }
282
283       break;
284
285     default:
286       break;
287     
288   }
289 }
290
291 /* Flying Platform */
292
293 Sprite *img_flying_platform;
294
295 FlyingPlatform::FlyingPlatform(LispReader& reader)
296 {
297   reader.read_int_vector("x", pos_x);
298   reader.read_int_vector("y", pos_y);
299
300   velocity = 2.0;
301   reader.read_float("velocity", velocity);
302
303   base.x = pos_x[0];
304   base.y = pos_y[0];
305   base.width = 96;
306   base.height = 40;
307
308   point = 0;
309   move = false;
310
311   float x = pos_x[point+1] - pos_x[point];
312   float y = pos_y[point+1] - pos_y[point];
313   vel_x = x*velocity / sqrt(x*x + y*y);
314   vel_y = -(velocity - vel_x);
315
316   frame = 0;
317 }
318
319 FlyingPlatform::FlyingPlatform(int x, int y)
320 {
321 base.x = x;
322 base.y = y;
323 point = 0;
324 move = false;
325 }
326
327 void
328 FlyingPlatform::write(LispWriter& writer)
329 {
330   writer.start_list("flying-trampoline");
331
332   writer.write_int_vector("x", pos_x);
333   writer.write_int_vector("y", pos_y);
334   writer.write_float("velocity", velocity);
335
336   writer.end_list("flying-trampoline");
337 }
338
339 void
340 FlyingPlatform::draw(DrawingContext& context)
341 {
342   img_flying_platform->draw(context, base, LAYER_OBJECTS);
343 }
344
345 void
346 FlyingPlatform::action(float frame_ratio)
347 {
348   // TODO: Remove if we're too far off the screen
349
350 if(!move)
351   return;
352
353 if((unsigned)point+1 != pos_x.size())
354   {
355   if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
356       (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
357       pos_x[point] == pos_x[point+1]) &&
358     ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
359       (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
360       pos_y[point] == pos_y[point+1]))
361     {
362     point++;
363
364     float x = pos_x[point+1] - pos_x[point];
365     float y = pos_y[point+1] - pos_y[point];
366     vel_x = x*velocity / sqrt(x*x + y*y);
367     vel_y = -(velocity - vel_x);
368     }
369   }
370 else   // last point
371   {
372   // point = 0;
373   // reverse vector
374   return;
375   }
376 /*
377 if(pos_x[point+1] > base.x)
378   base.x += velocity * frame_ratio;
379 else if(pos_x[point+1] < base.x)
380   base.x -= velocity * frame_ratio;
381
382 if(pos_y[point+1] > base.y)
383   base.y += velocity * frame_ratio;
384 else if(pos_y[point+1] < base.y)
385   base.y -= velocity * frame_ratio;
386 */
387
388 base.x += vel_x * frame_ratio;
389 base.y += vel_y * frame_ratio;
390 }
391
392 void
393 FlyingPlatform::collision(const MovingObject&, int)
394 {
395   // comes later
396 }
397
398 void
399 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
400 {
401 (void) p_c_object;
402 (void) type;
403
404 //  Player* pplayer_c = NULL;
405   switch (c_object)
406   {
407     case CO_PLAYER:
408 //      pplayer_c = (Player*) p_c_object;
409       move = true;
410
411       break;
412
413     default:
414       break;
415     
416   }
417 }
418
419 Sprite *img_smoke_cloud;
420
421 SmokeCloud::SmokeCloud(const Vector& pos)
422   : position(pos)
423 {
424   timer.start(300);
425 }
426
427 void
428 SmokeCloud::action(float elapsed_time)
429 {
430   position.y -= 1.2 * elapsed_time;
431
432   if(!timer.check())
433     remove_me();
434 }
435
436 void
437 SmokeCloud::draw(DrawingContext& context)
438 {
439   img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
440 }
441
442 Particles::Particles(const Vector& epicenter, const Vector& velocity, const Vector& acceleration, int number, Color color_, int size_, int life_time)
443   : color(color_), size(size_), vel(velocity), accel(acceleration)
444 {
445   if(life_time == 0)
446     {
447     live_forever = true;
448     }
449   else
450     {
451     live_forever = false;
452     timer.start(life_time);
453     }
454
455   // create particles
456   for(int p = 0; p < number; p++)
457     {
458     Particle* particle = new Particle;
459     particle->pos = epicenter;
460     particle->angle = (rand() % 360) * (M_PI / 180);  // in radius
461
462     particles.push_back(particle);
463     }
464 }
465
466 Particles::~Particles()
467 {
468   // free particles
469   for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
470     delete (*i);
471 }
472
473 void
474 Particles::action(float elapsed_time)
475 {
476   vel.x += accel.x * elapsed_time;
477   vel.y += accel.y * elapsed_time;
478
479   int camera_x = (int)Sector::current()->camera->get_translation().x;
480   int camera_y = (int)Sector::current()->camera->get_translation().y;
481
482   // update particles
483   for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
484     {
485     (*i)->pos.x += sin((*i)->angle) * vel.x * elapsed_time;
486     (*i)->pos.y += cos((*i)->angle) * vel.y * elapsed_time;
487
488     if((*i)->pos.x < camera_x || (*i)->pos.x > screen->w + camera_x ||
489        (*i)->pos.y < camera_y || (*i)->pos.y > screen->h + camera_y)
490       {
491       delete (*i);
492       particles.erase(i);
493       }
494     }
495
496   if((!timer.check() && !live_forever) || particles.size() == 0)
497     remove_me();
498 }
499
500 void
501 Particles::draw(DrawingContext& context)
502 {
503   // draw particles
504   for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
505     {
506     context.draw_filled_rect((*i)->pos, Vector(size,size), color, LAYER_OBJECTS+10);
507     }
508 }
509
510 void load_object_gfx()
511 {
512   img_trampoline = sprite_manager->load("trampoline");
513   img_trampoline->start_animation(0);
514   img_flying_platform = sprite_manager->load("flying_platform");
515   img_smoke_cloud = sprite_manager->load("stomp");
516 }