#include "client/Game.h" #include "client/GameClient.h" #include "client/gui/StartGUI.h" #include "client/rendering/Engine.h" #include "common/network/Packets.h" #include "rendering/renderer/WorldRenderer.h" #include "utils/Logger.h" #include "utils/Utils.h" BlockRegistry Game::blockRegistry; World Game::world{blockRegistry}; static WorldRenderer worldRenderer{Game::world}; Controller Game::controller; typedef void (*State)(); static State tickState; static State renderState; static BaseGUI baseGUI; static StartGUI startGUI; static Vector3 lastPos; static Vector3 pos; static Quaternion lastRotation; static Quaternion rotation; static void tickConnectedState() { GameClient::consumeEvents(); } static void renderConnectedState() { } static void tickConnectState() { startGUI.tick(); StartGUI::Address a; if(startGUI.getAddress(a)) { Error error = GameClient::connect(a, 11196, 3000); if(error.has()) { LOG_INFO(error.message); } else { LOG_INFO("connected"); tickState = tickConnectedState; renderState = renderConnectedState; } } } static void renderConnectState() { startGUI.render(); } bool Game::init() { if(worldRenderer.init()) { return true; } controller.init(); tickState = tickConnectState; renderState = renderConnectState; pos = Vector3(0.0f, 30.0f, 0.0f); rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation; rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation; return false; } void Game::tick() { tickState(); lastRotation = rotation; lastPos = pos; Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f); Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f); Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f); const float speed = 2.0f; if(controller.down.isDown()) { pos += back * speed; } if(controller.up.isDown()) { pos -= back * speed; } if(controller.left.isDown()) { pos -= right * speed; } if(controller.right.isDown()) { pos += right * speed; } if(controller.jump.isDown()) { pos += up * speed; } if(controller.sneak.isDown()) { pos -= up * speed; } const float rotationSpeed = 5.0f; if(controller.camLeft.isDown()) { rotation = Quaternion(up, -rotationSpeed) * rotation; } if(controller.camRight.isDown()) { rotation = Quaternion(up, rotationSpeed) * rotation; } if(controller.camUp.isDown()) { rotation = Quaternion(right, -rotationSpeed) * rotation; } if(controller.camDown.isDown()) { rotation = Quaternion(right, rotationSpeed) * rotation; } } void Game::renderWorld() { Engine::matrix.update(Utils::interpolate(lastPos, pos, Engine::lag), lastRotation.lerp(Engine::lag, rotation)); worldRenderer.render(); } void Game::renderOverlay() { renderState(); Engine::matrix.identity().scale(2.0f).update(); StringBuffer<100> s; s.append("FPS: &074") .append(Engine::getFrameClock().getUpdatesPerSecond()) .append(" &999TPS: &722") .append(Engine::getTickClock().getUpdatesPerSecond()); Engine::renderer.renderString(Vector2(10.0f, 10.0f), s); }