-Added new object system and converted some GameObjects to it.
[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(ViewPort& 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(ViewPort& 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(ViewPort& 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+1);
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(ViewPort& 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 void load_object_gfx()
149 {
150   char sprite_name[16];
151
152   for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
153   {
154     sprintf(sprite_name, "trampoline-%i", i+1);
155     img_trampoline[i] = sprite_manager->load(sprite_name);
156   }
157 }
158
159 void
160 Trampoline::init(float x, float y)
161 {
162   base.x = x;
163   base.y = y;
164   base.width = 32;
165   base.height = 32;
166
167   frame = 0;
168   mode = M_NORMAL;
169   physic.reset();
170 }
171
172 void
173 Trampoline::draw()
174 {
175   img_trampoline[frame]->draw((int)base.x, (int)base.y);
176
177   frame = 0;
178
179   if (debug_mode)
180     fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75, 75, 0, 150);
181 }
182
183 void
184 Trampoline::action(double 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(void *p_c_object, int c_object, CollisionType type)
236 {
237   Player* pplayer_c = NULL;
238   switch (c_object)
239   {
240     case CO_PLAYER:
241       pplayer_c = (Player*) p_c_object;
242
243       if (type == COLLISION_NORMAL)
244       {
245         // Pick up if HELD (done in Player)
246       }
247
248       else if (type == COLLISION_SQUISH)
249       {
250         int squish_amount = (32 - (int)pplayer_c->base.y % 32);
251
252         if (squish_amount < 24)
253           frame = 3;
254         else if (squish_amount < 28)
255           frame = 2;
256         else if (squish_amount < 30)
257           frame = 1;
258         else
259           frame = 0;
260
261         if (squish_amount < 20)
262           pplayer_c->physic.set_velocity_y(power);
263         else if (pplayer_c->physic.get_velocity_y() < 0)
264           pplayer_c->physic.set_velocity_y(-squish_amount/32);
265       }
266
267       break;
268
269     default:
270       break;
271     
272   }
273 }
274
275
276 /* Object Manager */
277 //---------------------------------------------------------------------------
278
279 ObjectManager::ObjectManager()
280 {
281   std::string filename = datadir + "/images/tilesets/supertux.stbg";
282   load_badguys(filename);
283 }
284
285 ObjectManager::~ObjectManager()
286 {
287   for(std::vector<BadGuy*>::iterator i = badguys.begin(); i != badguys.end(); ++i) {
288     delete *i;                                                                  
289   }
290 }
291
292 void ObjectManager::load_badguys(std::string filename)
293 {
294   (void) filename;
295 /*
296   lisp_object_t* root_obj = lisp_read_from_file(filename);
297
298   if (!root_obj)
299     st_abort("Couldn't load file", filename);
300
301   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-badguys") == 0)
302     {
303       lisp_object_t* cur = lisp_cdr(root_obj);
304
305       while(!lisp_nil_p(cur))
306         {
307           lisp_object_t* element = lisp_car(cur);
308
309           if (strcmp(lisp_symbol(lisp_car(element)), "badguy") == 0)
310             {
311               
312              
313               Tile* tile = new Tile;
314               tile->id      = -1;
315               tile->solid   = false;
316               tile->brick   = false;
317               tile->ice     = false;
318               tile->water   = false;
319               tile->fullbox = false;
320               tile->distro  = false;
321               tile->goal    = false;
322               tile->data    = 0;
323               tile->next_tile  = 0;
324               tile->anim_speed = 25;
325
326               LispReader reader(lisp_cdr(element));
327               assert(reader.read_int("id",  &tile->id));
328               reader.read_bool("solid",     &tile->solid);
329               reader.read_bool("brick",     &tile->brick);
330               reader.read_bool("ice",       &tile->ice);
331               reader.read_bool("water",     &tile->water);
332               reader.read_bool("fullbox",   &tile->fullbox);
333               reader.read_bool("distro",    &tile->distro);
334               reader.read_bool("goal",      &tile->goal);
335               reader.read_int("data",       &tile->data);
336               reader.read_int("anim-speed", &tile->anim_speed);
337               reader.read_int("next-tile",  &tile->next_tile);
338               reader.read_string_vector("images",  &tile->filenames);
339               reader.read_string_vector("editor-images", &tile->editor_filenames);
340
341               for(std::vector<std::string>::iterator it = tile->
342                   filenames.begin();
343                   it != tile->filenames.end();
344                   ++it)
345                 {
346                   Surface* cur_image;
347                   tile->images.push_back(cur_image);
348                   tile->images[tile->images.size()-1] = new Surface(
349                                datadir +  "/images/tilesets/" + (*it),
350                                USE_ALPHA);
351                 }
352               for(std::vector<std::string>::iterator it = tile->editor_filenames.begin();
353                   it != tile->editor_filenames.end();
354                   ++it)
355                 {
356                   Surface* cur_image;
357                   tile->editor_images.push_back(cur_image);
358                   tile->editor_images[tile->editor_images.size()-1] = new Surface(
359                                datadir + "/images/tilesets/" + (*it),
360                                USE_ALPHA);
361                 }
362                 
363               if (tile->id + tileset_id >= int(tiles.size())
364                  )
365                 tiles.resize(tile->id + tileset_id+1);
366
367               tiles[tile->id + tileset_id] = tile;
368             }
369           else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
370             {
371               LispReader reader(lisp_cdr(element));
372               std::string filename;
373               reader.read_string("file",  &filename);
374               filename = datadir + "/images/tilesets/" + filename;
375               load_tileset(filename);
376             }
377           else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
378             {
379               TileGroup new_;
380               LispReader reader(lisp_cdr(element));
381               reader.read_string("name",  &new_.name);
382               reader.read_int_vector("tiles", &new_.tiles);           
383               if(!tilegroups_)
384                 tilegroups_ = new std::set<TileGroup>;
385               tilegroups_->insert(new_).first;
386             }
387           else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
388             {
389               LispReader reader(lisp_cdr(element));
390               reader.read_int("id",  &tileset_id);
391               tileset_id *= 1000;
392             }
393           else
394             {
395               puts("Unhandled symbol");
396             }
397
398           cur = lisp_cdr(cur);
399         }
400     }
401   else
402     {
403       assert(0);
404     }
405
406   lisp_free(root_obj);
407 */
408 }
409
410 void ObjectManager::draw_bg()
411 {
412 /*
413   for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
414     bouncy_bricks[i]->draw();
415
416   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
417     (*i)->draw();
418
419   for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
420     (*i)->draw();
421 */
422 }
423
424 void ObjectManager::draw_fg()
425 {
426 /*
427   for (unsigned int i = 0; i < bullets.size(); ++i)
428     bullets[i].draw();
429
430   for (unsigned int i = 0; i < floating_scores.size(); ++i)
431     floating_scores[i]->draw();
432
433   for (unsigned int i = 0; i < upgrades.size(); ++i)
434     upgrades[i].draw();
435
436   for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
437     bouncy_distros[i]->draw();
438
439   for (unsigned int i = 0; i < broken_bricks.size(); ++i)
440     broken_bricks[i]->draw();
441 */
442 }
443
444 void ObjectManager::actions()
445 {
446 /*
447   for (unsigned int i = 0; i < bouncy_distros.size(); i++)
448     bouncy_distros[i]->action(frame_ratio);
449
450   for (unsigned int i = 0; i < broken_bricks.size(); i++)
451     broken_bricks[i]->action(frame_ratio);
452
453   // Handle all kinds of game objects
454   for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
455     bouncy_bricks[i]->action(frame_ratio);
456   
457   for (unsigned int i = 0; i < floating_scores.size(); i++)
458     floating_scores[i]->action(frame_ratio);
459
460   for (unsigned int i = 0; i < bullets.size(); ++i)
461     bullets[i].action(frame_ratio);
462   
463   for (unsigned int i = 0; i < upgrades.size(); i++)
464     upgrades[i].action(frame_ratio);
465
466   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
467     (*i)->action(frame_ratio);
468
469   for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
470      (*i)->action(frame_ratio);
471 */
472 }
473
474 /* EOF */
475