4 * Created on: Sep 29, 2012
13 #include "../graphics/Sprite.h"
14 #include "../loader/TypeDescription.h"
15 #include "../sdl/utility.h"
19 using geometry::Vector;
20 using loader::FieldDescription;
21 using loader::TypeDescription;
39 Area *Map::AreaAt(const Vector<int> &offset) {
41 Vector<int> coords(TileCoordinates(offset));
42 Vector<int> areaOffset(coords / areas[0].Size());
43 int areaIndex(areaOffset.Index(width));
44 if (areaIndex < numAreas) {
45 return areas + areaIndex;
51 const Area *Map::AreaAt(const Vector<int> &offset) const {
53 Vector<int> coords(TileCoordinates(offset));
54 Vector<int> areaOffset(coords / areas[0].Size());
55 int areaIndex(areaOffset.Index(width));
56 if (areaIndex < numAreas) {
57 return areas + areaIndex;
63 Tile *Map::TileAt(const Vector<int> &offset) {
64 Area *area(AreaAt(offset));
66 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
67 return area->TileAt(tileOffset);
73 const Tile *Map::TileAt(const Vector<int> &offset) const {
74 const Area *area(AreaAt(offset));
76 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
77 return area->TileAt(tileOffset);
83 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
84 // TODO: add support for multiple triggers on a tile?
85 Vector<int> coords(TileCoordinates(offset));
86 for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
87 if (i->TilePosition() == coords) {
94 SDL_Surface *Map::BattleBackgroundAt(const geometry::Vector<int> &position) {
95 Tile *tile(TileAt(position));
96 if (tile && tile->BattleBackground()) {
97 return tile->BattleBackground();
99 Area *area(AreaAt(position));
100 if (area && area->BattleBackground()) {
101 return area->BattleBackground();
106 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
107 return position / tileset->Size();
111 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
112 // TODO: skip invisible areas
113 for (int i(0); i < numAreas; ++i) {
114 const Area &area(areas[i]);
115 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
116 area.Render(dest, tileset, offset);
120 void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
121 // TODO: skip invisible areas
122 for (int i(0); i < numAreas; ++i) {
123 const Area &area(areas[i]);
124 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
125 area.RenderDebug(dest, tileset, offset);
127 for (int i(0); i < numTriggers; ++i) {
128 Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
129 switch (triggers[i].GetType()) {
130 case Trigger::TYPE_NORTH:
131 sdl::HorizontalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
133 case Trigger::TYPE_EAST:
134 sdl::VerticalLine(dest, offset + Vector<int>(tileset->Width() * 3 / 4, tileset->Height() / 4), tileset->Height() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
136 case Trigger::TYPE_SOUTH:
137 sdl::HorizontalLine(dest, offset + Vector<int>(tileset->Width() / 4, tileset->Height() * 3 / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
139 case Trigger::TYPE_WEST:
140 sdl::VerticalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
142 case Trigger::TYPE_CONTACT: {
144 destRect.x = offset.X() + (tileset->Width() / 4);
145 destRect.y = offset.Y() + (tileset->Height() / 4);
146 destRect.w = tileset->Width() / 2;
147 destRect.h = tileset->Height() / 2;
148 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
156 void Map::CreateTypeDescription() {
159 int areaId(TypeDescription::GetTypeId("Area"));
160 int entityId(TypeDescription::GetTypeId("Entity"));
161 int imageId(TypeDescription::GetTypeId("Image"));
162 int numberId(TypeDescription::GetTypeId("Number"));
163 int spriteId(TypeDescription::GetTypeId("Sprite"));
164 int triggerId(TypeDescription::GetTypeId("Trigger"));
166 TypeDescription &td(TypeDescription::CreateOrGet("Map"));
167 td.SetConstructor(&Construct);
168 td.SetSize(sizeof(Map));
170 td.AddField("tileset", FieldDescription(((char *)&m.tileset) - ((char *)&m), spriteId).SetReferenced());
171 td.AddField("battlebg", FieldDescription(((char *)&m.battlebg) - ((char *)&m), imageId).SetReferenced());
172 td.AddField("areas", FieldDescription(((char *)&m.areas) - ((char *)&m), areaId).SetReferenced().SetAggregate());
173 td.AddField("triggers", FieldDescription(((char *)&m.triggers) - ((char *)&m), triggerId).SetReferenced().SetAggregate());
174 td.AddField("entities", FieldDescription(((char *)&m.entities) - ((char *)&m), entityId).SetReferenced().SetAggregate());
175 td.AddField("width", FieldDescription(((char *)&m.width) - ((char *)&m), numberId));
178 void Map::Construct(void *data) {