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