123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #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();
- }
- 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.position = Vector3(0.0f, 30.0f, 0.0f);
- player.lastLengthAngle = 0.0f;
- player.lengthAngle = 0.0f;
- player.lastWidthAngle = 0.0f;
- player.widthAngle = 0.0f;
- world.addPlayer(&player);
- return false;
- }
- void Game::tick() {
- tickState();
- world.preTick();
- Quaternion q(Vector3(0.0f, 1.0f, 0.0f), player.lengthAngle);
- q *= Quaternion(Vector3(1.0f, 0.0f, 0.0f), player.widthAngle);
- Vector3 up(0.0f, 1.0f, 0.0f);
- Vector3 back = q * Vector3(0.0f, 0.0f, -1.0f);
- // back.setAngles(player.widthAngle, player.lengthAngle);
- // back[1] = 0.0f;
- // back.normalize();
- Vector3 right = back.cross(up);
- const float speed = 0.25f;
- if(controller.down.isDown()) {
- player.acceleration += back * speed;
- }
- if(controller.up.isDown()) {
- player.acceleration -= back * speed;
- }
- if(controller.left.isDown()) {
- player.acceleration -= right * speed;
- }
- if(controller.right.isDown()) {
- player.acceleration += right * speed;
- }
- if(controller.jump.isDown()) {
- player.acceleration += up * speed;
- }
- if(controller.sneak.isDown()) {
- player.acceleration -= up * speed;
- }
- const float rotationSpeed = 5.0f;
- if(controller.camLeft.isDown()) {
- player.lengthAngle -= rotationSpeed;
- }
- if(controller.camRight.isDown()) {
- player.lengthAngle += rotationSpeed;
- }
- if(controller.camUp.isDown() &&
- player.widthAngle - rotationSpeed > -90.0f) {
- player.widthAngle -= rotationSpeed;
- }
- if(controller.camDown.isDown() &&
- player.widthAngle + rotationSpeed < 90.0f) {
- player.widthAngle += rotationSpeed;
- }
- world.tick();
- }
- void Game::renderWorld() {
- Quaternion q(Vector3(0.0f, 1.0f, 0.0f),
- Utils::interpolate(player.lastLengthAngle, player.lengthAngle,
- Engine::lag));
- q *= Quaternion(Vector3(1.0f, 0.0f, 0.0f),
- Utils::interpolate(player.lastWidthAngle, player.widthAngle,
- Engine::lag));
- Engine::matrix.update(
- Utils::interpolate(player.lastPosition, player.position, Engine::lag),
- q);
- 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);
- }
|