]> git.localhorst.tv Git - l2e.git/blob - src/map/Map.cpp
extracted map loading/unloading
[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 , entities(0)
27 , numEntities(0)
28 , width(0) {
29
30 }
31
32
33 const Area &Map::AreaAt(const Vector<int> &offset) const {
34         if (numAreas > 0) {
35                 Vector<int> coords(TileCoordinates(offset));
36                 Vector<int> areaOffset(coords / areas[0].Size());
37                 int areaIndex(areaOffset.Index(width));
38                 if (areaIndex < numAreas) {
39                         return areas[areaIndex];
40                 }
41         }
42         throw std::out_of_range("area offset out of bounds");
43 }
44
45 const Tile &Map::TileAt(const Vector<int> &offset) const {
46         const Area &area(AreaAt(offset));
47         Vector<int> tileOffset(TileCoordinates(offset) % area.Size());
48         return area.TileAt(tileOffset);
49 }
50
51 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
52         // TODO: add support for multiple triggers on a tile?
53         Vector<int> coords(TileCoordinates(offset));
54         for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
55                 if (i->TilePosition() == coords) {
56                         return i;
57                 }
58         }
59         return 0;
60 }
61
62 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
63         return position / tileset->Size();
64 }
65
66
67 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
68         // TODO: skip invisible areas
69         for (int i(0); i < numAreas; ++i) {
70                 const Area &area(areas[i]);
71                 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
72                 area.Render(dest, tileset, offset);
73         }
74 }
75
76 void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
77         // TODO: skip invisible areas
78         for (int i(0); i < numAreas; ++i) {
79                 const Area &area(areas[i]);
80                 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
81                 area.RenderDebug(dest, tileset, offset);
82         }
83         for (int i(0); i < numTriggers; ++i) {
84                 Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
85                 SDL_Rect destRect;
86                 destRect.x = offset.X() + (tileset->Width() / 4);
87                 destRect.y = offset.Y() + (tileset->Height() / 4);
88                 destRect.w = tileset->Width() / 2;
89                 destRect.h = tileset->Height() / 2;
90                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
91         }
92 }
93
94 }