Changed FloatingScore to support text (changed name to FloatingText). I guess this...
[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 FloatingText::FloatingText(const Vector& pos, std::string& text_)
118   : position(pos), text(text_)
119 {
120   timer.start(1000);
121   position.x -= text.size() * 8;
122 }
123
124 FloatingText::FloatingText(const Vector& pos, int score)
125   : position(pos)
126 {
127   timer.start(1000);
128
129   // turn int into a string
130   char str[10];
131   snprintf(str, 10, "%d", score);
132   text = str;
133
134   position.x -= text.size() * 8;
135 }
136
137 void
138 FloatingText::action(float elapsed_time)
139 {
140   position.y -= 1.4 * elapsed_time;
141
142   if(!timer.check())
143     remove_me();
144 }
145
146 #define FADING_TIME 350
147
148 void
149 FloatingText::draw(DrawingContext& context)
150 {
151   // make an alpha animation when disapearing
152   int alpha;
153   if(timer.get_left() < FADING_TIME)
154     alpha = timer.get_left() * 255 / FADING_TIME;
155   else
156     alpha = 255;
157
158   context.draw_text(gold_text, text, position, LEFT_ALLIGN, LAYER_OBJECTS, NONE_EFFECT, alpha);
159 }
160
161 /* Trampoline */
162
163 Sprite *img_trampoline;
164
165 Trampoline::Trampoline(LispReader& reader)
166 {
167   reader.read_float("x", base.x);
168   reader.read_float("y", base.y); 
169   base.width = 32;
170   base.height = 32;
171   power = 7.5;
172   reader.read_float("power", power);
173
174   frame = 0;
175   mode = M_NORMAL;
176   physic.reset();
177 }
178
179 Trampoline::Trampoline(float x, float y)
180 {
181   base.x = x;
182   base.y = y;
183   base.width = 32;
184   base.height = 32;
185   power = 7.5;
186
187   frame = 0;
188   mode = M_NORMAL;
189   physic.reset();
190 }
191
192 void
193 Trampoline::write(LispWriter& writer)
194 {
195   writer.start_list("trampoline");
196
197   writer.write_float("x", base.x);
198   writer.write_float("y", base.y);
199   writer.write_float("power", power);
200
201   writer.end_list("trampoline");
202 }
203
204 void
205 Trampoline::draw(DrawingContext& context)
206 {
207   img_trampoline->set_frame(frame);
208   img_trampoline->draw(context, base, LAYER_OBJECTS);
209   frame = 0;
210 }
211
212 void
213 Trampoline::action(float frame_ratio)
214 {
215   // TODO: Remove if we're too far off the screen
216
217   // Falling
218   if (mode != M_HELD)
219   {
220     if (issolid(base.x + base.width/2, base.y + base.height))
221     {
222       base.y = int((base.y + base.height)/32) * 32 - base.height;
223
224       physic.enable_gravity(false);
225       physic.set_velocity_y(0.0f);
226
227       physic.set_velocity_x(0);
228     }
229     else
230     {
231       physic.enable_gravity(true);
232     }
233   }
234   else // Player is carrying us around
235   {
236     /* FIXME: The trampoline object shouldn't know about pplayer objects. */
237     /* If we're holding the iceblock */
238     Player& tux = *Sector::current()->player;
239     Direction dir = tux.dir;
240
241     if(dir == RIGHT)
242     {
243       base.x = tux.base.x + 16;
244       base.y = tux.base.y + tux.base.height/1.5 - base.height;
245     }
246     else /* facing left */
247     {
248       base.x = tux.base.x - 16;
249       base.y = tux.base.y + tux.base.height/1.5 - base.height;
250     }
251
252     if(collision_object_map(base))
253     {
254       base.x = tux.base.x;
255       base.y = tux.base.y + tux.base.height/1.5 - base.height;
256     }
257   }
258
259   physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
260   collision_swept_object_map(&old_base, &base);
261 }
262
263 void
264 Trampoline::collision(const MovingObject&, int)
265 {
266   // comes later
267 }
268
269 void
270 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
271 {
272   Player* pplayer_c = NULL;
273   switch (c_object)
274   {
275     case CO_PLAYER:
276       pplayer_c = (Player*) p_c_object;
277
278       if (type == COLLISION_NORMAL)
279       {
280         // Pick up if HELD (done in Player)
281       }
282
283       else if (type == COLLISION_SQUISH)
284       {
285         int squish_amount = (32 - (int)pplayer_c->base.y % 32);
286
287         if (squish_amount < 24)
288           frame = 3;
289         else if (squish_amount < 28)
290           frame = 2;
291         else if (squish_amount < 30)
292           frame = 1;
293         else
294           frame = 0;
295
296         if (squish_amount < 20) {
297           pplayer_c->physic.set_velocity_y(power);
298           pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
299         }
300         else if (pplayer_c->physic.get_velocity_y() < 0)
301           pplayer_c->physic.set_velocity_y(-squish_amount/32);
302       }
303
304       break;
305
306     default:
307       break;
308     
309   }
310 }
311
312 /* Flying Platform */
313
314 Sprite *img_flying_platform;
315
316 FlyingPlatform::FlyingPlatform(LispReader& reader)
317 {
318   reader.read_int_vector("x", pos_x);
319   reader.read_int_vector("y", pos_y);
320
321   velocity = 2.0;
322   reader.read_float("velocity", velocity);
323
324   base.x = pos_x[0];
325   base.y = pos_y[0];
326   base.width = 96;
327   base.height = 40;
328
329   point = 0;
330   move = false;
331
332   float x = pos_x[point+1] - pos_x[point];
333   float y = pos_y[point+1] - pos_y[point];
334   vel_x = x*velocity / sqrt(x*x + y*y);
335   vel_y = -(velocity - vel_x);
336
337   frame = 0;
338 }
339
340 FlyingPlatform::FlyingPlatform(int x, int y)
341 {
342 base.x = x;
343 base.y = y;
344 point = 0;
345 move = false;
346 }
347
348 void
349 FlyingPlatform::write(LispWriter& writer)
350 {
351   writer.start_list("flying-trampoline");
352
353   writer.write_int_vector("x", pos_x);
354   writer.write_int_vector("y", pos_y);
355   writer.write_float("velocity", velocity);
356
357   writer.end_list("flying-trampoline");
358 }
359
360 void
361 FlyingPlatform::draw(DrawingContext& context)
362 {
363   img_flying_platform->draw(context, base, LAYER_OBJECTS);
364 }
365
366 void
367 FlyingPlatform::action(float frame_ratio)
368 {
369   // TODO: Remove if we're too far off the screen
370
371 if(!move)
372   return;
373
374 if((unsigned)point+1 != pos_x.size())
375   {
376   if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
377       (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
378       pos_x[point] == pos_x[point+1]) &&
379     ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
380       (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
381       pos_y[point] == pos_y[point+1]))
382     {
383     point++;
384
385     float x = pos_x[point+1] - pos_x[point];
386     float y = pos_y[point+1] - pos_y[point];
387     vel_x = x*velocity / sqrt(x*x + y*y);
388     vel_y = -(velocity - vel_x);
389     }
390   }
391 else   // last point
392   {
393   // point = 0;
394   // reverse vector
395   return;
396   }
397 /*
398 if(pos_x[point+1] > base.x)
399   base.x += velocity * frame_ratio;
400 else if(pos_x[point+1] < base.x)
401   base.x -= velocity * frame_ratio;
402
403 if(pos_y[point+1] > base.y)
404   base.y += velocity * frame_ratio;
405 else if(pos_y[point+1] < base.y)
406   base.y -= velocity * frame_ratio;
407 */
408
409 base.x += vel_x * frame_ratio;
410 base.y += vel_y * frame_ratio;
411 }
412
413 void
414 FlyingPlatform::collision(const MovingObject&, int)
415 {
416   // comes later
417 }
418
419 void
420 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
421 {
422 (void) p_c_object;
423 (void) type;
424
425 //  Player* pplayer_c = NULL;
426   switch (c_object)
427   {
428     case CO_PLAYER:
429 //      pplayer_c = (Player*) p_c_object;
430       move = true;
431
432       break;
433
434     default:
435       break;
436     
437   }
438 }
439
440 Sprite *img_smoke_cloud;
441
442 SmokeCloud::SmokeCloud(const Vector& pos)
443   : position(pos)
444 {
445   timer.start(300);
446 }
447
448 void
449 SmokeCloud::action(float elapsed_time)
450 {
451   position.y -= 1.2 * elapsed_time;
452
453   if(!timer.check())
454     remove_me();
455 }
456
457 void
458 SmokeCloud::draw(DrawingContext& context)
459 {
460   img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
461 }
462
463 Particles::Particles(const Vector& epicenter, const Vector& velocity, const Vector& acceleration, int number, Color color_, int size_, int life_time)
464   : color(color_), size(size_), vel(velocity), accel(acceleration)
465 {
466   if(life_time == 0)
467     {
468     live_forever = true;
469     }
470   else
471     {
472     live_forever = false;
473     timer.start(life_time);
474     }
475
476   // create particles
477   for(int p = 0; p < number; p++)
478     {
479     Particle* particle = new Particle;
480     particle->pos = epicenter;
481     particle->angle = (rand() % 360) * (M_PI / 180);  // in radius
482
483     particles.push_back(particle);
484     }
485 }
486
487 Particles::~Particles()
488 {
489   // free particles
490   for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
491     delete (*i);
492 }
493
494 void
495 Particles::action(float elapsed_time)
496 {
497   vel.x += accel.x * elapsed_time;
498   vel.y += accel.y * elapsed_time;
499
500   int camera_x = (int)Sector::current()->camera->get_translation().x;
501   int camera_y = (int)Sector::current()->camera->get_translation().y;
502
503   // update particles
504   for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
505     {
506     (*i)->pos.x += sin((*i)->angle) * vel.x * elapsed_time;
507     (*i)->pos.y += cos((*i)->angle) * vel.y * elapsed_time;
508
509     if((*i)->pos.x < camera_x || (*i)->pos.x > screen->w + camera_x ||
510        (*i)->pos.y < camera_y || (*i)->pos.y > screen->h + camera_y)
511       {
512       delete (*i);
513       particles.erase(i);
514       }
515     }
516
517   if((!timer.check() && !live_forever) || particles.size() == 0)
518     remove_me();
519 }
520
521 void
522 Particles::draw(DrawingContext& context)
523 {
524   // draw particles
525   for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
526     {
527     context.draw_filled_rect((*i)->pos, Vector(size,size), color, LAYER_OBJECTS+10);
528     }
529 }
530
531 void load_object_gfx()
532 {
533   img_trampoline = sprite_manager->load("trampoline");
534   img_trampoline->start_animation(0);
535   img_flying_platform = sprite_manager->load("flying_platform");
536   img_smoke_cloud = sprite_manager->load("stomp");
537 }