minimize some #includes and replace with forward decls
[supertux.git] / src / flip_level_transformer.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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.hpp"
23 #include "object/tilemap.hpp"
24 #include "object/camera.hpp"
25 #include "badguy/badguy.hpp"
26 #include "sector.hpp"
27 #include "tile_manager.hpp"
28 #include "spawn_point.hpp"
29 #include "object/platform.hpp"
30 #include "object/block.hpp"
31
32 void
33 FlipLevelTransformer::transform_sector(Sector* sector)
34 {
35   float height = sector->get_height();
36
37   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
38       i != sector->gameobjects.end(); ++i) {
39     GameObject* object = *i;
40
41     TileMap* tilemap = dynamic_cast<TileMap*> (object);
42     if(tilemap) {
43       transform_tilemap(tilemap);
44     }
45     Player* player = dynamic_cast<Player*> (object);
46     if(player) {
47       Vector pos = player->get_pos();
48       pos.y = height - pos.y - player->get_bbox().get_height();
49       player->move(pos);
50       continue;
51     }
52     BadGuy* badguy = dynamic_cast<BadGuy*> (object);
53     if(badguy) {
54       transform_badguy(height, badguy);
55     }
56     Platform* platform = dynamic_cast<Platform*> (object);
57     if(platform) {
58       transform_platform(height, *platform);
59     }
60     Block* block = dynamic_cast<Block*> (object);
61     if(block) {
62       transform_block(height, *block);
63     }
64     MovingObject* mobject = dynamic_cast<MovingObject*> (object);
65     if(mobject) {
66       transform_moving_object(height, mobject);
67     }
68   }
69   for(Sector::SpawnPoints::iterator i = sector->spawnpoints.begin();
70       i != sector->spawnpoints.end(); ++i) {
71     transform_spawnpoint(height, *i);
72   }
73
74   if(sector->camera != 0 && sector->player != 0)
75     sector->camera->reset(sector->player->get_pos());
76 }
77
78 void
79 FlipLevelTransformer::transform_tilemap(TileMap* tilemap)
80 {
81   for(size_t x = 0; x < tilemap->get_width(); ++x) {
82     for(size_t y = 0; y < tilemap->get_height()/2; ++y) {
83       // swap tiles
84       int y2 = tilemap->get_height()-1-y;
85       const Tile* t1 = tilemap->get_tile(x, y);
86       const Tile* t2 = tilemap->get_tile(x, y2);
87       tilemap->change(x, y, t2->getID());
88       tilemap->change(x, y2, t1->getID());
89     }
90   }
91   if(tilemap->get_drawing_effect() != 0) {
92     tilemap->set_drawing_effect(NO_EFFECT);
93   } else {
94     tilemap->set_drawing_effect(VERTICAL_FLIP);
95   }
96 }
97
98 void
99 FlipLevelTransformer::transform_badguy(float height, BadGuy* badguy)
100 {
101   Vector pos = badguy->get_start_position();
102   pos.y = height - pos.y;
103   badguy->set_start_position(pos);
104 }
105
106 void
107 FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint* spawn)
108 {
109   Vector pos = spawn->pos;
110   pos.y = height - pos.y;
111   spawn->pos = pos;
112 }
113
114 void
115 FlipLevelTransformer::transform_moving_object(float height, MovingObject*object)
116 {
117   Vector pos = object->get_pos();
118   pos.y = height - pos.y - object->get_bbox().get_height();
119   object->set_pos(pos);
120 }
121
122 void
123 FlipLevelTransformer::transform_platform(float height, Platform& platform)
124 {
125   Path& path = platform.get_path();
126   for (std::vector<Path::Node>::iterator i = path.nodes.begin(); i != path.nodes.end(); i++) {
127     Vector& pos = i->position;
128     pos.y = height - pos.y - platform.get_bbox().get_height();
129   }
130 }
131
132 void
133 FlipLevelTransformer::transform_block(float height, Block& block)
134 {
135   block.original_y = height - block.original_y - block.get_bbox().get_height();
136 }