RType
ClientCore.cpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-type
4 ** File description:
5 ** R-type
6 */
7 
8 #include "ClientCore.hpp"
9 
10 /**
11  * @brief Construct a new Client Core:: Client Core object
12  *
13  */
15 {
16  _socket = std::make_shared<ClientSocket>();
17  _scenes["menu"] = std::make_unique<MenuScene>(this, _socket);
18  _scenes["main"] = std::make_unique<MainScene>(this, _socket);
19  _scenes["game"] = std::make_unique<GameScene>(this, _socket);
20  _currentScene = _scenes["main"];
21 }
22 
23 /**
24  * @brief init_socket, initialize the socket with the ip and the port
25  *
26  * @param ip
27  * @param port
28  */
29 bool ClientCore::init_socket(const std::string &ip, int port)
30 {
31  if (_socket->isInit())
32  return true;
33  bool init = _socket->init_client(ip, port);
34  if (!init)
35  return false;
36  return init;
37 }
38 
39 /**
40  * @brief run, run the client
41  *
42  */
43 void ClientCore::run()
44 {
45  _window.create(sf::VideoMode(800, 600), "R-Type");
46  sf::Event event{};
47 
48  _window.setFramerateLimit(120);
49 
50  while (_window.isOpen()) {
51  _window.clear();
52  _socket->init_fd_set();
53  _currentScene->handleEvent(event, _window);
54  _currentScene->receiveData();
55  _currentScene->update();
56  _currentScene->display(_window);
57  _window.display();
58  }
59  if (_heartBeatThread.joinable())
60  _heartBeatThread.join();
61 }
62 
63 /**
64  * @brief getSceneByName, get the scene by name
65  *
66  * @param name
67  * @return std::shared_ptr<IScene>
68  */
69 std::shared_ptr<IScene> ClientCore::getSceneByName(const std::string &name)
70 {
71  return _scenes[name];
72 }
73 
74 /**
75  * @brief setCurrentScene, set the current scene
76  *
77  * @param name
78  */
79 void ClientCore::setCurrentScene(const std::string &name)
80 {
81  _currentScene->continueScene = false;
82  _currentScene->pauseScene();
83  _currentScene = getSceneByName(name);
84  _currentScene->continueScene = true;
85  _currentScene->resumeScene();
86 }
87 
88 /**
89  * @brief sendHeartBeat, send the heart beat
90  *
91  * @param window
92  */
93 void ClientCore::sendHeartBeat(sf::RenderWindow &window)
94 {
95  std::unique_ptr<Packet> packet = std::make_unique<Packet>();
96 
97  packet->code = HEARTBEAT;
98  packet->data_size = sizeof(timeval);
99  packet->data = malloc(packet->data_size);
100  while (window.isOpen()) {
101  gettimeofday((timeval *)packet->data, nullptr);
102  _socket->send(packet.get(), _socket->serv_addr);
103  std::this_thread::sleep_for(std::chrono::milliseconds(500));
104  }
105  free(packet->data);
106 }
107 
108 /**
109  * @brief getCurrentScene, get the current scene
110  *
111  * @return std::shared_ptr<IScene>
112  */
113 std::shared_ptr<IScene> ClientCore::getCurrentScene() const
114 {
115  return _currentScene;
116 }
117 
118 /**
119  * @brief startHeartBeat, start the heart beat
120  *
121  */
123 {
124  if (_heartBeatThread.joinable())
125  _heartBeatThread.join();
126  _heartBeatThread = std::thread(&ClientCore::sendHeartBeat, this, std::ref(_window));
127 }