123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #include <Network.h>
- #include <Welt3D.h>
- #include <GraphicsApi.h>
- #include <iostream>
- #include "World.h"
- #include "Globals.h"
- #include "WorldUpdate.h"
- #include "Constants.h"
- #include "Registries.h"
- #include "BasicBlocks.h"
- #include "Game.h"
- #include <AsynchronCall.h>
- using namespace Network;
- using namespace Framework;
- World::World( Bildschirm3D* zScreen )
- : Thread()
- {
- renderedWorld = new Welt3D();
- renderedWorld->addDiffuseLight( DiffuseLight{ Vec3<float>( 0.5f, 0.5f, -1.f ), Vec3<float>( 1.f, 1.f, 1.f ) } );
- dimensions = new RCArray<Dimension>();
- currentPlayer = new CurrentPlayer();
- zScreenPtr = zScreen;
- kam = new PlayerKam( zScreen );
- kam->setWelt( renderedWorld );
- zScreen->addKamera( kam );
- firstMessage = 1;
- hasTarget = 0;
- entityTarget = -1;
- ownEntityId = -1;
- currentTarget = 0;
- start();
- }
- World::~World()
- {
- zScreenPtr->removeKamera( kam );
- dimensions->release();
- currentPlayer->release();
- if( currentTarget )
- currentTarget->release();
- }
- void World::update( bool background )
- {
- NetworkReader* serverMessageReader = 0;
- unsigned char type = 0;
- while( background ? serverMessageReader = network->zFactoryClient()->getNextBackgroundMessage() : serverMessageReader = network->zFactoryClient()->getNextForegroundMessage() )
- {
- serverMessageReader->lese( (char*)&type, 1 );
- if( type == 2 ) // WORLD UPDATE
- {
- int id = 0;
- serverMessageReader->lese( (char*)&id, 4 );
- STATIC_REGISTRY( WorldUpdateType ).zElement( id )->applyUpdateAndCheck( serverMessageReader );
- }
- if( type == 3 ) // API MESSAGE
- {
- // TODO: process messages
- }
- if( type == 4 ) // POSITION UPDATE
- {
- serverMessageReader->lese( (char*)&ownEntityId, 4 );
- }
- network->zFactoryClient()->endMessageReading( background );
- }
- network->zFactoryClient()->endMessageReading( background );
- Entity* player = getCurrentPlayerEntity();
- if( player )
- {
- renderedWorld->lock();
- for( Dimension* dim : *dimensions )
- dim->removeDistantChunks( { (int)player->getPos().x, (int)player->getPos().y }, this );
- renderedWorld->unlock();
- }
- }
- void World::setChunk( Chunk* chunk, int dimensionId )
- {
- zScreenPtr->lock();
- Dimension* zDim = zDimension( dimensionId );
- if( !zDim )
- {
- zDim = new Dimension( dimensionId );
- dimensions->add( zDim );
- }
- zDim->setChunk( chunk, chunk->getCenter(), this );
- zScreenPtr->unlock();
- }
- void World::thread()
- {
- new AsynchronCall( [this]() {
- while( true )
- {
- zScreenPtr->lock();
- if( currentGame != this )
- {
- zScreenPtr->unlock();
- return;
- }
- zScreenPtr->unlock();
- update( 0 );
- Sleep( 10 );
- }
- } );
- while( true )
- {
- zScreenPtr->lock();
- if( currentGame != this )
- {
- zScreenPtr->unlock();
- return;
- }
- zScreenPtr->unlock();
- update( 1 );
- Sleep( 10 );
- }
- }
- Block* World::zBlockAt( Framework::Vec3<int> location, int dimension ) const
- {
- Dimension* dim = zDimension( dimension );
- if( dim )
- return dim->zBlock( location );
- return 0;
- }
- Block* World::getBlockAt( Framework::Vec3<int> location, int dimension ) const
- {
- Dimension* dim = zDimension( dimension );
- if( dim )
- return dim->getBlock( location );
- return 0;
- }
- Dimension* World::zDimension( int id ) const
- {
- for( auto dim : *dimensions )
- {
- if( dim->getDimensionId() == id )
- return dim;
- }
- return 0;
- }
- Dimension* World::zDimensionOrCreate( int id )
- {
- zScreenPtr->lock();
- Dimension* d = zDimension( id );
- if( !d )
- {
- d = new Dimension( id );
- dimensions->add( d );
- }
- zScreenPtr->unlock();
- return d;
- }
- void World::setVisibility( Chunk* zChunk, bool visible )
- {
- renderedWorld->lock();
- if( visible )
- renderedWorld->addCollection( dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()) );
- else
- renderedWorld->removeCollection( zChunk );
- renderedWorld->unlock();
- }
- void World::setVisibility( Entity* zEntity, bool visible )
- {
- renderedWorld->lock();
- if( visible )
- renderedWorld->addZeichnung( dynamic_cast<Framework::Model3D*>(zEntity->getThis()) );
- else
- renderedWorld->removeZeichnung( zEntity );
- renderedWorld->unlock();
- }
- Framework::Punkt World::getChunkCenter( int x, int y ) const
- {
- 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 );
- }
- Entity* World::zEntity( int id ) const
- {
- for( Dimension* d : *dimensions )
- {
- Entity* e = d->zEntity( id );
- if( e )
- return e;
- }
- return 0;
- }
- Entity* World::getEntity( int id ) const
- {
- for( Dimension* d : *dimensions )
- {
- Entity* e = d->getEntity( id );
- if( e )
- return e;
- }
- return 0;
- }
- void World::removeEntity( int id )
- {
- for( Dimension* d : *dimensions )
- d->removeEntity( id );
- }
- PlayerKam* World::zKamera() const
- {
- return kam;
- }
- int World::getCurrentPlayerId() const
- {
- return ownEntityId;
- }
- Entity* World::getCurrentPlayerEntity() const
- {
- for( Dimension* d : *dimensions )
- {
- Entity* e = d->zEntity( ownEntityId );
- if( e )
- return e;
- }
- return 0;
- }
- void World::setTarget( Framework::Model3D* zTarget )
- {
- if( zTarget != currentTarget )
- {
- if( currentTarget )
- {
- currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() - 0.2f );
- currentTarget->release();
- currentTarget = 0;
- }
- if( zTarget )
- {
- currentTarget = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
- if( currentTarget )
- currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() + 0.2f );
- }
- }
- }
|