Explorar el Código

fixed a bug that caused destroyed blocks to appear again

Kolja Strohm hace 3 años
padre
commit
09650b38f0

+ 21 - 18
FactoryCraft/BlockChangedUpdate.cpp

@@ -17,31 +17,34 @@ void BlockChangedUpdateType::applyUpdate( Framework::StreamReader* zReader )
     zReader->lese( (char*)&pos.z, 4 );
     unsigned short id;
     zReader->lese( (char*)&id, 2 );
-    bool d = 1;
-    zReader->lese( (char*)&d, 1 );
-    Block* b = currentGame->zBlockAt( pos, dimension );
-    if( !d )
+    if( id )
     {
-        if( !b || b->zBlockType()->getId() != id )
+        bool d = 1;
+        zReader->lese( (char*)&d, 1 );
+        Block* b = currentGame->zBlockAt( pos, dimension );
+        if( !d )
         {
-            if( STATIC_REGISTRY( BlockType ).zElement( id )->needsInstance() )
+            if( !b || b->zBlockType()->getId() != id )
             {
-                b = STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( pos );
-                currentGame->zDimension( dimension )->setBlock( b );
+                if( STATIC_REGISTRY( BlockType ).zElement( id )->needsInstance() )
+                {
+                    b = STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( pos );
+                    currentGame->zDimension( dimension )->setBlock( b );
+                }
+                else if( b )
+                    currentGame->zDimension( dimension )->removeBlock( b );
             }
-            else if( b )
-                currentGame->zDimension( dimension )->removeBlock( b );
         }
-    }
-    else
-    {
-        if( b )
-            STATIC_REGISTRY( BlockType ).zElement( id )->updateBlock( b, zReader );
         else
         {
-            b = STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( pos );
-            STATIC_REGISTRY( BlockType ).zElement( id )->updateBlock( b, zReader );
-            currentGame->zDimension( dimension )->setBlock( b );
+            if( b )
+                STATIC_REGISTRY( BlockType ).zElement( id )->updateBlock( b, zReader );
+            else
+            {
+                b = STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( pos );
+                STATIC_REGISTRY( BlockType ).zElement( id )->updateBlock( b, zReader );
+                currentGame->zDimension( dimension )->setBlock( b );
+            }
         }
     }
 }

+ 72 - 2
FactoryCraft/Entity.cpp

@@ -1,12 +1,82 @@
 #include "Entity.h"
+#include "Globals.h"
+
+ActionTarget::ActionTarget( Framework::Vec3<int> blockPos, Direction blockSide )
+    : blockPos( blockPos ),
+    targetBlockSide( blockSide ),
+    entityId( -1 )
+{}
+
+ActionTarget::ActionTarget( int entityId )
+    : entityId( entityId )
+{}
+
+ActionTarget* ActionTarget::load( Framework::StreamReader* zReader )
+{
+    char b;
+    zReader->lese( &b, 1 );
+    if( b == 1 )
+    {
+        int id;
+        zReader->lese( (char*)&id, 4 );
+        return new ActionTarget( id );
+    }
+    else if( b == 2 )
+    {
+        Framework::Vec3<int> pos;
+        Direction side;
+        zReader->lese( (char*)&pos.x, 4 );
+        zReader->lese( (char*)&pos.y, 4 );
+        zReader->lese( (char*)&pos.z, 4 );
+        zReader->lese( (char*)&side, 4 );
+        return new ActionTarget( pos, side );
+    }
+    return 0;
+}
+
+bool ActionTarget::isBlock() const
+{
+    return entityId == -1;
+}
+
+bool ActionTarget::isEntity() const
+{
+    return entityId >= 0;
+}
+
+int ActionTarget::getEntityId() const
+{
+    return entityId;
+}
+
+Framework::Vec3<int> ActionTarget::getBlockPos() const
+{
+    return blockPos;
+}
+
+Direction ActionTarget::getBlockSide() const
+{
+    return targetBlockSide;
+}
+
+Framework::Either<Block*, Entity*> ActionTarget::zTarget( int dimension ) const
+{
+    if( entityId >= 0 )
+        return currentGame->zEntity( entityId );
+    else
+        return currentGame->zBlockAt( blockPos, dimension );
+}
 
 
 Entity::Entity( const EntityType* zType, bool hasInventory )
-    : Model3D(), Inventory( { 0.f, 0.f, 0.f }, hasInventory ), zEntityType( zType )
+    : Model3D(), Inventory( { 0.f, 0.f, 0.f }, hasInventory ), zEntityType( zType ), target( 0 )
 {}
 
 Entity::~Entity()
