-renamed ViewPort to Camera
[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(Camera& 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(Camera& 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(Camera& 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(Camera& 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(Camera& 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           pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
270         }
271         else if (pplayer_c->physic.get_velocity_y() < 0)
272           pplayer_c->physic.set_velocity_y(-squish_amount/32);
273       }
274
275       break;
276
277     default:
278       break;
279     
280   }
281 }
282
283 /* Flying Platform */
284
285 Sprite *img_flying_platform;
286
287 FlyingPlatform::FlyingPlatform(DisplayManager& displaymanager, LispReader& reader)
288 {
289   displaymanager.add_drawable(this, LAYER_OBJECTS);
290
291   reader.read_int_vector("x",  &pos_x);
292   reader.read_int_vector("y",  &pos_y);
293
294   velocity = 2.0;
295   reader.read_float("velocity", &velocity);
296
297   base.x = pos_x[0];
298   base.y = pos_y[0];
299   base.width = 96;
300   base.height = 40;
301
302   point = 0;
303   move = false;
304
305   frame = 0;
306 }
307
308 void
309 FlyingPlatform::write(LispWriter& writer)
310 {
311   writer.start_list("flying-trampoline");
312
313   writer.write_int_vector("x", pos_x);
314   writer.write_int_vector("y", pos_y);
315   writer.write_float("velocity", velocity);
316
317   writer.end_list("flying-trampoline");
318 }
319
320 void
321 FlyingPlatform::draw(Camera& viewport, int )
322 {
323 img_flying_platform->draw(viewport.world2screen(Vector(base.x, base.y)));
324 }
325
326 void
327 FlyingPlatform::action(float frame_ratio)
328 {
329   // TODO: Remove if we're too far off the screen
330
331 if(!move)
332   return;
333
334 if((unsigned)point+1 != pos_x.size())
335   {
336   if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
337       (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
338       pos_x[point] == pos_x[point+1]) &&
339     ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
340       (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
341       pos_y[point] == pos_y[point+1]))
342     {
343     point++;
344     }
345   }
346 else   // last point
347   {
348   // point = 0;
349   // reverse vector
350   return;
351   }
352
353 if(pos_x[point+1] > base.x)
354   base.x += velocity * frame_ratio;
355 else if(pos_x[point+1] < base.x)
356   base.x -= velocity * frame_ratio;
357
358 if(pos_y[point+1] > base.y)
359   base.y += velocity * frame_ratio;
360 else if(pos_y[point+1] < base.y)
361   base.y -= velocity * frame_ratio;
362 /*
363 float x = pos_x[point+1] - pos_x[point];
364 float y = pos_y[point+1] - pos_y[point];
365 float vel_x = x*velocity / sqrt(x*x + y*y);
366 float vel_y = velocity - vel_x;
367
368 base.x += vel_x * frame_ratio;
369 base.y += vel_y * frame_ratio;
370 */
371 }
372
373 void
374 FlyingPlatform::collision(const MovingObject&, int)
375 {
376   // comes later
377 }
378
379 void
380 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
381 {
382 (void) p_c_object;
383 (void) type;
384
385 //  Player* pplayer_c = NULL;
386   switch (c_object)
387   {
388     case CO_PLAYER:
389 //      pplayer_c = (Player*) p_c_object;
390       move = true;
391
392       break;
393
394     default:
395       break;
396     
397   }
398 }
399
400 void load_object_gfx()
401 {
402   char sprite_name[16];
403
404   for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
405   {
406     sprintf(sprite_name, "trampoline-%i", i+1);
407     img_trampoline[i] = sprite_manager->load(sprite_name);
408   }
409
410   img_flying_platform = sprite_manager->load("flying_platform");
411 }