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