6 #include "../graphics/Sprite.h"
7 #include "../loader/Interpreter.h"
8 #include "../loader/TypeDescription.h"
9 #include "../sdl/utility.h"
13 using geometry::Vector;
14 using graphics::Sprite;
15 using loader::FieldDescription;
16 using loader::Interpreter;
17 using loader::TypeDescription;
35 Area *Map::AreaAt(const Vector<int> &offset) {
37 Vector<int> coords(TileCoordinates(offset));
38 Vector<int> areaOffset(coords / areas[0].Size());
39 int areaIndex(areaOffset.Index(width));
40 if (areaIndex < numAreas) {
41 return areas + areaIndex;
47 const Area *Map::AreaAt(const Vector<int> &offset) const {
49 Vector<int> coords(TileCoordinates(offset));
50 Vector<int> areaOffset(coords / areas[0].Size());
51 int areaIndex(areaOffset.Index(width));
52 if (areaIndex < numAreas) {
53 return areas + areaIndex;
59 Tile *Map::TileAt(const Vector<int> &offset) {
60 Area *area(AreaAt(offset));
62 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
63 return area->TileAt(tileOffset);
69 const Tile *Map::TileAt(const Vector<int> &offset) const {
70 const Area *area(AreaAt(offset));
72 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
73 return area->TileAt(tileOffset);
79 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
80 // TODO: add support for multiple triggers on a tile?
81 Vector<int> coords(TileCoordinates(offset));
82 for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
83 if (i->TilePosition() == coords) {
90 SDL_Surface *Map::BattleBackgroundAt(const geometry::Vector<int> &position) {
91 Tile *tile(TileAt(position));
92 if (tile && tile->BattleBackground()) {
93 return tile->BattleBackground();
95 Area *area(AreaAt(position));
96 if (area && area->BattleBackground()) {
97 return area->BattleBackground();
102 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
103 return position / tileset->Size();
107 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
108 // TODO: skip invisible areas
109 for (int i(0); i < numAreas; ++i) {
110 const Area &area(areas[i]);
111 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
112 area.Render(dest, tileset, offset);
116 void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
117 // TODO: skip invisible areas
118 for (int i(0); i < numAreas; ++i) {
119 const Area &area(areas[i]);
120 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
121 area.RenderDebug(dest, tileset, offset);
123 for (int i(0); i < numTriggers; ++i) {
124 Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
125 switch (triggers[i].GetType()) {
126 case Trigger::TYPE_NORTH:
127 sdl::HorizontalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
129 case Trigger::TYPE_EAST:
130 sdl::VerticalLine(dest, offset + Vector<int>(tileset->Width() * 3 / 4, tileset->Height() / 4), tileset->Height() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
132 case Trigger::TYPE_SOUTH:
133 sdl::HorizontalLine(dest, offset + Vector<int>(tileset->Width() / 4, tileset->Height() * 3 / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
135 case Trigger::TYPE_WEST:
136 sdl::VerticalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
138 case Trigger::TYPE_CONTACT: {
140 destRect.x = offset.X() + (tileset->Width() / 4);
141 destRect.y = offset.Y() + (tileset->Height() / 4);
142 destRect.w = tileset->Width() / 2;
143 destRect.h = tileset->Height() / 2;
144 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
152 void Map::CreateTypeDescription() {
155 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Map"));
156 td.SetConstructor(&Construct);
157 td.SetSize(sizeof(Map));
159 td.AddField("tileset", FieldDescription(((char *)&m.tileset) - ((char *)&m), Sprite::TYPE_ID).SetReferenced());
160 td.AddField("battlebg", FieldDescription(((char *)&m.battlebg) - ((char *)&m), Interpreter::IMAGE_ID).SetReferenced());
161 td.AddField("areas", FieldDescription(((char *)&m.areas) - ((char *)&m), Area::TYPE_ID).SetReferenced().SetAggregate());
162 td.AddField("triggers", FieldDescription(((char *)&m.triggers) - ((char *)&m), Trigger::TYPE_ID).SetReferenced().SetAggregate());
163 td.AddField("entities", FieldDescription(((char *)&m.entities) - ((char *)&m), Entity::TYPE_ID).SetReferenced().SetAggregate());
164 td.AddField("width", FieldDescription(((char *)&m.width) - ((char *)&m), Interpreter::NUMBER_ID));
167 void Map::Construct(void *data) {