123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #include "Client.h"
- #include <iostream>
- #include "../block/Blocks.h"
- #include "../client/rendering/block/BlockRenderers.h"
- #include "rendering/entity/EntityRenderer.h"
- #include <cmath>
- #include "rendering/gui/StartMenu.h"
- using namespace std;
- Client::Client()
- {
- BlockRegistry::registerBlocks();
- BlockRenderers::init();
- 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);
-
- activeGUI = new StartMenu();
- }
- Client::~Client()
- {
- if(activeGUI != nullptr)
- {
- delete activeGUI;
- }
- }
- void Client::tick()
- {
- tps.update();
-
- diffMouseY = 0;
- diffMouseX = 0;
-
- mouseManager.tick();
- keyManager.tick();
-
- if(activeGUI != nullptr)
- {
- GUI* newGUI = activeGUI->tick(keyManager, mouseManager);
- if(newGUI != activeGUI)
- {
- delete activeGUI;
- activeGUI = newGUI;
- }
- }
- }
- void Client::render3DTick(float lag)
- {
- fps.update();
- }
- void Client::render2DTick(float lag)
- {
- if(activeGUI != nullptr)
- {
- activeGUI->render2DTick(shader, directRenderer, lag);
- }
-
- Engine::setMixMode();
- shader.setToIdentity();
- shader.updateModelMatrix();
-
- 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);
- }
- }
|