コード例 #1
0
ファイル: AIController.cs プロジェクト: igyvigy/idle-craft
 void Update()
 {
     if (clearAll)
     {
         clearAll = false;
         currentBlockDestroying.ClearQueue();
         aiState.mode = AIStateMode.Idle;
         currentPathFinding.Clear();
         didStartPathFinding = false;
         foreach (var bUI in blocksUI)
         {
             GameObject.Destroy(bUI.gameObject);
         }
         blocksUI = new List <BlockUI>();
     }
     if (!currentBlockDestroying.hasBlocksToDestroy)
     {
         selectionController.Deselect();
     }
     if (currentBlockDestroying.hasBlocksToDestroy && isIdleing)
     {
         int3x3  data          = currentBlockDestroying.nextBlockData.Value;
         int3    block         = currentBlockDestroying.nextBlock.Value;
         int3    blockHealth   = currentBlockDestroying.nextBlockHealth.Value;
         int3    blockWorldPos = currentBlockDestroying.nextBlockWorldPos.Value;
         Vector3 blockCenter   = Utils.CenterOfBlockWithWorldPos(blockWorldPos);
         if (Utils.IsTargetReachable(transform.position, blockCenter))
         {
             // start destroying
             int maxHp = Mathf.RoundToInt(Block.GetMaxHealth(block));
             ModifyBlockHealth(data, 0, new int3(maxHp, maxHp, 0));
             int3 coord = Utils.CoordByPosition(blockCenter);
             selectionController.SelectBlockAt(block, blockWorldPos);
             StartCoroutine(DestroyBlock(() =>
             {
                 int4 chunkPos = Utils.ChunkPosbyPosition(blockCenter);
                 Chunk chunk   = WorldSettings.Chunks[chunkPos];
                 int index     = Utils.to1D(coord);
                 if (chunk.CanDestroyBlockAt(index))
                 {
                     Block b = chunk.DestroyBlockAt(index);
                     foreach (Item i in b.DropItems())
                     {
                         inventory.Add(i);
                     }
                 }
                 ;
             }));
         }
         else
         {
             //move closer
             aiState.mode = AIStateMode.MoveToTarget;
             Vector3?closestPathPos     = null;
             var     availablePathNodes = PathFinding.ClosestPathBlockTowardsPos(blockCenter, transform.position);
             if (availablePathNodes.Count > 0)
             {
                 closestPathPos = availablePathNodes[0].pos;
             }
             if (closestPathPos != null)
             {
                 var pathFinding = new PathFinding(new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z), closestPathPos.Value);
                 StartCoroutine(pathFinding.BuildPathToTarget(() =>
                 {
                     if (pathFinding.isClosed)
                     {
                         aiState.mode = AIStateMode.MoveToTarget;
                         playerMovement.ReachToPoint(closestPathPos.Value, pathFinding);
                     }
                     else
                     {
                         var poppedBottom = currentBlockDestroying.PopBottomBlock();
                         selectionController.Deselect();
                         aiState.mode = AIStateMode.Idle;
                         currentBlockDestroying.UpdateDestroyBlockQueueUI();
                     }
                 }));
             }
             else
             {
                 var poppedBottom = currentBlockDestroying.PopBottomBlock();
                 selectionController.Deselect();
                 aiState.mode = AIStateMode.Idle;
                 currentBlockDestroying.UpdateDestroyBlockQueueUI();
             }
         }
     }
     ;
 }