123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #include "client/Game.h"
- #include "common/network/Packets.h"
- #include "gaming-core/utils/Utils.h"
- Game::Game(TextInput& textInput, const Controller& controller, const Clock& fps,
- const Clock& tps, RenderSettings& settings, const Size& size,
- Client& client)
- : textInput(textInput), controller(controller), fps(fps), tps(tps),
- renderSettings(settings), size(size), client(client),
- world(blockRegistry), worldRenderer(world), connected(false) {
- pos = Vector3(16.0f, 30.0f, -10.0f);
- rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
- rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
- textInput.setActive(true);
- }
- void Game::tick() {
- if(!connected && controller.down.wasReleased()) {
- if(client.connect("127.0.0.1", 11196, 3000)) {
- std::cout << client.getError() << '\n';
- } else {
- std::cout << "connected\n";
- }
- } else {
- client.consumeEvents(*this);
- }
- lastRotation = rotation;
- lastPos = pos;
- Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f);
- Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
- Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
- const float speed = 2.0f;
- if(controller.down.isDown()) {
- pos += back * speed;
- }
- if(controller.up.isDown()) {
- pos -= back * speed;
- }
- if(controller.left.isDown()) {
- pos -= right * speed;
- }
- if(controller.right.isDown()) {
- pos += right * speed;
- }
- if(controller.jump.isDown()) {
- pos += up * speed;
- }
- if(controller.sneak.isDown()) {
- pos -= up * speed;
- }
- const float rotationSpeed = 5.0f;
- if(controller.camLeft.isDown()) {
- rotation = Quaternion(up, -rotationSpeed) * rotation;
- }
- if(controller.camRight.isDown()) {
- rotation = Quaternion(up, rotationSpeed) * rotation;
- }
- if(controller.camUp.isDown()) {
- rotation = Quaternion(right, -rotationSpeed) * rotation;
- }
- if(controller.camDown.isDown()) {
- rotation = Quaternion(right, rotationSpeed) * rotation;
- }
- }
- void Game::renderWorld(float lag, Renderer& renderer) {
- renderer.update(Utils::interpolate(lastPos, pos, lag),
- lastRotation.lerp(lag, rotation));
- worldRenderer.render(lag, renderer);
- }
- void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) {
- (void)lag;
- renderer.scale(2.0f).update();
- StringBuffer<100> s;
- s.append("FPS: &074")
- .append(fps.getUpdatesPerSecond())
- .append(" &999TPS: &722")
- .append(tps.getUpdatesPerSecond());
- fr.drawString(10, 10, s);
- s.clear();
- textInput.getInput(s);
- fr.drawString(10, 30, s);
- }
- bool Game::isRunning() const {
- return true;
- }
- void Game::onConnect() {
- }
- void Game::onDisconnect() {
- }
- void Game::onPacket(InPacket& in) {
- uint16 id;
- if(in.readU16(id)) {
- return;
- }
- switch(id) {
- case ServerPacket::CHAT:
- StringBuffer<256> s;
- in.readString(s);
- std::cout << s << '\n';
- break;
- }
- }
|