Browse Source

automatically send new target uiml when uiml was changed during a tick

Kolja Strohm 1 year ago
parent
commit
68bfee6a33

+ 9 - 1
FactoryCraft/Block.cpp

@@ -96,6 +96,7 @@ void Block::tick(TickQueue* zQueue)
                 wasTicked = 0;
                 ticksLeftCounter--;
                 onTickCalled = 0;
+                zQueue->addToQueue(this);
                 return;
             }
             if (result)
@@ -113,7 +114,14 @@ void Block::tick(TickQueue* zQueue)
     {
         onTickCalled = 1;
         bool blocked = 0;
-        bool result = onTick(zQueue, 1, blocked);
+        onTick(zQueue, 1, blocked);
+        if (blocked)
+        {
+            wasTicked = 0;
+            onTickCalled = 0;
+            zQueue->addToQueue(this);
+            return;
+        }
     }
 }
 

+ 1 - 1
FactoryCraft/Entity.cpp

@@ -23,7 +23,7 @@ bool ActionTarget::isBlock(
     Framework::Vec3<int> blockPos, Direction blockSide) const
 {
     return this->entityId == -1 && this->blockPos == blockPos
-        && this->targetBlockSide == targetBlockSide;
+        && (this->targetBlockSide == targetBlockSide || blockSide == NO_DIRECTION);
 }
 
 bool ActionTarget::isEntity(int entityId) const

+ 1 - 1
FactoryCraft/Entity.h

@@ -80,7 +80,6 @@ protected:
         Framework::Vec3<float> location,
         int dimensionId,
         int entityId);
-    virtual void onTargetChange();
     void addMovementFrame(MovementFrame& frame);
     void calculateTarget(
         Framework::Vec3<float> basePos, Framework::Vec3<float> direction);
@@ -91,6 +90,7 @@ public:
 
     virtual void api(
         Framework::StreamReader* zRequest, NetworkMessage* zResponse);
+    virtual void onTargetChange();
 
     virtual void onFall(float collisionSpeed);
     void setPosition(Framework::Vec3<float> pos);

+ 25 - 0
FactoryCraft/Game.cpp

@@ -742,6 +742,31 @@ bool Game::doesChunkExist(int x, int y, int dimension)
     return result;
 }
 
+void Game::blockTargetChanged(Block* zBlock)
+{
+    for (GameClient* client : *this->clients)
+    {
+        if (client->zEntity()->zTarget()
+            && client->zEntity()->zTarget()->isBlock(
+                zBlock->getPos(), NO_DIRECTION))
+        {
+            client->zEntity()->onTargetChange();
+        }
+    }
+}
+
+void Game::entityTargetChanged(Entity* zEntity)
+{
+    for (GameClient* client : *this->clients)
+    {
+        if (client->zEntity()->zTarget()
+            && client->zEntity()->zTarget()->isEntity(zEntity->getId()))
+        {
+            client->zEntity()->onTargetChange();
+        }
+    }
+}
+
 Framework::Either<Block*, int> Game::zBlockAt(
     Framework::Vec3<int> location, int dimension) const
 {

+ 2 - 0
FactoryCraft/Game.h

@@ -109,6 +109,8 @@ public:
     Framework::Text createPlayer(Framework::Text name);
     GameClient* addPlayer(FCKlient* client, Framework::Text name);
     bool doesChunkExist(int x, int y, int dimension);
+    void blockTargetChanged(Block* zBlock);
+    void entityTargetChanged(Entity* zEntity);
     bool isChunkLoaded(int x, int y, int dimension) const;
     Framework::Either<Block*, int> zBlockAt(
         Framework::Vec3<int> location, int dimension) const;

+ 1 - 1
FactoryCraft/Player.h

@@ -30,11 +30,11 @@ private:
     void useItemSlot(ItemSlot* zSlot);
     Player(Framework::Vec3<float> location, int dimensionId, int entityId);
 
-    virtual void onTargetChange() override;
     Framework::Text getInventoryUIML();
     Framework::Text getPlayerGUI();
 
 public:
+    virtual void onTargetChange() override;
     void setName(Framework::Text name);
     const char* getName() const;
     void tick(const Dimension* zDimension) override;

+ 6 - 1
FactoryCraft/TreeSeblingBlock.cpp

@@ -22,7 +22,12 @@ TreeSeblingBlock::TreeSeblingBlock(int typeId,
 
 bool TreeSeblingBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
 {
+    int lastPercentage = (int)(seblingTicks / (float)seblingTicksMax * 100.f);
     seblingTicks += 1;
+    if ((int)(seblingTicks / (float)seblingTicksMax * 100.f) != lastPercentage)
+    {
+        Game::INSTANCE->blockTargetChanged(this);
+    }
     return 0;
 }
 
@@ -55,7 +60,7 @@ Framework::Text TreeSeblingBlock::getTargetUIML()
 {
     return Text("<targetInfo><text width=\"auto\" height=\"auto\">")
          + StaticRegistry<BlockType>::INSTANCE.zElement(typeId)->getName()
-         + "\n" + "Growth: " + Text((int)(seblingTicks / seblingTicksMax * 100))
+         + "\n" + "Growth: " + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
          + "%</text></targetInfo>";
 }