World.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <Network.h>
  2. #include <Welt3D.h>
  3. #include <GraphicsApi.h>
  4. #include <iostream>
  5. #include "World.h"
  6. #include "Globals.h"
  7. #include "WorldUpdate.h"
  8. #include "Constants.h"
  9. #include "Registries.h"
  10. #include "BasicBlocks.h"
  11. #include "Game.h"
  12. #include <AsynchronCall.h>
  13. using namespace Network;
  14. using namespace Framework;
  15. World::World( Bildschirm3D* zScreen )
  16. : Thread()
  17. {
  18. renderedWorld = new Welt3D();
  19. renderedWorld->addDiffuseLight( DiffuseLight{ Vec3<float>( 0.5f, 0.5f, -1.f ), Vec3<float>( 1.f, 1.f, 1.f ) } );
  20. dimensions = new RCArray<Dimension>();
  21. currentPlayer = new CurrentPlayer();
  22. zScreenPtr = zScreen;
  23. kam = new PlayerKam( zScreen );
  24. kam->setWelt( renderedWorld );
  25. zScreen->addKamera( kam );
  26. firstMessage = 1;
  27. hasTarget = 0;
  28. entityTarget = -1;
  29. ownEntityId = -1;
  30. currentTarget = 0;
  31. start();
  32. }
  33. World::~World()
  34. {
  35. zScreenPtr->removeKamera( kam );
  36. dimensions->release();
  37. currentPlayer->release();
  38. if( currentTarget )
  39. currentTarget->release();
  40. }
  41. void World::update( bool background )
  42. {
  43. NetworkReader* serverMessageReader = 0;
  44. unsigned char type = 0;
  45. while( background ? serverMessageReader = network->zFactoryClient()->getNextBackgroundMessage() : serverMessageReader = network->zFactoryClient()->getNextForegroundMessage() )
  46. {
  47. serverMessageReader->lese( (char*)&type, 1 );
  48. if( type == 2 ) // WORLD UPDATE
  49. {
  50. int id = 0;
  51. serverMessageReader->lese( (char*)&id, 4 );
  52. STATIC_REGISTRY( WorldUpdateType ).zElement( id )->applyUpdateAndCheck( serverMessageReader );
  53. }
  54. if( type == 3 ) // API MESSAGE
  55. {
  56. // TODO: process messages
  57. }
  58. if( type == 4 ) // POSITION UPDATE
  59. {
  60. serverMessageReader->lese( (char*)&ownEntityId, 4 );
  61. }
  62. network->zFactoryClient()->endMessageReading( background );
  63. }
  64. network->zFactoryClient()->endMessageReading( background );
  65. Entity* player = getCurrentPlayerEntity();
  66. if( player )
  67. {
  68. for( Dimension* dim : *dimensions )
  69. dim->removeDistantChunks( { (int)player->getPos().x, (int)player->getPos().y }, this );
  70. }
  71. }
  72. void World::setChunk( Chunk* chunk, int dimensionId )
  73. {
  74. zScreenPtr->lock();
  75. Dimension* zDim = zDimension( dimensionId );
  76. if( !zDim )
  77. {
  78. zDim = new Dimension( dimensionId );
  79. dimensions->add( zDim );
  80. }
  81. zDim->setChunk( chunk, chunk->getCenter(), this );
  82. zScreenPtr->unlock();
  83. }
  84. void World::thread()
  85. {
  86. new AsynchronCall( [this]() {
  87. while( true )
  88. {
  89. zScreenPtr->lock();
  90. if( currentGame != this )
  91. {
  92. zScreenPtr->unlock();
  93. return;
  94. }
  95. zScreenPtr->unlock();
  96. update( 0 );
  97. Sleep( 10 );
  98. }
  99. } );
  100. while( true )
  101. {
  102. zScreenPtr->lock();
  103. if( currentGame != this )
  104. {
  105. zScreenPtr->unlock();
  106. return;
  107. }
  108. zScreenPtr->unlock();
  109. update( 1 );
  110. Sleep( 10 );
  111. }
  112. }
  113. Block* World::zBlockAt( Framework::Vec3<int> location, int dimension ) const
  114. {
  115. Dimension* dim = zDimension( dimension );
  116. if( dim )
  117. return dim->zBlock( location );
  118. return 0;
  119. }
  120. Block* World::getBlockAt( Framework::Vec3<int> location, int dimension ) const
  121. {
  122. Dimension* dim = zDimension( dimension );
  123. if( dim )
  124. return dim->getBlock( location );
  125. return 0;
  126. }
  127. Dimension* World::zDimension( int id ) const
  128. {
  129. for( auto dim : *dimensions )
  130. {
  131. if( dim->getDimensionId() == id )
  132. return dim;
  133. }
  134. return 0;
  135. }
  136. Dimension* World::zDimensionOrCreate( int id )
  137. {
  138. zScreenPtr->lock();
  139. Dimension* d = zDimension( id );
  140. if( !d )
  141. {
  142. d = new Dimension( id );
  143. dimensions->add( d );
  144. }
  145. zScreenPtr->unlock();
  146. return d;
  147. }
  148. void World::setVisibility( Chunk* zChunk, bool visible )
  149. {
  150. renderedWorld->lock();
  151. if( visible )
  152. renderedWorld->addCollection( dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()) );
  153. else
  154. renderedWorld->removeCollection( zChunk );
  155. renderedWorld->unlock();
  156. }
  157. void World::setVisibility( Entity* zEntity, bool visible )
  158. {
  159. renderedWorld->lock();
  160. if( visible )
  161. renderedWorld->addZeichnung( dynamic_cast<Framework::Model3D*>(zEntity->getThis()) );
  162. else
  163. renderedWorld->removeZeichnung( zEntity );
  164. renderedWorld->unlock();
  165. }
  166. Framework::Bildschirm3D* World::zScreen() const
  167. {
  168. return zScreenPtr;
  169. }
  170. Framework::Punkt World::getChunkCenter( int x, int y ) const
  171. {
  172. return Punkt( ((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2, ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2 );
  173. }
  174. Entity* World::zEntity( int id ) const
  175. {
  176. for( Dimension* d : *dimensions )
  177. {
  178. Entity* e = d->zEntity( id );
  179. if( e )
  180. return e;
  181. }
  182. return 0;
  183. }
  184. Entity* World::getEntity( int id ) const
  185. {
  186. for( Dimension* d : *dimensions )
  187. {
  188. Entity* e = d->getEntity( id );
  189. if( e )
  190. return e;
  191. }
  192. return 0;
  193. }
  194. void World::removeEntity( int id )
  195. {
  196. for( Dimension* d : *dimensions )
  197. d->removeEntity( id );
  198. }
  199. PlayerKam* World::zKamera() const
  200. {
  201. return kam;
  202. }
  203. int World::getCurrentPlayerId() const
  204. {
  205. return ownEntityId;
  206. }
  207. Entity* World::getCurrentPlayerEntity() const
  208. {
  209. for( Dimension* d : *dimensions )
  210. {
  211. Entity* e = d->zEntity( ownEntityId );
  212. if( e )
  213. return e;
  214. }
  215. return 0;
  216. }
  217. void World::setTarget( Framework::Model3D* zTarget )
  218. {
  219. if( zTarget != currentTarget )
  220. {
  221. if( currentTarget )
  222. {
  223. currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() - 0.2f );
  224. currentTarget->release();
  225. currentTarget = 0;
  226. }
  227. if( zTarget )
  228. {
  229. currentTarget = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  230. if( currentTarget )
  231. currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() + 0.2f );
  232. }
  233. }
  234. }