Block.cpp 5.4 KB

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