123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #include "Client.h"
- #include <iostream>
- #include "../block/Blocks.h"
- #include "../client/rendering/block/BlockRenderers.h"
- #include "rendering/entity/EntityRenderer.h"
- #include <cmath>
- using namespace std;
- Client::Client() : world(&chunkProvider)
- {
- BlockRegistry::registerBlocks();
- BlockRenderers::init();
- position.set(0, -9, 3);
- lengthAngle = 180;
- widthAngle = 0;
-
- camera.setPosition(position.getX(), position.getY(), position.getZ(), 0, 0);
- camera.storePosition();
-
- keyManager.map(KEY_LEFT, GLFW_KEY_A);
- keyManager.map(KEY_RIGHT, GLFW_KEY_D);
- keyManager.map(KEY_UP, GLFW_KEY_W);
- keyManager.map(KEY_DOWN, GLFW_KEY_S);
- keyManager.map(KEY_JUMP, GLFW_KEY_SPACE);
- keyManager.map(KEY_SNEAK, GLFW_KEY_LEFT_SHIFT);
-
- keyManager.map(KEY_TEST, GLFW_KEY_T);
-
- mouseManager.map(MOUSE_LEFT, GLFW_MOUSE_BUTTON_1);
-
- world.registerChunkListener(&chunkRenderer);
- world.updateDirtyChunks();
-
- Engine::setMouseTrapped(mouseTrapped);
- }
- Client::~Client()
- {
- }
- void Client::tick()
- {
- if(keyManager.isReleased(KEY_TEST))
- {
- mouseTrapped = !mouseTrapped;
- Engine::setMouseTrapped(mouseTrapped);
- }
-
- tps.update();
-
- camera.storePosition();
-
- float factor = 0.05f;
- if(keyManager.isDown(KEY_LEFT))
- {
- position.addMul(camera.getFlatLeft(), factor);
- }
- if(keyManager.isDown(KEY_RIGHT))
- {
- position.addMul(camera.getFlatRight(), factor);
- }
- if(keyManager.isDown(KEY_UP))
- {
- position.addMul(camera.getFlatFront(), factor);
- }
- if(keyManager.isDown(KEY_DOWN))
- {
- position.addMul(camera.getFlatBack(), factor);
- }
- if(keyManager.isDown(KEY_JUMP))
- {
- position.addMul(camera.getFlatUp(), factor);
- }
- if(keyManager.isDown(KEY_SNEAK))
- {
- position.addMul(camera.getFlatDown(), factor);
- }
-
- widthAngle += diffMouseY;
- if(widthAngle < -89)
- {
- widthAngle = -89;
- }
- else if(widthAngle > 89)
- {
- widthAngle = 89;
- }
- diffMouseY = 0;
-
- lengthAngle += diffMouseX;
- if(lengthAngle < 0)
- {
- lengthAngle += 360;
- camera.addToOldLengthAngle(360);
- }
- else if(lengthAngle > 360)
- {
- lengthAngle -= 360;
- camera.addToOldLengthAngle(-360);
- }
- diffMouseX = 0;
-
- camera.setPosition(position.getX(), position.getY(), position.getZ(), lengthAngle, widthAngle);
-
- entity.tick();
-
- mouseManager.tick();
- keyManager.tick();
- }
- void Client::render3DTick(float lag)
- {
- fps.update();
-
- camera.update(lag);
- Engine::setWorldViewMatrix(camera.getViewMatrix());
-
- shader.setToIdentity();
- Engine::setWorldModelMatrix(shader.getModelMatrix());
- chunkRenderer.renderTick(shader, camera, directRenderer, lag);
-
- entity.renderTick(shader, camera, directRenderer, lag);
- }
- void Client::render2DTick(float lag)
- {
- shader.setToIdentity();
- Engine::setOverlayModelMatrix(shader.getModelMatrix());
-
- string wusi;
- wusi = "FPS: " + to_string(fps.getUpdatesPerSecond());
- float y = directRenderer.drawString(10, 10, true, wusi);
- wusi = "TPS: " + to_string(tps.getUpdatesPerSecond());
- directRenderer.drawString(10, y, true, wusi);
- }
- void Client::onKeyEvent(int key, int scancode, int action, int mods)
- {
- if(action == GLFW_PRESS)
- {
- keyManager.press(key);
- }
- else if(action == GLFW_RELEASE)
- {
- keyManager.release(key);
- }
- }
- double Client::transformMouse(double x)
- {
- if(x >= -MOUSE_BORDER && x <= MOUSE_BORDER)
- {
- return x * x * x;
- }
- else if(x >= MOUSE_BORDER)
- {
- const double k = (3 * MOUSE_BORDER * MOUSE_BORDER);
- const double y = MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER;
- const double d = y - k * MOUSE_BORDER;
- return k * x + d;
- }
- else
- {
- const double k = (3 * MOUSE_BORDER * MOUSE_BORDER);
- const double y = -MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER;
- const double d = y - k * MOUSE_BORDER;
- return k * x + d;
- }
- }
- void Client::onMouseMove(double x, double y)
- {
- if(mouseTrapped)
- {
- diffMouseX = transformMouse(oldMouseX - x) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3));
- diffMouseY = transformMouse(oldMouseY - y) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3));
- oldMouseX = x;
- oldMouseY = y;
- }
- }
- void Client::onMouseClick(int button, int action, int mods)
- {
- if(action == GLFW_PRESS)
- {
- mouseManager.press(button);
- }
- else if(action == GLFW_RELEASE)
- {
- mouseManager.release(button);
- }
- }
|