]> git.localhorst.tv Git - l2e.git/blob - src/map/Map.cpp
check blocking flags of tiles in map state
[l2e.git] / src / map / Map.cpp
1 /*
2  * Map.cpp
3  *
4  *  Created on: Sep 29, 2012
5  *      Author: holy
6  */
7
8 #include "Map.h"
9
10 #include "Area.h"
11 #include "../graphics/Sprite.h"
12
13 #include <stdexcept>
14
15 using geometry::Vector;
16
17 namespace map {
18
19 Map::Map()
20 : tileset(0)
21 , areas(0)
22 , numAreas(0)
23 , width(0) {
24
25 }
26
27
28 const Area &Map::AreaAt(const Vector<int> &offset) const {
29         if (numAreas > 0) {
30                 Vector<int> tileOffset(offset.X() / tileset->Width(), offset.Y() / tileset->Height());
31                 Vector<int> areaOffset(tileOffset.X() / areas[0].Width(), tileOffset.Y() / areas[0].Height());
32                 int areaIndex(areaOffset.Y() * width + areaOffset.X());
33                 if (areaIndex < numAreas) {
34                         return areas[areaIndex];
35                 }
36         }
37         throw std::out_of_range("area offset out of bounds");
38 }
39
40 const Tile &Map::TileAt(const Vector<int> &offset) const {
41         const Area &area(AreaAt(offset));
42         Vector<int> tileOffset((offset.X() / tileset->Width()) % area.Width(), (offset.Y() / tileset->Height()) % area.Height());
43         return area.TileAt(tileOffset);
44 }
45
46
47 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
48         // TODO: skip invisible areas
49         for (int i(0); i < numAreas; ++i) {
50                 const Area &area(areas[i]);
51                 Vector<int> offset(
52                                 inOffset.X() + (i % width) * area.Width() * tileset->Width(),
53                                 inOffset.Y() + (i / width) * area.Height() * tileset->Height());
54                 area.Render(dest, tileset, offset);
55         }
56 }
57
58 }