Coverage report for gui


src/
File: src/render/scenes/world/ui/WorldUi.cpp
Date: 2024-06-25 10:57:02
Lines:
0/170
0.0%
Functions:
0/10
0.0%
Branches:
0/384
0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** WorldUi
6 */
7
8 #include "WorldUi.hpp"
9 #include "../World.hpp"
10
11 static const std::vector<std::string> orientation = {
12 "NORTH",
13 "EAST",
14 "SOUTH",
15 "WEST"
16 };
17
18 void WorldUi::init()
19 {
20 }
21
22 bool WorldUi::update(sf::Event event, sf::RenderWindow &window)
23 {
24 int index = 0;
25
26 for (auto &button : _layer1) {
27 if (button->update(event, window)) {
28 _panelState = (panelState)index;
29 return true;
30 }
31 index++;
32 }
33 if (_panelState != NONE) {
34 if (_layer2["closeButton"]->update(event, window)) {
35 _panelState = NONE;
36 return true;
37 }
38 }
39 if (_panelState == TRANTORIAN) {
40 if (_layer2["prevButton"]->update(event, window)) {
41 if (_world->getNbTrantorian() == 0)
42 return false;
43 _idPlayer--;
44 if (_idPlayer < 0)
45 _idPlayer = _world->getNbTrantorian() - 1;
46 }
47 if (_layer2["nextButton"]->update(event, window)) {
48 if (_world->getNbTrantorian() == 0)
49 return false;
50 _idPlayer = (_idPlayer + 1) % _world->getNbTrantorian();
51 }
52 if (_world->getNbTrantorian() != 0)
53 if (_layer2["lookButton"]->update(event, window)) {
54 _world->lookTrantorian(_idPlayer);
55 }
56 }
57 if (_panelState == FLAG) {
58 if (_layer2["prevButton"]->update(event, window)) {
59 _idTeam--;
60 if (_idTeam < 0)
61 _idTeam = _world->_teams.size() - 1;
62 }
63
64 if (_layer2["nextButton"]->update(event, window)) {
65 _idTeam = (_idTeam + 1) % _world->_teams.size();
66 }
67
68 }
69 return false;
70 }
71
72 void WorldUi::update(float /*fElapsedTime*/)
73 {
74 }
75
76 void WorldUi::draw(sf::RenderWindow &window)
77 {
78 for (auto &button : _layer1)
79 button->draw(window);
80 if (_panelState != NONE)
81 _layer2["closeButton"]->draw(window);
82 if (_panelState == TRANTORIAN)
83 drawPlayerInventory(window);
84 else if (_world->_selectedTile.x != -1 && _world->_selectedTile.y != -1)
85 drawTileInventory(window);
86 if (_panelState == TROPHY)
87 drawRanking(window);
88 if (_panelState == TRANTORIAN)
89 drawTrantorian(window);
90 if (_panelState == FLAG)
91 drawTeam(window);
92 }
93
94 void WorldUi::drawInventoryTile(int nb, sf::RenderWindow &window, std::string name)
95 {
96
97 _inventoryText.setPosition(sf::Vector2f(
98 _sprites["inventory"]->getPosition().x - 64 * 4 + 17 + 61 * nb,
99 _sprites["inventory"]->getPosition().y - 5
100 ));
101 _inventoryText.setString(name);
102 window.draw(_inventoryText);
103 }
104
105 void WorldUi::drawRanking(sf::RenderWindow &window)
106 {
107 _sprites["trophyPanel"]->draw(window);
108 int index = 0;
109 for (auto rank : _world->_rankings) {
110 _inventoryText.setPosition(sf::Vector2f(
111 _sprites["trophyPanel"]->getPosition().x + 96,
112 _sprites["trophyPanel"]->getPosition().y + 32 + 40 * index
113 ));
114 _inventoryText.setString(rank._teamName + " : " + std::to_string(rank._score));
115 window.draw(_inventoryText);
116 index++;
117 }
118 }
119
120 void WorldUi::drawTeam(sf::RenderWindow &window)
121 {
122 _sprites["flagPanel"]->draw(window);
123 _layer2["prevButton"]->draw(window);
124 _layer2["nextButton"]->draw(window);
125
126 Ranking rank;
127 for (auto team: _world->_rankings) {
128 if (team._teamName == _world->_teams[_idTeam]) {
129 rank = team;
130 break;
131 }
132 }
133
134 _inventoryText.setPosition(sf::Vector2f(
135 _sprites["trophyPanel"]->getPosition().x + 96,
136 _sprites["trophyPanel"]->getPosition().y + 40
137 ));
138 _inventoryText.setString("team " + rank._teamName);
139 window.draw(_inventoryText);
140
141 _inventoryText.setPosition(sf::Vector2f(
142 _sprites["trophyPanel"]->getPosition().x + 96,
143 _sprites["trophyPanel"]->getPosition().y + 40 + 25
144 ));
145 _inventoryText.setString(std::to_string(rank._score) + " points");
146 window.draw(_inventoryText);
147
148 _inventoryText.setPosition(sf::Vector2f(
149 _sprites["trophyPanel"]->getPosition().x + 96,
150 _sprites["trophyPanel"]->getPosition().y + 40 + 50
151 ));
152 _inventoryText.setString(std::to_string(rank._nbPlayer) + " trantorians");
153 window.draw(_inventoryText);
154
155 }
156
157 void WorldUi::drawTrantorian(sf::RenderWindow &window)
158 {
159 _sprites["trantorianPanel"]->draw(window);
160 _layer2["prevButton"]->draw(window);
161 _layer2["nextButton"]->draw(window);
162 _sprites["bousolle"]->setPosition(sf::Vector2f(
163 window.getSize().x - 256 / 4,
164 256 / 4
165 ));
166 _sprites["bousolle"]->draw(window);
167 if (_world->getNbTrantorian() == 0)
168 return;
169 _layer2["lookButton"]->draw(window);
170 auto trantorian = _world->getTrantorian(_idPlayer);
171 _inventoryText.setPosition(sf::Vector2f(
172 _sprites["trantorianPanel"]->getPosition().x + + 96,
173 _sprites["trantorianPanel"]->getPosition().y + 32
174 ));
175 if (trantorian.isDead()) {
176 _inventoryText.setString("Dead");
177 window.draw(_inventoryText);
178 return;
179 }
180 _inventoryText.setString("Trantorian #" + std::to_string(trantorian._id));
181 window.draw(_inventoryText);
182 _inventoryText.setPosition(sf::Vector2f(
183 _sprites["trantorianPanel"]->getPosition().x + + 96,
184 _sprites["trantorianPanel"]->getPosition().y + 32 + 25
185 ));
186 _inventoryText.setString("Level " + std::to_string(trantorian._level));
187 window.draw(_inventoryText);
188 _inventoryText.setPosition(sf::Vector2f(
189 _sprites["trantorianPanel"]->getPosition().x + + 96,
190 _sprites["trantorianPanel"]->getPosition().y + 32 + 50
191 ));
192 int facing = trantorian._facing - 1;
193 if (facing < 0)
194 facing = 3;
195 if (facing > 3)
196 facing = 0;
197 _inventoryText.setString("Facing " + orientation[facing]);
198 window.draw(_inventoryText);
199 }
200
201 void WorldUi::drawTileInventory(sf::RenderWindow &window)
202 {
203 _sprites["inventory"]->setPosition(sf::Vector2f(
204 window.getSize().x / 2,
205 window.getSize().y - 32
206 ));
207 _sprites["inventory"]->draw(window);
208 drawInventoryTile(0, window, std::to_string(_world->_chuncks[_world->_selectedTile.x][_world->_selectedTile.y]._inventory[1]));
209 drawInventoryTile(1, window, std::to_string(_world->_chuncks[_world->_selectedTile.x][_world->_selectedTile.y]._inventory[2]));
210 drawInventoryTile(2, window, std::to_string(_world->_chuncks[_world->_selectedTile.x][_world->_selectedTile.y]._inventory[3]));
211 drawInventoryTile(3, window, std::to_string(_world->_chuncks[_world->_selectedTile.x][_world->_selectedTile.y]._inventory[4]));
212 drawInventoryTile(4, window, std::to_string(_world->_chuncks[_world->_selectedTile.x][_world->_selectedTile.y]._inventory[5]));
213 drawInventoryTile(5, window, std::to_string(_world->_chuncks[_world->_selectedTile.x][_world->_selectedTile.y]._inventory[6]));
214 _sprites["food"]->setPosition(sf::Vector2f(
215 _sprites["inventory"]->getPosition().x + 61 * 2,
216 _sprites["inventory"]->getPosition().y
217 ));
218 _sprites["food"]->draw(window);
219 _sprites["trantorian"]->setPosition(sf::Vector2f(
220 _sprites["inventory"]->getPosition().x + 61 * 3,
221 _sprites["inventory"]->getPosition().y + 10
222 ));
223 _sprites["trantorian"]->draw(window);
224 drawInventoryTile(6, window, std::to_string(_world->_chuncks[_world->_selectedTile.x][_world->_selectedTile.y]._inventory[0]));
225 drawInventoryTile(7, window, std::to_string(_world->_chuncks[_world->_selectedTile.x][_world->_selectedTile.y]._nbTrantorians));
226 _inventoryText.setString("Chunck inventory");
227 _inventoryText.setPosition(sf::Vector2f(
228 _sprites["inventory"]->getPosition().x,
229 _sprites["inventory"]->getPosition().y - 64
230 ));
231 window.draw(_inventoryText);
232 }
233
234 void WorldUi::drawPlayerInventory(sf::RenderWindow &window)
235 {
236 if (_world->getNbTrantorian() == 0)
237 return;
238 _sprites["inventory"]->setPosition(sf::Vector2f(
239 window.getSize().x / 2,
240 window.getSize().y - 32
241 ));
242 _sprites["inventory"]->draw(window);
243 drawInventoryTile(0, window, std::to_string(_world->getTrantorian(_idPlayer)._inventory[1]));
244 drawInventoryTile(1, window, std::to_string(_world->getTrantorian(_idPlayer)._inventory[2]));
245 drawInventoryTile(2, window, std::to_string(_world->getTrantorian(_idPlayer)._inventory[3]));
246 drawInventoryTile(3, window, std::to_string(_world->getTrantorian(_idPlayer)._inventory[4]));
247 drawInventoryTile(4, window, std::to_string(_world->getTrantorian(_idPlayer)._inventory[5]));
248 drawInventoryTile(5, window, std::to_string(_world->getTrantorian(_idPlayer)._inventory[6]));
249 _sprites["food"]->setPosition(sf::Vector2f(
250 _sprites["inventory"]->getPosition().x + 61 * 2,
251 _sprites["inventory"]->getPosition().y
252 ));
253 _sprites["food"]->draw(window);
254 drawInventoryTile(6, window, std::to_string(_world->getTrantorian(_idPlayer)._inventory[0]));
255 _inventoryText.setString("Trantorian inventory");
256 _inventoryText.setPosition(sf::Vector2f(
257 _sprites["inventory"]->getPosition().x,
258 _sprites["inventory"]->getPosition().y - 64
259 ));
260 window.draw(_inventoryText);
261 }
262