-{}
+{
+    if( target )
+        delete target;
+}
 
 bool Entity::tick( double time )
 {

+ 25 - 0
FactoryCraft/Entity.h

@@ -1,9 +1,33 @@
 #pragma once
 
 #include <Model3D.h>
+#include <Either.h>
 #include "EntityType.h"
 #include "Inventory.h"
 
+class Block;
+
+class ActionTarget
+{
+private:
+    Framework::Vec3<int> blockPos;
+    Direction targetBlockSide;
+    int entityId;
+
+public:
+    ActionTarget( Framework::Vec3<int> blockPos, Direction blockSide );
+    ActionTarget( int entityId );
+
+    static ActionTarget* load( Framework::StreamReader* zReader );
+
+    bool isBlock() const;
+    bool isEntity() const;
+    int getEntityId() const;
+    Framework::Vec3<int> getBlockPos() const;
+    Direction getBlockSide() const;
+    Framework::Either<Block*, Entity*> zTarget( int dimensionId ) const;
+};
+
 class Entity : public Framework::Model3D, public Inventory
 {
 protected:
@@ -22,6 +46,7 @@ protected:
     int currentDimensionId;
     float gravityMultiplier;
     int id;
+    ActionTarget* target;
 
 public:
     Entity( const EntityType* zType, bool hasInventory );

+ 3 - 0
FactoryCraft/EntityType.cpp

@@ -30,6 +30,9 @@ void EntityType::loadSuperEntity( Entity* zEntity, Framework::StreamReader* zRea
     zReader->lese( (char*)&zEntity->location.x, 4 );
     zReader->lese( (char*)&zEntity->location.y, 4 );
     zReader->lese( (char*)&zEntity->location.z, 4 );
+    if( zEntity->target )
+        delete zEntity->target;
+    zEntity->target = ActionTarget::load( zReader );
 }
 
 void EntityType::createSuperEntity( Entity* zEntity ) const

+ 1 - 1
FactoryCraft/Main.cpp

@@ -36,7 +36,7 @@ int KSGStart Framework::Start( Framework::Startparam p )
     WNDCLASS wc = Framework::F_Normal( p.hinst );
     wc.lpszClassName = "Factory Craft";
     window.erstellen( WS_POPUPWINDOW, wc );
-    Monitor m = Framework::getMonitor( 0 );
+    Monitor m = Framework::getMonitor( 2 );
     window.setBounds( Punkt( m.x, m.y ), Punkt( m.breite, m.height ) );
     window.setAnzeigeModus( SW_SHOWNORMAL );
     window.setVSchließAktion( [&window]( void* p, void* f ) {

+ 13 - 35
FactoryCraft/Player.cpp

@@ -41,45 +41,23 @@ bool Player::tick( double time )
     if( currentGame->getCurrentPlayerId() == id )
     {
         currentGame->zKamera()->setPosition( pos + Vec3<float>( 0.f, 0.f, 1.5f ) );
-        //currentGame->zKamera()->setDirection( faceDir );
-        /*
-        char b = 0;
-        serverMessageReader->lese( &b, 1 );
-        if( hasTarget && dimensions->hat( 0 ) )
+        if( target )
         {
-            if( entityTarget == -1 )
-            {
-                Block* block = zBlockAt( target, dimensions->z( 0 )->getDimensionId() );
-                if( block )
-                    block->setAmbientFactor( block->getAmbientFactor() - 0.2f );
-            }
-        }
-        if( b == 1 )
-        {
-            serverMessageReader->lese( (char*)&entityTarget, 4 );
-            hasTarget = 1;
-        }
-        else if( b == 2 )
-        {
-            serverMessageReader->lese( (char*)&target.x, 4 );
-            serverMessageReader->lese( (char*)&target.y, 4 );
-            serverMessageReader->lese( (char*)&target.z, 4 );
-            int side = 0;
-            serverMessageReader->lese( (char*)&side, 4 );
-            hasTarget = 1;
+            auto t = target->zTarget( currentDimensionId );
+            if( t.isA() && t.getA() )
+                currentGame->setTarget( t.getA() );
+            if( t.isB() && t.getB() )
+                currentGame->setTarget( t.getB() );
+            if( (t.isA() && t.getA()) || (t.isB() && t.getB()) )
+                ((Game*)(Menu*)menuRegister->get( "game" ))->updatePosition( pos, 1, t.isA() ? t.getA()->getPos() : t.getB()->getPos() );
+            else
+                ((Game*)(Menu*)menuRegister->get( "game" ))->updatePosition( pos, 0, { 0, 0, 0 } );
         }
         else
-            hasTarget = 0;
-        if( hasTarget && dimensions->hat( 0 ) )
         {
-            if( entityTarget == -1 )
-            {
-                Block* block = zBlockAt( target, dimensions->z( 0 )->getDimensionId() );
-                if( block )
-                    block->setAmbientFactor( block->getAmbientFactor() + 0.2f );
-            }
-        }*/
-        ((Game*)(Menu*)menuRegister->get( "game" ))->updatePosition( pos, false, { 0, 0, 0 } );
+            currentGame->setTarget( 0 );
+            ((Game*)(Menu*)menuRegister->get( "game" ))->updatePosition( pos, 0, { 0, 0, 0 } );
+        }
     }
     return Entity::tick( time );
 }

+ 13 - 0
FactoryCraft/World.cpp

@@ -30,6 +30,7 @@ World::World( Bildschirm3D* zScreen )
     hasTarget = 0;
     entityTarget = -1;
     ownEntityId = -1;
+    currentTarget = 0;
     start();
 }
 
@@ -212,4 +213,16 @@ Entity* World::getCurrentPlayerEntity() const
             return e;
     }
     return 0;
+}
+
+void World::setTarget( Framework::Model3D* zTarget )
+{
+    if( zTarget != currentTarget )
+    {
+        if( currentTarget )
+            currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() - 0.2f );
+        currentTarget = zTarget;
+        if( currentTarget )
+            currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() + 0.2f );
+    }
 }

+ 2 - 0
FactoryCraft/World.h

@@ -22,6 +22,7 @@ private:
     bool hasTarget;
     bool firstMessage;
     int ownEntityId;
+    Framework::Model3D* currentTarget;
 
 public:
     World( Framework::Bildschirm3D* zScreen );
@@ -42,4 +43,5 @@ public:
     PlayerKam* zKamera() const;
     int getCurrentPlayerId() const;
     Entity* getCurrentPlayerEntity() const;
+    void setTarget( Framework::Model3D* zTarget );
 };