summaryrefslogtreecommitdiff
path: root/src/world.cpp
blob: d11e4bcdd7b78b52ca9fb14bf26af7a88227b648 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "world.hpp"

namespace app {

World::World(xe::GameObject& viewer, int renderDistance, int worldSeed) 
  : viewer{viewer}, 
    renderDistance{renderDistance},
    worldSeed{worldSeed},
    skinnedRenderer{Chunk::getTextures()} {
  reloadChunks(renderDistance);
}

World::~World() {}

void World::reloadChunks() {
  int currentViewX = static_cast<int>(floor(viewer.transform.translation.x / Chunk::CHUNK_SIZE.x));
  int currentViewZ = static_cast<int>(floor(viewer.transform.translation.z / Chunk::CHUNK_SIZE.z));
  if(currentViewX != viewX || currentViewZ != viewZ) {
    viewX = currentViewX;
    viewZ = currentViewZ;
    unloadOldChunks();
    loadNewChunks();
  }
  updateChunkMeshs();
}

void World::reloadChunks(int newRenderDistance) {
  renderDistance = newRenderDistance;
  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));
  resetChunks();
  loadNewChunks();
  updateChunkMeshs();
}

void World::resetChunks() {
  unloadOldChunks();
  loadedChunks.clear();
  int width = 2*renderDistance+1;
  for(int i = 0; i < width*width; i++) {
    auto gameObject = xe::GameObject::createGameObject();
    loadedChunks.push_back(std::move(gameObject));
  }
}

void World::unloadOldChunks() {
  int minX = viewX - renderDistance;
  int minZ = viewZ - renderDistance;
  int maxX = viewX + renderDistance;
  int maxZ = viewZ + renderDistance;
  for(auto &object : loadedChunks) {
    int gridX = static_cast<int>(floor(object.transform.translation.x / Chunk::CHUNK_SIZE.x));
    int gridZ = static_cast<int>(floor(object.transform.translation.z / Chunk::CHUNK_SIZE.z));
    if(gridX < minX || gridZ < minZ || gridX > maxX || gridZ > maxZ){
        Chunk::deleteChunk(gridX, gridZ);
        object.model = nullptr;
    }
  }
}

void World::loadNewChunks() {
  int width = 2*renderDistance+1;
  int minX = viewX - renderDistance;
  int minZ = viewZ - renderDistance;
  int maxX = viewX + renderDistance;
  int maxZ = viewZ + renderDistance;
  int i = 0;
  for(auto &object : loadedChunks) {
    int gridX = minX + i % width;
    int gridZ = minZ + i / width;
    Chunk* chunk = Chunk::getChunk(gridX, gridZ);
    if(chunk == nullptr) {
      chunk = Chunk::newChunk(gridX, gridZ, worldSeed);
      Chunk::generateAsync(chunk);
    }
    object.transform.translation = glm::vec3(gridX * Chunk::CHUNK_SIZE.x, 0, gridZ * Chunk::CHUNK_SIZE.z);
    i++;
  }
}

void World::updateChunkMeshs() {
  for(auto &object : loadedChunks) {
    int gridX = static_cast<int>(floor(object.transform.translation.x / Chunk::CHUNK_SIZE.x));
    int gridZ = static_cast<int>(floor(object.transform.translation.z / Chunk::CHUNK_SIZE.z));
    Chunk* chunk = Chunk::getChunk(gridX, gridZ);
    if(chunk == nullptr) continue;
    if(chunk->getMesh() == nullptr)
      Chunk::createMeshAsync(chunk);
    object.model = chunk->getMesh();
  }
}

void World::render(xe::Camera& camera) {
  camera.setViewYXZ(viewer.transform.translation, viewer.transform.rotation);
  // World::Ray ray = raycast(7, 100);
  skinnedRenderer.render(loadedChunks, camera);
}

World::Ray World::raycast(float distance, int steps) {

  glm::vec3 position = glm::vec3(viewer.transform.translation);

  float pitch = viewer.transform.rotation.x;
  float yaw = viewer.transform.rotation.y;
  float clamp = 1-fabs(sin(-pitch));
  const glm::vec3 step = glm::normalize(glm::vec3(sin(yaw)*clamp, sin(-pitch), cos(yaw)*clamp)) * (distance/steps);

  for(int i = 0; i < steps; i++) {
    position += step;
    int hit = Chunk::getGlobalBlock(position.x, position.y, position.z);
    if(hit == AIR) continue;
    return World::Ray{position, hit};
  }
  return World::Ray{position, INVALID};
}

}