diff --git a/lua/file.lua b/lua/file.lua index 5831185b..11517a4c 100644 --- a/lua/file.lua +++ b/lua/file.lua @@ -2,6 +2,12 @@ -- Register basic blocks -- +-- Air +zeus.register_block("_:air", { + name = "Air", + textures = {"_missing"} +}) + -- Grass zeus.register_block("default:grass", { name = "Grass", diff --git a/zeus/blocks/BlockAtlas.cpp b/zeus/blocks/BlockAtlas.cpp index f2ac1787..91bfb172 100644 --- a/zeus/blocks/BlockAtlas.cpp +++ b/zeus/blocks/BlockAtlas.cpp @@ -6,7 +6,6 @@ BlockAtlas::BlockAtlas(TextureAtlas *textureAtlas) { this->textureAtlas = textureAtlas; - definitions.push_back(nullptr); //Air } void BlockAtlas::registerBlock(BlockDef* def) { diff --git a/zeus/blocks/BlockDef.cpp b/zeus/blocks/BlockDef.cpp index 5dd3dd15..461ca709 100644 --- a/zeus/blocks/BlockDef.cpp +++ b/zeus/blocks/BlockDef.cpp @@ -1,3 +1,5 @@ +#include + // // Created by aurailus on 02/12/18. // @@ -9,10 +11,14 @@ BlockModel *BlockDef::getModel() { } BlockDef::BlockDef(std::string identifier, BlockModel *model) { - this->identifier = identifier; + this->identifier = std::move(identifier); this->model = model; } BlockDef::~BlockDef() { delete this->model; } + +std::string BlockDef::getIdentifier() { + return identifier; +} diff --git a/zeus/blocks/BlockDef.h b/zeus/blocks/BlockDef.h index 056c0b6e..452e854f 100644 --- a/zeus/blocks/BlockDef.h +++ b/zeus/blocks/BlockDef.h @@ -13,6 +13,7 @@ public: BlockDef(std::string identifier, BlockModel* model); BlockModel* getModel(); + std::string getIdentifier(); ~BlockDef(); private: diff --git a/zeus/engine/Camera.cpp b/zeus/engine/Camera.cpp index 3678849e..bbb32697 100644 --- a/zeus/engine/Camera.cpp +++ b/zeus/engine/Camera.cpp @@ -93,4 +93,8 @@ glm::vec3 *Camera::getPosition() { return &position; } +void Camera::setPosition(glm::vec3 pos) { + position = pos; +} + Camera::~Camera() = default; diff --git a/zeus/engine/Camera.h b/zeus/engine/Camera.h index 6aa20267..61f02e1e 100644 --- a/zeus/engine/Camera.h +++ b/zeus/engine/Camera.h @@ -19,6 +19,7 @@ public: void mouseControl(double deltaX, double deltaY); glm::vec3* getPosition(); + void setPosition(glm::vec3 pos); glm::mat4 calculateViewMatrix(); diff --git a/zeus/mesh/TextBuilder.cpp b/zeus/mesh/TextBuilder.cpp index 25b05594..ee411b94 100644 --- a/zeus/mesh/TextBuilder.cpp +++ b/zeus/mesh/TextBuilder.cpp @@ -11,31 +11,40 @@ Mesh* TextBuilder::build(std::string text) { float texPosWidth = 1/128.0f * (float)w; float texPosHeight = 1/54.0f * (float)h; - for (unsigned int i = 0; i < text.length(); i++) { - char letter = text[i]; + float left = 0, top = 0; + unsigned int indOffset = 0; + + for (char letter : text) { + if (letter == '\n') { + top += h; + left = 0; + continue; + } int baseIndex = (int)letter - 32; - float left = i; //Cast to float so the vector doesn't error - float texPosX = baseIndex%18 / 18.0f; //18 = Characters in row float texPosY = baseIndex/18 / 6.0f; //6 = Number of columns auto letterVerts = std::vector { - left * w, 0, 0, texPosX, texPosY, 0, 0, 0, - left * w + w, 0, 0, texPosX + texPosWidth, texPosY, 0, 0, 0, - left * w + w, h, 0, texPosX + texPosWidth, texPosY + texPosHeight, 0, 0, 0, - left * w, h, 0, texPosX, texPosY + texPosHeight, 0, 0, 0, + left, top, 0, texPosX, texPosY, 0, 0, 0, + left + w, top, 0, texPosX + texPosWidth, texPosY, 0, 0, 0, + left + w, top + h, 0, texPosX + texPosWidth, texPosY + texPosHeight, 0, 0, 0, + left, top + h, 0, texPosX, texPosY + texPosHeight, 0, 0, 0, }; for (float f : letterVerts) textVertices->push_back(f); - textIndices->push_back(i*4); - textIndices->push_back(3 + i*4); - textIndices->push_back(1 + i*4); - textIndices->push_back(3 + i*4); - textIndices->push_back(2 + i*4); - textIndices->push_back(1 + i*4); + textIndices->push_back( indOffset); + textIndices->push_back(3 + indOffset); + textIndices->push_back(1 + indOffset); + textIndices->push_back(3 + indOffset); + textIndices->push_back(2 + indOffset); + textIndices->push_back(1 + indOffset); + + indOffset += 4; + + left += w; } Mesh* m = new Mesh(); diff --git a/zeus/world/GameInstance.cpp b/zeus/world/GameInstance.cpp index 95ee4411..e9cd46b8 100644 --- a/zeus/world/GameInstance.cpp +++ b/zeus/world/GameInstance.cpp @@ -26,20 +26,24 @@ void GameInstance::initialize(Renderer* renderer) { //The world requires the blockAtlas for meshing and handling inputs. world = new World(blockAtlas); - int SIZE = 24; - for (int i = -SIZE; i < SIZE; i++) { - for (int j = 0; j < 3; j++) { - for (int k = -SIZE; k < SIZE; k++) { - world->genNewChunk(glm::vec3(i, j, k)); - } - } - } + renderer->getCamera()->setPosition(glm::vec3(8, 24, 8)); + +// int SIZE = 24; +// for (int i = -SIZE; i < SIZE; i++) { +// for (int j = 0; j < 3; j++) { +// for (int k = -SIZE; k < SIZE; k++) { +// world->genNewChunk(glm::vec3(i, j, k)); +// } +// } +// } + + world->genNewChunk(glm::vec3(0, 0, 0)); fontTexture = Texture((char*)"../tex/font.png"); fontTexture.load(); alphaText = new HudText(); - alphaText->set(std::string("Zeus ALPHA")); + alphaText->set("Zeus Alpha"); alphaText->setScale(2.5); alphaText->setPosition(glm::vec3(8, 4, 0)); guiEntities.push_back(alphaText); @@ -48,6 +52,11 @@ void GameInstance::initialize(Renderer* renderer) { fpsText->setScale(2); fpsText->setPosition(glm::vec3(8, 32, 0)); guiEntities.push_back(fpsText); + + blockText = new HudText(); + blockText->setScale(2); + blockText->setPosition(glm::vec3(8, 52, 0)); + guiEntities.push_back(blockText); } void GameInstance::update(GLfloat deltaTime) { @@ -59,6 +68,27 @@ void GameInstance::update(GLfloat deltaTime) { camera->keyControl(window->getKeysArray(), deltaTime); camera->mouseControl(window->getDeltaX(), window->getDeltaY()); + glm::vec3 round = World::roundVec(*camera->getPosition()); + round.y -= 2; + glm::vec3 chunk = World::chunkVec(round); + glm::vec3 local = World::localVec(round); + + int block = world->getBlock(round); + std::string name = "Null"; + + if (block >= 0) { + name = blockAtlas->getBlock(block)->getIdentifier(); + } + + if (block < 1) { + camera->getPosition()->y -= 0.1; + } + + blockText->set( + "World Pos: (" + to_string((int)round.x) + ", " + to_string((int)round.y) + ", " + to_string((int)round.z) + ")\n" + + "Chunk Pos: (" + to_string((int)chunk.x) + ", " + to_string((int)chunk.y) + ", " + to_string((int)chunk.z) + ")\n" + + "Local Pos: (" + to_string((int)local.x) + ", " + to_string((int)local.y) + ", " + to_string((int)local.z) + ")\n" + + "Block: " + name); world->update(); } diff --git a/zeus/world/GameInstance.h b/zeus/world/GameInstance.h index 522f58d0..a99d8794 100644 --- a/zeus/world/GameInstance.h +++ b/zeus/world/GameInstance.h @@ -46,6 +46,7 @@ public: Texture fontTexture; HudText* alphaText; + HudText* blockText; }; diff --git a/zeus/world/World.cpp b/zeus/world/World.cpp index 110303e7..cc5b0186 100644 --- a/zeus/world/World.cpp +++ b/zeus/world/World.cpp @@ -46,6 +46,28 @@ void World::update() { // world.printElapsedMs(); } +int World::getBlock(glm::vec3 pos) { +// glm::vec3 chunkPos(round(pos.x / 16), round(pos.y / 16), round(pos.z / 16)); +// return 0; + + auto chunkPos = World::chunkVec(World::roundVec(pos)); + auto local = World::localVec(World::roundVec(pos)); + + auto chunk = getChunk(chunkPos); + if (chunk != nullptr) { + return chunk->getBlock(&local); + } + + return -1; +} + +BlockChunk* World::getChunk(glm::vec3 chunkPos) { + if (blockChunks.count(chunkPos) == 1) { + return blockChunks[chunkPos]; + } + return nullptr; +} + void World::handleChunkGenQueue() { for (auto threadDef : genThreads) { std::unique_lock lock(threadDef->lock, std::defer_lock); diff --git a/zeus/world/World.h b/zeus/world/World.h index 2196f9db..ef658acb 100644 --- a/zeus/world/World.h +++ b/zeus/world/World.h @@ -44,6 +44,39 @@ public: void update(); std::unordered_map* getMeshChunks(); + + BlockChunk* getChunk(glm::vec3 chunkPos); + + int getBlock(glm::vec3 pos); + + static glm::vec3 roundVec(glm::vec3 vec) { + return glm::vec3(floor(vec.x), floor(vec.y), floor(vec.z)); + } + + static glm::vec3 chunkVec(glm::vec3 globalVec) { + return glm::vec3(floor(globalVec.x / 16), floor(globalVec.y / 16), floor(globalVec.z / 16)); + } + + static glm::vec3 localVec(glm::vec3 globalVec) { + glm::vec3 out; + + if (globalVec.x < 0) + out.x = 15 + (((int)globalVec.x + 1) % CHUNK_SIZE); + else + out.x = ((int)globalVec.x) % CHUNK_SIZE; + + if (globalVec.y < 0) + out.y = 15 + (((int)globalVec.y + 1) % CHUNK_SIZE); + else + out.y = ((int)globalVec.y) % CHUNK_SIZE; + + if (globalVec.z < 0) + out.z = 15 + (((int)globalVec.z + 1) % CHUNK_SIZE); + else + out.z = ((int)globalVec.z) % CHUNK_SIZE; + + return out; + } private: //Global lists for storing blockChunks and meshChunks std::unordered_map blockChunks;