| 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 "trackdata.hpp" |
| 17 | |
| 18 | #include "targetnode.hpp" |
| 19 | #include "tracktile.hpp" |
| 20 | |
| 21 | #include "object.hpp" |
| 22 | #include "objectfactory.hpp" |
| 23 | |
| 24 | #include <memory> |
| 25 | |
| 26 | TrackData::TrackData(QString name, bool isUserTrack, size_t cols, size_t rows) |
| 27 | : TrackDataBase(name, isUserTrack) |
| 28 | , m_map(cols, rows) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | TrackData::TrackData(const TrackData & other) |
| 33 | : TrackDataBase(other) |
| 34 | , m_fileName(other.m_fileName) |
| 35 | , m_map(other.m_map) |
| 36 | { |
| 37 | copyObjects(other); |
| 38 | |
| 39 | copyRoute(other); |
| 40 | } |
| 41 | |
| 42 | void TrackData::copyObjects(const TrackData & other) |
| 43 | { |
| 44 | m_objects.clear(); |
| 45 | |
| 46 | for (auto iter = other.m_objects.cbegin(); iter != other.m_objects.cend(); iter++) |
| 47 | { |
| 48 | const auto object = std::dynamic_pointer_cast<Object>(*iter); |
| 49 | const auto newObject = std::make_shared<Object>(*object); |
| 50 | |
| 51 | m_objects.add(ObjectBasePtr(newObject)); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void TrackData::copyRoute(const TrackData & other) |
| 56 | { |
| 57 | m_route.clear(); |
| 58 | |
| 59 | for (auto iter = other.m_route.cbegin(); iter != other.m_route.cend(); iter++) |
| 60 | { |
| 61 | const auto node = std::dynamic_pointer_cast<TargetNode>(*iter); |
| 62 | const auto newNode = std::make_shared<TargetNode>(*node); |
| 63 | |
| 64 | m_route.push(newNode); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | QString TrackData::fileName() const |
| 69 | { |
| 70 | return m_fileName; |
| 71 | } |
| 72 | |
| 73 | void TrackData::setFileName(QString newFileName) |
| 74 | { |
| 75 | m_fileName = newFileName; |
| 76 | } |
| 77 | |
| 78 | Route & TrackData::route() |
| 79 | { |
| 80 | return m_route; |
| 81 | } |
| 82 | |
| 83 | const Route & TrackData::route() const |
| 84 | { |
| 85 | return m_route; |
| 86 | } |
| 87 | |
| 88 | MapBase & TrackData::map() |
| 89 | { |
| 90 | return m_map; |
| 91 | } |
| 92 | |
| 93 | const MapBase & TrackData::map() const |
| 94 | { |
| 95 | return m_map; |
| 96 | } |
| 97 | |
| 98 | Objects & TrackData::objects() |
| 99 | { |
| 100 | return m_objects; |
| 101 | } |
| 102 | |
| 103 | const Objects & TrackData::objects() const |
| 104 | { |
| 105 | return m_objects; |
| 106 | } |
| 107 | |
| 108 | void TrackData::insertColumn(size_t at, MapBase::InsertDirection insertDirection) |
| 109 | { |
| 110 | at = m_map.insertColumn(at, insertDirection); |
| 111 | |
| 112 | moveObjectsAfterColumnInsertion(at); |
| 113 | |
| 114 | moveTargetNodesAfterColumnInsertion(at); |
| 115 | } |
| 116 | |
| 117 | void TrackData::insertRow(size_t at, MapBase::InsertDirection insertDirection) |
| 118 | { |
| 119 | at = m_map.insertRow(at, insertDirection); |
| 120 | |
| 121 | moveObjectsAfterRowInsertion(at); |
| 122 | |
| 123 | moveTargetNodesAfterRowInsertion(at); |
| 124 | } |
| 125 | |
| 126 | std::vector<TrackTileBasePtr> TrackData::deleteColumn(size_t at) |
| 127 | { |
| 128 | moveObjectsAfterColumnDeletion(at); |
| 129 | |
| 130 | moveTargetNodesAfterColumnDeletion(at); |
| 131 | |
| 132 | return m_map.deleteColumn(at); |
| 133 | } |
| 134 | |
| 135 | std::vector<TrackTileBasePtr> TrackData::deleteRow(size_t at) |
| 136 | { |
| 137 | moveObjectsAfterRowDeletion(at); |
| 138 | |
| 139 | moveTargetNodesAfterRowDeletion(at); |
| 140 | |
| 141 | return m_map.deleteRow(at); |
| 142 | } |
| 143 | |
| 144 | void TrackData::moveObjectsAfterColumnInsertion(size_t at) |
| 145 | { |
| 146 | for (auto && object : m_objects) |
| 147 | { |
| 148 | if (object->location().x() > TrackTile::width() * at) |
| 149 | { |
| 150 | object->setLocation(QPointF(object->location().x() + TrackTile::width(), object->location().y())); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void TrackData::moveObjectsAfterRowInsertion(size_t at) |
| 156 | { |
| 157 | for (auto && object : m_objects) |
| 158 | { |
| 159 | if (object->location().y() > TrackTile::height() * at) |
| 160 | { |
| 161 | object->setLocation(QPointF(object->location().x(), object->location().y() + TrackTile::height())); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void TrackData::moveTargetNodesAfterColumnInsertion(size_t at) |
| 167 | { |
| 168 | for (auto && targetNode : m_route) |
| 169 | { |
| 170 | if (targetNode->location().x() > TrackTile::width() * at) |
| 171 | { |
| 172 | targetNode->setLocation(QPointF(targetNode->location().x() + TrackTile::width(), targetNode->location().y())); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void TrackData::moveTargetNodesAfterRowInsertion(size_t at) |
| 178 | { |
| 179 | for (auto && targetNode : m_route) |
| 180 | { |
| 181 | if (targetNode->location().y() > TrackTile::height() * at) |
| 182 | { |
| 183 | targetNode->setLocation(QPointF(targetNode->location().x(), targetNode->location().y() + TrackTile::height())); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void TrackData::moveObjectsAfterColumnDeletion(size_t at) |
| 189 | { |
| 190 | for (auto && object : m_objects) |
| 191 | { |
| 192 | if (object->location().x() > TrackTile::width() * at) |
| 193 | { |
| 194 | object->setLocation(QPointF(object->location().x() - TrackTile::width(), object->location().y())); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void TrackData::moveObjectsAfterRowDeletion(size_t at) |
| 200 | { |
| 201 | for (auto && object : m_objects) |
| 202 | { |
| 203 | if (object->location().y() > TrackTile::height() * at) |
| 204 | { |
| 205 | object->setLocation(QPointF(object->location().x(), object->location().y() - TrackTile::height())); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void TrackData::moveTargetNodesAfterColumnDeletion(size_t at) |
| 211 | { |
| 212 | for (auto && targetNode : m_route) |
| 213 | { |
| 214 | if (targetNode->location().x() > TrackTile::width() * at) |
| 215 | { |
| 216 | targetNode->setLocation(QPointF(targetNode->location().x() - TrackTile::width(), targetNode->location().y())); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void TrackData::moveTargetNodesAfterRowDeletion(size_t at) |
| 222 | { |
| 223 | for (auto && targetNode : m_route) |
| 224 | { |
| 225 | if (targetNode->location().y() > TrackTile::height() * at) |
| 226 | { |
| 227 | targetNode->setLocation(QPointF(targetNode->location().x(), targetNode->location().y() - TrackTile::height())); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | TrackData::~TrackData() |
| 233 | { |
| 234 | } |