Skip to content

Commit

Permalink
Fixed some issues with singleplayer mode. See #75 for more info.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 15, 2020
1 parent 988fe53 commit a95cd77
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 29 deletions.
14 changes: 8 additions & 6 deletions client/source/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ BlockCursor::BlockCursor(ClientPlayer &player, ClientWorld &world, ClientCommand
void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
if (event.type == SDL_MOUSEBUTTONDOWN && m_selectedBlock.w != -1) {
if (event.button.button == SDL_BUTTON_LEFT) {
m_animationStart = gk::GameClock::getTicks();
m_animationStart = gk::GameClock::getInstance().getTicks();
m_currentTool = &m_player.inventory().getStack(hotbar.cursorPos(), 0);
}
else if (event.button.button == SDL_BUTTON_RIGHT) {
Expand Down Expand Up @@ -133,24 +133,26 @@ void BlockCursor::update(const Hotbar &hotbar) {
// m_selectedBlock.w = -1;
// }

u32 ticks = gk::GameClock::getInstance().getTicks();

if (selectedBlockChanged)
m_animationStart = (m_animationStart) ? gk::GameClock::getTicks() : 0;
m_animationStart = (m_animationStart) ? ticks : 0;

const ItemStack &currentStack = m_player.inventory().getStack(hotbar.cursorPos(), 0);
float timeToBreak = 0;
if (m_animationStart) {
if (m_currentTool->item().id() != currentStack.item().id()) {
m_animationStart = gk::GameClock::getTicks();
m_animationStart = ticks;
m_currentTool = &currentStack;
}
else {
timeToBreak = m_currentBlock->timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed());

if (gk::GameClock::getTicks() > m_animationStart + timeToBreak * 1000) {
if (ticks > m_animationStart + timeToBreak * 1000) {
ItemStack itemDrop = m_currentBlock->getItemDrop();
m_player.inventory().addStack(itemDrop.item().stringID(), itemDrop.amount());
m_world.setBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z, 0);
m_animationStart = gk::GameClock::getTicks();
m_animationStart = ticks;

m_client.sendPlayerDigBlock(m_selectedBlock);
m_client.sendPlayerInvUpdate();
Expand All @@ -167,7 +169,7 @@ void BlockCursor::update(const Hotbar &hotbar) {

if (m_animationStart && m_currentBlock)
updateAnimationVertexBuffer(*m_currentBlock, orientation,
(gk::GameClock::getTicks() - m_animationStart) / (timeToBreak * 100));
(ticks - m_animationStart) / (timeToBreak * 100));
}

using namespace BlockGeometry;
Expand Down
2 changes: 1 addition & 1 deletion client/source/hud/HUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void HUD::update() {
m_hotbar.update();

if (Config::isFpsCounterEnabled)
m_fpsText.setText(std::to_string(gk::GameClock::getFpsAverage()) + " FPS");
m_fpsText.setText(std::to_string(gk::GameClock::getInstance().getFpsAverage()) + " FPS");

m_blockCursor.update(m_hotbar);

Expand Down
2 changes: 1 addition & 1 deletion client/source/network/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void Client::sendKeyState() {
gk::InputHandler *inputHandler = gk::GamePad::getInputHandler();
if (inputHandler) {
sf::Packet packet;
packet << Network::Command::KeyState << gk::GameClock::getTicks() << m_id;
packet << Network::Command::KeyState << gk::GameClock::getInstance().getTicks() << m_id;
for (auto &it : inputHandler->keysPressed()) {
packet << static_cast<u8>(it.first) << it.second;
}
Expand Down
4 changes: 2 additions & 2 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void GameState::update() {

m_hud.update();

if (gk::GameClock::getTicks() % 100 < 10) {
if (gk::GameClock::getInstance().getTicks() % 100 < 10) {
m_clientCommandHandler.sendPlayerPosUpdate();
}

Expand All @@ -171,7 +171,7 @@ void GameState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
// FIXME: This uniform is not used anymore since water/leaves effects are disabled
// gk::Shader::bind(&m_shader);
// m_shader.setUniform("u_time", gk::GameClock::getTicks());
// m_shader.setUniform("u_time", gk::GameClock::getInstance().getTicks());
// gk::Shader::bind(nullptr);

states.shader = &m_shader;
Expand Down
29 changes: 20 additions & 9 deletions client/source/states/TitleScreenState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void TitleScreenState::centerBackground() {

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

void TitleScreenState::onEvent(const SDL_Event &event) {
Expand All @@ -86,23 +87,27 @@ void TitleScreenState::onEvent(const SDL_Event &event) {
}

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

m_stateStack->push<ServerLoadingState>(game, m_showLoadingState, this);
}
}

void TitleScreenState::startSingleplayer(bool showLoadingState) {
m_showLoadingState = showLoadingState;

if (m_thread.joinable())
m_thread.join();

m_thread = std::thread([this] () {
ServerApplication app;
ServerApplication app{*m_eventHandler};
app.setSingleplayer(true);
app.setPort(m_port);
app.run();
});

std::this_thread::sleep_for(std::chrono::milliseconds(200));

auto &game = m_stateStack->push<GameState>();
game.setSingleplayer(true);
game.connect("localhost", m_port);

m_stateStack->push<ServerLoadingState>(game, showLoadingState, this);
}

void TitleScreenState::startMultiplayer(const std::string &host) {
Expand All @@ -118,7 +123,13 @@ void TitleScreenState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
m_menuWidget.onGuiScaleChanged(event);
}

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

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

prepareDraw(target, states);

target.draw(m_background, states);
Expand Down
5 changes: 5 additions & 0 deletions client/source/states/TitleScreenState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "MenuWidget.hpp"

struct GuiScaleChangedEvent;
struct ServerOnlineEvent;

class TitleScreenState : public InterfaceState {
public:
Expand All @@ -52,6 +53,7 @@ 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 @@ -62,6 +64,9 @@ class TitleScreenState : public InterfaceState {
std::thread m_thread;

u16 m_port = 4242;

bool m_showLoadingState = false;
bool m_isServerOnline = false;
};

#endif // TITLESCREENSTATE_HPP_
18 changes: 15 additions & 3 deletions server/source/core/ServerApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@
namespace fs = ghc::filesystem;

ServerApplication::ServerApplication(int argc, char **argv) : m_argumentParser(argc, argv) {
std::srand(std::time(nullptr));
BlockGeometry::initOrientation();
}

ServerApplication::ServerApplication(gk::EventHandler &eventHandler) {
m_eventHandler = &eventHandler;
}

void ServerApplication::init() {
std::srand(std::time(nullptr));

BlockGeometry::initOrientation();

m_argumentParser.addArgument("port", {"-p", "--port", true});
m_argumentParser.addArgument("working_dir", {"-w", "--working-dir", true});

Expand Down Expand Up @@ -68,6 +74,9 @@ void ServerApplication::init() {
m_scriptEngine.luaCore().setRegistry(&m_registry);

std::cout << "Server is running on localhost:" << m_port << std::endl;

if (m_eventHandler)
m_eventHandler->emplaceEvent<ServerOnlineEvent>(true);
}

int ServerApplication::run(bool isProtected) {
Expand All @@ -77,6 +86,9 @@ int ServerApplication::run(bool isProtected) {
mainLoop();
}
catch(const gk::Exception &e) {
if (m_eventHandler)
m_eventHandler->emplaceEvent<ServerOnlineEvent>(false);

std::cerr << "Fatal error " << e.what() << std::endl;
return 1;
}
Expand All @@ -92,7 +104,7 @@ int ServerApplication::run(bool isProtected) {
void ServerApplication::update() {
m_worldController.update();

if (gk::GameClock::getTicks() % 100 < 10) {
if (m_clock.getTicks() % 100 < 10) {
for (auto &it : m_players) {
m_serverCommandHandler.sendPlayerPosUpdate(it.first);
}
Expand Down
9 changes: 8 additions & 1 deletion server/source/core/ServerApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <gk/core/ArgumentParser.hpp>
#include <gk/core/GameClock.hpp>
#include <gk/core/EventHandler.hpp>

#include "LuaCore.hpp"
#include "Registry.hpp"
Expand All @@ -38,9 +39,14 @@
#include "ServerPlayer.hpp"
#include "WorldController.hpp"

struct ServerOnlineEvent {
bool isOnline;
};

class ServerApplication {
public:
ServerApplication(int argc = 0, char **argv = nullptr);
ServerApplication(gk::EventHandler &eventHandler);

void init();

Expand All @@ -57,14 +63,15 @@ class ServerApplication {

gk::ArgumentParser m_argumentParser;
gk::GameClock m_clock;
gk::EventHandler *m_eventHandler = nullptr;

ScriptEngine m_scriptEngine;

Registry m_registry;

u16 m_port = 4242;

WorldController m_worldController{m_registry};
WorldController m_worldController{m_registry, m_clock};
std::unordered_map<u16, ServerPlayer> m_players;

Server m_server;
Expand Down
4 changes: 2 additions & 2 deletions server/source/world/ServerWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#include "ServerWorld.hpp"

void ServerWorld::update() {
if (m_lastTick < gk::GameClock::getTicks() / 50) {
m_lastTick = gk::GameClock::getTicks() / 50;
if (m_lastTick < m_clock.getTicks() / 50) {
m_lastTick = m_clock.getTicks() / 50;

for (auto &it : m_chunks) {
it.second->tick(*this, *m_server);
Expand Down
10 changes: 8 additions & 2 deletions server/source/world/ServerWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "TerrainGenerator.hpp"
#include "World.hpp"

namespace gk {
class GameClock;
}

class ClientInfo;
class Dimension;
class ServerCommandHandler;
Expand All @@ -42,8 +46,8 @@ class ServerWorld : public World {
using ChunkMap = std::unordered_map<gk::Vector3i, std::unique_ptr<ServerChunk>>;

public:
ServerWorld(const Dimension &dimension)
: m_dimension(dimension), m_terrainGenerator(dimension) {}
ServerWorld(const Dimension &dimension, gk::GameClock &clock)
: m_dimension(dimension), m_terrainGenerator(dimension), m_clock(clock) {}

void update();

Expand Down Expand Up @@ -73,6 +77,8 @@ class ServerWorld : public World {
TerrainGenerator m_terrainGenerator;

ServerCommandHandler *m_server = nullptr;

gk::GameClock &m_clock;
};

#endif // SERVERWORLD_HPP_
2 changes: 1 addition & 1 deletion server/source/world/WorldController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

void WorldController::init() {
for (const Dimension &dimension : m_registry.dimensions()) {
m_worldList.emplace_back(dimension);
m_worldList.emplace_back(dimension, m_clock);
m_worldList.back().setServer(m_server);
}
}
Expand Down
8 changes: 7 additions & 1 deletion server/source/world/WorldController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@

#include "ServerWorld.hpp"

namespace gk {
class GameClock;
}

class Registry;

class WorldController {
public:
WorldController(Registry &registry) : m_registry(registry) {}
WorldController(Registry &registry, gk::GameClock &clock) : m_registry(registry), m_clock(clock) {}

void init();

Expand All @@ -50,6 +54,8 @@ class WorldController {

Registry &m_registry;

gk::GameClock &m_clock;

ServerCommandHandler *m_server = nullptr;
};

Expand Down

0 comments on commit a95cd77

Please sign in to comment.