| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "World.hpp" | ||
| 2 | #include "../../core/Core.hpp" | ||
| 3 | #include "../../../utils/Random.hpp" | ||
| 4 | |||
| 5 | ✗ | bool World::update(sf::Event event, [[maybe_unused]] sf::RenderWindow &window) | |
| 6 | { | ||
| 7 | ✗ | _worldUi.update(event, window); | |
| 8 | ✗ | _mousePos = _core->getMousePos() * _zoom + _view.getCenter() - _view.getSize() * (1.f/2.f); | |
| 9 | |||
| 10 | ✗ | if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) | |
| 11 | ✗ | _core->_upperState = GameState::MENU; | |
| 12 | ✗ | if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) { | |
| 13 | ✗ | _isDragging = false; | |
| 14 | ✗ | _offset = sf::Vector2f(0, 0); | |
| 15 | ✗ | _tmpOffset = sf::Vector2f(0, 0); | |
| 16 | ✗ | _selectedTile = sf::Vector2f(-1, -1); | |
| 17 | } | ||
| 18 | ✗ | if (event.type == sf::Event::MouseMoved || | |
| 19 | event.type == sf::Event::MouseButtonPressed) { | ||
| 20 | ✗ | for (int i = 0; i < _worldSize.x; i++) { | |
| 21 | ✗ | for (int j = 0; j < _worldSize.y; j++) { | |
| 22 | ✗ | _diamond.setPosition(sf::Vector2f( | |
| 23 | _chuncks[i][j]._pos.x, | ||
| 24 | ✗ | _chuncks[i][j]._pos.y + _chuncks[i][j]._yOffset | |
| 25 | )); | ||
| 26 | ✗ | if (_diamond.checkCollision(_mousePos)) { | |
| 27 | ✗ | _hoveredTile = sf::Vector2f(i, j); | |
| 28 | ✗ | if (event.mouseButton.button == sf::Mouse::Button::Left) { | |
| 29 | ✗ | _selectedTile = sf::Vector2f(i, j); | |
| 30 | ✗ | _sounds["interact"].stop(); | |
| 31 | ✗ | _sounds["interact"].play(); | |
| 32 | } | ||
| 33 | break; | ||
| 34 | } | ||
| 35 | |||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | ✗ | moveMap(event); | |
| 40 | ✗ | return true; | |
| 41 | } | ||
| 42 | |||
| 43 | ✗ | void World::update(float fElapsedTime) | |
| 44 | { | ||
| 45 | int i = 0; | ||
| 46 | ✗ | for (auto &bubble : _bubbles) { | |
| 47 | bubble.update(fElapsedTime); | ||
| 48 | if (bubble.isFinished()) | ||
| 49 | ✗ | _bubbles.erase(_bubbles.begin() + i); | |
| 50 | ✗ | i++; | |
| 51 | } | ||
| 52 | ✗ | if (_core->_server.connectionState == ServerConnect::ConnectionState::SERVERDOWN | |
| 53 | ✗ | || _core->_server.connectionState == ServerConnect::ConnectionState::NOTCONNECTED) { | |
| 54 | ✗ | _core->backToHome(); | |
| 55 | ✗ | return; | |
| 56 | } | ||
| 57 | ✗ | if (_worldUi.getPanelState() != WorldUi::panelState::NONE) | |
| 58 | ✗ | _sprites["aura"]->update(_core->getDeltaTime()); | |
| 59 | ✗ | updateTrantorians(); | |
| 60 | ✗ | updateChuncks(); | |
| 61 | ✗ | updateIncantation(); | |
| 62 | ✗ | _rankTime += _core->getDeltaTime(); | |
| 63 | ✗ | if (_rankTime > 10) | |
| 64 | ✗ | Ranking::getRanking(_rankings, _core->_data); | |
| 65 | ✗ | if (_core->_funMode) | |
| 66 | ✗ | for (auto &starling : _starlings) | |
| 67 | ✗ | starling.update(fElapsedTime); | |
| 68 | } | ||
| 69 | |||
| 70 | ✗ | bool World::moveMap(sf::Event event) | |
| 71 | { | ||
| 72 | ✗ | if (event.type == sf::Event::MouseWheelScrolled) { | |
| 73 | ✗ | if (event.mouseWheelScroll.delta > 0) | |
| 74 | ✗ | _zoom -= 65 * _core->getDeltaTime(); | |
| 75 | else | ||
| 76 | ✗ | _zoom += 65 * _core->getDeltaTime(); | |
| 77 | ✗ | if (_zoom < 0.25f) | |
| 78 | ✗ | _zoom = 0.25f; | |
| 79 | ✗ | if (_zoom > 2) | |
| 80 | ✗ | _zoom = 2; | |
| 81 | ✗ | _view.setSize(sf::Vector2f(1920 * _zoom, 1080 * _zoom)); | |
| 82 | } | ||
| 83 | ✗ | if (event.type == sf::Event::MouseButtonPressed && | |
| 84 | event.mouseButton.button == sf::Mouse::Button::Left) { | ||
| 85 | ✗ | _isDragging = true; | |
| 86 | ✗ | _dragStart = _core->getMousePos(); | |
| 87 | } | ||
| 88 | ✗ | if (event.type == sf::Event::MouseButtonReleased && | |
| 89 | event.mouseButton.button == sf::Mouse::Button::Left) { | ||
| 90 | ✗ | _isDragging = false; | |
| 91 | _offset += _tmpOffset; | ||
| 92 | ✗ | _tmpOffset = sf::Vector2f(0, 0); | |
| 93 | } | ||
| 94 | ✗ | if (_isDragging) { | |
| 95 | ✗ | if (_core->getMousePos().x < 0 || _core->getMousePos().x > 1920 || | |
| 96 | ✗ | _core->getMousePos().y < 0 || _core->getMousePos().y > 1080) { | |
| 97 | ✗ | _isDragging = false; | |
| 98 | _offset += _tmpOffset; | ||
| 99 | ✗ | _tmpOffset = sf::Vector2f(0, 0); | |
| 100 | ✗ | return true; | |
| 101 | } | ||
| 102 | ✗ | _tmpOffset = (- _core->getMousePos() + _dragStart) * _zoom; | |
| 103 | } | ||
| 104 | return true; | ||
| 105 | } | ||
| 106 | |||
| 107 | ✗ | void World::updateTrantorians() | |
| 108 | { | ||
| 109 | std::map<int, std::shared_ptr<Player>> players; | ||
| 110 | bool exisitingPlayers = false; | ||
| 111 | |||
| 112 | ✗ | players = _core->_data.getPlayers(); | |
| 113 | ✗ | for (auto &player : players) { | |
| 114 | exisitingPlayers = false; | ||
| 115 | ✗ | auto tile = sf::Vector2f(player.second->getPosition()[0], player.second->getPosition()[1]); | |
| 116 | ✗ | for (auto &trantorian : _trantorians) { | |
| 117 | ✗ | if (trantorian._id == player.first) { | |
| 118 | exisitingPlayers = true; | ||
| 119 | ✗ | trantorian.setTile(tile, _chuncks[tile.x][tile.y].getMiddle()); | |
| 120 | ✗ | trantorian._level = player.second->getLvl(); | |
| 121 | ✗ | trantorian._facing = player.second->getOrientation(); | |
| 122 | ✗ | trantorian._inventory = player.second->getInventory(); | |
| 123 | ✗ | if (player.second->getAlive() == false) | |
| 124 | trantorian.kill(); | ||
| 125 | break; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | if (!exisitingPlayers) { | ||
| 129 | ✗ | std::vector<Sprite> trantorianSprites; | |
| 130 | |||
| 131 | ✗ | Trantorian trantorian(tile, _chuncks[tile.x][tile.y].getMiddle()); | |
| 132 | ✗ | trantorian._id = player.first; | |
| 133 | ✗ | trantorian._team = player.second->getTeam(); | |
| 134 | int teamIndex = 0; | ||
| 135 | ✗ | for (auto &team : _teams) { | |
| 136 | ✗ | if (team == trantorian._team) | |
| 137 | break; | ||
| 138 | ✗ | teamIndex++; | |
| 139 | } | ||
| 140 | ✗ | trantorian._teamIndex = teamIndex; | |
| 141 | ✗ | _trantorians.push_back(trantorian); | |
| 142 | ✗ | } | |
| 143 | } | ||
| 144 | ✗ | for (auto &trantorian : _trantorians) | |
| 145 | ✗ | trantorian.update(_core->getDeltaTime()); | |
| 146 | ✗ | std::optional<Broadcast> broadcast = _core->_data.getNextBroadcast(); | |
| 147 | ✗ | if (broadcast.has_value()) { | |
| 148 | ✗ | if (_bubbles.size() > 6) | |
| 149 | ✗ | return; | |
| 150 | ✗ | sf::Color color = sf::Color::White; | |
| 151 | ✗ | for (auto &trantorian : _trantorians) { | |
| 152 | ✗ | if (trantorian._id == broadcast.value().getPlayerNb()) { | |
| 153 | ✗ | color = _teamsColor[trantorian._teamIndex > 5 ? 5 : trantorian._teamIndex]; | |
| 154 | ✗ | break; | |
| 155 | } | ||
| 156 | } | ||
| 157 | ✗ | int random = rand() % 2; | |
| 158 | ✗ | _sounds["talk1"].stop(); | |
| 159 | ✗ | _sounds["talk2"].stop(); | |
| 160 | ✗ | if (random == 0) | |
| 161 | ✗ | _sounds["talk1"].play(); | |
| 162 | else | ||
| 163 | ✗ | _sounds["talk2"].play(); | |
| 164 | ✗ | _chat->addMessage(std::to_string(broadcast.value().getPlayerNb()) + " : " + broadcast.value().getMessage(), color); | |
| 165 | ✗ | Bubble bubble = Bubble(broadcast.value().getMessage(), sf::Vector2f(broadcast.value().getPosition()[0], broadcast.value().getPosition()[1])); | |
| 166 | ✗ | _bubbles.push_back(bubble); | |
| 167 | } | ||
| 168 | |||
| 169 | } | ||
| 170 | |||
| 171 | ✗ | void World::updateChuncks() | |
| 172 | { | ||
| 173 | ✗ | auto &map = _core->_data.getMap(); | |
| 174 | ✗ | for (int i = 0; i < _worldSize.x; i++) { | |
| 175 | ✗ | for (int j = 0; j < _worldSize.y; j++) { | |
| 176 | ✗ | _chuncks[i][j]._inventory = map.getTileAt(i, j).getRessources(); | |
| 177 | ✗ | _chuncks[i][j]._nbTrantorians = 0; | |
| 178 | ✗ | for (auto &trantorian : _trantorians) | |
| 179 | ✗ | if (trantorian.getTile().x == i && trantorian.getTile().y == j) | |
| 180 | ✗ | _chuncks[i][j]._nbTrantorians++; | |
| 181 | } | ||
| 182 | } | ||
| 183 | ✗ | } | |
| 184 | |||
| 185 | ✗ | void World::updateIncantation() | |
| 186 | { | ||
| 187 | ✗ | auto incantations = _core->_data.getIncantations(); | |
| 188 | |||
| 189 | ✗ | if ((int)incantations.size() != _nbIncantations) { | |
| 190 | ✗ | _chat->addMessage("Incantation started"); | |
| 191 | ✗ | _sounds["wololo"].stop(); | |
| 192 | ✗ | _sounds["wololo"].play(); | |
| 193 | ✗ | auto incantation = incantations[incantations.size() - 1]; | |
| 194 | ✗ | sf::Vector2f tile = sf::Vector2f(incantation->getPosition()[0], incantation->getPosition()[1]); | |
| 195 | ✗ | sf::Vector2f pos = _chuncks[tile.x][tile.y].getMiddle(); | |
| 196 | ✗ | _lvlUpAnims.push_back(LvlUpAnim(incantations.size() - 1, | |
| 197 | pos, tile, | ||
| 198 | incantation->getLvl())); | ||
| 199 | ✗ | _nbIncantations = incantations.size(); | |
| 200 | } | ||
| 201 | ✗ | for (auto &lvlUpAnim : _lvlUpAnims) { | |
| 202 | ✗ | int state = incantations[lvlUpAnim.getId()]->getStatus(); | |
| 203 | ✗ | if (state == IncantationOutcome::SUCCESS) | |
| 204 | if (lvlUpAnim.setSuccess()) { | ||
| 205 | ✗ | _chat->addMessage("Incantation success"); | |
| 206 | ✗ | _sounds["hourray"].stop(); | |
| 207 | ✗ | _sounds["hourray"].play(); | |
| 208 | ✗ | _buildings[lvlUpAnim.getTile().x][lvlUpAnim.getTile().y] = lvlUpAnim.getLvl(); | |
| 209 | } | ||
| 210 | ✗ | if (state == IncantationOutcome::FAILURE) | |
| 211 | lvlUpAnim.setFailure(); | ||
| 212 | ✗ | lvlUpAnim.update(_core->getDeltaTime()); | |
| 213 | ✗ | if (lvlUpAnim.isFinished()) | |
| 214 | ✗ | _lvlUpAnims.erase(_lvlUpAnims.begin()); | |
| 215 | } | ||
| 216 | ✗ | } | |
| 217 |