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