Browse Source

entities are now synchronized

Kolja Strohm 3 years ago
parent
commit
9d547874bd

+ 10 - 0
FactoryCraft/Dimension.cpp

@@ -263,4 +263,14 @@ void Dimension::removeOldChunks()
         chunk->prepareRemove();
         setChunk( 0, chunk->getCenter() );
     }
+}
+
+Entity* Dimension::zEntity( int id )
+{
+    for( auto entity : *entities )
+    {
+        if( entity->getId() == id )
+            return entity;
+    }
+    return 0;
 }

+ 1 - 0
FactoryCraft/Dimension.h

@@ -35,4 +35,5 @@ public:
     Chunk* zChunk( Framework::Punkt wPos ) const;
     float getGravity() const;
     void removeOldChunks();
+    Entity* zEntity( int id );
 };

+ 33 - 0
FactoryCraft/EntityChangedUpdate.cpp

@@ -0,0 +1,33 @@
+#include "EntityChangedUpdate.h"
+#include "Game.h"
+#include "EntityType.h"
+
+
+EntityChangedUpdate::EntityChangedUpdate( int entityId, Framework::Vec3<int> pos, int dimension )
+    : WorldUpdate( EntityChangedUpdateType::ID, dimension, pos, pos ),
+    entityId( entityId )
+{}
+
+EntityChangedUpdate::~EntityChangedUpdate()
+{}
+
+void EntityChangedUpdate::onUpdate( Dimension* zDimension )
+{}
+
+void EntityChangedUpdate::write( Framework::StreamWriter* zWriter )
+{
+    Entity* entity = Game::INSTANCE->zEntity( entityId, getAffectedDimension() );
+    if( entity )
+    {
+        int type = entity->zType()->getId();
+        WorldUpdate::write( zWriter );
+        zWriter->schreibe( (char*)&entityId, 4 );
+        zWriter->schreibe( (char*)&type, 4 );
+        StaticRegistry<EntityType>::INSTANCE.zElement( type )->saveEntity( entity, zWriter );
+    }
+}
+
+
+EntityChangedUpdateType::EntityChangedUpdateType()
+    : WorldUpdateType( ID )
+{}

+ 25 - 0
FactoryCraft/EntityChangedUpdate.h

@@ -0,0 +1,25 @@
+#pragma once
+
+#include "WorldUpdate.h"
+
+class EntityChangedUpdate : public WorldUpdate
+{
+private:
+    int entityId;
+
+public:
+    EntityChangedUpdate( int entityId, Framework::Vec3<int> pos, int dimension );
+    ~EntityChangedUpdate();
+
+    void onUpdate( Dimension* zDimension ) override;
+    void write( Framework::StreamWriter* zWriter ) override;
+};
+
+class EntityChangedUpdateType : WorldUpdateType
+{
+    REGISTRABLE( EntityChangedUpdateType )
+
+protected:
+    EntityChangedUpdateType();
+};
+REGISTER( EntityChangedUpdateType, WorldUpdateType )

+ 2 - 0
FactoryCraft/FactoryCraft.vcxproj

@@ -108,6 +108,7 @@
     <ClInclude Include="Effect.h" />
     <ClInclude Include="EffectFactory.h" />
     <ClInclude Include="Entity.h" />
+    <ClInclude Include="EntityChangedUpdate.h" />
     <ClInclude Include="EntityType.h" />
     <ClInclude Include="FastNoiseLite.h" />
     <ClInclude Include="FastNoiseWrapper.h" />
@@ -154,6 +155,7 @@
     <ClCompile Include="Dimension.cpp" />
     <ClCompile Include="DimensionGenerator.cpp" />
     <ClCompile Include="Entity.cpp" />
+    <ClCompile Include="EntityChangedUpdate.cpp" />
     <ClCompile Include="EntityType.cpp" />
     <ClCompile Include="FastNoiseWrapper.cpp" />
     <ClCompile Include="Game.cpp" />

+ 6 - 0
FactoryCraft/FactoryCraft.vcxproj.filters

@@ -195,6 +195,9 @@
     <ClInclude Include="AddEntityUpdate.h">
       <Filter>world\update</Filter>
     </ClInclude>
+    <ClInclude Include="EntityChangedUpdate.h">
+      <Filter>world\update</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Server.cpp">
@@ -326,5 +329,8 @@
     <ClCompile Include="AddEntityUpdate.cpp">
       <Filter>world\update</Filter>
     </ClCompile>
+    <ClCompile Include="EntityChangedUpdate.cpp">
+      <Filter>world\update</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 9 - 1
FactoryCraft/Game.cpp

@@ -66,7 +66,7 @@ void GameClient::sendWorldUpdate( WorldUpdate* update )
             for( auto update2 : updateQueue )
             {
                 int dist2 = update2->distanceTo( pos.x, pos.y );
-                if( dist2 >= dist )
+                if( dist2 > dist )
                     break;
                 index++;
             }
@@ -543,4 +543,12 @@ void Game::initialize( Framework::Text name, Framework::Text worldsDir )
         Game::INSTANCE = new Game( name, worldsDir );
         Game::INSTANCE->initialize();
     }
+}
+
+Entity* Game::zEntity( int id, int dimensionId ) const
+{
+    Dimension* d = zDimension( dimensionId );
+    if( d )
+        return d->zEntity( id );
+    return 0;
 }

+ 1 - 0
FactoryCraft/Game.h

@@ -98,6 +98,7 @@ public:
     void addDimension( Dimension* d );
     int getNextEntityId();
     WorldGenerator* zGenerator() const;
+    Entity* zEntity( int id, int dimensionId ) const;
 
     static Game* INSTANCE;
     static void initialize( Framework::Text name, Framework::Text worldsDir );

+ 8 - 0
FactoryCraft/ItemEntity.cpp

@@ -1,4 +1,6 @@
 #include "ItemEntity.h"
+#include "Game.h"
+#include "EntityChangedUpdate.h"
 
 
 ItemEntity::ItemEntity( Framework::Vec3<float> location, int dimensionId, int entityId )
@@ -20,7 +22,13 @@ ItemEntity::ItemEntity( Framework::Vec3<float> location, int dimensionId, int en
 
 void ItemEntity::tick( const Dimension* zDimension )
 {
+    Framework::Vec3<float> pos = location;
     // TODO: add speed to next entity with free inventory
+    Entity::tick( zDimension );
+    if( pos != location )
+    {
+        Game::INSTANCE->requestWorldUpdate( new EntityChangedUpdate( id, (Framework::Vec3<int>)location, currentDimensionId ) );
+    }
 }
 
 void ItemEntity::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )

+ 2 - 1
FactoryCraft/StaticInitializerOrder.cpp

@@ -30,4 +30,5 @@ const c *c::INSTANCE = new c();
 #include "PlaceBlockUpdate.h"
 #include "BlockChangedUpdate.h"
 #include "BlockRemovedUpdate.h"
-#include "AddEntityUpdate.h"
+#include "AddEntityUpdate.h"
+#include "EntityChangedUpdate.h"