Added block obj

This commit is contained in:
Gorbunov 2024-05-15 12:54:06 +03:00
parent 87cb04f0f8
commit 1521d9f0b3
2 changed files with 165 additions and 0 deletions

71
include/block.h Normal file
View File

@ -0,0 +1,71 @@
#pragma once
#include <glm/vec2.hpp>
#include <iostream>
enum BLOCKS
{
air = 0,
grass,
dirt,
stone,
ice,
BLOCKS_COUNT
};
class Block
{
public:
Block() {};
Block(int type):type(type){};
Block(Block &other) { this->type = other.type; }
uint8_t &getType() { return type; }
uint8_t getType() const { return type; }
bool isTransparent() const
{
return !isOpaque();
}
bool isTranslucent() const
{
if(type == ice)
{
return true;
}
return false;
}
bool isOpaque() const
{
if(type == air || type == ice)
{
return false;
}
return true;
}
bool isAir() const
{
return type == air;
}
const char *getBlockName() const;
glm::ivec2 getPositionInAtlas(int face);
friend std::ostream &operator<<(std::ostream &os, const Block &block);
Block & operator=(const Block &other)
{
this->type = other.type;
return *this;
}
private:
unsigned char type = 0;
};

94
src/block.cpp Normal file
View File

@ -0,0 +1,94 @@
#include "block.h"
glm::ivec2 frontFaces[BLOCKS_COUNT] =
{
{0,0},
{3, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 backFaces[BLOCKS_COUNT] =
{
{0,0},
{3, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 topFaces[BLOCKS_COUNT] =
{
{0,0},
{0, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 bottomFaces[BLOCKS_COUNT] =
{
{0,0},
{2, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 leftFaces[BLOCKS_COUNT] =
{
{0,0},
{3, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 rightFaces[BLOCKS_COUNT] =
{
{0,0},
{3, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
static glm::ivec2 *faces[6] =
{
frontFaces, backFaces, topFaces, bottomFaces, leftFaces, rightFaces
};
glm::ivec2 Block::getPositionInAtlas(int face)
{
return faces[face][type];
}
static const char *blockNames[BLOCKS_COUNT + 1]
{
"air",
"grass block",
"dirt block",
"stone block",
"ice"
""
};
const char *Block::getBlockName() const
{
return blockNames[type];
}
std::ostream &operator<<(std::ostream &os, const Block &block)
{
os << block.getBlockName();
return os;
}