Skip to content

Commit

Permalink
Fixed #96.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 20, 2020
1 parent c35da59 commit fe07116
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 72 deletions.
2 changes: 0 additions & 2 deletions source/client/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ void ClientApplication::init() {
m_resourceHandler.add<Font>("font-ascii", "texture-font", "resources/textures/font.properties");
m_resourceHandler.add<TextureAtlas>("atlas-blocks");

Registry::setInstance(m_registry);

auto &titleScreen = m_stateStack.push<TitleScreenState>(m_port);
if (m_argumentParser.getArgument("texture-pack").isFound)
titleScreen.setTexturePack(m_argumentParser.getArgument("texture-pack").parameter);
Expand Down
3 changes: 0 additions & 3 deletions source/client/core/ClientApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <gk/core/CoreApplication.hpp>

#include "KeyboardHandler.hpp"
#include "Registry.hpp"

class ClientApplication : public gk::CoreApplication {
public:
Expand All @@ -48,8 +47,6 @@ class ClientApplication : public gk::CoreApplication {

KeyboardHandler m_keyboardHandler;

Registry m_registry;

std::string m_host = "localhost";
u16 m_port = 4242;
};
Expand Down
2 changes: 2 additions & 0 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
GameState::GameState()
: m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks"))
{
Registry::setInstance(m_registry);

initShaders();

m_clientCommandHandler.setupCallbacks();
Expand Down
3 changes: 3 additions & 0 deletions source/client/states/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "Config.hpp"
#include "HUD.hpp"
#include "PlayerBox.hpp"
#include "Registry.hpp"

class TextureAtlas;

Expand Down Expand Up @@ -88,6 +89,8 @@ class GameState : public gk::ApplicationState {
HUD m_hud{m_player, m_world, m_clientCommandHandler};

TextureAtlas &m_textureAtlas;

Registry m_registry;
};

#endif // GAMESTATE_HPP_
52 changes: 26 additions & 26 deletions source/client/states/ServerConnectState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,37 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface
m_connectButton.setScale(Config::guiScale, Config::guiScale);
m_connectButton.setCallback([this](TextButton &) {
size_t sep = m_textInput.text().find_first_of(':');

std::string host = m_textInput.text().substr(0, sep);

int port = 0;
try {
port = std::stoi(m_textInput.text().substr(sep + 1));
}
catch (const std::invalid_argument &e) {
std::cerr << "Error: Invalid server address." << std::endl;
// TODO: Use m_errorText
gkError() << "Error: Invalid server address.";
}

auto &game = m_stateStack->push<GameState>();

try {
game.connect(host, port);
// try {
// game.connect(host, port);

auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, true, this);
auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, true, host, port, this);
serverLoadingState.setTexturePack(m_texturePack);
}
catch (ClientConnectException &e) {
gkError() << e.what();

m_stateStack->pop();

m_errorText.setText(e.what());
m_errorText.updateVertexBuffer();
m_errorText.setPosition(
Config::screenWidth / 2.0f - m_errorText.getSize().x * Config::guiScale / 2.0f,
Config::screenHeight / 2.0f - 30 * Config::guiScale
);
}
// }
// catch (ClientConnectException &e) {
// gkError() << e.what();
//
// m_stateStack->pop();
//
// m_errorText.setText(e.what());
// m_errorText.updateVertexBuffer();
// m_errorText.setPosition(
// Config::screenWidth / 2.0f - m_errorText.getSize().x * Config::guiScale / 2.0f,
// Config::screenHeight / 2.0f - 30 * Config::guiScale
// );
// }
});

m_cancelButton.setText("Cancel");
Expand All @@ -85,8 +85,8 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface
m_stateStack->pop();
});

m_errorText.setColor(gk::Color::Red);
m_errorText.setScale(Config::guiScale, Config::guiScale);
// m_errorText.setColor(gk::Color::Red);
// m_errorText.setScale(Config::guiScale, Config::guiScale);
}

void ServerConnectState::onEvent(const sf::Event &event) {
Expand All @@ -99,10 +99,10 @@ void ServerConnectState::onEvent(const sf::Event &event) {
m_connectButton.setPosition(Config::screenWidth / 2.0f - m_connectButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 340);
m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 261);

m_errorText.setPosition(
Config::screenWidth / 2.0f - m_errorText.getSize().x * Config::guiScale / 2.0f,
Config::screenHeight / 2.0f - 30 * Config::guiScale
);
// m_errorText.setPosition(
// Config::screenWidth / 2.0f - m_errorText.getSize().x * Config::guiScale / 2.0f,
// Config::screenHeight / 2.0f - 30 * Config::guiScale
// );
}

