Changed the background feeding format for the new level format.
[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;
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->set_frame(frame);
185   img_trampoline->draw(context, base, LAYER_OBJECTS);
186   frame = 0;
187 }
188
189 void
190 Trampoline::action(float frame_ratio)
191 {
192   // TODO: Remove if we're too far off the screen
193
194   // Falling
195   if (mode != M_HELD)
196   {
197     if (issolid(base.x + base.width/2, base.y + base.height))
198     {
199       base.y = int((base.y + base.height)/32) * 32 - base.height;
200
201       physic.enable_gravity(false);
202       physic.set_velocity_y(0.0f);
203
204       physic.set_velocity_x(0);
205     }
206     else
207     {
208       physic.enable_gravity(true);
209     }
210   }
211   else // Player is carrying us around
212   {
213     /* FIXME: The trampoline object shouldn't know about pplayer objects. */
214     /* If we're holding the iceblock */
215     Player& tux = *Sector::current()->player;
216     Direction dir = tux.dir;
217
218     if(dir == RIGHT)
219     {
220       base.x = tux.base.x + 16;
221       base.y = tux.base.y + tux.base.height/1.5 - base.height;
222     }
223     else /* facing left */
224     {
225       base.x = tux.base.x - 16;
226       base.y = tux.base.y + tux.base.height/1.5 - base.height;
227     }
228
229     if(collision_object_map(base))
230     {
231       base.x = tux.base.x;
232       base.y = tux.base.y + tux.base.height/1.5 - base.height;
233     }
234   }
235
236   physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
237   collision_swept_object_map(&old_base, &base);
238 }
239
240 void
241 Trampoline::collision(const MovingObject&, int)
242 {
243   // comes later
244 }
245
246 void
247 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
248 {
249   Player* pplayer_c = NULL;
250   switch (c_object)
251   {
252     case CO_PLAYER:
253       pplayer_c = (Player*) p_c_object;
254
255       if (type == COLLISION_NORMAL)
256       {
257         // Pick up if HELD (done in Player)
258       }
259
260       else if (type == COLLISION_SQUISH)
261       {
262         int squish_amount = (32 - (int)pplayer_c->base.y % 32);
263
264         if (squish_amount < 24)
265           frame = 3;
266         else if (squish_amount < 28)
267           frame = 2;
268         else if (squish_amount < 30)
269           frame = 1;
270         else
271           frame = 0;
272
273         if (squish_amount < 20) {
274           pplayer_c->physic.set_velocity_y(power);
275           pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
276         }
277         else if (pplayer_c->physic.get_velocity_y() < 0)
278           pplayer_c->physic.set_velocity_y(-squish_amount/32);
279       }
280
281       break;
282
283     default:
284       break;
285     
286   }
287 }
288
289 /* Flying Platform */
290
291 Sprite *img_flying_platform;
292
293 FlyingPlatform::FlyingPlatform(LispReader& reader)
294 {
295   reader.read_int_vector("x", pos_x);
296   reader.read_int_vector("y", pos_y);
297
298   velocity = 2.0;
299   reader.read_float("velocity", velocity);
300
301   base.x = pos_x[0];
302   base.y = pos_y[0];
303   base.width = 96;
304   base.height = 40;
305
306   point = 0;
307   move = false;
308
309   float x = pos_x[point+1] - pos_x[point];
310   float y = pos_y[point+1] - pos_y[point];
311   vel_x = x*velocity / sqrt(x*x + y*y);
312   vel_y = -(velocity - vel_x);
313
314   frame = 0;
315 }
316
317 FlyingPlatform::FlyingPlatform(int x, int y)
318 {
319 base.x = x;
320 base.y = y;
321 point = 0;
322 move = false;
323 }
324
325 void
326 FlyingPlatform::write(LispWriter& writer)
327 {
328   writer.start_list("flying-trampoline");
329
330   writer.write_int_vector("x", pos_x);
331   writer.write_int_vector("y", pos_y);
332   writer.write_float("velocity", velocity);
333
334   writer.end_list("flying-trampoline");
335 }
336
337 void
338 FlyingPlatform::draw(DrawingContext& context)
339 {
340   img_flying_platform->draw(context, base, LAYER_OBJECTS);
341 }
342
343 void
344 FlyingPlatform::action(float frame_ratio)
345 {
346   // TODO: Remove if we're too far off the screen
347
348 if(!move)
349   return;
350
351 if((unsigned)point+1 != pos_x.size())
352   {
353   if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
354       (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
355       pos_x[point] == pos_x[point+1]) &&
356     ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
357       (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
358       pos_y[point] == pos_y[point+1]))
359     {
360     point++;
361
362     float x = pos_x[point+1] - pos_x[point];
363     float y = pos_y[point+1] - pos_y[point];
364     vel_x = x*velocity / sqrt(x*x + y*y);
365     vel_y = -(velocity - vel_x);
366     }
367   }
368 else   // last point
369   {
370   // point = 0;
371   // reverse vector
372   return;
373   }
374 /*
375 if(pos_x[point+1] > base.x)
376   base.x += velocity * frame_ratio;
377 else if(pos_x[point+1] < base.x)
378   base.x -= velocity * frame_ratio;
379
380 if(pos_y[point+1] > base.y)
381   base.y += velocity * frame_ratio;
382 else if(pos_y[point+1] < base.y)
383   base.y -= velocity * frame_ratio;
384 */
385
386 base.x += vel_x * frame_ratio;
387 base.y += vel_y * frame_ratio;
388 }
389
390 void
391 FlyingPlatform::collision(const MovingObject&, int)
392 {
393   // comes later
394 }
395
396 void
397 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
398 {
399 (void) p_c_object;
400 (void) type;
401
402 //  Player* pplayer_c = NULL;
403   switch (c_object)
404   {
405     case CO_PLAYER:
406 //      pplayer_c = (Player*) p_c_object;
407       move = true;
408
409       break;
410
411     default:
412       break;
413     
414   }
415 }
416
417 Sprite *img_smoke_cloud;
418
419 SmokeCloud::SmokeCloud(const Vector& pos)
420   : position(pos)
421 {
422   timer.start(300);
423 }
424
425 void
426 SmokeCloud::action(float elapsed_time)
427 {
428   position.y -= 1.2 * elapsed_time;
429
430   if(!timer.check())
431     remove_me();
432 }
433
434 void
435 SmokeCloud::draw(DrawingContext& context)
436 {
437   img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
438 }
439
440 void load_object_gfx()
441 {
442   img_trampoline = sprite_manager->load("trampoline");
443   img_trampoline->start_animation(0);
444   img_flying_platform = sprite_manager->load("flying_platform");
445   img_smoke_cloud = sprite_manager->load("stomp");
446 }