- fixed the bouncybrick artifact bug
[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 "tile.h"
27 #include "gameloop.h"
28 #include "gameobjs.h"
29 #include "sprite_manager.h"
30 #include "resources.h"
31 #include "sector.h"
32 #include "tilemap.h"
33
34 BouncyDistro::BouncyDistro(const Vector& pos)
35   : position(pos)
36 {
37   ym = -2;
38 }
39
40 void
41 BouncyDistro::action(float elapsed_time)
42 {
43   position.y += ym * elapsed_time;
44
45   ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
46   if(ym >= 0)
47     remove_me();
48 }
49
50 void
51 BouncyDistro::draw(DrawingContext& context)
52 {
53   context.draw_surface(img_distro[0], position, LAYER_OBJECTS);
54 }
55
56
57 BrokenBrick::BrokenBrick(Tile* ntile,const Vector& pos, const Vector& nmovement)
58   : tile(ntile), position(pos), movement(nmovement)
59 {
60   timer.start(200);
61 }
62
63 void
64 BrokenBrick::action(float elapsed_time)
65 {
66   position += movement * elapsed_time;
67
68   if (!timer.check())
69     remove_me();
70 }
71
72 void
73 BrokenBrick::draw(DrawingContext& context)
74 {
75   if (tile->images.size() > 0)
76     context.draw_surface_part(tile->images[0],
77         Vector(rand() % 16, rand() % 16),
78         Vector(16, 16),
79         position, LAYER_OBJECTS + 1);
80 }
81
82 BouncyBrick::BouncyBrick(const Vector& pos)
83   : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED), 
84     shape(Sector::current()->solids->get_tile_id_at(pos))
85
86   shape.hidden = true;
87 }
88
89 void
90 BouncyBrick::action(float elapsed_time)
91 {
92   offset += offset_m * elapsed_time;
93
94   /* Go back down? */
95   if (offset < -BOUNCY_BRICK_MAX_OFFSET)
96     offset_m = BOUNCY_BRICK_SPEED;
97
98   /* Stop bouncing? */
99   if (offset >= 0)
100     {
101       shape.hidden = false;
102       remove_me();
103     }
104 }
105
106 void
107 BouncyBrick::draw(DrawingContext& context)
108 {
109   TileManager::instance()->
110     draw_tile(context, shape.id, position + Vector(0, offset), LAYER_TILES+1);
111 }
112
113 FloatingScore::FloatingScore(const Vector& pos, int score)
114   : position(pos)
115 {
116   timer.start(1000);
117   snprintf(str, 10, "%d", score);
118   position.x -= strlen(str) * 8;
119 }
120
121 void
122 FloatingScore::action(float elapsed_time)
123 {
124   position.y -= 2 * elapsed_time;
125
126   if(!timer.check())
127     remove_me();
128 }
129
130 void
131 FloatingScore::draw(DrawingContext& context)
132 {
133   context.draw_text(gold_text, str, position, LAYER_OBJECTS);
134 }
135
136 /* Trampoline */
137
138 #define TRAMPOLINE_FRAMES 4
139 Sprite *img_trampoline[TRAMPOLINE_FRAMES];
140
141 Trampoline::Trampoline(LispReader& reader)
142 {
143   reader.read_float("x", base.x);
144   reader.read_float("y", base.y); 
145   base.width = 32;
146   base.height = 32;
147   power = 7.5;
148   reader.read_float("power", power);
149
150   frame = 0;
151   mode = M_NORMAL;
152   physic.reset();
153 }
154
155 void
156 Trampoline::write(LispWriter& writer)
157 {
158   writer.start_list("trampoline");
159
160   writer.write_float("x", base.x);
161   writer.write_float("y", base.y);
162   writer.write_float("power", power);
163
164   writer.end_list("trampoline");
165 }
166
167 void
168 Trampoline::draw(DrawingContext& context)
169 {
170   img_trampoline[frame]->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
171   frame = 0;
172 }
173
174 void
175 Trampoline::action(float frame_ratio)
176 {
177   // TODO: Remove if we're too far off the screen
178
179   // Falling
180   if (mode != M_HELD)
181   {
182     if (issolid(base.x + base.width/2, base.y + base.height))
183     {
184       base.y = int((base.y + base.height)/32) * 32 - base.height;
185
186       physic.enable_gravity(false);
187       physic.set_velocity_y(0.0f);
188
189       physic.set_velocity_x(0);
190     }
191     else
192     {
193       physic.enable_gravity(true);
194     }
195   }
196   else // Player is carrying us around
197   {
198     /* FIXME: The trampoline object shouldn't know about pplayer objects. */
199     /* If we're holding the iceblock */
200     Player& tux = *Sector::current()->player;
201     Direction dir = tux.dir;
202
203     if(dir == RIGHT)
204     {
205       base.x = tux.base.x + 16;
206       base.y = tux.base.y + tux.base.height/1.5 - base.height;
207     }
208     else /* facing left */
209     {
210       base.x = tux.base.x - 16;
211       base.y = tux.base.y + tux.base.height/1.5 - base.height;
212     }
213
214     if(collision_object_map(base))
215     {
216       base.x = tux.base.x;
217       base.y = tux.base.y + tux.base.height/1.5 - base.height;
218     }
219   }
220
221   physic.apply(frame_ratio, base.x, base.y);
222   collision_swept_object_map(&old_base, &base);
223 }
224
225 void
226 Trampoline::collision(const MovingObject&, int)
227 {
228   // comes later
229 }
230
231 void
232 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
233 {
234   Player* pplayer_c = NULL;
235   switch (c_object)
236   {
237     case CO_PLAYER:
238       pplayer_c = (Player*) p_c_object;
239
240       if (type == COLLISION_NORMAL)
241       {
242         // Pick up if HELD (done in Player)
243       }
244
245       else if (type == COLLISION_SQUISH)
246       {
247         int squish_amount = (32 - (int)pplayer_c->base.y % 32);
248
249         if (squish_amount < 24)
250           frame = 3;
251         else if (squish_amount < 28)
252           frame = 2;
253         else if (squish_amount < 30)
254           frame = 1;
255         else
256           frame = 0;
257
258         if (squish_amount < 20) {
259           pplayer_c->physic.set_velocity_y(power);
260           pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
261         }
262         else if (pplayer_c->physic.get_velocity_y() < 0)
263           pplayer_c->physic.set_velocity_y(-squish_amount/32);
264       }
265
266       break;
267
268     default:
269       break;
270     
271   }
272 }
273
274 /* Flying Platform */
275
276 Sprite *img_flying_platform;
277
278 FlyingPlatform::FlyingPlatform(LispReader& reader)
279 {
280   reader.read_int_vector("x", pos_x);
281   reader.read_int_vector("y", pos_y);
282
283   velocity = 2.0;
284   reader.read_float("velocity", velocity);
285
286   base.x = pos_x[0];
287   base.y = pos_y[0];
288   base.width = 96;
289   base.height = 40;
290
291   point = 0;
292   move = false;
293
294   float x = pos_x[point+1] - pos_x[point];
295   float y = pos_y[point+1] - pos_y[point];
296   vel_x = x*velocity / sqrt(x*x + y*y);
297   vel_y = -(velocity - vel_x);
298
299   frame = 0;
300 }
301
302 void
303 FlyingPlatform::write(LispWriter& writer)
304 {
305   writer.start_list("flying-trampoline");
306
307   writer.write_int_vector("x", pos_x);
308   writer.write_int_vector("y", pos_y);
309   writer.write_float("velocity", velocity);
310
311   writer.end_list("flying-trampoline");
312 }
313
314 void
315 FlyingPlatform::draw(DrawingContext& context)
316 {
317   img_flying_platform->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
318 }
319
320 void
321 FlyingPlatform::action(float frame_ratio)
322 {
323   // TODO: Remove if we're too far off the screen
324
325 if(!move)
326   return;
327
328 if((unsigned)point+1 != pos_x.size())
329   {
330   if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
331       (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
332       pos_x[point] == pos_x[point+1]) &&
333     ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
334       (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
335       pos_y[point] == pos_y[point+1]))
336     {
337     point++;
338
339     float x = pos_x[point+1] - pos_x[point];
340     float y = pos_y[point+1] - pos_y[point];
341     vel_x = x*velocity / sqrt(x*x + y*y);
342     vel_y = -(velocity - vel_x);
343     }
344   }
345 else   // last point
346   {
347   // point = 0;
348   // reverse vector
349   return;
350   }
351 /*
352 if(pos_x[point+1] > base.x)
353   base.x += velocity * frame_ratio;
354 else if(pos_x[point+1] < base.x)
355   base.x -= velocity * frame_ratio;
356
357 if(pos_y[point+1] > base.y)
358   base.y += velocity * frame_ratio;
359 else if(pos_y[point+1] < base.y)
360   base.y -= velocity * frame_ratio;
361 */
362
363 base.x += vel_x * frame_ratio;
364 base.y += vel_y * frame_ratio;
365 }
366
367 void
368 FlyingPlatform::collision(const MovingObject&, int)
369 {
370   // comes later
371 }
372
373 void
374 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
375 {
376 (void) p_c_object;
377 (void) type;
378
379 //  Player* pplayer_c = NULL;
380   switch (c_object)
381   {
382     case CO_PLAYER:
383 //      pplayer_c = (Player*) p_c_object;
384       move = true;
385
386       break;
387
388     default:
389       break;
390     
391   }
392 }
393
394 void load_object_gfx()
395 {
396   char sprite_name[16];
397
398   for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
399   {
400     sprintf(sprite_name, "trampoline-%i", i+1);
401     img_trampoline[i] = sprite_manager->load(sprite_name);
402   }
403
404   img_flying_platform = sprite_manager->load("flying_platform");
405 }