summaryrefslogtreecommitdiff
path: root/src/minecraft.cpp
blob: a11421d5e070f78c0f2598f43cd141e73356ed16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "minecraft.hpp"

#include <chrono>
using namespace std::chrono;

namespace app {

Minecraft::Minecraft() : xeEngine{WIDTH, HEIGHT, "Minecraft Vulkan", "res/image/icon.png"} {};

Minecraft::~Minecraft() {}

void Minecraft::run() {

  Chunk::load();

  auto viewerObject = xe::GameObject::createGameObject();
  viewerObject.transform.translation = {0.f, 40.f, 0.f};
  viewerObject.transform.rotation.y = glm::radians(45.f);

  createGameObjects(viewerObject);

  ChunkRenderer renderer{Chunk::getTextures()};

  xe::Sound sound{"res/sound/when_the_world_ends.wav"};
  sound.setLooping(true);
  sound.play();
    
  PlayerController cameraController{xeEngine.getInput(), viewerObject};

  while (xeEngine.poll()) {

    float frameTime = xeEngine.getFrameTime();

    cameraController.update(frameTime);
    xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation);

    if(xeEngine.beginFrame()) {
      renderer.render(loadedChunks, xeEngine.getCamera());
      xeEngine.endFrame();
    }
    
    reloadLoadedChunks(viewerObject);

  }

  xeEngine.close();

  Chunk::unload();

}

void Minecraft::createGameObjects(xe::GameObject& viewer) {
  int width = 2*RENDER_DISTANCE+1;
  loadedChunks.clear();
  for(int32_t x = 0; x < width; x++) {
    for(int32_t z = 0; z < width; z++) {
      auto gameObject = xe::GameObject::createGameObject();
      gameObject.transform.translation = glm::vec3(0.f);
      loadedChunks.push_back(std::move(gameObject));
    }
  }
}

void Minecraft::reloadLoadedChunks(xe::GameObject& viewer) {
  viewX = static_cast<int>(floor(viewer.transform.translation.x / Chunk::CHUNK_SIZE.x));
  viewZ = static_cast<int>(floor(viewer.transform.translation.z / Chunk::CHUNK_SIZE.z));
  int width = 2*RENDER_DISTANCE+1;
  int minX = viewX - RENDER_DISTANCE;
  int minZ = viewZ - RENDER_DISTANCE;
  int maxX = viewX + RENDER_DISTANCE;
  int maxZ = viewZ + RENDER_DISTANCE;
  for(int32_t x = 0; x < width; x++) {
    for(int32_t z = 0; z < width; z++) {
      auto& gameObject = loadedChunks[x + z * width];
      int gridX = static_cast<int>(floor(gameObject.transform.translation.x / Chunk::CHUNK_SIZE.x));
      int gridZ = static_cast<int>(floor(gameObject.transform.translation.z / Chunk::CHUNK_SIZE.z));
      int newGridX = minX + x;
      int newGridZ = minZ + z;
      if(gridX < minX || gridZ < minZ || gridX > maxX || gridZ > maxZ)
        Chunk::deleteChunk(gridX, gridZ);
      Chunk* chunk = Chunk::getChunk(newGridX, newGridZ);
      if(chunk == nullptr) {
        chunk = Chunk::newChunk(newGridX, newGridZ, 12345);
        Chunk::generateAsync(chunk);
      }
      if(chunk->getMesh() == nullptr){
        Chunk::createMeshAsync(chunk);
      }
      gameObject.model = chunk->getMesh();
      gameObject.transform.translation = glm::vec3(newGridX * Chunk::CHUNK_SIZE.x, 0, newGridZ * Chunk::CHUNK_SIZE.z);
    }
  }
}

}