123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include <iostream>
- #include <cmath>
- #include "client/GameClient.h"
- #include "common/block/Blocks.h"
- #include "client/rendering/block/BlockRenderers.h"
- #include "client/rendering/entity/EntityRenderer.h"
- #include "client/rendering/gui/StartMenu.h"
- using namespace std;
- GameClient::GameClient()
- {
- 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();
- }
- GameClient::~GameClient()
- {
- if(activeGUI != nullptr)
- {
- delete activeGUI;
- }
- }
- void GameClient::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 GameClient::render3DTick(float lag)
- {
- fps.update();
- }
- void GameClient::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 GameClient::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 GameClient::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 GameClient::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 GameClient::onMouseClick(int button, int action, int mods)
- {
- if(action == GLFW_PRESS)
- {
- mouseManager.press(button);
- }
- else if(action == GLFW_RELEASE)
- {
- mouseManager.release(button);
- }
- }
|