6 #include "../graphics/Sprite.h"
7 #include "../loader/Interpreter.h"
8 #include "../loader/TypeDescription.h"
9 #include "../math/Vector.h"
10 #include "../sdl/utility.h"
15 using graphics::Sprite;
16 using loader::FieldDescription;
17 using loader::Interpreter;
18 using loader::TypeDescription;
36 Area *Map::AreaAt(const Vector<int> &offset) {
38 Vector<int> coords(TileCoordinates(offset));
39 Vector<int> areaOffset(coords / areas[0].Size());
40 int areaIndex(areaOffset.Index(width));
41 if (areaIndex < numAreas) {
42 return areas + areaIndex;
48 const Area *Map::AreaAt(const Vector<int> &offset) const {
50 Vector<int> coords(TileCoordinates(offset));
51 Vector<int> areaOffset(coords / areas[0].Size());
52 int areaIndex(areaOffset.Index(width));
53 if (areaIndex < numAreas) {
54 return areas + areaIndex;
60 Tile *Map::TileAt(const Vector<int> &offset) {
61 Area *area(AreaAt(offset));
63 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
64 return area->TileAt(tileOffset);
70 const Tile *Map::TileAt(const Vector<int> &offset) const {
71 const Area *area(AreaAt(offset));
73 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
74 return area->TileAt(tileOffset);
80 Trigger *Map::TriggerAt(const math::Vector<int> &offset) {
81 // TODO: add support for multiple triggers on a tile?
82 Vector<int> coords(TileCoordinates(offset));
83 for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
84 if (i->TilePosition() == coords) {
91 SDL_Surface *Map::BattleBackgroundAt(const math::Vector<int> &position) {
92 Tile *tile(TileAt(position));
93 if (tile && tile->BattleBackground()) {
94 return tile->BattleBackground();
96 Area *area(AreaAt(position));
97 if (area && area->BattleBackground()) {
98 return area->BattleBackground();
103 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
104 return position / tileset->Size();
110 const Vector<int> &inOffset,
111 unsigned int frame) 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, frame);
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 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Map"));
160 td.SetConstructor(&Construct);
161 td.SetSize(sizeof(Map));
163 td.AddField("tileset", FieldDescription(((char *)&m.tileset) - ((char *)&m), Sprite::TYPE_ID).SetReferenced());
164 td.AddField("battlebg", FieldDescription(((char *)&m.battlebg) - ((char *)&m), Interpreter::IMAGE_ID).SetReferenced());
165 td.AddField("areas", FieldDescription(((char *)&m.areas) - ((char *)&m), Area::TYPE_ID).SetAggregate());
166 td.AddField("triggers", FieldDescription(((char *)&m.triggers) - ((char *)&m), Trigger::TYPE_ID).SetAggregate());
167 td.AddField("entities", FieldDescription(((char *)&m.entities) - ((char *)&m), Entity::TYPE_ID).SetAggregate());
168 td.AddField("width", FieldDescription(((char *)&m.width) - ((char *)&m), Interpreter::NUMBER_ID));
171 void Map::Construct(void *data) {