Coverage report for gui


src/
File: src/render/scenes/world/World.cpp
Date: 2024-06-25 10:57:02
Lines:
0/119
0.0%
Functions:
0/7
0.0%
Branches:
0/342
0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** world
6 */
7
8
9 #include "World.hpp"
10 #include "../../core/Core.hpp"
11 #include "../../../utils/Random.hpp"
12
13 World::World(Core *core)
14 : _core(core), _worldUi(WorldUi(this))
15 {
16 _sprite = std::make_shared<Sprite>("./assets/grass.png");
17 _diamond = Diamond(sf::Vector2f(TILE_SIZE_X, TILE_SIZE_Y));
18 _sprites["halo1"] = std::make_shared<Sprite>("./assets/halo1.png");
19 _sprites["hover1"] = std::make_shared<Sprite>("./assets/hover1.png");
20 _sprites["aura"] = std::make_shared<Sprite>("./assets/aura.png", 6, 0.1f);
21 _sprites["hoverEgg"] = std::make_shared<Sprite>("./assets/hoverEgg.png");
22 _sprites["trantorian_run"] = std::make_shared<Sprite>("./assets/trantorian_run.png", 6, 0.1f);
23 _sprites["trantorian_spawn"] = std::make_shared<Sprite>("./assets/trantorian_spawn.png", 12, 0.1f);
24 _sprites["trantorian_death"] = std::make_shared<Sprite>("./assets/trantorian_death.png", 13, 0.1f);
25 _sprites["grass"] = std::make_shared<Sprite>("./assets/grass_detail(1).png");
26 _sprites["grass2"] = std::make_shared<Sprite>("./assets/stone(1).png");
27 _sprites["tree2"] = std::make_shared<Sprite>("./assets/trees/tree(2).png");
28 _sprites["tree4"] = std::make_shared<Sprite>("./assets/trees/tree(4).png");
29 _sprites["stone1"] = std::make_shared<Sprite>("./assets/stone(1).png");
30 _sprites["lvlbanner"] = std::make_shared<Sprite>("./assets/lvlBanner.png", 8, 0.1f);
31 _sprites["bubble"] = std::make_shared<Sprite>("./assets/bubble.png");
32 _sprites["background"] = std::make_shared<Sprite>("assets/background.jpg");
33 _sprites["background"]->resetOrigin();
34 _sprites["starling"] = std::make_shared<Sprite>("assets/trantorian.png");
35 _sprites["starling"]->setScale(2);
36 _sprites["buildings"] = std::make_shared<Sprite>("./assets/houses.png", 8, 0.1f);
37 _sprites["lvlup"] = std::make_shared<Sprite>("./assets/lvlup.png", 8, 0.1f);
38 _view.setSize(sf::Vector2f(1920 , 1080));
39 _chat = std::make_shared<Chat>(_core->getFont(), 7);
40 _bubbleText = sf::Text("", _core->getFont(), 15);
41 _bubbleText.setFillColor(sf::Color::Black);
42 initSounds();
43 }
44
45 void World::init()
46 {
47 reset();
48 _chat->addMessage("Connection to server established");
49 getServerInit();
50
51 PerlinNoise noise;
52
53 for (int i = 0; i < _worldSize.x; i++) {
54 std::vector<Chunck> chuncks;
55 for (int j = 0; j < _worldSize.y; j++) {
56 Chunck chunck;
57 chunck._pos = sf::Vector2f(
58 i * TILE_SIZE_MX - j * TILE_SIZE_MX - TILE_SIZE_X / 4 * 3,
59 j * TILE_SIZE_MY + i * TILE_SIZE_MY
60 );
61 chunck._yOffset = noise.noise(i * 0.1, j * 0.1) * 80;
62 if (rand() % 2 == 0)
63 chunck.addElement(_sprites[_elements[rand() % _elements.size()]]);
64 if (chunck._yOffset > 0 && rand() % 2 == 0)
65 chunck.addElement(_sprites[_elements[rand() % 2]]);
66
67 chuncks.push_back(chunck);
68 }
69 _chuncks.push_back(chuncks);
70 }
71
72 for (int i = 0; i < _worldSize.x; i++) {
73 std::vector<int> innerVector(_worldSize.y, 0);
74 _buildings.push_back(innerVector);
75 }
76
77 _pos = sf::Vector2f(
78 (int)(_worldSize.x / 2) * TILE_SIZE_MX- (int)(_worldSize.x / 2) * TILE_SIZE_MX - TILE_SIZE_MY,
79 (int)(_worldSize.y / 2) * TILE_SIZE_MY + (int)(_worldSize.y / 2) * TILE_SIZE_MY - TILE_SIZE_Y
80 );
81
82 _starlings.push_back(Starlings());
83 _starlings.push_back(Starlings());
84 _starlings.push_back(Starlings());
85 _starlings.push_back(Starlings());
86 _starlings.push_back(Starlings());
87 }
88
89 void World::reset()
90 {
91 _trantorians.clear();
92 _buildings.clear();
93 _chuncks.clear();
94 _worldSize = sf::Vector2f(0, 0);
95 _selectedTile = sf::Vector2f(-1, -1);
96 _hoveredTile = sf::Vector2f(-1, -1);
97 _offset = sf::Vector2f(0, 0);
98 _tmpOffset = sf::Vector2f(0, 0);
99 _isDragging = false;
100 _zoom = 1;
101 _view.setSize(sf::Vector2f(1920, 1080));
102 _view.setCenter(sf::Vector2f(1920 / 2, 1080 / 2));
103 _pos = sf::Vector2f(0, 0);
104 _dragStart = sf::Vector2f(0, 0);
105 }
106
107 void World::getServerInit()
108 {
109 while (_core->_data.getMap().getSize()[0] == 0)
110 _core->_parser.updateData(_core->_data, _core->_server);
111 _worldSize = sf::Vector2f(_core->_data.getMap().getSize()[0], _core->_data.getMap().getSize()[1]);
112 _chat->addMessage("World size: " + std::to_string((int)_worldSize.x) + "x" + std::to_string((int)_worldSize.y));
113 while (_core->_data.getTeams().size() == 0)
114 _core->_parser.updateData(_core->_data, _core->_server);
115 _chat->addMessage("Teams: ");
116 for (auto &team : _core->_data.getTeams()) {
117 _teams.push_back(team);
118 _chat->addMessage(" - " + team);
119 }
120 _core->_tickRate = _core->_data.getTickRate();
121 _chat->addMessage("Tick rate: " + std::to_string(_core->_tickRate));
122 Ranking::getRanking(_rankings, _core->_data);
123 }
124
125 void World::initSounds()
126 {
127 _sounds["talk1"].openFromFile("./assets/audio/talk1.ogg");
128 _sounds["talk1"].setVolume(100);
129 _sounds["talk2"].openFromFile("./assets/audio/talk2.ogg");
130 _sounds["talk2"].setVolume(100);
131 _sounds["hourray"].openFromFile("./assets/audio/hourray.ogg");
132 _sounds["hourray"].setVolume(100);
133 _sounds["wololo"].openFromFile("./assets/audio/wololo.ogg");
134 _sounds["wololo"].setVolume(100);
135 _sounds["interact"].openFromFile("./assets/audio/interact.ogg");
136 _sounds["interact"].setVolume(25);
137 }
138
139 void World::lookTrantorian(int index)
140 {
141 Trantorian trantorian = _trantorians[index];
142
143 sf::Vector2f pos = trantorian.getPos();
144 sf::Vector2f viewPos = _pos + _tmpOffset + _offset;
145 sf::Vector2f offset = sf::Vector2f(
146 pos.x - viewPos.x,
147 pos.y - viewPos.y
148 );
149 _offset += offset;
150 }
151
152 void World::checkWinner()
153 {
154 try {
155 std::optional<std::string> winner = _core->_data.getWinner();
156 if (winner.has_value()) {
157 _core->_winner = winner.value();
158 _core->_upperState = GameState::WIN;
159 }
160 } catch (std::exception &e) {
161 std::cout << e.what() << std::endl;
162 }
163 }
164