Block.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, 0, 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::setLightData(Direction dir, unsigned char* data)
  59. {
  60. memcpy(lightData + getDirectionIndex(dir) * 6, data, 6);
  61. if (model->getVertexAnzahl() == 24)
  62. {
  63. float dayLightFactor = 1.0; // TODO: day (1.f) - night (0.1f) adjustment
  64. const Vertex3D* verticies = model->zVertexBuffer();
  65. for (int i = 0; i < 24; i++)
  66. { // TODO: calculate rotations
  67. Directions dirs = getDirectionsFromVector(verticies[i].pos);
  68. Vec3<unsigned char> light(0, 0, 0);
  69. for (int d = 0; d < 6; d++)
  70. {
  71. if ((dirs | getDirectionFromIndex(d)) == dirs)
  72. {
  73. 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));
  74. light = Vec3<float>(max(lightData[d * 6 + 3], light.x), max(lightData[d * 6 + 4], light.y), max(lightData[d * 6 + 5], light.z));
  75. }
  76. }
  77. int l = ((((light.x << 8) | light.y) << 8) | light.z) << 8;
  78. *(int*)(lightBuffer + i * 4) = l;
  79. }
  80. }
  81. if (data[0] | data[1] | data[2] | data[3] | data[4] | data[5])
  82. sideVisible[getDirectionIndex(dir)] = 1;
  83. else
  84. sideVisible[getDirectionIndex(dir)] = 0;
  85. }
  86. bool Block::isVisible() const
  87. {
  88. return sideVisible[0] || sideVisible[1] || sideVisible[2] || sideVisible[3] || sideVisible[4] || sideVisible[5];
  89. }
  90. Vec3<int> Block::getLocation() const
  91. {
  92. return location;
  93. }
  94. const BlockType* Block::zBlockType() const
  95. {
  96. return zType;
  97. }
  98. Textur* Block::zEffectTextur()
  99. {
  100. if (hp < maxHP)
  101. return breakTextur;
  102. return 0;
  103. }
  104. float Block::getEffectPercentage()
  105. {
  106. return 1 - hp / maxHP;
  107. }
  108. Text Block::printLightInfo()
  109. {
  110. Text result = "NORTH[0;-1;0](";
  111. result += (int)lightData[0];
  112. result += ",";
  113. result += (int)lightData[1];
  114. result += ",";
  115. result += (int)lightData[2];
  116. result += ";";
  117. result += (int)lightData[3];
  118. result += ",";
  119. result += (int)lightData[4];
  120. result += ",";
  121. result += (int)lightData[5];
  122. result += ")\n";
  123. result += "EAST[1;0;0](";
  124. result += (int)lightData[6];
  125. result += ",";
  126. result += (int)lightData[7];
  127. result += ",";
  128. result += (int)lightData[8];
  129. result += ";";
  130. result += (int)lightData[9];
  131. result += ",";
  132. result += (int)lightData[10];
  133. result += ",";
  134. result += (int)lightData[11];
  135. result += ")\n";
  136. result += "SOUTH[0;1;0](";
  137. result += (int)lightData[12];
  138. result += ",";
  139. result += (int)lightData[13];
  140. result += ",";
  141. result += (int)lightData[14];
  142. result += ";";
  143. result += (int)lightData[15];
  144. result += ",";
  145. result += (int)lightData[16];
  146. result += ",";
  147. result += (int)lightData[17];
  148. result += ")\n";
  149. result += "WEST[-1;0;0](";
  150. result += (int)lightData[18];
  151. result += ",";
  152. result += (int)lightData[19];
  153. result += ",";
  154. result += (int)lightData[20];
  155. result += ";";
  156. result += (int)lightData[21];
  157. result += ",";
  158. result += (int)lightData[22];
  159. result += ",";
  160. result += (int)lightData[23];
  161. result += ")\n";
  162. result += "TOP[0;0;1](";
  163. result += (int)lightData[24];
  164. result += ",";
  165. result += (int)lightData[25];
  166. result += ",";
  167. result += (int)lightData[26];
  168. result += ";";
  169. result += (int)lightData[27];
  170. result += ",";
  171. result += (int)lightData[28];
  172. result += ",";
  173. result += (int)lightData[29];
  174. result += ")\n";
  175. result += "BOTTOM[0;0;-1](";
  176. result += (int)lightData[30];
  177. result += ",";
  178. result += (int)lightData[31];
  179. result += ",";
  180. result += (int)lightData[32];
  181. result += ";";
  182. result += (int)lightData[33];
  183. result += ",";
  184. result += (int)lightData[34];
  185. result += ",";
  186. result += (int)lightData[35];
  187. result += ")\n";
  188. return result;
  189. }