Dimension.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "Dimension.h"
  2. #include "Constants.h"
  3. #include "Datei.h"
  4. #include "Game.h"
  5. #include "Globals.h"
  6. using namespace Framework;
  7. Dimension::Dimension( int id )
  8. : dimensionId( id ),
  9. chunks( new Trie<Chunk>() ),
  10. entities( new RCArray<Entity>() )
  11. {}
  12. Dimension::~Dimension()
  13. {
  14. entities->release();
  15. chunks->release();
  16. }
  17. void Dimension::updateVisibility()
  18. {
  19. bool changed = true;
  20. while( changed )
  21. {
  22. changed = false;
  23. for( auto chunk : chunkList )
  24. {
  25. if( chunk )
  26. changed |= chunk->updateVisibility();
  27. }
  28. }
  29. }
  30. void Dimension::getAddrOf( Punkt cPos, char* addr ) const
  31. {
  32. *(int*)addr = cPos.x;
  33. *((int*)addr + 1) = cPos.y;
  34. }
  35. void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
  36. {
  37. if( wPos.x < 0 )
  38. wPos.x -= CHUNK_SIZE;
  39. if( wPos.y < 0 ) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
  40. wPos.y -= CHUNK_SIZE;
  41. wPos /= CHUNK_SIZE;
  42. getAddrOf( wPos, addr );
  43. }
  44. Chunk* Dimension::zChunk( Punkt wPos ) const
  45. {
  46. char addr[ 8 ];
  47. getAddrOfWorld( wPos, addr );
  48. return chunks->z( addr, 8 );
  49. }
  50. Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location )
  51. {
  52. Chunk* c = zChunk( currentGame->getChunkCenter( location.x, location.y ) );
  53. if( c )
  54. {
  55. int x = location.x % CHUNK_SIZE;
  56. int y = location.y % CHUNK_SIZE;
  57. if( x < 0 )
  58. x += CHUNK_SIZE;
  59. if( y < 0 )
  60. y += CHUNK_SIZE;
  61. return c->zBlockAt( Vec3<int>( x, y, location.z ) );
  62. }
  63. return 0;
  64. }
  65. void Dimension::addEntity( Entity* entity )
  66. {
  67. entities->add( entity );
  68. }
  69. void Dimension::addChunk( Chunk* chunk )
  70. {
  71. char addr[ 8 ];
  72. getAddrOfWorld( chunk->getCenter(), addr );
  73. chunks->set( addr, 8, chunk );
  74. chunkList.add( chunk );
  75. getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
  76. Chunk* zChunk = chunks->z( addr, 8 );
  77. if( zChunk )
  78. {
  79. zChunk->setNeighbor( WEST, chunk );
  80. chunk->setNeighbor( EAST, chunk );
  81. }
  82. getAddrOfWorld( chunk->getCenter() + Punkt( -CHUNK_SIZE, 0 ), addr );
  83. zChunk = chunks->z( addr, 8 );
  84. if( zChunk )
  85. {
  86. zChunk->setNeighbor( EAST, chunk );
  87. chunk->setNeighbor( WEST, chunk );
  88. }
  89. getAddrOfWorld( chunk->getCenter() + Punkt( 0, CHUNK_SIZE ), addr );
  90. zChunk = chunks->z( addr, 8 );
  91. if( zChunk )
  92. {
  93. zChunk->setNeighbor( NORTH, chunk );
  94. chunk->setNeighbor( SOUTH, chunk );
  95. }
  96. getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
  97. zChunk = chunks->z( addr, 8 );
  98. if( zChunk )
  99. {
  100. zChunk->setNeighbor( SOUTH, chunk );
  101. chunk->setNeighbor( NORTH, chunk );
  102. }
  103. }
  104. int Dimension::getDimensionId() const
  105. {
  106. return dimensionId;
  107. }
  108. bool Dimension::hasChunck( int x, int y ) const
  109. {
  110. return zChunk( Punkt( x, y ) );
  111. }