]> git.localhorst.tv Git - l2e.git/blobdiff - src/map/Map.cpp
removed lazy fwd headers
[l2e.git] / src / map / Map.cpp
index 450c00463ab7d3aa28ae90fcafd067f3620d833f..682f4f2a8a7f6a43a68cb300db0e7abf36cb2edd 100644 (file)
-/*
- * 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)
+, numTriggers(0)
+, entities(0)
+, numEntities(0)
 , width(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));
+               Vector<int> areaOffset(coords / areas[0].Size());
+               int areaIndex(areaOffset.Index(width));
+               if (areaIndex < numAreas) {
+                       return areas + areaIndex;
+               }
+       }
+       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) {
+               Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
+               return area->TileAt(tileOffset);
+       } else {
+               return 0;
+       }
+}
+
+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) {
+               if (i->TilePosition() == coords) {
+                       return 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();
+}
+
+
 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
        // TODO: skip invisible areas
        for (int i(0); i < numAreas; ++i) {
                const Area &area(areas[i]);
-               Vector<int> offset(
-                               inOffset.X() + (i % width) * area.Width() * tileset->Width(),
-                               inOffset.Y() + (i / width) * area.Height() * tileset->Height());
+               Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
                area.Render(dest, tileset, offset);
        }
 }
 
+void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
+       // TODO: skip invisible areas
+       for (int i(0); i < numAreas; ++i) {
+               const Area &area(areas[i]);
+               Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
+               area.RenderDebug(dest, tileset, offset);
+       }
+       for (int i(0); i < numTriggers; ++i) {
+               Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
+               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;
+}
+
 }