Block.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "Block.h"
  2. #include "Inventory.h"
  3. #include "Globals.h"
  4. Block::Block( const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos, bool hasInventory )
  5. : Inventory( pos, hasInventory ),
  6. Model3D(),
  7. zType( zType )
  8. {
  9. transparent = false;
  10. passable = false;
  11. hp = 1;
  12. maxHP = 1;
  13. hardness = 1;
  14. this->zTool = zTool;
  15. speedModifier = 1;
  16. memset( sideVisible, 1, 6 );
  17. Model3D::setPosition( (Framework::Vec3<float>)pos + Framework::Vec3<float>{0.5f, 0.5f, 0.5f} );
  18. }
  19. Block::~Block()
  20. {}
  21. bool Block::isTransparent() const
  22. {
  23. return transparent;
  24. }
  25. bool Block::isPassable() const
  26. {
  27. return passable;
  28. }
  29. void Block::setSideVisible( Direction dir, bool visible )
  30. {
  31. sideVisible[ getDirectionIndex( dir ) ] = visible;
  32. }
  33. const BlockType* Block::zBlockType() const
  34. {
  35. return zType;
  36. }
  37. BasicBlockItem::BasicBlockItem( const ItemType* zType, const char* name )
  38. : Item( zType, name )
  39. {
  40. placeable = 1;
  41. transparent = 0;
  42. passable = 0;
  43. hp = 0;
  44. maxHP = 0;
  45. hardness = 0;
  46. toolId = 0;
  47. speedModifier = 0;
  48. }
  49. BasicBlockItemType::BasicBlockItemType( int id )
  50. : ItemType( id )
  51. {}
  52. void BasicBlockItemType::loadSuperItem( Item* zItem, Framework::StreamReader* zReader ) const
  53. {
  54. ItemType::loadSuperItem( zItem, zReader );
  55. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  56. if( !item )
  57. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  58. zReader->lese( (char*)&item->transparent, 1 );
  59. zReader->lese( (char*)&item->passable, 1 );
  60. zReader->lese( (char*)&item->hp, 4 );
  61. zReader->lese( (char*)&item->maxHP, 4 );
  62. zReader->lese( (char*)&item->hardness, 4 );
  63. zReader->lese( (char*)&item->toolId, 4 );
  64. zReader->lese( (char*)&item->speedModifier, 4 );
  65. }
  66. Framework::Model3DData* BasicBlockItemType::getItemModel() const
  67. {
  68. if( window->zBildschirm()->zGraphicsApi()->hasModel( "itemCube" ) )
  69. return window->zBildschirm()->zGraphicsApi()->getModel( "itemCube" );
  70. else
  71. {
  72. Framework::Model3DData* data = window->zBildschirm()->zGraphicsApi()->createModel( "itemCube" );
  73. data->setAmbientFactor( 0.8f );
  74. data->setDiffusFactor( 0.1f );
  75. data->setSpecularFactor( 0.1f );
  76. float size = 0.2f;
  77. float left, right, top, bottom;
  78. // Calculate the screen coordinates of the left side of the bitmap.
  79. left = (float)((size / 2.0) * -1);
  80. // Calculate the screen coordinates of the right side of the bitmap.
  81. right = left + (float)size;
  82. // Calculate the screen coordinates of the top of the bitmap.
  83. top = (float)(size / 2.0);
  84. // Calculate the screen coordinates of the bottom of the bitmap.
  85. bottom = top - (float)size;
  86. float front = -size / 2;
  87. float back = front + size;
  88. Vertex3D* vertecies = new Vertex3D[ 24 ];
  89. for( int i = 0; i < 24; i++ )
  90. vertecies[ i ].knochenId = 0;
  91. vertecies[ 0 ].pos = Vec3<float >( left, top, front );
  92. vertecies[ 0 ].tPos = Vec2< float >( 0.f, 0.f );
  93. vertecies[ 1 ].pos = Vec3<float >( right, top, front );
  94. vertecies[ 1 ].tPos = Vec2< float >( 1.f, 0.f );
  95. vertecies[ 2 ].pos = Vec3<float >( left, bottom, front );
  96. vertecies[ 2 ].tPos = Vec2< float >( 0.f, 1.f );
  97. vertecies[ 3 ].pos = Vec3<float >( right, bottom, front );
  98. vertecies[ 3 ].tPos = Vec2< float >( 1.f, 1.f );
  99. vertecies[ 4 ].pos = Vec3<float >( left, top, back );
  100. vertecies[ 4 ].tPos = Vec2< float >( 0.0f, 0.0f );
  101. vertecies[ 5 ].pos = Vec3<float >( right, top, back );
  102. vertecies[ 5 ].tPos = Vec2< float >( 1.0f, 0.0f );
  103. vertecies[ 6 ].pos = Vec3<float >( left, bottom, back );
  104. vertecies[ 6 ].tPos = Vec2< float >( 0.0f, 1.0f );
  105. vertecies[ 7 ].pos = Vec3<float >( right, bottom, back );
  106. vertecies[ 7 ].tPos = Vec2< float >( 1.0f, 1.0f );
  107. vertecies[ 8 ].pos = Vec3<float >( left, top, front );
  108. vertecies[ 8 ].tPos = Vec2< float >( 1.f, 0.f );
  109. vertecies[ 9 ].pos = Vec3<float >( right, top, front );
  110. vertecies[ 9 ].tPos = Vec2< float >( 0.f, 0.f );
  111. vertecies[ 10 ].pos = Vec3<float >( left, bottom, front );
  112. vertecies[ 10 ].tPos = Vec2< float >( 1.f, 1.f );
  113. vertecies[ 11 ].pos = Vec3<float >( right, bottom, front );
  114. vertecies[ 11 ].tPos = Vec2< float >( 0.f, 1.f );
  115. vertecies[ 12 ].pos = Vec3<float >( left, top, back );
  116. vertecies[ 12 ].tPos = Vec2< float >( 0.0f, 0.0f );
  117. vertecies[ 13 ].pos = Vec3<float >( right, top, back );
  118. vertecies[ 13 ].tPos = Vec2< float >( 1.0f, 0.0f );
  119. vertecies[ 14 ].pos = Vec3<float >( left, bottom, back );
  120. vertecies[ 14 ].tPos = Vec2< float >( 0.0f, 1.0f );
  121. vertecies[ 15 ].pos = Vec3<float >( right, bottom, back );
  122. vertecies[ 15 ].tPos = Vec2< float >( 1.0f, 1.0f );
  123. vertecies[ 16 ].pos = Vec3<float >( left, top, front );
  124. vertecies[ 16 ].tPos = Vec2< float >( 0.f, 1.f );
  125. vertecies[ 17 ].pos = Vec3<float >( right, top, front );
  126. vertecies[ 17 ].tPos = Vec2< float >( 1.f, 1.f );
  127. vertecies[ 18 ].pos = Vec3<float >( left, bottom, front );
  128. vertecies[ 18 ].tPos = Vec2< float >( 0.f, 0.f );
  129. vertecies[ 19 ].pos = Vec3<float >( right, bottom, front );
  130. vertecies[ 19 ].tPos = Vec2< float >( 1.f, 0.f );
  131. vertecies[ 20 ].pos = Vec3<float >( left, top, back );
  132. vertecies[ 20 ].tPos = Vec2< float >( 0.0f, 0.0f );
  133. vertecies[ 21 ].pos = Vec3<float >( right, top, back );
  134. vertecies[ 21 ].tPos = Vec2< float >( 1.0f, 0.0f );
  135. vertecies[ 22 ].pos = Vec3<float >( left, bottom, back );
  136. vertecies[ 22 ].tPos = Vec2< float >( 0.0f, 1.0f );
  137. vertecies[ 23 ].pos = Vec3<float >( right, bottom, back );
  138. vertecies[ 23 ].tPos = Vec2< float >( 1.0f, 1.0f );
  139. data->setVertecies( vertecies, 24 );
  140. // the order of the polygons has to be NORTH, EAST, SOUTH, WEST, TOP, BOTTOM according to the Area definition
  141. // down side
  142. Polygon3D* p = new Polygon3D();
  143. p->indexAnz = 6;
  144. p->indexList = new int[ p->indexAnz ];
  145. p->indexList[ 0 ] = 6 + 16;
  146. p->indexList[ 1 ] = 2 + 16;
  147. p->indexList[ 2 ] = 3 + 16;
  148. p->indexList[ 3 ] = 6 + 16;
  149. p->indexList[ 4 ] = 3 + 16;
  150. p->indexList[ 5 ] = 7 + 16;
  151. data->addPolygon( p );
  152. // right side
  153. p = new Polygon3D();
  154. p->indexAnz = 6;
  155. p->indexList = new int[ p->indexAnz ];
  156. p->indexList[ 0 ] = 1 + 8;
  157. p->indexList[ 1 ] = 7 + 8;
  158. p->indexList[ 2 ] = 3 + 8;
  159. p->indexList[ 3 ] = 1 + 8;
  160. p->indexList[ 4 ] = 5 + 8;
  161. p->indexList[ 5 ] = 7 + 8;
  162. data->addPolygon( p );
  163. // top side
  164. p = new Polygon3D();
  165. p->indexAnz = 6;
  166. p->indexList = new int[ p->indexAnz ];
  167. p->indexList[ 0 ] = 4 + 16;
  168. p->indexList[ 1 ] = 1 + 16;
  169. p->indexList[ 2 ] = 0 + 16;
  170. p->indexList[ 3 ] = 4 + 16;
  171. p->indexList[ 4 ] = 5 + 16;
  172. p->indexList[ 5 ] = 1 + 16;
  173. data->addPolygon( p );
  174. // left side
  175. p = new Polygon3D();
  176. p->indexAnz = 6;
  177. p->indexList = new int[ p->indexAnz ];
  178. p->indexList[ 0 ] = 0 + 8;
  179. p->indexList[ 1 ] = 2 + 8;
  180. p->indexList[ 2 ] = 6 + 8;
  181. p->indexList[ 3 ] = 0 + 8;
  182. p->indexList[ 4 ] = 6 + 8;
  183. p->indexList[ 5 ] = 4 + 8;
  184. data->addPolygon( p );
  185. // back side
  186. p = new Polygon3D();
  187. p->indexAnz = 6;
  188. p->indexList = new int[ p->indexAnz ];
  189. p->indexList[ 0 ] = 4;
  190. p->indexList[ 1 ] = 6;
  191. p->indexList[ 2 ] = 7;
  192. p->indexList[ 3 ] = 4;
  193. p->indexList[ 4 ] = 7;
  194. p->indexList[ 5 ] = 5;
  195. data->addPolygon( p );
  196. // front side
  197. p = new Polygon3D();
  198. p->indexAnz = 6;
  199. p->indexList = new int[ p->indexAnz ];
  200. p->indexList[ 0 ] = 0;
  201. p->indexList[ 1 ] = 3;
  202. p->indexList[ 2 ] = 2;
  203. p->indexList[ 3 ] = 0;
  204. p->indexList[ 4 ] = 1;
  205. p->indexList[ 5 ] = 3;
  206. data->addPolygon( p );
  207. data->calculateNormals();
  208. return data;
  209. }
  210. }
  211. Framework::Model3DTextur* BasicBlockItemType::getItemTextur() const
  212. {
  213. return 0;
  214. }