| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library 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 GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
#ifndef FIFE_LOCATION_H |
| 23 |
#define FIFE_LOCATION_H |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
#include <iostream> |
| 27 |
|
| 28 |
// 3rd party library includes |
| 29 |
|
| 30 |
// FIFE includes |
| 31 |
// These includes are split up in two parts, separated by one empty line |
| 32 |
// First block: files included from the FIFE root src directory |
| 33 |
// Second block: files included from the same folder |
| 34 |
#include "model/metamodel/modelcoords.h" |
| 35 |
#include "util/base/exception.h" |
| 36 |
|
| 37 |
namespace FIFE { |
| 38 |
class Map; |
| 39 |
class Layer; |
| 40 |
|
| 41 |
class Location { |
| 42 |
public: |
| 43 |
/** Default constructor |
| 44 |
*/ |
| 45 |
Location(); |
| 46 |
|
| 47 |
/** Copy constructor |
| 48 |
*/ |
| 49 |
Location(const Location& loc); |
| 50 |
|
| 51 |
/** Utility constructor |
| 52 |
*/ |
| 53 |
Location(Layer* layer); |
| 54 |
|
| 55 |
/** Destructor |
| 56 |
*/ |
| 57 |
~Location(); |
| 58 |
|
| 59 |
/** Resets location (so that layer and coordinate information becomes invalid) |
| 60 |
*/ |
| 61 |
void reset(); |
| 62 |
|
| 63 |
/** Assignment operator |
| 64 |
*/ |
| 65 |
Location& operator=(const Location& rhs); |
| 66 |
|
| 67 |
/** Compares equality of two locations |
| 68 |
*/ |
| 69 |
inline bool operator==(const Location& loc) const { |
| 70 |
return ((m_layer == loc.m_layer) && (m_exact_layer_coords == loc.m_exact_layer_coords)); |
| 71 |
} |
| 72 |
|
| 73 |
/** Compares unequality of two locations |
| 74 |
*/ |
| 75 |
inline bool operator!=(const Location& loc) const { |
| 76 |
return !(*this == loc); |
| 77 |
} |
| 78 |
|
| 79 |
/** Gets the map where this location is pointing to |
| 80 |
* @note this information is fetched from the set layer |
| 81 |
* @return map where this location is pointing to, NULL in case its invalid |
| 82 |
*/ |
| 83 |
Map* getMap() const; |
| 84 |
|
| 85 |
/** Sets layer where this location is pointing to |
| 86 |
* @param layer layer to set |
| 87 |
*/ |
| 88 |
void setLayer(Layer* layer); |
| 89 |
|
| 90 |
/** Gets the layer where this location is pointing to |
| 91 |
* @return layer where this location is pointing to, NULL in case its invalid |
| 92 |
*/ |
| 93 |
Layer* getLayer() const; |
| 94 |
|
| 95 |
/** Sets precise layer coordinates to this location |
| 96 |
* @throws NotSet in the following cases: |
| 97 |
* - layer is not set (NULL) |
| 98 |
* - layer does not have cellgrid assigned |
| 99 |
* @param coordinates coordinates to set |
| 100 |
*/ |
| 101 |
void setExactLayerCoordinates(const ExactModelCoordinate& coordinates); |
| 102 |
|
| 103 |
/** Sets "cell precise" layer coordinates to this location |
| 104 |
* @throws NotSet in the following cases: |
| 105 |
* - layer is not set (NULL) |
| 106 |
* - layer does not have cellgrid assigned |
| 107 |
* @see setLayerCoordinates(const ExactModelCoordinate& coordinates) |
| 108 |
*/ |
| 109 |
void setLayerCoordinates(const ModelCoordinate& coordinates); |
| 110 |
|
| 111 |
/** Sets map coordinates to this location |
| 112 |
* @param coordinates coordinates to set |
| 113 |
*/ |
| 114 |
void setMapCoordinates(const ExactModelCoordinate& coordinates); |
| 115 |
|
| 116 |
/** Gets reference to exact layer coordinates. This means that if you |
| 117 |
* modify the coordinates, location gets modified directly |
| 118 |
* @return reference to exact layer coordinates |
| 119 |
*/ |
| 120 |
ExactModelCoordinate& getExactLayerCoordinatesRef(); |
| 121 |
|
| 122 |
/** Gets exact layer coordinates set to this location |
| 123 |
* @return exact layer coordinates |
| 124 |
*/ |
| 125 |
ExactModelCoordinate getExactLayerCoordinates() const; |
| 126 |
|
| 127 |
/** Gets exact layer coordinates of this location mapped on given layer |
| 128 |
* @throws NotSet in the following cases: |
| 129 |
* - given layer is not set (NULL) |
| 130 |
* - given layer does not have cellgrid assigned |
| 131 |
* @return exact layer coordinates |
| 132 |
*/ |
| 133 |
ExactModelCoordinate getExactLayerCoordinates(const Layer* layer) const; |
| 134 |
|
| 135 |
/** Gets cell precision layer coordinates set to this location |
| 136 |
* @see getExactLayerCoordinates() |
| 137 |
*/ |
| 138 |
ModelCoordinate getLayerCoordinates() const; |
| 139 |
|
| 140 |
/** Gets cell precision layer coordinates of this location mapped on given layer |
| 141 |
* @see getExactLayerCoordinates(const Layer* layer) |
| 142 |
*/ |
| 143 |
ModelCoordinate getLayerCoordinates(const Layer* layer) const; |
| 144 |
|
| 145 |
/** Gets map coordinates set to this location |
| 146 |
* @return map coordinates |
| 147 |
*/ |
| 148 |
ExactModelCoordinate getMapCoordinates() const; |
| 149 |
|
| 150 |
/** Gets offset distance from cell center |
| 151 |
* @return offset distance |
| 152 |
*/ |
| 153 |
double getCellOffsetDistance() const; |
| 154 |
|
| 155 |
/** Tells if location is valid |
| 156 |
* Location is valid if: |
| 157 |
* - layer is set |
| 158 |
* - layer has cellgrid |
| 159 |
*/ |
| 160 |
bool isValid() const; |
| 161 |
|
| 162 |
/** Gets distance in map coordinates to another location on the Map |
| 163 |
* @param location is the location you want to get the distance to |
| 164 |
*/ |
| 165 |
double getMapDistanceTo(const Location& location) const; |
| 166 |
|
| 167 |
/** Gets layer distance to another location |
| 168 |
* @param location is the location you want to get the distance to |
| 169 |
* In case location resides on different layer, it is mapped to this layer |
| 170 |
*/ |
| 171 |
double getLayerDistanceTo(const Location& location) const; |
| 172 |
|
| 173 |
private: |
| 174 |
bool isValid(const Layer* layer) const; |
| 175 |
|
| 176 |
Layer* m_layer; |
| 177 |
ExactModelCoordinate m_exact_layer_coords; |
| 178 |
}; |
| 179 |
|
| 180 |
/** Stream output operator. |
| 181 |
* |
| 182 |
* Useful for debugging purposes |
| 183 |
*/ |
| 184 |
std::ostream& operator<<(std::ostream&, const Location&); |
| 185 |
} |
| 186 |
#endif //FIFE_LOCATION_H |