123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include "Chunk.h"
- #include "Constants.h"
- #include "Globals.h"
- #include "Registries.h"
- Chunk::Chunk( Framework::Punkt location, int dimensionId )
- : ReferenceCounter(),
- dimensionId( dimensionId ),
- location( location )
- {}
- Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReader* zReader )
- : Chunk( location, dimensionId )
- {
- load( zReader );
- }
- Chunk::~Chunk()
- {}
- Block* Chunk::zBlockAt( Framework::Vec3<int> location ) const
- {
- for( Block* b : blocks )
- {
- if( (Framework::Vec3<int>)b->getPos() == location )
- return b;
- }
- return 0;
- }
- void Chunk::setBlock( Block* block )
- {
- Framework::Vec3<int> pos = (Framework::Vec3<int>)block->getPos();
- for( Framework::Iterator<Block*> iterator = blocks.begin(); iterator; iterator++ )
- {
- if( pos == (Framework::Vec3<int>)iterator->getPos() )
- {
- iterator->release();
- iterator.set( block );
- return;
- }
- }
- blocks.add( block );
- }
- void Chunk::load( Framework::StreamReader* zReader )
- {
- Framework::Vec3<int> pos = { 0, 0, 0 };
- unsigned short id;
- zReader->lese( (char*)&id, 2 );
- while( id )
- {
- zReader->lese( (char*)&pos.x, 4 );
- zReader->lese( (char*)&pos.y, 4 );
- zReader->lese( (char*)&pos.z, 4 );
- bool d;
- zReader->lese( (char*)&d, 1 );
- if( d )
- {
- Block* block = STATIC_REGISTRY( BlockType ).zElement( id )->loadBlock( { pos.x + location.x - CHUNK_SIZE / 2, pos.y + location.y - CHUNK_SIZE / 2, pos.z }, zReader );
- if( block )
- setBlock( block );
- }
- else if( STATIC_REGISTRY( BlockType ).zElement( id )->needsInstance() )
- setBlock( STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( { pos.x + location.x - CHUNK_SIZE / 2, pos.y + location.y - CHUNK_SIZE / 2, pos.z } ) );
- zReader->lese( (char*)&id, 2 );
- }
- }
- int Chunk::getDimensionId() const
- {
- return dimensionId;
- }
- Framework::Punkt Chunk::getCenter() const
- {
- return location;
- }
- Framework::Vec3<int> Chunk::getMin() const
- {
- return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 };
- }
- Framework::Vec3<int> Chunk::getMax() const
- {
- return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
- }
- void Chunk::forAll( std::function<void( Model3D* )> f )
- {
- for( Block* b : blocks )
- f( b );
- }
|