World.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. renderedWorld->lock();
  69. for( Dimension* dim : *dimensions )
  70. dim->removeDistantChunks( { (int)player->getPos().x, (int)player->getPos().y }, this );
  71. renderedWorld->unlock();
  72. }
  73. }
  74. void World::setChunk( Chunk* chunk, int dimensionId )
  75. {
  76. zScreenPtr->lock();
  77. Dimension* zDim = zDimension( dimensionId );
  78. if( !zDim )
  79. {
  80. zDim = new Dimension( dimensionId );
  81. dimensions->add( zDim );
  82. }
  83. zDim->setChunk( chunk, chunk->getCenter(), this );
  84. zScreenPtr->unlock();
  85. }
  86. void World::thread()
  87. {
  88. new AsynchronCall( "World Update", [this]() {
  89. while( true )
  90. {
  91. zScreenPtr->lock();
  92. if( currentGame != this )
  93. {
  94. zScreenPtr->unlock();
  95. return;
  96. }
  97. zScreenPtr->unlock();
  98. update( 0 );
  99. Sleep( 10 );
  100. }
  101. } );
  102. while( true )
  103. {
  104. zScreenPtr->lock();
  105. if( currentGame != this )
  106. {
  107. zScreenPtr->unlock();
  108. return;
  109. }
  110. zScreenPtr->unlock();
  111. update( 1 );
  112. Sleep( 10 );
  113. }
  114. }
  115. Block* World::zBlockAt( Framework::Vec3<int> location, int dimension ) const
  116. {
  117. Dimension* dim = zDimension( dimension );
  118. if( dim )
  119. return dim->zBlock( location );
  120. return 0;
  121. }
  122. Block* World::getBlockAt( Framework::Vec3<int> location, int dimension ) const
  123. {
  124. Dimension* dim = zDimension( dimension );
  125. if( dim )
  126. return dim->getBlock( location );
  127. return 0;
  128. }
  129. Dimension* World::zDimension( int id ) const
  130. {
  131. for( auto dim : *dimensions )
  132. {
  133. if( dim->getDimensionId() == id )
  134. return dim;
  135. }
  136. return 0;
  137. }
  138. Dimension* World::zDimensionOrCreate( int id )
  139. {
  140. zScreenPtr->lock();
  141. Dimension* d = zDimension( id );
  142. if( !d )
  143. {
  144. d = new Dimension( id );
  145. dimensions->add( d );
  146. }
  147. zScreenPtr->unlock();
  148. return d;
  149. }
  150. void World::setVisibility( Chunk* zChunk, bool visible )
  151. {
  152. renderedWorld->lock();
  153. if( visible )
  154. renderedWorld->addCollection( dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()) );
  155. else
  156. renderedWorld->removeCollection( zChunk );
  157. renderedWorld->unlock();
  158. }
  159. void World::setVisibility( Entity* zEntity, bool visible )
  160. {
  161. renderedWorld->lock();
  162. if( visible )
  163. renderedWorld->addZeichnung( dynamic_cast<Framework::Model3D*>(zEntity->getThis()) );
  164. else
  165. renderedWorld->removeZeichnung( zEntity );
  166. renderedWorld->unlock();
  167. }
  168. Framework::Punkt World::getChunkCenter( int x, int y ) const
  169. {
  170. 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 );
  171. }
  172. Entity* World::zEntity( int id ) const
  173. {
  174. for( Dimension* d : *dimensions )
  175. {
  176. Entity* e = d->zEntity( id );
  177. if( e )
  178. return e;
  179. }
  180. return 0;
  181. }
  182. Entity* World::getEntity( int id ) const
  183. {
  184. for( Dimension* d : *dimensions )
  185. {
  186. Entity* e = d->getEntity( id );
  187. if( e )
  188. return e;
  189. }
  190. return 0;
  191. }
  192. void World::removeEntity( int id )
  193. {
  194. for( Dimension* d : *dimensions )
  195. d->removeEntity( id );
  196. }
  197. PlayerKam* World::zKamera() const
  198. {
  199. return kam;
  200. }
  201. int World::getCurrentPlayerId() const
  202. {
  203. return ownEntityId;
  204. }
  205. Entity* World::getCurrentPlayerEntity() const
  206. {
  207. for( Dimension* d : *dimensions )
  208. {
  209. Entity* e = d->zEntity( ownEntityId );
  210. if( e )
  211. return e;
  212. }
  213. return 0;
  214. }
  215. void World::setTarget( Framework::Model3D* zTarget )
  216. {
  217. if( zTarget != currentTarget )
  218. {
  219. if( currentTarget )
  220. {
  221. currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() - 0.2f );
  222. currentTarget->release();
  223. currentTarget = 0;
  224. }
  225. if( zTarget )
  226. {
  227. currentTarget = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  228. if( currentTarget )
  229. currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() + 0.2f );
  230. }
  231. }
  232. }
  233. void World::lockWorld()
  234. {
  235. renderedWorld->lock();
  236. }
  237. void World::unlockWorld()
  238. {
  239. renderedWorld->unlock();
  240. }