BasicBlocks.cpp 8.8 KB

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