if (!m_stateStack->empty() && &m_stateStack->top() == this) {
Expand All @@ -128,8 +128,8 @@ void ServerConnectState::draw(gk::RenderTarget &target, gk::RenderStates states)
target.draw(m_connectButton, states);
target.draw(m_cancelButton, states);

if (!m_errorText.text().empty())
target.draw(m_errorText, states);
// if (!m_errorText.text().empty())
// target.draw(m_errorText, states);
}
}

2 changes: 1 addition & 1 deletion source/client/states/ServerConnectState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ServerConnectState : public InterfaceState {
TextButton m_connectButton;
TextButton m_cancelButton;

Text m_errorText;
// Text m_errorText;

std::string m_texturePack;
};
Expand Down
34 changes: 32 additions & 2 deletions source/client/states/ServerLoadingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,30 @@
#include "ServerLoadingState.hpp"
#include "TextureAtlas.hpp"

ServerLoadingState::ServerLoadingState(GameState &game, bool showLoadingState, gk::ApplicationState *parent)
#include "ServerApplication.hpp" // For ServerOnlineEvent

ServerLoadingState::ServerLoadingState(GameState &game, bool showLoadingState, const std::string &host, u16 port, gk::ApplicationState *parent)
: InterfaceState(parent), m_game(game), m_showLoadingState(showLoadingState)
{
m_text.setText("Loading world...");
m_text.setColor(gk::Color::White);
m_text.updateVertexBuffer();
m_text.setScale(Config::guiScale * 2, Config::guiScale * 2);

m_host = host;
m_port = port;

centerText();

// FIXME: SFML_RAW_MOUSE
// gk::Mouse::setCursorVisible(true);
// gk::Mouse::setCursorGrabbed(false);
}

void ServerLoadingState::init() {
m_eventHandler->addListener<ServerOnlineEvent>(&ServerLoadingState::onServerOnlineEvent, this);
}

