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

@@ -7,6 +7,7 @@
#include "render/gui/TowerGui.h"
#include "render/gui/imgui/imgui.h"
#include "render/gui/imgui/imgui_filebrowser.h"
#include "imgui/imgui_impl_opengl3.h"
#include "imgui/imgui_impl_sdl.h"
#include <SDL2/SDL.h>
@@ -14,12 +15,11 @@
#include "game/client/Client.h"
#include "game/server/Server.h"
#include "misc/Time.h"
#include "imgui/imgui_filebrowser.h"
#include "render/Renderer.h"
#include "network/Network.h"
#include "render/gui/GuiManager.h"
#include "render/gui/SummonMenu.h"
#include "render/gui/MainMenu.h"
#include "render/gui/GameMenu.h"
#include <iostream>
#include <cmath>
@@ -31,12 +31,14 @@ static SDL_GLContext gl_context;
static std::unique_ptr<td::client::Client> client;
static std::thread* serverThread;
static td::render::Renderer* renderer;
static td::gui::GuiManager guiManager;
static std::unique_ptr<td::gui::MainMenu> mainMenu;
static std::unique_ptr<td::gui::GameMenu> gameMenu;
bool serverShouldStop = false;
void initWidgets(){
guiManager.addWidgets(std::make_shared<td::gui::SummonMenu>(client.get()));
mainMenu = std::make_unique<td::gui::MainMenu>(client.get());
gameMenu = std::make_unique<td::gui::GameMenu>(client.get());
}
void init(SDL_Window* sdl_window, SDL_GLContext sdlContext, td::render::Renderer* render) {
@@ -81,197 +83,6 @@ void renderFPSCounter() {
ImGui::End();
}
bool startServer(int port, const std::string& worldFilePath) {
if (worldFilePath.empty())
return false;
std::shared_ptr<td::server::Server> server = std::make_shared<td::server::Server>(worldFilePath);
if (!server->start(port)) {
return false;
}
serverThread = new std::thread([server]() {
while (!serverShouldStop) {
static std::uint64_t lastTime = td::utils::getTime();
std::uint64_t time = td::utils::getTime();
std::uint64_t delta = time - lastTime;
if (delta >= SERVER_TICK) {
server->tick(delta);
lastTime = td::utils::getTime();
std::uint64_t sleepTime = SERVER_TICK - (delta - SERVER_TICK);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
server->stop();
});
return true;
}
void renderMainMenu() {
ImGui::Begin("Main Menu");
if (ImGui::Button("Rejoindre une partie##join")) {
ImGui::OpenPopup("Rejoindre une partie##join_popup");
}
if (ImGui::Button("Créer une partie")) {
ImGui::OpenPopup("Créer une partie##create_popup");
}
if (ImGui::Button("Options")) {
}
static bool triedToConnect = false;
if (ImGui::BeginPopup("Rejoindre une partie##join_popup")) {
static char buffer[512] = "localhost";
static int port = 25565;
ImGui::InputText("Server Adress", buffer, sizeof(buffer));
ImGui::InputInt("Port", &port, -1);
if (ImGui::Button("Rejoindre")) {
client->connect(td::network::Dns::Resolve(buffer), port);
triedToConnect = true;
}
if (triedToConnect) {
ImGui::Text("Impossible de se connecter");
}
ImGui::EndPopup();
} else {
triedToConnect = false;
}
static bool triedToCreate = false;
if (ImGui::BeginPopup("Créer une partie##create_popup")) {
static imgui_addons::ImGuiFileBrowser file_dialog;
static int port = 25565;
static std::string worldFilePath;
ImGui::InputInt("Server Port", &port, -1);
ImGui::Text("%s", std::string("Fichier de monde sélectionné : " + (worldFilePath.empty() ? std::string("Aucun") : worldFilePath)).c_str());
ImGui::SameLine();
if (ImGui::Button("Ouvrir un fichier")) {
ImGui::OpenPopup("WorldFileDialog");
}
if (file_dialog.showFileDialog("WorldFileDialog", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(600, 300), ".tdmap")) {
worldFilePath = file_dialog.selected_path;
}
if (ImGui::Button("Créer")) {
if (!startServer(port, worldFilePath)) {
triedToCreate = true;
} else {
client->connect(td::network::Dns::Resolve("localhost"), port);
}
}
if (triedToCreate)
ImGui::Text("Failed to launch server");
ImGui::EndPopup();
} else {
triedToCreate = false;
}
ImGui::End();
}
ImVec4 getImGuiTeamColor(td::game::TeamColor color) {
switch (color) {
case td::game::TeamColor::None:
break;
case td::game::TeamColor::Red:
return ImVec4(1, 0, 0, 1);
case td::game::TeamColor::Blue:
return ImVec4(0, 0, 1, 1);
}
return ImVec4(1, 1, 1, 1);
}
void showPlayers() {
if (ImGui::TreeNode(std::string("Players (" + std::to_string(client->getGame().getPlayers().size()) + ")##player_list").c_str())) {
for (auto pair : client->getGame().getPlayers()) {
const td::game::Player& player = pair.second;
ImGui::PushStyleColor(ImGuiCol_Text, getImGuiTeamColor(player.getTeamColor()));
ImGui::Text("%s", player.getName().c_str());
ImGui::PopStyleColor();
}
ImGui::TreePop();
}
}
void showTeamSelection() {
if (client->getGame().getPlayer() == nullptr)
return;
td::game::TeamColor playerTeam = client->getGame().getPlayer()->getTeamColor();
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Red ? "Leave" : "Join") + std::string(" Red Team")).c_str())) {
if (playerTeam == td::game::TeamColor::Red)
client->selectTeam(td::game::TeamColor::None);
else
client->selectTeam(td::game::TeamColor::Red);
}
ImGui::SameLine();
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Blue ? "Leave" : "Join") + std::string(" Blue Team")).c_str())) {
if (playerTeam == td::game::TeamColor::Blue)
client->selectTeam(td::game::TeamColor::None);
else
client->selectTeam(td::game::TeamColor::Blue);
}
}
void showLobbyProgress() {
const int timePassed = LOBBY_WAITING_TIME - client->getGame().getLobbyTime();
const float progress = (float)timePassed / (float)(LOBBY_WAITING_TIME);
if (progress > 0 && progress < 1) {
ImGui::ProgressBar(progress, ImVec2(0.0f, 0.0f), std::string(std::to_string(client->getGame().getLobbyTime() / 1000) + "s").c_str());
ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
ImGui::Text("Time Remaining");
} else {
ImGui::Text("Waiting for players ...\n");
}
}
void showTPS() {
ImGui::Text("Server TPS : %.1f", client->getConnexion().getServerTPS());
ImGui::Text("Server Ping : %i", client->getConnexion().getServerPing());
}
void showStats() {
ImGui::Text("Gold : %i", client->getGame().getPlayer()->getGold());
}
void capValues(int* values, int& value) {
value = std::max(0, value);
value = std::min(12, value);
int total = 0;
for (int j = 0; j < 16; j++) {
total += values[j];
}
if (total == 13)
value--;
}
void renderSummonMenu() {
}
void renderGame() {
if (client->getGame().getGameState() == td::game::GameState::Lobby) {
ImGui::Begin("Lobby");
showTPS();
showPlayers();
showLobbyProgress();
showTeamSelection();
ImGui::End();
}
if (client->getGame().getGameState() == td::game::GameState::Game) {
ImGui::Begin("Game");
showTPS();
showStats();
showPlayers();
guiManager.renderWidgets();
ImGui::End();
}
}
void tick() {
static std::uint64_t lastTime = td::utils::getTime();
std::uint64_t time = td::utils::getTime();
@@ -287,9 +98,9 @@ void render() {
beginFrame();
client->render();
if (client->isConnected())
renderGame();
gameMenu->render();
else
renderMainMenu();
mainMenu->render();
static bool demo_open = false;
if (demo_open)
ImGui::ShowDemoWindow(&demo_open);