Explorar o código

entities are now synchronized

Kolja Strohm %!s(int64=3) %!d(string=hai) anos
pai
achega
2c6f503ecc

+ 10 - 0
FactoryCraft/Dimension.cpp

@@ -135,4 +135,14 @@ void Dimension::removeBlock( Block* zBlock )
     if( c )
         c->removeBlock( zBlock );
     cs.unlock();
+}
+
+Entity* Dimension::zEntity( int id )
+{
+    for( Entity* e : *entities )
+    {
+        if( e->getId() == id )
+            return e;
+    }
+    return 0;
 }

+ 1 - 0
FactoryCraft/Dimension.h

@@ -33,4 +33,5 @@ public:
     void removeDistantChunks( Framework::Punkt wPos, World* zWorld );
     void setBlock( Block* block );
     void removeBlock( Block* zBlock );
+    Entity* zEntity( int id );
 };

+ 24 - 0
FactoryCraft/EntityChangedUpdate.cpp

@@ -0,0 +1,24 @@
+#include "EntityChangedUpdate.h"
+#include "Globals.h"
+#include "Registries.h"
+
+
+EntityChangedUpdateType::EntityChangedUpdateType()
+    : WorldUpdateType( ID )
+{}
+
+void EntityChangedUpdateType::applyUpdate( Framework::StreamReader* zReader )
+{
+    int id;
+    int type;
+    zReader->lese( (char*)&id, 4 );
+    zReader->lese( (char*)&type, 4 );
+    Entity* e = currentGame->zEntity( id );
+    if( !e )
+    {
+        e = STATIC_REGISTRY( EntityType ).zElement( type )->loadEntity( zReader );
+        currentGame->zDimensionOrCreate( e->getCurrentDimension() )->addEntity( e );
+    }
+    else
+        STATIC_REGISTRY( EntityType ).zElement( type )->updateEntity( e, zReader );
+}

+ 16 - 0
FactoryCraft/EntityChangedUpdate.h

@@ -0,0 +1,16 @@
+#pragma once
+
+#include "WorldUpdate.h"
+
+class EntityChangedUpdateType : WorldUpdateType
+{
+    REGISTRABLE( EntityChangedUpdateType )
+
+protected:
+    EntityChangedUpdateType();
+
+public:
+    void applyUpdate( Framework::StreamReader* zReader ) override;
+};
+
+REGISTER( EntityChangedUpdateType, WorldUpdateType )

+ 5 - 0
FactoryCraft/EntityType.cpp

@@ -43,6 +43,11 @@ Entity* EntityType::loadEntity( Framework::StreamReader* zReader ) const
     return entity;
 }
 
+void EntityType::updateEntity( Entity* e, Framework::StreamReader* zReader ) const
+{
+    loadSuperEntity( e, zReader );
+}
+
 int EntityType::getId() const
 {
     return id;

+ 1 - 0
FactoryCraft/EntityType.h

@@ -20,6 +20,7 @@ protected:
 
 public:
     virtual Entity* loadEntity( Framework::StreamReader* zReader ) const;
+    virtual void updateEntity( Entity* e, Framework::StreamReader* zReader ) const;
 
     int getId() const;
 };

+ 2 - 0
FactoryCraft/FactoryCraft.vcxproj

@@ -182,6 +182,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClCompile Include="Dimension.cpp" />
     <ClCompile Include="DirectConnect.cpp" />
     <ClCompile Include="Entity.cpp" />
+    <ClCompile Include="EntityChangedUpdate.cpp" />
     <ClCompile Include="EntityType.cpp" />
     <ClCompile Include="FactoryClient.cpp" />
     <ClCompile Include="Game.cpp" />
@@ -219,6 +220,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClInclude Include="Dimension.h" />
     <ClInclude Include="DirectConnect.h" />
     <ClInclude Include="Entity.h" />
+    <ClInclude Include="EntityChangedUpdate.h" />
     <ClInclude Include="EntityType.h" />
     <ClInclude Include="FactoryClient.h" />
     <ClInclude Include="Game.h" />

+ 6 - 0
FactoryCraft/FactoryCraft.vcxproj.filters

@@ -147,6 +147,9 @@
     <ClCompile Include="ItemEntity.cpp">
       <Filter>Headerdateien\Entity</Filter>
     </ClCompile>
+    <ClCompile Include="EntityChangedUpdate.cpp">
+      <Filter>world\update</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="Area.h">
@@ -257,5 +260,8 @@
     <ClInclude Include="ItemEntity.h">
       <Filter>Headerdateien\Entity</Filter>
     </ClInclude>
+    <ClInclude Include="EntityChangedUpdate.h">
+      <Filter>world\update</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>

+ 2 - 1
FactoryCraft/StaticInitializerOrder.cpp

@@ -25,4 +25,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"

+ 11 - 0
FactoryCraft/World.cpp

@@ -219,4 +219,15 @@ Framework::Bildschirm3D* World::zScreen() const
 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;
 }

+ 1 - 0
FactoryCraft/World.h

@@ -36,4 +36,5 @@ public:
     void setVisibility( Entity* zEntity, bool visible );
     Framework::Bildschirm3D* zScreen() const;
     Framework::Punkt getChunkCenter( int x, int y ) const;
+    Entity* zEntity( int id ) const;
 };