| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2017 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 "minimap.hpp" |
| 17 | |
| 18 | #include "game.hpp" |
| 19 | #include "graphicsfactory.hpp" |
| 20 | #include "race.hpp" |
| 21 | #include "renderer.hpp" |
| 22 | #include "scene.hpp" |
| 23 | #include "tracktile.hpp" |
| 24 | |
| 25 | #include <MCSurface> |
| 26 | #include <MCSurfaceView> |
| 27 | |
| 28 | #include "../common/mapbase.hpp" |
| 29 | |
| 30 | #include <memory> |
| 31 | |
| 32 | Minimap::Minimap() |
| 33 | : m_markerSurface(GraphicsFactory::generateMinimapMarker()) |
| 34 | { |
| 35 | m_markerSurface->setShaderProgram(Renderer::instance().program("menu")); |
| 36 | m_markerSurface->material()->setAlphaBlend(true); |
| 37 | } |
| 38 | |
| 39 | Minimap::Minimap(Car & carToFollow, const MapBase & trackMap, int x, int y, size_t size) |
| 40 | { |
| 41 | initialize(carToFollow, trackMap, x, y, size); |
| 42 | } |
| 43 | |
| 44 | void Minimap::initialize(Car & carToFollow, const MapBase & trackMap, int x, int y, size_t size) |
| 45 | { |
| 46 | m_carToFollow = &carToFollow; |
| 47 | |
| 48 | m_center = MCVector3dF(x, y); |
| 49 | |
| 50 | // Set m_tileW and m_tileH so that they are squares |
| 51 | m_tileW = size / trackMap.cols(); |
| 52 | m_tileH = size / trackMap.rows(); |
| 53 | |
| 54 | if (m_tileW > m_tileH) |
| 55 | { |
| 56 | m_tileW = m_tileH; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | m_tileH = m_tileW; |
| 61 | } |
| 62 | |
| 63 | float initX, initY; |
| 64 | |
| 65 | // Center the map |
| 66 | if (trackMap.cols() % 2 == 0) |
| 67 | { |
| 68 | initX = x - trackMap.cols() * m_tileW / 2 + m_tileW / 4; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | initX = x - trackMap.cols() * m_tileW / 2; |
| 73 | } |
| 74 | |
| 75 | initY = y - trackMap.rows() * m_tileH / 2; |
| 76 | |
| 77 | m_map.clear(); |
| 78 | |
| 79 | // Loop through the visible tile matrix and store relevant tiles |
| 80 | float tileX, tileY; |
| 81 | tileY = initY; |
| 82 | for (size_t j = 0; j < trackMap.rows(); j++) |
| 83 | { |
| 84 | tileX = initX; |
| 85 | for (size_t i = 0; i < trackMap.cols(); i++) |
| 86 | { |
| 87 | const auto tile = std::static_pointer_cast<TrackTile>(trackMap.getTile(i, j)); |
| 88 | const auto surface = tile->previewSurface(); |
| 89 | if (surface && !tile->excludeFromMinimap()) |
| 90 | { |
| 91 | surface->setShaderProgram(Renderer::instance().program("menu")); |
| 92 | surface->setColor({ 1.0, 1.0, 1.0 }); |
| 93 | surface->setSize(m_tileH, m_tileW); |
| 94 | |
| 95 | MinimapTile minimapTile; |
| 96 | minimapTile.pos = MCVector3dF(tileX + m_tileW / 2, tileY + m_tileH / 2); |
| 97 | minimapTile.rotation = tile->rotation(); |
| 98 | |
| 99 | m_map[surface].push_back(minimapTile); |
| 100 | } |
| 101 | |
| 102 | tileX += m_tileW; |
| 103 | } |
| 104 | |
| 105 | tileY += m_tileH; |
| 106 | } |
| 107 | |
| 108 | m_trackWidth = trackMap.cols() * TrackTile::width(); |
| 109 | m_trackHeight = trackMap.rows() * TrackTile::height(); |
| 110 | |
| 111 | m_size = MCVector3dF(trackMap.cols() * m_tileW, trackMap.rows() * m_tileH); |
| 112 | } |
| 113 | |
| 114 | void Minimap::renderMap() |
| 115 | { |
| 116 | for (auto && i : m_map) |
| 117 | { |
| 118 | const auto surface = i.first; |
| 119 | surface->bind(); |
| 120 | surface->setColor({ 1.0, 1.0, 1.0 }); |
| 121 | surface->setSize(m_tileH, m_tileW); |
| 122 | |
| 123 | for (auto && j : i.second) |
| 124 | { |
| 125 | const MinimapTile minimapTile = j; |
| 126 | surface->render(nullptr, minimapTile.pos, minimapTile.rotation); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void Minimap::renderMarkers(const Minimap::CarVector & cars, const Race & race) |
| 132 | { |
| 133 | const auto size = std::max(Scene::width() * 0.01f, m_tileH * 0.75f); |
| 134 | m_markerSurface->setSize(size, size); |
| 135 | |
| 136 | auto && leader = race.getLeader(); |
| 137 | auto && loser = race.getLoser(); |
| 138 | |
| 139 | std::list<Car *> renderOrder; |
| 140 | for (auto && car : cars) |
| 141 | { |
| 142 | if (car.get() == m_carToFollow) |
| 143 | { |
| 144 | renderOrder.push_back(car.get()); |
| 145 | } |
| 146 | else if (car.get() == &leader) |
| 147 | { |
| 148 | renderOrder.push_back(car.get()); |
| 149 | } |
| 150 | else if (car.get() == &loser) |
| 151 | { |
| 152 | renderOrder.push_back(car.get()); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | renderOrder.push_front(car.get()); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | for (auto && car : renderOrder) |
| 161 | { |
| 162 | if (car == m_carToFollow) |
| 163 | { |
| 164 | auto color = std::dynamic_pointer_cast<MCSurfaceView>(car->shape()->view())->surface()->averageColor(); |
| 165 | color.setA(0.9f); |
| 166 | m_markerSurface->setColor(color); |
| 167 | } |
| 168 | else if (car == &leader) |
| 169 | { |
| 170 | const auto green = MCGLColor(0.1f, 0.9f, 0.1f, 0.9f); |
| 171 | m_markerSurface->setColor(green); |
| 172 | } |
| 173 | else if (car == &loser) |
| 174 | { |
| 175 | const auto red = MCGLColor(0.9f, 0.1f, 0.1f, 0.9f); |
| 176 | m_markerSurface->setColor(red); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | const auto gray = MCGLColor(0.2f, 0.2f, 0.2f, 0.9f); |
| 181 | m_markerSurface->setColor(gray); |
| 182 | } |
| 183 | |
| 184 | m_markerSurface->render(nullptr, m_center + car->location() * m_size.i() / m_trackWidth - m_size * 0.5f, 0); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void Minimap::render(const Minimap::CarVector & cars, const Race & race) |
| 189 | { |
| 190 | renderMap(); |
| 191 | |
| 192 | renderMarkers(cars, race); |
| 193 | } |