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