Exemple #1
0
    public void HandlePathFindingInput(Vector3 blockCenter, int3 block)
    {
        if (!didStartPathFinding)
        {
            didStartPathFinding = true;
            Vector3 pos = Utils.CentrifyPosition(new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z));
            currentPathFinding = new PathFinding(pos, blockCenter);
            if (isOnManualPathFinding)
            {
                var moves = currentPathFinding.OpenPossibleNewMoves(pos);
                RemoveFromBlocksUIIfExists(moves);
                foreach (var node in moves)
                {
                    var bUI = BlockUI.Create(Utils.WorldBlockPosForCenter(node.pos), int3.zero, String.Format("{0}\n{1}\n{2}", node.startValue, node.value, node.endValue));
                    blocksUI.Add(bUI);
                }
                var sbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(pos), int3.zero, "Start");
                sbUI.ToggleSelection();
                blocksUI.Add(sbUI);
                var fbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(blockCenter), int3.zero, "Finish");
                fbUI.ToggleSelection();
                blocksUI.Add(fbUI);
            }
            else
            {
                StartCoroutine(currentPathFinding.BuildPathToTarget(() =>
                {
                    if (currentPathFinding.isClosed)
                    {
                        foreach (var bbbUI in blocksUI)
                        {
                            GameObject.Destroy(bbbUI.gameObject);
                        }
                        foreach (var node in currentPathFinding.pathNodes)
                        {
                            var bbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(node.pos), int3.zero, String.Format("{0}\n{1}\n{2}", node.startValue, node.value, node.endValue));
                            blocksUI.Add(bbUI);
                        }
                        var sbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(currentPathFinding.startPos), int3.zero, "Start");
                        sbUI.ToggleSelection();
                        blocksUI.Add(sbUI);
                        var fbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(currentPathFinding.endPos), int3.zero, "Finish");
                        fbUI.ToggleSelection();
                        blocksUI.Add(fbUI);
                    }
                    else
                    {
                        var poppedBottom = currentBlockDestroying.PopBottomBlock();
                        selectionController.Deselect();
                        aiState.mode = AIStateMode.Idle;
                        currentBlockDestroying.UpdateDestroyBlockQueueUI();
                    }
                }));
            }
        }
        else if (blocksUI.Find(b => b.pos.Equals(blockCenter)) != null)
        {
            var n   = currentPathFinding.SuggestNextNode();
            var bUI = blocksUI.Find(b => b.pos.Equals(n.pos));
            bUI.ToggleSelection();
            var moves = currentPathFinding.OpenPossibleNewMoves(bUI.pos, n);

            RemoveFromBlocksUIIfExists(moves);
            foreach (var node in moves)
            {
                var bbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(node.pos), int3.zero, String.Format("{0}\n{1}\n{2}", node.startValue, node.value, node.endValue));
                blocksUI.Add(bbUI);
            }
            if (currentPathFinding.CheckIfEnd(n))
            {
                foreach (var bbbUI in blocksUI)
                {
                    GameObject.Destroy(bbbUI.gameObject);
                }
                foreach (var node in currentPathFinding.pathNodes)
                {
                    var bbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(node.pos), int3.zero, String.Format("{0}\n{1}\n{2}", node.startValue, node.value, node.endValue));
                    blocksUI.Add(bbUI);
                }
                var sbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(currentPathFinding.startPos), int3.zero, "Start");
                sbUI.ToggleSelection();
                blocksUI.Add(sbUI);
                var fbUI = BlockUI.Create(Utils.WorldBlockPosForCenter(currentPathFinding.endPos), int3.zero, "Finish");
                fbUI.ToggleSelection();
                blocksUI.Add(fbUI);
            }
        }
        else
        {
            didStartPathFinding = false;
            foreach (var bUI in blocksUI)
            {
                GameObject.Destroy(bUI.gameObject);
            }
            blocksUI = new List <BlockUI>();
        }
    }