refactor: create MainMenu and GameMenu classes

This commit is contained in:
2021-11-04 11:01:46 +01:00
parent 1186e5ee9c
commit 2fc0f28b27
9 changed files with 303 additions and 205 deletions

View File

@@ -5,7 +5,7 @@
namespace td {
namespace server {
Server::Server(const std::string& worldFilePath) {
Server::Server(const std::string& worldFilePath) : m_ServerRunning(false) {
m_Game.getWorld()->loadMapFromFile(worldFilePath);
}
@@ -13,6 +13,31 @@ void Server::lauchGame() {
m_Game.startGame();
}
void Server::startThread() {
m_Thread = std::thread([this]() {
std::uint64_t lastTime = td::utils::getTime();
while (m_ServerRunning) {
std::uint64_t time = td::utils::getTime();
std::uint64_t delta = time - lastTime;
if (delta >= SERVER_TICK) {
tick(delta);
lastTime = td::utils::getTime();
std::uint64_t sleepTime = SERVER_TICK - (delta - SERVER_TICK);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
});
}
void Server::stopThread(){
m_ServerRunning = false;
if(m_Thread.joinable())
m_Thread.join();
}
bool Server::start(std::uint16_t port) {
if (!m_Listener.listen(port, 10)) {
std::cout << "Failed to bind port " << port << " !\n";
@@ -24,10 +49,16 @@ bool Server::start(std::uint16_t port) {
}
std::cout << "Server started at port " << port << " !\n";
m_TickCounter.reset();
m_ServerRunning = true;
startThread();
return true;
}
void Server::stop() {
if(!m_ServerRunning)
return;
stopThread();
protocol::DisconnectPacket packet("Server closed");
broadcastPacket(&packet);