4 * Created on: Sep 29, 2012
12 #include "../graphics/Sprite.h"
16 using geometry::Vector;
31 const Area &Map::AreaAt(const Vector<int> &offset) const {
33 Vector<int> coords(TileCoordinates(offset));
34 Vector<int> areaOffset(coords.X() / areas[0].Width(), coords.Y() / areas[0].Height());
35 int areaIndex(areaOffset.Y() * width + areaOffset.X());
36 if (areaIndex < numAreas) {
37 return areas[areaIndex];
40 throw std::out_of_range("area offset out of bounds");
43 const Tile &Map::TileAt(const Vector<int> &offset) const {
44 const Area &area(AreaAt(offset));
45 Vector<int> tileOffset((offset.X() / tileset->Width()) % area.Width(), (offset.Y() / tileset->Height()) % area.Height());
46 return area.TileAt(tileOffset);
49 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
50 // TODO: add support for multiple triggers on a tile?
51 Vector<int> coords(TileCoordinates(offset));
52 for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
53 if (i->TilePosition() == coords) {
60 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
61 return Vector<int>(position.X() / tileset->Width(), position.Y() / tileset->Height());
65 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
66 // TODO: skip invisible areas
67 for (int i(0); i < numAreas; ++i) {
68 const Area &area(areas[i]);
70 inOffset.X() + (i % width) * area.Width() * tileset->Width(),
71 inOffset.Y() + (i / width) * area.Height() * tileset->Height());
72 area.Render(dest, tileset, offset);