location.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 #include <SDL.h>
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "util/base/exception.h"
00032 #include "util/structures/purge.h"
00033 #include "model/metamodel/grids/cellgrid.h"
00034 
00035 #include "layer.h"
00036 #include "instance.h"
00037 
00038 namespace FIFE {
00039     static std::string INVALID_LAYER_SET = "Cannot set layer coordinates, given layer is not initialized properly";
00040     static std::string INVALID_LAYER_GET = "Cannot get layer coordinates, layer is not initialized properly";
00041     
00042     Location::Location() {
00043         reset();
00044     }
00045 
00046     Location::Location(const Location& loc) {
00047         reset();
00048         m_layer = loc.m_layer;
00049         m_exact_layer_coords = loc.m_exact_layer_coords;
00050     }
00051     
00052     Location::Location(Layer* layer) {
00053         reset();
00054         m_layer = layer;
00055     }
00056     
00057     Location::~Location() {
00058         reset();
00059     }
00060     
00061     void Location::reset() {
00062         m_exact_layer_coords.x = 0;
00063         m_exact_layer_coords.y = 0;
00064         m_layer = NULL;
00065     }
00066     
00067     Location& Location::operator=(const Location& rhs) {
00068         m_layer = rhs.m_layer;
00069         m_exact_layer_coords.x = rhs.m_exact_layer_coords.x;
00070         m_exact_layer_coords.y = rhs.m_exact_layer_coords.y;
00071         return *this;
00072     }
00073     
00074     Map* Location::getMap() const {
00075         if (!m_layer) {
00076             return NULL;
00077         }
00078         return m_layer->getMap();
00079     }
00080         
00081     void Location::setLayer(Layer* layer) {
00082         m_layer = layer;
00083     }
00084     
00085     Layer* Location::getLayer() const {
00086         return m_layer;
00087     }
00088     
00089     void Location::setExactLayerCoordinates(const ExactModelCoordinate& coordinates) {
00090         if (!isValid()) {
00091             throw NotSet(INVALID_LAYER_SET);
00092         }
00093         m_exact_layer_coords = coordinates;
00094     }
00095     
00096     void Location::setLayerCoordinates(const ModelCoordinate& coordinates) {
00097         setExactLayerCoordinates(intPt2doublePt(coordinates));
00098     }
00099     
00100     void Location::setMapCoordinates(const ExactModelCoordinate& coordinates) {
00101         if (!isValid()) {
00102             throw NotSet(INVALID_LAYER_SET);
00103         }
00104         m_exact_layer_coords = m_layer->getCellGrid()->toExactLayerCoordinates(coordinates);
00105     }
00106     
00107     ExactModelCoordinate& Location::getExactLayerCoordinatesRef() {
00108         return m_exact_layer_coords;
00109     }
00110     
00111     ExactModelCoordinate Location::getExactLayerCoordinates() const {
00112         return m_exact_layer_coords;
00113     }
00114     
00115     ModelCoordinate Location::getLayerCoordinates() const {
00116         return getLayerCoordinates(m_layer);
00117     }
00118     
00119     ExactModelCoordinate Location::getMapCoordinates() const {
00120         return m_layer->getCellGrid()->toMapCoordinates(m_exact_layer_coords);
00121     }
00122     
00123     bool Location::isValid() const {
00124         return isValid(m_layer);
00125     }
00126     
00127     bool Location::isValid(const Layer* layer) const {
00128         return (layer && layer->getCellGrid());
00129     }
00130     
00131     ExactModelCoordinate Location::getExactLayerCoordinates(const Layer* layer) const {
00132         return m_exact_layer_coords;
00133     }
00134     
00135     ModelCoordinate Location::getLayerCoordinates(const Layer* layer) const {
00136         if (!isValid(layer)) {
00137             throw NotSet(INVALID_LAYER_GET);
00138         }
00139         CellGrid* cg1 = m_layer->getCellGrid();
00140         CellGrid* cg2 = layer->getCellGrid();
00141         return cg2->toLayerCoordinates(cg1->toMapCoordinates(m_exact_layer_coords));
00142     }
00143     
00144     double Location::getCellOffsetDistance() const {
00145         const ExactModelCoordinate& pt  = m_exact_layer_coords;
00146         double dx = pt.x - static_cast<double>(static_cast<int>(pt.x));
00147         double dy = pt.y - static_cast<double>(static_cast<int>(pt.y));
00148         return sqrt(dx*dx + dy*dy);
00149     }
00150     
00151     std::ostream& operator<<(std::ostream& os, const Location& l) {
00152         ExactModelCoordinate p = l.getExactLayerCoordinates();
00153         return os << "x=" << p.x << ", y=" << p.y;
00154     }
00155     
00156     double Location::getMapDistanceTo(const Location& location) const{
00157         ExactModelCoordinate current = getMapCoordinates();
00158         ExactModelCoordinate target = location.getMapCoordinates();
00159         
00160         double rx = current.x - target.x;
00161         double ry = current.y - target.y;
00162         double rz = current.z - target.z;
00163         
00164         return sqrt(rx*rx + ry*ry + rz*rz);
00165     }
00166 
00167     double Location::getLayerDistanceTo(const Location& location) const{
00168         ModelCoordinate current = getLayerCoordinates();
00169         ModelCoordinate target = location.getLayerCoordinates(m_layer);
00170         
00171         double rx = current.x - target.x;
00172         double ry = current.y - target.y;
00173         double rz = current.z - target.z;
00174         
00175         return sqrt(rx*rx + ry*ry + rz*rz);
00176     }   
00177 }
Generated by  doxygen 1.6.2-20100208