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