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