]> git.localhorst.tv Git - l2e.git/blob - src/map/Map.cpp
extracted tile coordinates calculation
[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 "Trigger.h"
12 #include "../graphics/Sprite.h"
13
14 #include <stdexcept>
15
16 using geometry::Vector;
17
18 namespace map {
19
20 Map::Map()
21 : tileset(0)
22 , areas(0)
23 , numAreas(0)
24 , triggers(0)
25 , numTriggers(0)
26 , width(0) {
27
28 }
29
30
31 const Area &Map::AreaAt(const Vector<int> &offset) const {
32         if (numAreas > 0) {
33                 Vector<int> coords(TileCoordinates(offset));
34                 Vector<int> areaOffset(coords.X() / areas[0].Width(), coords.Y() / areas[0].Height());
35                 int areaIndex(areaOffset.Y() * width + areaOffset.X());
36                 if (areaIndex < numAreas) {
37                         return areas[areaIndex];
38                 }
39         }
40         throw std::out_of_range("area offset out of bounds");
41 }
42
43 const Tile &Map::TileAt(const Vector<int> &offset) const {
44         const Area &area(AreaAt(offset));
45         Vector<int> tileOffset((offset.X() / tileset->Width()) % area.Width(), (offset.Y() / tileset->Height()) % area.Height());
46         return area.TileAt(tileOffset);
47 }
48
49 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
50         // TODO: add support for multiple triggers on a tile?
51         Vector<int> coords(TileCoordinates(offset));
52         for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
53                 if (i->TilePosition() == coords) {
54                         return i;
55                 }
56         }
57         return 0;
58 }
59
60 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
61         return Vector<int>(position.X() / tileset->Width(), position.Y() / tileset->Height());
62 }
63
64
65 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
66         // TODO: skip invisible areas
67         for (int i(0); i < numAreas; ++i) {
68                 const Area &area(areas[i]);
69                 Vector<int> offset(
70                                 inOffset.X() + (i % width) * area.Width() * tileset->Width(),
71                                 inOffset.Y() + (i / width) * area.Height() * tileset->Height());
72                 area.Render(dest, tileset, offset);
73         }
74 }
75
76 }