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