123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #include "client/Game.h"
- #include "common/network/Packets.h"
- #include "utils/Utils.h"
- Game::Game(TextInput*& textInput, const Controller& controller,
- const Clock& fps, const Clock& tps, RenderSettings& settings,
- const Size& size, Client& client)
- : controller(controller), fps(fps), tps(tps), renderSettings(settings),
- size(size), client(client), tickState(&Game::tickConnectState),
- renderState(&Game::renderConnectState),
- baseGUI(size, textInput, controller), startGUI(baseGUI),
- world(blockRegistry), worldRenderer(world) {
- 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;
- }
- void Game::tick() {
- (this->*tickState)();
- 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, ShaderMatrix& sm) {
- sm.update(Utils::interpolate(lastPos, pos, lag),
- lastRotation.lerp(lag, rotation));
- worldRenderer.render(lag, sm);
- }
- void Game::renderOverlay(float lag, ShaderMatrix& sm, Renderer& r) {
- (this->*renderState)(lag, sm, r);
- sm.identity().scale(2.0f).update();
- StringBuffer<100> s;
- s.append("FPS: &074")
- .append(fps.getUpdatesPerSecond())
- .append(" &999TPS: &722")
- .append(tps.getUpdatesPerSecond());
- r.renderString(Vector2(10.0f, 10.0f), 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;
- }
- case ServerPacket::WORLD:
- {
- std::cout << "World data:\n";
- for(int x = 0; x < world.getSize(); x++) {
- for(int y = 0; y < world.getHeight(); y++) {
- for(int z = 0; z < world.getSize(); z++) {
- BlockId id;
- if(in.readU16(id)) {
- std::cout << "WELL\n";
- return;
- }
- world.setBlock(x, y, z, blockRegistry.getBlock(id));
- }
- }
- }
- world.dirty = true;
- break;
- }
- }
- }
- void Game::tickConnectState() {
- startGUI.tick();
- StartGUI::Address a;
- if(startGUI.getAddress(a)) {
- if(client.connect(a, 11196, 3000)) {
- std::cout << client.getError() << '\n';
- } else {
- std::cout << "connected\n";
- tickState = &Game::tickConnectedState;
- renderState = &Game::renderConnectedState;
- }
- }
- }
- void Game::tickConnectedState() {
- client.consumeEvents(*this);
- }
- void Game::renderConnectState(float lag, ShaderMatrix& sm, Renderer& r) {
- startGUI.render(lag, sm, r);
- }
- void Game::renderConnectedState(float lag, ShaderMatrix& sm, Renderer& r) {
- (void)lag;
- (void)sm;
- (void)r;
- }
|