123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #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;
- Player Game::player;
- typedef void (*State)();
- static State tickState;
- static State renderState;
- static BaseGUI baseGUI;
- static StartGUI startGUI;
- static void tickConnectedState() {
- GameClient::consumeEvents();
- Game::world.tick();
- }
- 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;
- player.setPosition(Vector3(0.0f, 30.0f, 0.0f));
- world.addPlayer(&player);
- return false;
- }
- void Game::tick() {
- tickState();
- Quaternion q = player.getRotation();
- Vector3 up(0.0f, 1.0f, 0.0f);
- Vector3 back = q * Vector3(0.0f, 0.0f, -1.0f);
- back[1] = 0.0f;
- back.normalize();
- Vector3 right = back.cross(up);
- constexpr float speed = 0.1f;
- if(controller.down.isDown()) {
- player.addForce(back * speed);
- }
- if(controller.up.isDown()) {
- player.addForce(back * -speed);
- }
- if(controller.left.isDown()) {
- player.addForce(right * -speed);
- }
- if(controller.right.isDown()) {
- player.addForce(right * speed);
- }
- if(controller.jump.isDown() && player.isOnGround()) {
- player.addForce(up * 0.5f);
- }
- if(controller.sneak.isDown()) {
- player.addForce(up * -speed);
- }
- constexpr float rotationSpeed = 4.0f;
- if(controller.camLeft.isDown()) {
- player.addLengthAngle(-rotationSpeed);
- }
- if(controller.camRight.isDown()) {
- player.addLengthAngle(rotationSpeed);
- }
- if(controller.camUp.isDown()) {
- player.addWidthAngle(-rotationSpeed * 0.5f);
- }
- if(controller.camDown.isDown()) {
- player.addWidthAngle(rotationSpeed * 0.5f);
- }
- }
- void Game::renderWorld() {
- Engine::matrix.update(player.getRenderPosition(Engine::lag),
- player.getRenderRotation(Engine::lag));
- 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);
- }
|