BasicBlocks.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "BasicBlocks.h"
  2. #include <Model3D.h>
  3. #include <Globals.h>
  4. #include <GraphicsApi.h>
  5. #include <Bild.h>
  6. #include <Model3DList.h>
  7. #include "Globals.h"
  8. BasicBlock::BasicBlock( const BlockType* zType, const char* texture, ItemType* zTool, Framework::Vec3<int> pos, Framework::Textur* t )
  9. : Block( zType, zTool, pos, false )
  10. {
  11. Model3DData* data = 0;
  12. if( window->zBildschirm()->zGraphicsApi()->hasModel( "cube" ) )
  13. data = window->zBildschirm()->zGraphicsApi()->getModel( "cube" );
  14. else
  15. {
  16. data = window->zBildschirm()->zGraphicsApi()->createModel( "cube" );
  17. data->setAmbientFactor( 0.8f );
  18. data->setDiffusFactor( 0.1f );
  19. data->setSpecularFactor( 0.1f );
  20. float size = 1;
  21. float left, right, top, bottom;
  22. // Calculate the screen coordinates of the left side of the bitmap.
  23. left = (float)((size / 2.0) * -1);
  24. // Calculate the screen coordinates of the right side of the bitmap.
  25. right = left + (float)size;
  26. // Calculate the screen coordinates of the top of the bitmap.
  27. top = (float)(size / 2.0);
  28. // Calculate the screen coordinates of the bottom of the bitmap.
  29. bottom = top - (float)size;
  30. float front = -size / 2;
  31. float back = front + size;
  32. Vertex3D* vertecies = new Vertex3D[ 24 ];
  33. for( int i = 0; i < 24; i++ )
  34. vertecies[ i ].knochenId = 0;
  35. vertecies[ 0 ].pos = Vec3<float >( left, top, front );
  36. vertecies[ 0 ].tPos = Vec2< float >( 0.f, 0.f );
  37. vertecies[ 1 ].pos = Vec3<float >( right, top, front );
  38. vertecies[ 1 ].tPos = Vec2< float >( 1.f, 0.f );
  39. vertecies[ 2 ].pos = Vec3<float >( left, bottom, front );
  40. vertecies[ 2 ].tPos = Vec2< float >( 0.f, 1.f );
  41. vertecies[ 3 ].pos = Vec3<float >( right, bottom, front );
  42. vertecies[ 3 ].tPos = Vec2< float >( 1.f, 1.f );
  43. vertecies[ 4 ].pos = Vec3<float >( left, top, back );
  44. vertecies[ 4 ].tPos = Vec2< float >( 0.0f, 0.0f );
  45. vertecies[ 5 ].pos = Vec3<float >( right, top, back );
  46. vertecies[ 5 ].tPos = Vec2< float >( 1.0f, 0.0f );
  47. vertecies[ 6 ].pos = Vec3<float >( left, bottom, back );
  48. vertecies[ 6 ].tPos = Vec2< float >( 0.0f, 1.0f );
  49. vertecies[ 7 ].pos = Vec3<float >( right, bottom, back );
  50. vertecies[ 7 ].tPos = Vec2< float >( 1.0f, 1.0f );
  51. vertecies[ 8 ].pos = Vec3<float >( left, top, front );
  52. vertecies[ 8 ].tPos = Vec2< float >( 1.f, 0.f );
  53. vertecies[ 9 ].pos = Vec3<float >( right, top, front );
  54. vertecies[ 9 ].tPos = Vec2< float >( 0.f, 0.f );
  55. vertecies[ 10 ].pos = Vec3<float >( left, bottom, front );
  56. vertecies[ 10 ].tPos = Vec2< float >( 1.f, 1.f );
  57. vertecies[ 11 ].pos = Vec3<float >( right, bottom, front );
  58. vertecies[ 11 ].tPos = Vec2< float >( 0.f, 1.f );
  59. vertecies[ 12 ].pos = Vec3<float >( left, top, back );
  60. vertecies[ 12 ].tPos = Vec2< float >( 0.0f, 0.0f );
  61. vertecies[ 13 ].pos = Vec3<float >( right, top, back );
  62. vertecies[ 13 ].tPos = Vec2< float >( 1.0f, 0.0f );
  63. vertecies[ 14 ].pos = Vec3<float >( left, bottom, back );
  64. vertecies[ 14 ].tPos = Vec2< float >( 0.0f, 1.0f );
  65. vertecies[ 15 ].pos = Vec3<float >( right, bottom, back );
  66. vertecies[ 15 ].tPos = Vec2< float >( 1.0f, 1.0f );
  67. vertecies[ 16 ].pos = Vec3<float >( left, top, front );
  68. vertecies[ 16 ].tPos = Vec2< float >( 0.f, 1.f );
  69. vertecies[ 17 ].pos = Vec3<float >( right, top, front );
  70. vertecies[ 17 ].tPos = Vec2< float >( 1.f, 1.f );
  71. vertecies[ 18 ].pos = Vec3<float >( left, bottom, front );
  72. vertecies[ 18 ].tPos = Vec2< float >( 0.f, 0.f );
  73. vertecies[ 19 ].pos = Vec3<float >( right, bottom, front );
  74. vertecies[ 19 ].tPos = Vec2< float >( 1.f, 0.f );
  75. vertecies[ 20 ].pos = Vec3<float >( left, top, back );
  76. vertecies[ 20 ].tPos = Vec2< float >( 0.0f, 0.0f );
  77. vertecies[ 21 ].pos = Vec3<float >( right, top, back );
  78. vertecies[ 21 ].tPos = Vec2< float >( 1.0f, 0.0f );
  79. vertecies[ 22 ].pos = Vec3<float >( left, bottom, back );
  80. vertecies[ 22 ].tPos = Vec2< float >( 0.0f, 1.0f );
  81. vertecies[ 23 ].pos = Vec3<float >( right, bottom, back );
  82. vertecies[ 23 ].tPos = Vec2< float >( 1.0f, 1.0f );
  83. data->setVertecies( vertecies, 24 );
  84. // the order of the polygons has to be NORTH, EAST, SOUTH, WEST, TOP, BOTTOM according to the Area definition
  85. // down side
  86. Polygon3D* p = new Polygon3D();
  87. p->indexAnz = 6;
  88. p->indexList = new int[ p->indexAnz ];
  89. p->indexList[ 0 ] = 6 + 16;
  90. p->indexList[ 1 ] = 2 + 16;
  91. p->indexList[ 2 ] = 3 + 16;
  92. p->indexList[ 3 ] = 6 + 16;
  93. p->indexList[ 4 ] = 3 + 16;
  94. p->indexList[ 5 ] = 7 + 16;
  95. data->addPolygon( p );
  96. // right side
  97. p = new Polygon3D();
  98. p->indexAnz = 6;
  99. p->indexList = new int[ p->indexAnz ];
  100. p->indexList[ 0 ] = 1 + 8;
  101. p->indexList[ 1 ] = 7 + 8;
  102. p->indexList[ 2 ] = 3 + 8;
  103. p->indexList[ 3 ] = 1 + 8;
  104. p->indexList[ 4 ] = 5 + 8;
  105. p->indexList[ 5 ] = 7 + 8;
  106. data->addPolygon( p );
  107. // top side
  108. p = new Polygon3D();
  109. p->indexAnz = 6;
  110. p->indexList = new int[ p->indexAnz ];
  111. p->indexList[ 0 ] = 4 + 16;
  112. p->indexList[ 1 ] = 1 + 16;
  113. p->indexList[ 2 ] = 0 + 16;
  114. p->indexList[ 3 ] = 4 + 16;
  115. p->indexList[ 4 ] = 5 + 16;
  116. p->indexList[ 5 ] = 1 + 16;
  117. data->addPolygon( p );
  118. // left side
  119. p = new Polygon3D();
  120. p->indexAnz = 6;
  121. p->indexList = new int[ p->indexAnz ];
  122. p->indexList[ 0 ] = 0 + 8;
  123. p->indexList[ 1 ] = 2 + 8;
  124. p->indexList[ 2 ] = 6 + 8;
  125. p->indexList[ 3 ] = 0 + 8;
  126. p->indexList[ 4 ] = 6 + 8;
  127. p->indexList[ 5 ] = 4 + 8;
  128. data->addPolygon( p );
  129. // back side
  130. p = new Polygon3D();
  131. p->indexAnz = 6;
  132. p->indexList = new int[ p->indexAnz ];
  133. p->indexList[ 0 ] = 4;
  134. p->indexList[ 1 ] = 6;
  135. p->indexList[ 2 ] = 7;
  136. p->indexList[ 3 ] = 4;
  137. p->indexList[ 4 ] = 7;
  138. p->indexList[ 5 ] = 5;
  139. data->addPolygon( p );
  140. // front side
  141. p = new Polygon3D();
  142. p->indexAnz = 6;
  143. p->indexList = new int[ p->indexAnz ];
  144. p->indexList[ 0 ] = 0;
  145. p->indexList[ 1 ] = 3;
  146. p->indexList[ 2 ] = 2;
  147. p->indexList[ 3 ] = 0;
  148. p->indexList[ 4 ] = 1;
  149. p->indexList[ 5 ] = 3;
  150. data->addPolygon( p );
  151. data->calculateNormals();
  152. }
  153. setModelDaten( data );
  154. Bild* b = new Bild();
  155. b->neuBild( 10, 10, 0xFFFFFFFF );
  156. Textur* tex = currentGame->zScreen()->zGraphicsApi()->createOrGetTextur( texture, 0 );
  157. if( !tex->zBild() )
  158. tex->setBildZ( b );
  159. else
  160. b->release();
  161. Model3DTextur* textur = new Model3DTextur();
  162. textur->setPolygonTextur( 0, dynamic_cast<Textur*>(tex->getThis()) );
  163. textur->setPolygonTextur( 1, dynamic_cast<Textur*>(tex->getThis()) );
  164. textur->setPolygonTextur( 2, dynamic_cast<Textur*>(tex->getThis()) );
  165. textur->setPolygonTextur( 3, dynamic_cast<Textur*>(tex->getThis()) );
  166. textur->setPolygonTextur( 4, dynamic_cast<Textur*>(tex->getThis()) );
  167. textur->setPolygonTextur( 5, tex );
  168. setModelTextur( textur );
  169. breakTextur = currentGame->zScreen()->zGraphicsApi()->createOrGetTextur( "blocks.ltdb/crack", 0 );
  170. }
  171. BasicBlock::~BasicBlock()
  172. {
  173. breakTextur->release();
  174. }
  175. bool BasicBlock::needRenderPolygon( int index )
  176. {
  177. return 1;
  178. }
  179. Textur* BasicBlock::zEffectTextur()
  180. {
  181. if( hp < maxHP )
  182. return breakTextur;
  183. return 0;
  184. }
  185. float BasicBlock::getEffectPercentage()
  186. {
  187. return 1 - hp / maxHP;
  188. }
  189. DirtBlockType::DirtBlockType()
  190. : BlockType( ID )
  191. {}
  192. void DirtBlockType::loadSuperBlock( Block* zBlock, Framework::StreamReader* zReader )
  193. {
  194. BlockType::loadSuperBlock( zBlock, zReader );
  195. }
  196. Block* DirtBlockType::createBlock( Framework::Vec3<int> position )
  197. {
  198. Block* b = new BasicBlock( DirtBlockType::INSTANCE, "blocks.ltdb/dirt", 0, position, 0 ); // TODO: add efective tool
  199. initializeSuperBlock( b );
  200. return b;
  201. }
  202. void DirtBlockType::initializeSuperBlock( Block* zBlock )
  203. {
  204. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  205. if( !block )
  206. throw "DirtBlockType::createSuperBlock was called with a block witch is not an instance of BasicBlock";
  207. block->transparent = 0;
  208. block->passable = 0;
  209. block->hp = 100;
  210. block->maxHP = 100;
  211. block->hardness = 1;
  212. block->zTool = 0;
  213. block->speedModifier = 1;
  214. }
  215. bool DirtBlockType::needsInstance() const
  216. {
  217. return 1;
  218. }
  219. DirtBlockItemType::DirtBlockItemType()
  220. : BasicBlockItemType( ID )
  221. {}
  222. Item* DirtBlockItemType::createItem() const
  223. {
  224. BasicBlockItem* item = new BasicBlockItem( (ItemType*)this, "Dirt" );
  225. return item;
  226. }