3065fa833f0faaf58cd5a2b5467865b744d9a071
[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/flower.hpp"
21 #include "object/platform.hpp"
22 #include "object/player.hpp"
23 #include "object/tilemap.hpp"
24 #include "supertux/flip_level_transformer.hpp"
25 #include "supertux/sector.hpp"
26 #include "supertux/spawn_point.hpp"
27
28 void
29 FlipLevelTransformer::transform_sector(Sector* sector)
30 {
31   float height = sector->get_height();
32
33   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
34       i != sector->gameobjects.end(); ++i) {
35     GameObjectPtr object = *i;
36
37     TileMap* tilemap = dynamic_cast<TileMap*>(object.get());
38     if(tilemap) {
39       transform_tilemap(height, *tilemap);
40     }
41     Player* player = dynamic_cast<Player*>(object.get());
42     if(player) {
43       Vector pos = player->get_pos();
44       pos.y = height - pos.y - player->get_bbox().get_height();
45       player->move(pos);
46       continue;
47     }
48     BadGuy* badguy = dynamic_cast<BadGuy*>(object.get());
49     if(badguy) {
50       transform_badguy(height, *badguy);
51     }
52     Flower* flower = dynamic_cast<Flower*>(object.get());
53     if(flower) {
54       transform_flower(*flower);
55     }
56     Platform* platform = dynamic_cast<Platform*>(object.get());
57     if(platform) {
58       transform_platform(height, *platform);
59     }
60     Block* block = dynamic_cast<Block*>(object.get());
61     if(block) {
62       transform_block(height, *block);
63     }
64     MovingObject* mobject = dynamic_cast<MovingObject*>(object.get());
65     if(mobject) {
66       transform_moving_object(height, *mobject);
67     }
68   }
69   for(auto i = sector->spawnpoints.begin(); 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 DrawingEffect
78 FlipLevelTransformer::transform_drawing_effect(DrawingEffect effect)
79 {
80   if (effect & VERTICAL_FLIP) {
81     return effect & ~VERTICAL_FLIP;
82   } else {
83     return effect | VERTICAL_FLIP;
84   }
85 }
86
87 void
88 FlipLevelTransformer::transform_path(float height, float obj_height, Path& path)
89 {
90   for (std::vector<Path::Node>::iterator i = path.nodes.begin(); i != path.nodes.end(); i++) {
91     Vector& pos = i->position;
92     pos.y = height - pos.y - obj_height;
93   }
94 }
95
96 void
97 FlipLevelTransformer::transform_tilemap(float height, TileMap& tilemap)
98 {
99   for(size_t x = 0; x < tilemap.get_width(); ++x) {
100     for(size_t y = 0; y < tilemap.get_height()/2; ++y) {
101       // swap tiles
102       int y2 = tilemap.get_height()-1-y;
103       uint32_t t1 = tilemap.get_tile_id(x, y);
104       uint32_t t2 = tilemap.get_tile_id(x, y2);
105       tilemap.change(x, y, t2);
106       tilemap.change(x, y2, t1);
107     }
108   }
109   tilemap.set_drawing_effect(transform_drawing_effect(tilemap.get_drawing_effect()));
110   Vector offset = tilemap.get_offset();
111   offset.y = height - offset.y - tilemap.get_bbox().get_height();
112   tilemap.set_offset(offset);
113   Path* path = tilemap.get_path().get();
114   if (path)
115     transform_path(height, tilemap.get_bbox().get_height(), *path);
116 }
117
118 void
119 FlipLevelTransformer::transform_badguy(float height, BadGuy& badguy)
120 {
121   Vector pos = badguy.get_start_position();
122   pos.y = height - pos.y;
123   badguy.set_start_position(pos);
124 }
125
126 void
127 FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint& spawn)
128 {
129   Vector pos = spawn.pos;
130   pos.y = height - pos.y;
131   spawn.pos = pos;
132 }
133
134 void
135 FlipLevelTransformer::transform_moving_object(float height, MovingObject& object)
136 {
137   Vector pos = object.get_pos();
138   pos.y = height - pos.y - object.get_bbox().get_height();
139   object.set_pos(pos);
140 }
141
142 void
143 FlipLevelTransformer::transform_flower(Flower& flower)
144 {
145   flower.drawing_effect = transform_drawing_effect(flower.drawing_effect);
146 }
147
148 void
149 FlipLevelTransformer::transform_platform(float height, Platform& platform)
150 {
151   transform_path(height, platform.get_bbox().get_height(), platform.get_path());
152 }
153
154 void
155 FlipLevelTransformer::transform_block(float height, Block& block)
156 {
157   if (block.original_y != -1) block.original_y = height - block.original_y - block.get_bbox().get_height();
158 }
159
160 /* EOF */