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