src/supertux/flip_level_transformer.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "badguy/badguy.hpp"
00018 #include "object/block.hpp"
00019 #include "object/camera.hpp"
00020 #include "object/flower.hpp"
00021 #include "object/platform.hpp"
00022 #include "object/player.hpp"
00023 #include "object/tilemap.hpp"
00024 #include "supertux/flip_level_transformer.hpp"
00025 #include "supertux/sector.hpp"
00026 #include "supertux/spawn_point.hpp"
00027 
00028 void
00029 FlipLevelTransformer::transform_sector(Sector* sector)
00030 {
00031   float height = sector->get_height();
00032 
00033   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
00034       i != sector->gameobjects.end(); ++i) {
00035     GameObject* object = *i;
00036 
00037     TileMap* tilemap = dynamic_cast<TileMap*> (object);
00038     if(tilemap) {
00039       transform_tilemap(height, tilemap);
00040     }
00041     Player* player = dynamic_cast<Player*> (object);
00042     if(player) {
00043       Vector pos = player->get_pos();
00044       pos.y = height - pos.y - player->get_bbox().get_height();
00045       player->move(pos);
00046       continue;
00047     }
00048     BadGuy* badguy = dynamic_cast<BadGuy*> (object);
00049     if(badguy) {
00050       transform_badguy(height, badguy);
00051     }
00052     Flower* flower = dynamic_cast<Flower*> (object);
00053     if(flower) {
00054       transform_flower(flower);
00055     }
00056     Platform* platform = dynamic_cast<Platform*> (object);
00057     if(platform) {
00058       transform_platform(height, *platform);
00059     }
00060     Block* block = dynamic_cast<Block*> (object);
00061     if(block) {
00062       transform_block(height, *block);
00063     }
00064     MovingObject* mobject = dynamic_cast<MovingObject*> (object);
00065     if(mobject) {
00066       transform_moving_object(height, mobject);
00067     }
00068   }
00069   for(Sector::SpawnPoints::iterator i = sector->spawnpoints.begin();
00070       i != sector->spawnpoints.end(); ++i) {
00071     transform_spawnpoint(height, *i);
00072   }
00073 
00074   if(sector->camera != 0 && sector->player != 0)
00075     sector->camera->reset(sector->player->get_pos());
00076 }
00077 
00078 DrawingEffect
00079 FlipLevelTransformer::transform_drawing_effect(DrawingEffect effect)
00080 {
00081   if(effect != 0) {
00082     return NO_EFFECT;
00083   } else {
00084     return VERTICAL_FLIP;
00085   }
00086 }
00087 
00088 void
00089 FlipLevelTransformer::transform_path(float height, float obj_height, Path& path)
00090 {
00091   for (std::vector<Path::Node>::iterator i = path.nodes.begin(); i != path.nodes.end(); i++) {
00092     Vector& pos = i->position;
00093     pos.y = height - pos.y - obj_height;
00094   }
00095 }
00096 
00097 void
00098 FlipLevelTransformer::transform_tilemap(float height, TileMap* tilemap)
00099 {
00100   for(size_t x = 0; x < tilemap->get_width(); ++x) {
00101     for(size_t y = 0; y < tilemap->get_height()/2; ++y) {
00102       // swap tiles
00103       int y2 = tilemap->get_height()-1-y;
00104       uint32_t t1 = tilemap->get_tile_id(x, y);
00105       uint32_t t2 = tilemap->get_tile_id(x, y2);
00106       tilemap->change(x, y, t2);
00107       tilemap->change(x, y2, t1);
00108     }
00109   }
00110   tilemap->set_drawing_effect(transform_drawing_effect(tilemap->get_drawing_effect()));
00111   Vector offset = tilemap->get_offset();
00112   offset.y = height - offset.y - tilemap->get_bbox().get_height();
00113   tilemap->set_offset(offset);
00114   Path* path = tilemap->get_path().get();
00115   if (path)
00116     transform_path(height, tilemap->get_bbox().get_height(), *path);
00117 }
00118 
00119 void
00120 FlipLevelTransformer::transform_badguy(float height, BadGuy* badguy)
00121 {
00122   Vector pos = badguy->get_start_position();
00123   pos.y = height - pos.y;
00124   badguy->set_start_position(pos);
00125 }
00126 
00127 void
00128 FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint* spawn)
00129 {
00130   Vector pos = spawn->pos;
00131   pos.y = height - pos.y;
00132   spawn->pos = pos;
00133 }
00134 
00135 void
00136 FlipLevelTransformer::transform_moving_object(float height, MovingObject*object)
00137 {
00138   Vector pos = object->get_pos();
00139   pos.y = height - pos.y - object->get_bbox().get_height();
00140   object->set_pos(pos);
00141 }
00142 
00143 void
00144 FlipLevelTransformer::transform_flower(Flower* flower)
00145 {
00146   flower->drawing_effect = transform_drawing_effect(flower->drawing_effect);
00147 }
00148 
00149 void
00150 FlipLevelTransformer::transform_platform(float height, Platform& platform)
00151 {
00152   transform_path(height, platform.get_bbox().get_height(), platform.get_path());
00153 }
00154 
00155 void
00156 FlipLevelTransformer::transform_block(float height, Block& block)
00157 {
00158   if (block.original_y != -1) block.original_y = height - block.original_y - block.get_bbox().get_height();
00159 }
00160 
00161 /* EOF */

Generated on Mon Apr 14 03:38:25 2014 for SuperTux by  doxygen 1.5.1