]> git.localhorst.tv Git - l2e.git/commitdiff
made TileAt/AreaAt fail silently
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 7 Oct 2012 13:00:20 +0000 (15:00 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 7 Oct 2012 13:00:20 +0000 (15:00 +0200)
this way the game does not halt when the player walks off the map

src/map/Area.cpp
src/map/Area.h
src/map/Map.cpp
src/map/Map.h
src/map/MapState.cpp

index 6b3f9b186373714aa384748e7a3ba10eb8f19b22..9c066d646f021a0b8d0d644bb6806fbb5f98622d 100644 (file)
@@ -25,12 +25,12 @@ Area::Area()
 }
 
 
-const Tile &Area::TileAt(const geometry::Vector<int> &offset) const {
+const Tile *Area::TileAt(const geometry::Vector<int> &offset) const {
        int tileIndex(offset.Y() * width + offset.X());
        if (tileIndex < numTiles) {
-               return tiles[tileIndex];
+               return tiles +tileIndex;
        } else {
-               throw std::out_of_range("tile index out of range");
+               return 0;
        }
 }
 
index 0a563390c6221053ef3b2274360b08e197b6880a..d9cc5d0d8784fc49d411a4ebb972a1913763af94 100644 (file)
@@ -26,7 +26,7 @@ public:
        int Width() const { return width; }
        int Height() const { return numTiles / width + (numTiles % width ? 1 : 0); }
        geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
-       const Tile &TileAt(const geometry::Vector<int> &) const;
+       const Tile *TileAt(const geometry::Vector<int> &) const;
 
        void Render(SDL_Surface *dest, const graphics::Sprite *tileset, const geometry::Vector<int> &offset) const;
        void RenderDebug(SDL_Surface *dest, const graphics::Sprite *tileset, const geometry::Vector<int> &offset) const;
index d7c0b32e49c77c6fa1aa08927a7cbe74ff317243..fc664c1d7d4e26c0060176bf0283002abfbbe5a6 100644 (file)
@@ -30,22 +30,26 @@ Map::Map()
 }
 
 
-const Area &Map::AreaAt(const Vector<int> &offset) const {
+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 areas + areaIndex;
                }
        }
-       throw std::out_of_range("area offset out of bounds");
+       return 0;
 }
 
-const Tile &Map::TileAt(const Vector<int> &offset) const {
-       const Area &area(AreaAt(offset));
-       Vector<int> tileOffset(TileCoordinates(offset) % area.Size());
-       return area.TileAt(tileOffset);
+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 geometry::Vector<int> &offset) {
index 8c2804d0b3641a5493aa4cbac83243cc6b97ae27..ab0728b765ba8ec363c20d27f3a7e0b9e8e70454 100644 (file)
@@ -24,8 +24,8 @@ public:
 
 public:
        const graphics::Sprite *Tileset() const { return tileset; }
-       const Area &AreaAt(const geometry::Vector<int> &) const;
-       const Tile &TileAt(const geometry::Vector<int> &) const;
+       const Area *AreaAt(const geometry::Vector<int> &) const;
+       const Tile *TileAt(const geometry::Vector<int> &) const;
        Trigger *TriggerAt(const geometry::Vector<int> &);
        geometry::Vector<int> TileCoordinates(const geometry::Vector<int> &) const;
 
index 7e6d7328d2756bb533527cf41efc25a9a2b6f703..791cc0bb319373ccceaaba6a7ae70b5d4fdda211 100644 (file)
@@ -133,11 +133,14 @@ void MapState::OnTileLock() {
 }
 
 bool MapState::CheckBlocking() const {
-       const Tile &tile(map->TileAt(controlled->Position()));
+       const Tile *tile(map->TileAt(controlled->Position()));
+       if (!tile) {
+               return false;
+       }
        Vector<int> nextPosition;
        switch (nextDirection) {
                case Entity::ORIENTATION_NORTH:
-                       if (tile.BlocksNorth()) {
+                       if (tile->BlocksNorth()) {
                                return true;
                        } else {
                                nextPosition = Vector<int>(
@@ -146,7 +149,7 @@ bool MapState::CheckBlocking() const {
                        }
                        break;
                case Entity::ORIENTATION_EAST:
-                       if (tile.BlocksEast()) {
+                       if (tile->BlocksEast()) {
                                return true;
                        } else {
                                nextPosition = Vector<int>(
@@ -155,7 +158,7 @@ bool MapState::CheckBlocking() const {
                        }
                        break;
                case Entity::ORIENTATION_SOUTH:
-                       if (tile.BlocksSouth()) {
+                       if (tile->BlocksSouth()) {
                                return true;
                        } else {
                                nextPosition = Vector<int>(
@@ -164,7 +167,7 @@ bool MapState::CheckBlocking() const {
                        }
                        break;
                case Entity::ORIENTATION_WEST:
-                       if (tile.BlocksWest()) {
+                       if (tile->BlocksWest()) {
                                return true;
                        } else {
                                nextPosition = Vector<int>(