Coverage report for gui


src/
File: src/render/scenes/Home.cpp
Date: 2024-06-25 10:57:02
Lines:
0/65
0.0%
Functions:
0/5
0.0%
Branches:
0/108
0.0%

Line Branch Exec Source
1 #include "Home.hpp"
2 #include "../core/Core.hpp"
3
4 Home::Home(Core *core, int port, std::string ip) : _core(core) {
5
6 _quitButton = std::make_shared<Button>(sf::Vector2f(100, 500), sf::Vector2f(100, 100), "Quit", _core->getFont());
7 _startButton = std::make_shared<Button>(sf::Vector2f(100, 400), sf::Vector2f(100, 100), "Start", _core->getFont());
8 _portButton = std::make_shared<Input>(sf::Vector2f(100, 350), sf::Vector2f(100, 100), "Port", _core->getFont(),
9 "1234567890");
10 if (port != 0)
11 _portButton->setInput(std::to_string(port));
12 _ipButton = std::make_shared<Input>(sf::Vector2f(100, 300), sf::Vector2f(100, 100), "Ip", _core->getFont(),
13 "1234567890.");
14 if (ip != "")
15 _ipButton->setInput(ip);
16
17 _chat = std::make_shared<Chat>(_core->getFont(), 7);
18 _chat->setPosition(sf::Vector2f(50, 720 - 50));
19 _chat->addMessage("Welcome to Zappy 1.0");
20 initSprites();
21 }
22
23 void Home::draw(sf::RenderWindow &window) {
24 _sprites["background"]->draw(window);
25 _sprites["movingEarth"]->draw(window);
26 for (auto &starling : _starlings) {
27 _sprites["starling"]->setPosition(starling._pos);
28 _sprites["starling"]->setRotation(starling._rotation);
29 _sprites["starling"]->draw(window);
30 }
31 _quitButton->draw(window);
32 _startButton->draw(window);
33 _ipButton->draw(window, _core->getDeltaTime());
34 _portButton->draw(window, _core->getDeltaTime());
35 _chat->draw(window);
36 _sprites["logo"]->draw(window);
37 }
38
39 void Home::update(float fElapsedTime) {
40 _sprites["movingEarth"]->update(fElapsedTime);
41 for (auto &starling : _starlings) {
42 starling.update(fElapsedTime);
43 }
44 }
45
46 bool Home::update(sf::Event event, sf::RenderWindow &window) {
47 if (_quitButton->update(event, window))
48 window.close();
49 if (_startButton->update(event, window)) {
50 int port = 0;
51 try {
52 port = std::stoi(_portButton->getInput());
53 } catch (std::exception &e) {
54 _chat->addMessage("Invalid port", sf::Color::Red);
55 return false;
56 }
57 if (!_core->connectToServer(port, _ipButton->getInput())) {
58 _chat->addMessage("Failed to connect to server", sf::Color::Red);
59 return false;
60 }
61 _core->_scenes[GameState::GAME]->init();
62 _core->_state = GameState::GAME;
63 }
64 _ipButton->update(event, window);
65 _portButton->update(event, window);
66 return true;
67 }
68
69 void Home::initSprites() {
70 _sprites["background"] = std::make_shared<Sprite>("assets/background.jpg");
71 _sprites["background"]->resetOrigin();
72 _sprites["movingEarth"] = std::make_shared<Sprite>("assets/earthPlanetMenu75.png", 100, 0.3f);
73 _sprites["movingEarth"]->setScale(10);
74 _sprites["logo"] = std::make_shared<Sprite>("assets/logo.png");
75 _sprites["logo"]->setPosition(sf::Vector2f(200, 150));
76 _sprites["movingEarth"]->setPosition(sf::Vector2f(1080, 720));
77 _sprites["starling"] = std::make_shared<Sprite>("assets/trantorian.png");
78 _sprites["starling"]->setScale(2);
79 _starlings.push_back(Starlings());
80 _starlings.push_back(Starlings());
81 _starlings.push_back(Starlings());
82 _starlings.push_back(Starlings());
83 _starlings.push_back(Starlings());
84 _starlings.push_back(Starlings());
85 }
86