Browse Source

improved synchronizsation of player entities

Kolja Strohm 3 years ago
parent
commit
9ded5d0e8e

+ 1 - 1
FactoryCraft/Block.cpp

@@ -43,7 +43,7 @@ void Block::onDestroy()
         Item* blockItem = zType->getItemFromBlock( this );
         if( blockItem )
         {
-            ItemEntity* itemEntity = new ItemEntity( location + Framework::Vec3<float>( 0.5f, 0.5f, 0.5f ), dimensionId, Game::INSTANCE->getNextEntityId() );
+            ItemEntity* itemEntity = (ItemEntity*)ItemEntityType::INSTANCE->createEntity( location + Framework::Vec3<float>( 0.5f, 0.5f, 0.5f ), dimensionId, Game::INSTANCE->getNextEntityId() );
             ItemStack* stack = new ItemStack( blockItem, 1, blockItem->getMaxStackSize() );
             itemEntity->unsaveAddItem( stack, NO_DIRECTION );
             stack->release();

+ 2 - 0
FactoryCraft/ItemEntity.cpp

@@ -22,6 +22,8 @@ ItemEntity::ItemEntity( Framework::Vec3<float> location, int dimensionId, int en
 
 void ItemEntity::tick( const Dimension* zDimension )
 {
+    if( slot->zStack() == 0 && !removed )
+        throw "Illegal State exception";
     // add speed to next entity with free inventory
     Entity* zOther = Game::INSTANCE->zNearestEntity( currentDimensionId, location, [this]( Entity* zOther ) {
         return zOther != this && zOther->numberOfAddableItems( slot->zStack(), NO_DIRECTION );

+ 2 - 1
FactoryCraft/ItemEntity.h

@@ -7,11 +7,12 @@ class ItemEntityType;
 
 class ItemEntity : public Entity
 {
+private:
     ItemSlot* slot;
 
-public:
     ItemEntity( Framework::Vec3<float> location, int dimensionId, int entityId );
 
+public:
     void tick( const Dimension* zDimension ) override;
 
     void api( Framework::StreamReader* zRequest, NetworkResponse* zResponse ) override;

+ 1 - 1
FactoryCraft/ItemSlot.cpp

@@ -91,7 +91,7 @@ int ItemSlot::getPushPriority() const
 
 bool ItemSlot::isFull() const
 {
-    return items ? items->getSize() >= items->getMaxSize() : maxSize > 0;
+    return items ? items->getSize() >= items->getMaxSize() : maxSize == 0;
 }
 
 int ItemSlot::getFreeSpace() const

+ 34 - 1
FactoryCraft/Player.cpp

@@ -27,6 +27,30 @@ Player::Player( Framework::Vec3<float> location, int dimensionId, int entityId )
     targetDistanceLimit = 4;
 }
 
+void Player::afterPullStack( ItemSlot* zSlot, Direction dir, const Item* zItem, int count )
+{
+    for( auto slot : itemBar )
+    {
+        if( slot == zSlot )
+        {
+            needUpdate = 1;
+            return;
+        }
+    }
+}
+
+void Player::afterPushStack( ItemSlot* zSlot, Direction dir, const Item* zItem, int count )
+{
+    for( auto slot : itemBar )
+    {
+        if( slot == zSlot )
+        {
+            needUpdate = 1;
+            return;
+        }
+    }
+}
+
 void Player::useItemSlot( ItemSlot* zSlot )
 {
     if( zSlot->zStack() )
@@ -243,6 +267,7 @@ void Player::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse
     case 3:
         zRequest->lese( (char*)&leftHandPosition, 4 );
         leftHandPosition = leftHandPosition % itemBar.getEintragAnzahl();
+        needUpdate = 1;
     }
 }
 
@@ -259,11 +284,19 @@ PlayerEntityType::PlayerEntityType()
 
 void PlayerEntityType::loadSuperEntity( Entity* zEntity, Framework::StreamReader* zReader ) const
 {
-    EntityType::loadSuperEntity( zEntity, zReader );
+    Player* zPlayer = dynamic_cast<Player*>(zEntity);
+    if( !zPlayer )
+        throw "PlayerEntityType::loadSuperEntity was called with an entity witch is not an instance of Player";
+    zReader->lese( (char*)&zPlayer->leftHandPosition, 4 );
+    EntityType::loadSuperEntity( zPlayer, zReader );
 }
 
 void PlayerEntityType::saveSuperEntity( Entity* zEntity, Framework::StreamWriter* zWriter ) const
 {
+    Player* zPlayer = dynamic_cast<Player*>(zEntity);
+    if( !zPlayer )
+        throw "PlayerEntityType::saveSuperEntity was called with an entity witch is not an instance of Player";
+    zWriter->schreibe( (char*)&zPlayer->leftHandPosition, 4 );
     EntityType::saveSuperEntity( zEntity, zWriter );
 }
 

+ 4 - 1
FactoryCraft/Player.h

@@ -32,9 +32,12 @@ private:
     __int64 keyState;
 
     void useItemSlot( ItemSlot* zSlot );
+    Player( Framework::Vec3<float> location, int dimensionId, int entityId );
+
+    virtual void afterPullStack( ItemSlot* zSlot, Direction dir, const Item* zItem, int count ) override;
+    virtual void afterPushStack( ItemSlot* zSlot, Direction dir, const Item* zItem, int count ) override;
 
 public:
-    Player( Framework::Vec3<float> location, int dimensionId, int entityId );
     void setName( Framework::Text name );
     const char* getName() const;
     void tick( const Dimension* zDimension ) override;

+ 1 - 1
FactoryCraft/Server.cpp

@@ -36,7 +36,7 @@ FactoryCraftServer::FactoryCraftServer( InitDatei* zIni )
         exit( 2 );
     }
     Game::initialize( zIni->zWert( "World" )->getText(), zIni->zWert( "SaveDir" )->getText() );
-    new Framework::AsynchronCall( [this]() {
+    new Framework::AsynchronCall( "Server", [this]() {
         runningThreads++;
         while( server->isConnected() )
         {

+ 1 - 0
FactoryCraft/WorldGenerator.cpp

@@ -11,6 +11,7 @@ WorldGenerator::WorldGenerator( int seed )
     exit( 0 ),
     seed( seed )
 {
+    setName( "World Generator" );
     start();
 }
 

+ 1 - 0
FactoryCraft/WorldLoader.cpp

@@ -11,6 +11,7 @@ WorldLoader::WorldLoader()
     : Thread(),
     exit( 0 )
 {
+    setName( "World Loader" );
     Datei d;
     d.setDatei( Game::INSTANCE->getWorldDirectory() + "/dim" );
     RCArray<Text>* names = d.getDateiListe();