World methods to get player position and blocks at global pos.

- TextBuilder supports newlines
master
aurailus 2018-12-26 23:59:00 -08:00
parent 68edae3fcb
commit 6406a0350f
11 changed files with 137 additions and 25 deletions

View File

@ -2,6 +2,12 @@
-- Register basic blocks
--
-- Air
zeus.register_block("_:air", {
name = "Air",
textures = {"_missing"}
})
-- Grass
zeus.register_block("default:grass", {
name = "Grass",

View File

@ -6,7 +6,6 @@
BlockAtlas::BlockAtlas(TextureAtlas *textureAtlas) {
this->textureAtlas = textureAtlas;
definitions.push_back(nullptr); //Air
}
void BlockAtlas::registerBlock(BlockDef* def) {

View File

@ -1,3 +1,5 @@
#include <utility>
//
// 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;
}

View File

@ -13,6 +13,7 @@ public:
BlockDef(std::string identifier, BlockModel* model);
BlockModel* getModel();
std::string getIdentifier();
~BlockDef();
private:

View File

@ -93,4 +93,8 @@ glm::vec3 *Camera::getPosition() {
return &position;
}
void Camera::setPosition(glm::vec3 pos) {
position = pos;
}
Camera::~Camera() = default;

View File

@ -19,6 +19,7 @@ public:
void mouseControl(double deltaX, double deltaY);
glm::vec3* getPosition();
void setPosition(glm::vec3 pos);
glm::mat4 calculateViewMatrix();

View File

@ -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<float> {
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();

View File

@ -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();
}

View File

@ -46,6 +46,7 @@ public:
Texture fontTexture;
HudText* alphaText;
HudText* blockText;
};

View File

@ -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<std::mutex> lock(threadDef->lock, std::defer_lock);

View File

@ -44,6 +44,39 @@ public:
void update();
std::unordered_map<glm::vec3, MeshChunk*, vec3cmp>* 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<glm::vec3, BlockChunk*, vec3cmp> blockChunks;