| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2015 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "mapbase.hpp" |
| 17 | |
| 18 | #include "tracktilebase.hpp" |
| 19 | |
| 20 | #include <QPoint> |
| 21 | #include <QPointF> |
| 22 | |
| 23 | MapBase::MapBase(size_t cols, size_t rows) |
| 24 | : m_cols(cols) |
| 25 | , m_rows(rows) |
| 26 | , m_map(rows, TrackTileRow(m_cols, nullptr)) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | size_t MapBase::cols() const |
| 31 | { |
| 32 | return m_cols; |
| 33 | } |
| 34 | |
| 35 | size_t MapBase::rows() const |
| 36 | { |
| 37 | return m_rows; |
| 38 | } |
| 39 | |
| 40 | void MapBase::resize(size_t newCols, size_t newRows) |
| 41 | { |
| 42 | for (size_t row = 0; row < m_map.size(); row++) |
| 43 | { |
| 44 | if (newCols > m_map[row].size()) |
| 45 | { |
| 46 | m_map[row].resize(newCols, nullptr); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (newRows > m_rows) |
| 51 | { |
| 52 | m_map.resize(newRows, TrackTileRow(newCols, nullptr)); |
| 53 | } |
| 54 | |
| 55 | m_cols = newCols; |
| 56 | m_rows = newRows; |
| 57 | } |
| 58 | |
| 59 | bool MapBase::setTile(size_t x, size_t y, TrackTileBasePtr tile) |
| 60 | { |
| 61 | if (x >= m_cols || y >= m_rows) |
| 62 | return false; |
| 63 | |
| 64 | m_map[y][x] = tile; |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | TrackTileBasePtr MapBase::getTile(size_t x, size_t y) const |
| 70 | { |
| 71 | if (x >= m_cols || y >= m_rows) |
| 72 | return nullptr; |
| 73 | |
| 74 | return m_map[y][x]; |
| 75 | } |
| 76 | |
| 77 | size_t MapBase::insertColumn(size_t at, MapBase::InsertDirection insertDirection) |
| 78 | { |
| 79 | at = at + (insertDirection == MapBase::InsertDirection::Before ? 0 : 1); |
| 80 | |
| 81 | for (auto & row : m_map) |
| 82 | { |
| 83 | row.insert(row.begin() + at, nullptr); |
| 84 | } |
| 85 | |
| 86 | m_cols++; |
| 87 | |
| 88 | return at; |
| 89 | } |
| 90 | |
| 91 | std::vector<TrackTileBasePtr> MapBase::deleteColumn(size_t at) |
| 92 | { |
| 93 | std::vector<TrackTileBasePtr> deleted; |
| 94 | |
| 95 | for (auto & row : m_map) |
| 96 | { |
| 97 | deleted.push_back(*(row.begin() + at)); |
| 98 | row.erase(row.begin() + at); |
| 99 | } |
| 100 | |
| 101 | m_cols--; |
| 102 | |
| 103 | return deleted; |
| 104 | } |
| 105 | |
| 106 | size_t MapBase::insertRow(size_t at, MapBase::InsertDirection insertDirection) |
| 107 | { |
| 108 | at = at + (insertDirection == MapBase::InsertDirection::Before ? 0 : 1); |
| 109 | |
| 110 | m_map.insert(m_map.begin() + at, TrackTileRow(m_cols, nullptr)); |
| 111 | |
| 112 | m_rows++; |
| 113 | |
| 114 | return at; |
| 115 | } |
| 116 | |
| 117 | std::vector<TrackTileBasePtr> MapBase::deleteRow(size_t at) |
| 118 | { |
| 119 | std::vector<TrackTileBasePtr> deleted = *(m_map.begin() + at); |
| 120 | m_map.erase(m_map.begin() + at); |
| 121 | |
| 122 | m_rows--; |
| 123 | |
| 124 | return deleted; |
| 125 | } |
| 126 | |
| 127 | MapBase::~MapBase() |
| 128 | { |
| 129 | } |