- Refactored worldmap a bit to reuse GameObject from the rest of the game
[supertux.git] / src / flip_level_transformer.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "flip_level_transformer.h"
23 #include "object/tilemap.h"
24 #include "badguy/badguy.h"
25 #include "sector.h"
26 #include "tile_manager.h"
27 #include "spawn_point.h"
28
29 void
30 FlipLevelTransformer::transform_sector(Sector* sector)
31 {
32   float height = sector->solids->get_height() 
33     * sector->solids->get_tilemanager()->get_default_height();
34   
35   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
36       i != sector->gameobjects.end(); ++i) {
37     GameObject* object = *i;
38
39     TileMap* tilemap = dynamic_cast<TileMap*> (object);
40     if(tilemap) {
41       transform_tilemap(tilemap);
42     }
43     BadGuy* badguy = dynamic_cast<BadGuy*> (object);
44     if(badguy) {
45       transform_badguy(height, badguy);
46     } else {
47       MovingObject* mobject = dynamic_cast<MovingObject*> (object);
48       if(mobject) {
49         transform_moving_object(height, mobject);
50       }
51     }
52   }
53   for(Sector::SpawnPoints::iterator i = sector->spawnpoints.begin();
54       i != sector->spawnpoints.end(); ++i) {
55     transform_spawnpoint(height, *i);
56   }
57 }
58
59 void
60 FlipLevelTransformer::transform_tilemap(TileMap* tilemap)
61 {
62   for(size_t x = 0; x < tilemap->get_width(); ++x) {
63     for(size_t y = 0; y < tilemap->get_height()/2; ++y) {
64       // swap tiles
65       int y2 = tilemap->get_height()-1-y;
66       const Tile* t1 = tilemap->get_tile(x, y);
67       const Tile* t2 = tilemap->get_tile(x, y2);
68       tilemap->change(x, y, t2->getID());
69       tilemap->change(x, y2, t1->getID());
70     }
71   }
72   tilemap->set_drawing_effect(VERTICAL_FLIP);
73 }
74
75 void
76 FlipLevelTransformer::transform_badguy(float height, BadGuy* badguy)
77 {
78   Vector pos = badguy->get_start_position();
79   pos.y = height - pos.y;
80   badguy->set_start_position(pos);
81 }
82
83 void
84 FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint* spawn)
85 {
86   Vector pos = spawn->pos;
87   pos.y = height - pos.y;
88   spawn->pos = pos;
89 }
90
91 void
92 FlipLevelTransformer::transform_moving_object(float height, MovingObject*object)
93 {
94   Vector pos = object->get_pos();
95   pos.y = height - pos.y;
96   object->set_pos(pos);
97 }
98