Fixed some issues with singleplayer mode. See #75 for more info.
This commit is contained in:
parent
988fe5336a
commit
a95cd77dfd
@ -50,7 +50,7 @@ BlockCursor::BlockCursor(ClientPlayer &player, ClientWorld &world, ClientCommand
|
|||||||
void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
|
void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
|
||||||
if (event.type == SDL_MOUSEBUTTONDOWN && m_selectedBlock.w != -1) {
|
if (event.type == SDL_MOUSEBUTTONDOWN && m_selectedBlock.w != -1) {
|
||||||
if (event.button.button == SDL_BUTTON_LEFT) {
|
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);
|
m_currentTool = &m_player.inventory().getStack(hotbar.cursorPos(), 0);
|
||||||
}
|
}
|
||||||
else if (event.button.button == SDL_BUTTON_RIGHT) {
|
else if (event.button.button == SDL_BUTTON_RIGHT) {
|
||||||
@ -133,24 +133,26 @@ void BlockCursor::update(const Hotbar &hotbar) {
|
|||||||
// m_selectedBlock.w = -1;
|
// m_selectedBlock.w = -1;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
u32 ticks = gk::GameClock::getInstance().getTicks();
|
||||||
|
|
||||||
if (selectedBlockChanged)
|
if (selectedBlockChanged)
|
||||||
m_animationStart = (m_animationStart) ? gk::GameClock::getTicks() : 0;
|
m_animationStart = (m_animationStart) ? ticks : 0;
|
||||||
|
|
||||||
const ItemStack ¤tStack = m_player.inventory().getStack(hotbar.cursorPos(), 0);
|
const ItemStack ¤tStack = m_player.inventory().getStack(hotbar.cursorPos(), 0);
|
||||||
float timeToBreak = 0;
|
float timeToBreak = 0;
|
||||||
if (m_animationStart) {
|
if (m_animationStart) {
|
||||||
if (m_currentTool->item().id() != currentStack.item().id()) {
|
if (m_currentTool->item().id() != currentStack.item().id()) {
|
||||||
m_animationStart = gk::GameClock::getTicks();
|
m_animationStart = ticks;
|
||||||
m_currentTool = ¤tStack;
|
m_currentTool = ¤tStack;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
timeToBreak = m_currentBlock->timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed());
|
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();
|
ItemStack itemDrop = m_currentBlock->getItemDrop();
|
||||||
m_player.inventory().addStack(itemDrop.item().stringID(), itemDrop.amount());
|
m_player.inventory().addStack(itemDrop.item().stringID(), itemDrop.amount());
|
||||||
m_world.setBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z, 0);
|
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.sendPlayerDigBlock(m_selectedBlock);
|
||||||
m_client.sendPlayerInvUpdate();
|
m_client.sendPlayerInvUpdate();
|
||||||
@ -167,7 +169,7 @@ void BlockCursor::update(const Hotbar &hotbar) {
|
|||||||
|
|
||||||
if (m_animationStart && m_currentBlock)
|
if (m_animationStart && m_currentBlock)
|
||||||
updateAnimationVertexBuffer(*m_currentBlock, orientation,
|
updateAnimationVertexBuffer(*m_currentBlock, orientation,
|
||||||
(gk::GameClock::getTicks() - m_animationStart) / (timeToBreak * 100));
|
(ticks - m_animationStart) / (timeToBreak * 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
using namespace BlockGeometry;
|
using namespace BlockGeometry;
|
||||||
|
@ -84,7 +84,7 @@ void HUD::update() {
|
|||||||
m_hotbar.update();
|
m_hotbar.update();
|
||||||
|
|
||||||
if (Config::isFpsCounterEnabled)
|
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);
|
m_blockCursor.update(m_hotbar);
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ void Client::sendKeyState() {
|
|||||||
gk::InputHandler *inputHandler = gk::GamePad::getInputHandler();
|
gk::InputHandler *inputHandler = gk::GamePad::getInputHandler();
|
||||||
if (inputHandler) {
|
if (inputHandler) {
|
||||||
sf::Packet packet;
|
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()) {
|
for (auto &it : inputHandler->keysPressed()) {
|
||||||
packet << static_cast<u8>(it.first) << it.second;
|
packet << static_cast<u8>(it.first) << it.second;
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ void GameState::update() {
|
|||||||
|
|
||||||
m_hud.update();
|
m_hud.update();
|
||||||
|
|
||||||
if (gk::GameClock::getTicks() % 100 < 10) {
|
if (gk::GameClock::getInstance().getTicks() % 100 < 10) {
|
||||||
m_clientCommandHandler.sendPlayerPosUpdate();
|
m_clientCommandHandler.sendPlayerPosUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +171,7 @@ void GameState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
|
|||||||
void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
||||||
// FIXME: This uniform is not used anymore since water/leaves effects are disabled
|
// FIXME: This uniform is not used anymore since water/leaves effects are disabled
|
||||||
// gk::Shader::bind(&m_shader);
|
// 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);
|
// gk::Shader::bind(nullptr);
|
||||||
|
|
||||||
states.shader = &m_shader;
|
states.shader = &m_shader;
|
||||||
|
@ -71,6 +71,7 @@ void TitleScreenState::centerBackground() {
|
|||||||
|
|
||||||
void TitleScreenState::init() {
|
void TitleScreenState::init() {
|
||||||
m_eventHandler->addListener<GuiScaleChangedEvent>(&TitleScreenState::onGuiScaleChanged, this);
|
m_eventHandler->addListener<GuiScaleChangedEvent>(&TitleScreenState::onGuiScaleChanged, this);
|
||||||
|
m_eventHandler->addListener<ServerOnlineEvent>(&TitleScreenState::onServerOnlineEvent, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TitleScreenState::onEvent(const SDL_Event &event) {
|
void TitleScreenState::onEvent(const SDL_Event &event) {
|
||||||
@ -86,23 +87,27 @@ void TitleScreenState::onEvent(const SDL_Event &event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TitleScreenState::update() {
|
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) {
|
void TitleScreenState::startSingleplayer(bool showLoadingState) {
|
||||||
|
m_showLoadingState = showLoadingState;
|
||||||
|
|
||||||
|
if (m_thread.joinable())
|
||||||
|
m_thread.join();
|
||||||
|
|
||||||
m_thread = std::thread([this] () {
|
m_thread = std::thread([this] () {
|
||||||
ServerApplication app;
|
ServerApplication app{*m_eventHandler};
|
||||||
app.setSingleplayer(true);
|
app.setSingleplayer(true);
|
||||||
app.setPort(m_port);
|
app.setPort(m_port);
|
||||||
app.run();
|
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) {
|
void TitleScreenState::startMultiplayer(const std::string &host) {
|
||||||
@ -118,7 +123,13 @@ void TitleScreenState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
|
|||||||
m_menuWidget.onGuiScaleChanged(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 {
|
void TitleScreenState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
||||||
|
if (m_isServerOnline) return;
|
||||||
|
|
||||||
prepareDraw(target, states);
|
prepareDraw(target, states);
|
||||||
|
|
||||||
target.draw(m_background, states);
|
target.draw(m_background, states);
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
#include "MenuWidget.hpp"
|
#include "MenuWidget.hpp"
|
||||||
|
|
||||||
struct GuiScaleChangedEvent;
|
struct GuiScaleChangedEvent;
|
||||||
|
struct ServerOnlineEvent;
|
||||||
|
|
||||||
class TitleScreenState : public InterfaceState {
|
class TitleScreenState : public InterfaceState {
|
||||||
public:
|
public:
|
||||||
@ -52,6 +53,7 @@ class TitleScreenState : public InterfaceState {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void onGuiScaleChanged(const GuiScaleChangedEvent &event);
|
void onGuiScaleChanged(const GuiScaleChangedEvent &event);
|
||||||
|
void onServerOnlineEvent(const ServerOnlineEvent &event);
|
||||||
|
|
||||||
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
|
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
|
||||||
|
|
||||||
@ -62,6 +64,9 @@ class TitleScreenState : public InterfaceState {
|
|||||||
std::thread m_thread;
|
std::thread m_thread;
|
||||||
|
|
||||||
u16 m_port = 4242;
|
u16 m_port = 4242;
|
||||||
|
|
||||||
|
bool m_showLoadingState = false;
|
||||||
|
bool m_isServerOnline = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TITLESCREENSTATE_HPP_
|
#endif // TITLESCREENSTATE_HPP_
|
||||||
|
@ -33,11 +33,17 @@
|
|||||||
namespace fs = ghc::filesystem;
|
namespace fs = ghc::filesystem;
|
||||||
|
|
||||||
ServerApplication::ServerApplication(int argc, char **argv) : m_argumentParser(argc, argv) {
|
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() {
|
void ServerApplication::init() {
|
||||||
|
std::srand(std::time(nullptr));
|
||||||
|
|
||||||
|
BlockGeometry::initOrientation();
|
||||||
|
|
||||||
m_argumentParser.addArgument("port", {"-p", "--port", true});
|
m_argumentParser.addArgument("port", {"-p", "--port", true});
|
||||||
m_argumentParser.addArgument("working_dir", {"-w", "--working-dir", true});
|
m_argumentParser.addArgument("working_dir", {"-w", "--working-dir", true});
|
||||||
|
|
||||||
@ -68,6 +74,9 @@ void ServerApplication::init() {
|
|||||||
m_scriptEngine.luaCore().setRegistry(&m_registry);
|
m_scriptEngine.luaCore().setRegistry(&m_registry);
|
||||||
|
|
||||||
std::cout << "Server is running on localhost:" << m_port << std::endl;
|
std::cout << "Server is running on localhost:" << m_port << std::endl;
|
||||||
|
|
||||||
|
if (m_eventHandler)
|
||||||
|
m_eventHandler->emplaceEvent<ServerOnlineEvent>(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ServerApplication::run(bool isProtected) {
|
int ServerApplication::run(bool isProtected) {
|
||||||
@ -77,6 +86,9 @@ int ServerApplication::run(bool isProtected) {
|
|||||||
mainLoop();
|
mainLoop();
|
||||||
}
|
}
|
||||||
catch(const gk::Exception &e) {
|
catch(const gk::Exception &e) {
|
||||||
|
if (m_eventHandler)
|
||||||
|
m_eventHandler->emplaceEvent<ServerOnlineEvent>(false);
|
||||||
|
|
||||||
std::cerr << "Fatal error " << e.what() << std::endl;
|
std::cerr << "Fatal error " << e.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -92,7 +104,7 @@ int ServerApplication::run(bool isProtected) {
|
|||||||
void ServerApplication::update() {
|
void ServerApplication::update() {
|
||||||
m_worldController.update();
|
m_worldController.update();
|
||||||
|
|
||||||
if (gk::GameClock::getTicks() % 100 < 10) {
|
if (m_clock.getTicks() % 100 < 10) {
|
||||||
for (auto &it : m_players) {
|
for (auto &it : m_players) {
|
||||||
m_serverCommandHandler.sendPlayerPosUpdate(it.first);
|
m_serverCommandHandler.sendPlayerPosUpdate(it.first);
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
#include <gk/core/ArgumentParser.hpp>
|
#include <gk/core/ArgumentParser.hpp>
|
||||||
#include <gk/core/GameClock.hpp>
|
#include <gk/core/GameClock.hpp>
|
||||||
|
#include <gk/core/EventHandler.hpp>
|
||||||
|
|
||||||
#include "LuaCore.hpp"
|
#include "LuaCore.hpp"
|
||||||
#include "Registry.hpp"
|
#include "Registry.hpp"
|
||||||
@ -38,9 +39,14 @@
|
|||||||
#include "ServerPlayer.hpp"
|
#include "ServerPlayer.hpp"
|
||||||
#include "WorldController.hpp"
|
#include "WorldController.hpp"
|
||||||
|
|
||||||
|
struct ServerOnlineEvent {
|
||||||
|
bool isOnline;
|
||||||
|
};
|
||||||
|
|
||||||
class ServerApplication {
|
class ServerApplication {
|
||||||
public:
|
public:
|
||||||
ServerApplication(int argc = 0, char **argv = nullptr);
|
ServerApplication(int argc = 0, char **argv = nullptr);
|
||||||
|
ServerApplication(gk::EventHandler &eventHandler);
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
@ -57,6 +63,7 @@ class ServerApplication {
|
|||||||
|
|
||||||
gk::ArgumentParser m_argumentParser;
|
gk::ArgumentParser m_argumentParser;
|
||||||
gk::GameClock m_clock;
|
gk::GameClock m_clock;
|
||||||
|
gk::EventHandler *m_eventHandler = nullptr;
|
||||||
|
|
||||||
ScriptEngine m_scriptEngine;
|
ScriptEngine m_scriptEngine;
|
||||||
|
|
||||||
@ -64,7 +71,7 @@ class ServerApplication {
|
|||||||
|
|
||||||
u16 m_port = 4242;
|
u16 m_port = 4242;
|
||||||
|
|
||||||
WorldController m_worldController{m_registry};
|
WorldController m_worldController{m_registry, m_clock};
|
||||||
std::unordered_map<u16, ServerPlayer> m_players;
|
std::unordered_map<u16, ServerPlayer> m_players;
|
||||||
|
|
||||||
Server m_server;
|
Server m_server;
|
||||||
|
@ -34,8 +34,8 @@
|
|||||||
#include "ServerWorld.hpp"
|
#include "ServerWorld.hpp"
|
||||||
|
|
||||||
void ServerWorld::update() {
|
void ServerWorld::update() {
|
||||||
if (m_lastTick < gk::GameClock::getTicks() / 50) {
|
if (m_lastTick < m_clock.getTicks() / 50) {
|
||||||
m_lastTick = gk::GameClock::getTicks() / 50;
|
m_lastTick = m_clock.getTicks() / 50;
|
||||||
|
|
||||||
for (auto &it : m_chunks) {
|
for (auto &it : m_chunks) {
|
||||||
it.second->tick(*this, *m_server);
|
it.second->tick(*this, *m_server);
|
||||||
|
@ -33,6 +33,10 @@
|
|||||||
#include "TerrainGenerator.hpp"
|
#include "TerrainGenerator.hpp"
|
||||||
#include "World.hpp"
|
#include "World.hpp"
|
||||||
|
|
||||||
|
namespace gk {
|
||||||
|
class GameClock;
|
||||||
|
}
|
||||||
|
|
||||||
class ClientInfo;
|
class ClientInfo;
|
||||||
class Dimension;
|
class Dimension;
|
||||||
class ServerCommandHandler;
|
class ServerCommandHandler;
|
||||||
@ -42,8 +46,8 @@ class ServerWorld : public World {
|
|||||||
using ChunkMap = std::unordered_map<gk::Vector3i, std::unique_ptr<ServerChunk>>;
|
using ChunkMap = std::unordered_map<gk::Vector3i, std::unique_ptr<ServerChunk>>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ServerWorld(const Dimension &dimension)
|
ServerWorld(const Dimension &dimension, gk::GameClock &clock)
|
||||||
: m_dimension(dimension), m_terrainGenerator(dimension) {}
|
: m_dimension(dimension), m_terrainGenerator(dimension), m_clock(clock) {}
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
@ -73,6 +77,8 @@ class ServerWorld : public World {
|
|||||||
TerrainGenerator m_terrainGenerator;
|
TerrainGenerator m_terrainGenerator;
|
||||||
|
|
||||||
ServerCommandHandler *m_server = nullptr;
|
ServerCommandHandler *m_server = nullptr;
|
||||||
|
|
||||||
|
gk::GameClock &m_clock;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SERVERWORLD_HPP_
|
#endif // SERVERWORLD_HPP_
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
|
|
||||||
void WorldController::init() {
|
void WorldController::init() {
|
||||||
for (const Dimension &dimension : m_registry.dimensions()) {
|
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);
|
m_worldList.back().setServer(m_server);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,11 +31,15 @@
|
|||||||
|
|
||||||
#include "ServerWorld.hpp"
|
#include "ServerWorld.hpp"
|
||||||
|
|
||||||
|
namespace gk {
|
||||||
|
class GameClock;
|
||||||
|
}
|
||||||
|
|
||||||
class Registry;
|
class Registry;
|
||||||
|
|
||||||
class WorldController {
|
class WorldController {
|
||||||
public:
|
public:
|
||||||
WorldController(Registry ®istry) : m_registry(registry) {}
|
WorldController(Registry ®istry, gk::GameClock &clock) : m_registry(registry), m_clock(clock) {}
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
@ -50,6 +54,8 @@ class WorldController {
|
|||||||
|
|
||||||
Registry &m_registry;
|
Registry &m_registry;
|
||||||
|
|
||||||
|
gk::GameClock &m_clock;
|
||||||
|
|
||||||
ServerCommandHandler *m_server = nullptr;
|
ServerCommandHandler *m_server = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user