diff --git a/include/block.h b/include/block.h new file mode 100644 index 0000000..10f1261 --- /dev/null +++ b/include/block.h @@ -0,0 +1,71 @@ +#pragma once +#include +#include + +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; +}; + diff --git a/src/block.cpp b/src/block.cpp new file mode 100644 index 0000000..da32264 --- /dev/null +++ b/src/block.cpp @@ -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; +}