-/*
- * Map.cpp
- *
- * Created on: Sep 29, 2012
- * Author: holy
- */
-
#include "Map.h"
#include "Area.h"
+#include "Tile.h"
#include "Trigger.h"
#include "../graphics/Sprite.h"
+#include "../loader/Interpreter.h"
+#include "../loader/TypeDescription.h"
+#include "../math/Vector.h"
+#include "../sdl/utility.h"
#include <stdexcept>
-using geometry::Vector;
+using math::Vector;
+using graphics::Sprite;
+using loader::FieldDescription;
+using loader::Interpreter;
+using loader::TypeDescription;
namespace map {
Map::Map()
: tileset(0)
+, battlebg(0)
, areas(0)
, numAreas(0)
, triggers(0)
}
+Area *Map::AreaAt(const Vector<int> &offset) {
+ if (numAreas > 0) {
+ Vector<int> coords(TileCoordinates(offset));
+ Vector<int> areaOffset(coords / areas[0].Size());
+ int areaIndex(areaOffset.Index(width));
+ if (areaIndex < numAreas) {
+ return areas + areaIndex;
+ }
+ }
+ return 0;
+}
+
const Area *Map::AreaAt(const Vector<int> &offset) const {
if (numAreas > 0) {
Vector<int> coords(TileCoordinates(offset));
return 0;
}
+Tile *Map::TileAt(const Vector<int> &offset) {
+ Area *area(AreaAt(offset));
+ if (area) {
+ Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
+ return area->TileAt(tileOffset);
+ } else {
+ return 0;
+ }
+}
+
const Tile *Map::TileAt(const Vector<int> &offset) const {
const Area *area(AreaAt(offset));
if (area) {
}
}
-Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
+Trigger *Map::TriggerAt(const math::Vector<int> &offset) {
// TODO: add support for multiple triggers on a tile?
Vector<int> coords(TileCoordinates(offset));
for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
return 0;
}
+SDL_Surface *Map::BattleBackgroundAt(const math::Vector<int> &position) {
+ Tile *tile(TileAt(position));
+ if (tile && tile->BattleBackground()) {
+ return tile->BattleBackground();
+ }
+ Area *area(AreaAt(position));
+ if (area && area->BattleBackground()) {
+ return area->BattleBackground();
+ }
+ return battlebg;
+}
+
Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
return position / tileset->Size();
}
}
for (int i(0); i < numTriggers; ++i) {
Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
- SDL_Rect destRect;
- destRect.x = offset.X() + (tileset->Width() / 4);
- destRect.y = offset.Y() + (tileset->Height() / 4);
- destRect.w = tileset->Width() / 2;
- destRect.h = tileset->Height() / 2;
- SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
+ switch (triggers[i].GetType()) {
+ case Trigger::TYPE_NORTH:
+ sdl::HorizontalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
+ break;
+ case Trigger::TYPE_EAST:
+ sdl::VerticalLine(dest, offset + Vector<int>(tileset->Width() * 3 / 4, tileset->Height() / 4), tileset->Height() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
+ break;
+ case Trigger::TYPE_SOUTH:
+ sdl::HorizontalLine(dest, offset + Vector<int>(tileset->Width() / 4, tileset->Height() * 3 / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
+ break;
+ case Trigger::TYPE_WEST:
+ sdl::VerticalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
+ break;
+ case Trigger::TYPE_CONTACT: {
+ SDL_Rect destRect;
+ destRect.x = offset.X() + (tileset->Width() / 4);
+ destRect.y = offset.Y() + (tileset->Height() / 4);
+ destRect.w = tileset->Width() / 2;
+ destRect.h = tileset->Height() / 2;
+ SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
+ }
+ break;
+ }
}
}
+
+void Map::CreateTypeDescription() {
+ Map m;
+
+ TypeDescription &td(TypeDescription::Create(TYPE_ID, "Map"));
+ td.SetConstructor(&Construct);
+ td.SetSize(sizeof(Map));
+
+ td.AddField("tileset", FieldDescription(((char *)&m.tileset) - ((char *)&m), Sprite::TYPE_ID).SetReferenced());
+ td.AddField("battlebg", FieldDescription(((char *)&m.battlebg) - ((char *)&m), Interpreter::IMAGE_ID).SetReferenced());
+ td.AddField("areas", FieldDescription(((char *)&m.areas) - ((char *)&m), Area::TYPE_ID).SetReferenced().SetAggregate());
+ td.AddField("triggers", FieldDescription(((char *)&m.triggers) - ((char *)&m), Trigger::TYPE_ID).SetReferenced().SetAggregate());
+ td.AddField("entities", FieldDescription(((char *)&m.entities) - ((char *)&m), Entity::TYPE_ID).SetReferenced().SetAggregate());
+ td.AddField("width", FieldDescription(((char *)&m.width) - ((char *)&m), Interpreter::NUMBER_ID));
+}
+
+void Map::Construct(void *data) {
+ new (data) Map;
+}
+
}