void ServerLoadingState::centerText() {
m_text.setPosition(Config::screenWidth / 2 - m_text.getSize().x * Config::guiScale * 2 / 2,
Config::screenHeight / 2 - m_text.getSize().y * Config::guiScale * 2 / 2);
Expand All @@ -64,7 +73,23 @@ void ServerLoadingState::onEvent(const sf::Event &event) {
}

void ServerLoadingState::update() {
m_game.client().update();
if (!m_isConnected && (m_isServerOnline || m_port != 0)) {
try {
m_game.connect(m_host, m_port);
m_isConnected = true;
}
catch (ClientConnectException &e) {
// TODO: Add a state to display this message instead of printing to the console

gkError() << "Failed to connect to" << m_host + std::to_string(m_port) + ":" << e.what();

m_stateStack->pop(); // GameState
m_stateStack->pop(); // ServerLoadingState
}
}

if (m_isConnected)
m_game.client().update();

if (m_game.clientCommandHandler().isRegistryInitialized()) {
if (m_game.textureAtlas().isReady() && (m_hasBeenRendered || !m_showLoadingState)) {
Expand All @@ -90,6 +115,11 @@ void ServerLoadingState::update() {
}
}

void ServerLoadingState::onServerOnlineEvent(const ServerOnlineEvent &event) {
m_isServerOnline = event.isOnline;
m_port = event.port;
}

void ServerLoadingState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_parent)
target.draw(*m_parent, states);
Expand Down
13 changes: 12 additions & 1 deletion source/client/states/ServerLoadingState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
#include "Text.hpp"

class GameState;
struct ServerOnlineEvent;

class ServerLoadingState : public InterfaceState {
public:
ServerLoadingState(GameState &game, bool showLoadingState, gk::ApplicationState *parent = nullptr);
ServerLoadingState(GameState &game, bool showLoadingState, const std::string &host, u16 port, gk::ApplicationState *parent = nullptr);

void init() override;

void centerText();

Expand All @@ -45,6 +48,8 @@ class ServerLoadingState : public InterfaceState {
void setTexturePack(const std::string &texturePack) { m_texturePack = texturePack; }

private:
void onServerOnlineEvent(const ServerOnlineEvent &event);

void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

Text m_text;
Expand All @@ -55,6 +60,12 @@ class ServerLoadingState : public InterfaceState {
mutable bool m_hasBeenRendered = false;

std::string m_texturePack;

bool m_isServerOnline = false;
bool m_isConnected = false;

std::string m_host;
u16 m_port = 4242;
};

#endif // SERVERLOADINGSTATE_HPP_
37 changes: 8 additions & 29 deletions source/client/states/TitleScreenState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "SettingsMenuState.hpp"
#include "TitleScreenState.hpp"

#include "ServerApplication.hpp"
#include "ServerApplication.hpp" // For ServerOnlineEvent

TitleScreenState::TitleScreenState(u16 port) : m_port(port) {
glClearColor(0.0, 0.0, 0.0, 1.0);
Expand Down Expand Up @@ -71,7 +71,6 @@ void TitleScreenState::centerBackground() {

void TitleScreenState::init() {
m_eventHandler->addListener<GuiScaleChangedEvent>(&TitleScreenState::onGuiScaleChanged, this);
m_eventHandler->addListener<ServerOnlineEvent>(&TitleScreenState::onServerOnlineEvent, this);
}

void TitleScreenState::onEvent(const sf::Event &event) {
Expand All @@ -90,18 +89,14 @@ void TitleScreenState::onEvent(const sf::Event &event) {
}

void TitleScreenState::update() {
if (m_isServerOnline) {
auto &game = m_stateStack->push<GameState>();
game.setSingleplayer(true);
game.connect("localhost", m_port);

auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, m_showLoadingState, this);
serverLoadingState.setTexturePack(m_texturePack);
}
}

void TitleScreenState::startSingleplayer(bool showLoadingState) {
m_showLoadingState = showLoadingState;
auto &game = m_stateStack->push<GameState>();
game.setSingleplayer(true);

auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, showLoadingState, "localhost", sf::Socket::AnyPort, this);
serverLoadingState.setTexturePack(m_texturePack);

if (m_thread.joinable())
m_thread.join();
Expand All @@ -112,20 +107,11 @@ void TitleScreenState::startSingleplayer(bool showLoadingState) {
app.setPort(sf::Socket::AnyPort);
app.run();
});

m_isServerLaunched = true;
}

void TitleScreenState::startMultiplayer(const std::string &host) {
auto &game = m_stateStack->push<GameState>();
try {
game.connect(host, m_port);
}
catch (ClientConnectException &e) {
throw EXCEPTION(e.what());
}

auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, false, this);
auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, false, host, m_port, this);
serverLoadingState.setTexturePack(m_texturePack);
}

Expand All @@ -135,19 +121,12 @@ void TitleScreenState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
m_menuWidget.onGuiScaleChanged(event);
}

void TitleScreenState::onServerOnlineEvent(const ServerOnlineEvent &event) {
m_isServerOnline = event.isOnline;
m_port = event.port;

m_isServerLaunched = false;
}

void TitleScreenState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
prepareDraw(target, states);

target.draw(m_background, states);

if (&m_stateStack->top() == this && !m_isServerLaunched && !m_isServerOnline) {
if (&m_stateStack->top() == this) {
target.draw(m_menuWidget, states);
}
}
Expand Down
10 changes: 2 additions & 8 deletions source/client/states/TitleScreenState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class TitleScreenState : public InterfaceState {

private:
void onGuiScaleChanged(const GuiScaleChangedEvent &event);
void onServerOnlineEvent(const ServerOnlineEvent &event);

void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

Expand All @@ -65,14 +64,9 @@ class TitleScreenState : public InterfaceState {

std::thread m_thread;

u16 m_port = 4242;

bool m_showLoadingState = false;

bool m_isServerOnline = false;
bool m_isServerLaunched = false;

std::string m_texturePack;

u16 m_port = 4242;
};

#endif // TITLESCREENSTATE_HPP_
2 changes: 2 additions & 0 deletions source/common/scene/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Scene {

virtual void update();

void clear() { m_registry.clear(); }

entt::entity createEntityFromModel(entt::registry &modelRegistry, entt::entity modelEntity);

const entt::registry &registry() const { return m_registry; }
Expand Down
Loading

0 comments on commit fe07116

Please sign in to comment.