Block.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <Textur.h>
  2. #include <Shader.h>
  3. #include "Block.h"
  4. #include "Globals.h"
  5. #include "CustomDX11API.h"
  6. Block::Block(const BlockType* zType, Framework::Vec3<int> pos, Model3DData* model, Model3DTextur* texture, int maxHP)
  7. : Model3D(),
  8. zType(zType),
  9. location(pos),
  10. maxHP((float)maxHP)
  11. {
  12. transparent = 0;
  13. hp = (float)maxHP;
  14. memset(sideVisible, 1, 6);
  15. Model3D::setPosition((Framework::Vec3<float>)pos + Framework::Vec3<float>{0.5f, 0.5f, 0.5f});
  16. setModelDaten(model);
  17. setModelTextur(texture);
  18. breakTextur = uiFactory.initParam.bildschirm->zGraphicsApi()->createOrGetTextur("blocks.ltdb/crack.png", 0);
  19. memset(lightData, 0, 6 * 6);
  20. }
  21. Block::~Block()
  22. {
  23. breakTextur->release();
  24. }
  25. void Block::beforeRender(GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  26. {
  27. CustomDX11API* cApi = dynamic_cast<CustomDX11API*>(api);
  28. if (cApi && model->getVertexAnzahl() == 24)
  29. {
  30. int count = 24;
  31. zVertexShader->füllConstBuffer((char*)&count, cApi->getVertexShaderLightBufferIndex(), 4);
  32. zVertexShader->füllConstBuffer(lightBuffer, cApi->getVertexShaderLightBufferIndex() + 1, 24 * 4);
  33. }
  34. }
  35. void Block::afterRender(GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  36. {
  37. CustomDX11API* cApi = dynamic_cast<CustomDX11API*>(api);
  38. if (cApi && model->getVertexAnzahl() == 24)
  39. {
  40. int count = 0;
  41. zVertexShader->füllConstBuffer((char*)&count, cApi->getVertexShaderLightBufferIndex(), 4);
  42. }
  43. }
  44. void Block::api(char* message)
  45. {
  46. // TODO: implement api
  47. switch (message[0])
  48. {
  49. case 0: // hp change
  50. hp = *(float*)(message + 1);
  51. break;
  52. }
  53. }
  54. bool Block::isTransparent() const
  55. {
  56. return transparent;
  57. }
  58. void Block::setSideVisible(Direction dir, bool visible)
  59. {
  60. sideVisible[getDirectionIndex(dir)] = visible;
  61. }
  62. void Block::setLightData(Direction dir, unsigned char* data)
  63. {
  64. memcpy(lightData + getDirectionIndex(dir) * 6, data, 6);
  65. if (model->getVertexAnzahl() == 24)
  66. {
  67. float dayLightFactor = 1.0; // TODO: day (1.f) - night (0.1f) adjustment
  68. const Vertex3D* verticies = model->zVertexBuffer();
  69. for (int i = 0; i < 24; i++)
  70. { // TODO: calculate rotations
  71. Directions dirs = getDirectionsFromVector(verticies[i].pos);
  72. Vec3<unsigned char> light(0, 0, 0);
  73. for (int d = 0; d < 6; d++)
  74. {
  75. if ((dirs | getDirectionFromIndex(d)) == dirs)
  76. {
  77. light = Vec3<unsigned char>(max((unsigned char)((float)lightData[d * 6] * dayLightFactor), light.x), max((unsigned char)((float)lightData[d * 6 + 1] * dayLightFactor), light.y), max((unsigned char)((float)lightData[d * 6 + 2] * dayLightFactor), light.z));
  78. light = Vec3<float>(max(lightData[d * 6 + 3], light.x), max(lightData[d * 6 + 4], light.y), max(lightData[d * 6 + 5], light.z));
  79. }
  80. }
  81. int l = ((((light.x << 8) | light.y) << 8) | light.z) << 8;
  82. *(int*)(lightBuffer + i * 4) = l;
  83. }
  84. }
  85. }
  86. Vec3<int> Block::getLocation() const
  87. {
  88. return location;
  89. }
  90. const BlockType* Block::zBlockType() const
  91. {
  92. return zType;
  93. }
  94. Textur* Block::zEffectTextur()
  95. {
  96. if (hp < maxHP)
  97. return breakTextur;
  98. return 0;
  99. }
  100. float Block::getEffectPercentage()
  101. {
  102. return 1 - hp / maxHP;
  103. }
  104. Text Block::printLightInfo()
  105. {
  106. Text result = "NORTH[0;-1;0](";
  107. result += (int)lightData[0];
  108. result += ",";
  109. result += (int)lightData[1];
  110. result += ",";
  111. result += (int)lightData[2];
  112. result += ";";
  113. result += (int)lightData[3];
  114. result += ",";
  115. result += (int)lightData[4];
  116. result += ",";
  117. result += (int)lightData[5];
  118. result += ")\n";
  119. result += "EAST[1;0;0](";
  120. result += (int)lightData[6];
  121. result += ",";
  122. result += (int)lightData[7];
  123. result += ",";
  124. result += (int)lightData[8];
  125. result += ";";
  126. result += (int)lightData[9];
  127. result += ",";
  128. result += (int)lightData[10];
  129. result += ",";
  130. result += (int)lightData[11];
  131. result += ")\n";
  132. result += "SOUTH[0;1;0](";
  133. result += (int)lightData[12];
  134. result += ",";
  135. result += (int)lightData[13];
  136. result += ",";
  137. result += (int)lightData[14];
  138. result += ";";
  139. result += (int)lightData[15];
  140. result += ",";
  141. result += (int)lightData[16];
  142. result += ",";
  143. result += (int)lightData[17];
  144. result += ")\n";
  145. result += "WEST[-1;0;0](";
  146. result += (int)lightData[18];
  147. result += ",";
  148. result += (int)lightData[19];
  149. result += ",";
  150. result += (int)lightData[20];
  151. result += ";";
  152. result += (int)lightData[21];
  153. result += ",";
  154. result += (int)lightData[22];
  155. result += ",";
  156. result += (int)lightData[23];
  157. result += ")\n";
  158. result += "TOP[0;0;1](";
  159. result += (int)lightData[24];
  160. result += ",";
  161. result += (int)lightData[25];
  162. result += ",";
  163. result += (int)lightData[26];
  164. result += ";";
  165. result += (int)lightData[27];
  166. result += ",";
  167. result += (int)lightData[28];
  168. result += ",";
  169. result += (int)lightData[29];
  170. result += ")\n";
  171. result += "BOTTOM[0;0;-1](";
  172. result += (int)lightData[30];
  173. result += ",";
  174. result += (int)lightData[31];
  175. result += ",";
  176. result += (int)lightData[32];
  177. result += ";";
  178. result += (int)lightData[33];
  179. result += ",";
  180. result += (int)lightData[34];
  181. result += ",";
  182. result += (int)lightData[35];
  183. result += ")\n";
  184. return result;
  185. }