Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / flip_level_transformer.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/badguy.hpp"
18 #include "object/block.hpp"
19 #include "object/camera.hpp"
20 #include "object/platform.hpp"
21 #include "object/player.hpp"
22 #include "object/tilemap.hpp"
23 #include "supertux/flip_level_transformer.hpp"
24 #include "supertux/sector.hpp"
25 #include "supertux/spawn_point.hpp"
26
27 void
28 FlipLevelTransformer::transform_sector(Sector* sector)
29 {
30   float height = sector->get_height();
31
32   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
33       i != sector->gameobjects.end(); ++i) {
34     GameObject* object = *i;
35
36     TileMap* tilemap = dynamic_cast<TileMap*> (object);
37     if(tilemap) {
38       transform_tilemap(tilemap);
39     }
40     Player* player = dynamic_cast<Player*> (object);
41     if(player) {
42       Vector pos = player->get_pos();
43       pos.y = height - pos.y - player->get_bbox().get_height();
44       player->move(pos);
45       continue;
46     }
47     BadGuy* badguy = dynamic_cast<BadGuy*> (object);
48     if(badguy) {
49       transform_badguy(height, badguy);
50     }
51     Platform* platform = dynamic_cast<Platform*> (object);
52     if(platform) {
53       transform_platform(height, *platform);
54     }
55     Block* block = dynamic_cast<Block*> (object);
56     if(block) {
57       transform_block(height, *block);
58     }
59     MovingObject* mobject = dynamic_cast<MovingObject*> (object);
60     if(mobject) {
61       transform_moving_object(height, mobject);
62     }
63   }
64   for(Sector::SpawnPoints::iterator i = sector->spawnpoints.begin();
65       i != sector->spawnpoints.end(); ++i) {
66     transform_spawnpoint(height, *i);
67   }
68
69   if(sector->camera != 0 && sector->player != 0)
70     sector->camera->reset(sector->player->get_pos());
71 }
72
73 void
74 FlipLevelTransformer::transform_tilemap(TileMap* tilemap)
75 {
76   for(size_t x = 0; x < tilemap->get_width(); ++x) {
77     for(size_t y = 0; y < tilemap->get_height()/2; ++y) {
78       // swap tiles
79       int y2 = tilemap->get_height()-1-y;
80       uint32_t t1 = tilemap->get_tile_id(x, y);
81       uint32_t t2 = tilemap->get_tile_id(x, y2);
82       tilemap->change(x, y, t2);
83       tilemap->change(x, y2, t1);
84     }
85   }
86   if(tilemap->get_drawing_effect() != 0) {
87     tilemap->set_drawing_effect(NO_EFFECT);
88   } else {
89     tilemap->set_drawing_effect(VERTICAL_FLIP);
90   }
91 }
92
93 void
94 FlipLevelTransformer::transform_badguy(float height, BadGuy* badguy)
95 {
96   Vector pos = badguy->get_start_position();
97   pos.y = height - pos.y;
98   badguy->set_start_position(pos);
99 }
100
101 void
102 FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint* spawn)
103 {
104   Vector pos = spawn->pos;
105   pos.y = height - pos.y;
106   spawn->pos = pos;
107 }
108
109 void
110 FlipLevelTransformer::transform_moving_object(float height, MovingObject*object)
111 {
112   Vector pos = object->get_pos();
113   pos.y = height - pos.y - object->get_bbox().get_height();
114   object->set_pos(pos);
115 }
116
117 void
118 FlipLevelTransformer::transform_platform(float height, Platform& platform)
119 {
120   Path& path = platform.get_path();
121   for (std::vector<Path::Node>::iterator i = path.nodes.begin(); i != path.nodes.end(); i++) {
122     Vector& pos = i->position;
123     pos.y = height - pos.y - platform.get_bbox().get_height();
124   }
125 }
126
127 void
128 FlipLevelTransformer::transform_block(float height, Block& block)
129 {
130   if (block.original_y != -1) block.original_y = height - block.original_y - block.get_bbox().get_height();
131 }
132
133 /* EOF */