Block.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "Block.h"
  2. #include "Inventory.h"
  3. #include "Globals.h"
  4. Block::Block(const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos, bool hasInventory)
  5. : Inventory(pos, hasInventory),
  6. Model3D(),
  7. zType(zType)
  8. {
  9. transparent = false;
  10. passable = false;
  11. hp = 1;
  12. maxHP = 1;
  13. hardness = 1;
  14. this->zTool = zTool;
  15. speedModifier = 1;
  16. memset(sideVisible, 1, 6);
  17. Model3D::setPosition((Framework::Vec3<float>)pos + Framework::Vec3<float>{0.5f, 0.5f, 0.5f});
  18. }
  19. Block::~Block()
  20. {}
  21. void Block::api(char* message)
  22. {
  23. // TODO: implement api
  24. }
  25. bool Block::isTransparent() const
  26. {
  27. return transparent;
  28. }
  29. bool Block::isPassable() const
  30. {
  31. return passable;
  32. }
  33. void Block::setSideVisible(Direction dir, bool visible)
  34. {
  35. sideVisible[getDirectionIndex(dir)] = visible;
  36. }
  37. const BlockType* Block::zBlockType() const
  38. {
  39. return zType;
  40. }
  41. BasicBlockItem::BasicBlockItem(const ItemType* zType, const char* name)
  42. : Item(zType, name)
  43. {
  44. placeable = 1;
  45. transparent = 0;
  46. passable = 0;
  47. hp = 0;
  48. maxHP = 0;
  49. hardness = 0;
  50. toolId = 0;
  51. speedModifier = 0;
  52. interactable = 0;
  53. }
  54. BasicBlockItemType::BasicBlockItemType(int id, const char* name, const char* texturPath)
  55. : ItemType(id, name),
  56. name(name),
  57. texturPath(texturPath)
  58. {}
  59. void BasicBlockItemType::loadSuperItem(Item* zItem, Framework::StreamReader* zReader) const
  60. {
  61. ItemType::loadSuperItem(zItem, zReader);
  62. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  63. if (!item)
  64. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  65. zReader->lese((char*)&item->transparent, 1);
  66. zReader->lese((char*)&item->passable, 1);
  67. zReader->lese((char*)&item->hp, 4);
  68. zReader->lese((char*)&item->maxHP, 4);
  69. zReader->lese((char*)&item->hardness, 4);
  70. zReader->lese((char*)&item->toolId, 4);
  71. zReader->lese((char*)&item->speedModifier, 4);
  72. zReader->lese((char*)&item->interactable, 1);
  73. }
  74. Framework::Model3DData* BasicBlockItemType::getItemModel() const
  75. {
  76. return uiFactory.initParam.bildschirm->zGraphicsApi()->getModel("itemCube");
  77. }
  78. Item* BasicBlockItemType::createItem() const
  79. {
  80. return new BasicBlockItem(this, name);
  81. }
  82. Framework::Model3DTextur* BasicBlockItemType::getItemTextur() const
  83. {
  84. Model3DTextur* textur = new Model3DTextur();
  85. Textur* tex = uiFactory.initParam.bildschirm->zGraphicsApi()->createOrGetTextur(texturPath, 0);
  86. textur->setPolygonTextur(0, dynamic_cast<Textur*>(tex->getThis()));
  87. textur->setPolygonTextur(1, dynamic_cast<Textur*>(tex->getThis()));
  88. textur->setPolygonTextur(2, dynamic_cast<Textur*>(tex->getThis()));
  89. textur->setPolygonTextur(3, dynamic_cast<Textur*>(tex->getThis()));
  90. textur->setPolygonTextur(4, dynamic_cast<Textur*>(tex->getThis()));
  91. textur->setPolygonTextur(5, tex);
  92. return textur;
  93. }