]> git.localhorst.tv Git - l2e.git/blobdiff - src/map/Map.cpp
added grid lock checking in map state
[l2e.git] / src / map / Map.cpp
index 450c00463ab7d3aa28ae90fcafd067f3620d833f..2e05e8bf9025832bd810e20d8a0d1861574e8e95 100644 (file)
@@ -10,6 +10,8 @@
 #include "Area.h"
 #include "../graphics/Sprite.h"
 
+#include <stdexcept>
+
 using geometry::Vector;
 
 namespace map {
@@ -23,6 +25,25 @@ Map::Map()
 }
 
 
+const Area &Map::AreaAt(const Vector<int> &offset) const {
+       if (numAreas > 0) {
+               Vector<int> tileOffset(offset.X() / tileset->Width(), offset.Y() / tileset->Height());
+               Vector<int> areaOffset(tileOffset.X() / areas[0].Width(), tileOffset.Y() / areas[0].Height());
+               int areaIndex(areaOffset.Y() * width + areaOffset.X());
+               if (areaIndex < numAreas) {
+                       return areas[areaIndex];
+               }
+       }
+       throw std::out_of_range("area offset out of bounds");
+}
+
+const Tile &Map::TileAt(const Vector<int> &offset) const {
+       const Area &area(AreaAt(offset));
+       Vector<int> tileOffset((offset.X() / tileset->Width()) % area.Width(), (offset.Y() / tileset->Height()) % area.Height());
+       return area.TileAt(tileOffset);
+}
+
+
 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
        // TODO: skip invisible areas
        for (int i(0); i < numAreas; ++i) {