Exemple #1
0
    void Update()
    {
        InputHandler();

        if (started)
        {
            if (!TickUtil.Tick())
            {
                return;
            }

            if (!status.Finished)
            {
                status = controller.Execute();
                grid   = controller.grid;
            }
            else
            {
                SolutionUtil.Execute(status.SolvedPath, grid);
            }
        }

        RenderGrid();
    }
    void Update()
    {
        Test();

        var mouse = 0;

        if (Input.GetMouseButton(0))
        {
            mouse = 1;
        }
        if (Input.GetMouseButton(1))
        {
            mouse = 2;
        }

        if (mouse > 0)
        {
            Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            var tpos = tilemap.WorldToCell(pos);
            if (mouse == 1)
            {
                tilemap.SetTile(tpos, wallTile);
            }
            else
            {
                tilemap.SetTile(tpos, emptyTile);
            }
        }
        if (Input.GetMouseButtonDown(2))
        {
            init = true;
        }

        if (!init)
        {
            return;
        }

        if (TickUtil.Tick())
        {
            if (found)
            {
                if (resultQueue.Count == 0)
                {
                    return;
                }

                var tpos = resultQueue.Dequeue();
                tilemap.SetTile(tpos, targetTile);
                return;
            }

            var node = queue.Dequeue();
            if (!first)
            {
                while (tilemap.GetTile(node.pos) == filledTile || tilemap.GetTile(node.pos) == wallTile || tilemap.GetTile(node.pos) == null || tilemap.GetTile(node.pos) == sourceTile)
                {
                    if (queue.Count == 0)
                    {
                        return;
                    }
                    node = queue.Dequeue();
                }
            }

            if (tilemap.GetTile(node.pos) == targetTile)//FOUND
            {
                found = true;
                var parent = node.parent;
                var count  = 0;
                while (parent != null)
                {
                    resultQueue.Enqueue(parent.pos);
                    parent = parent.parent;
                    count++;
                }
                Debug.LogWarning("TOTAL: " + count);
                return;
            }

            if (node.pos.x > tilemap.cellBounds.xMin)
            {
                var targetPos = new Vector3Int(node.pos.x - 1, node.pos.y, 0);
                var target    = tilemap.GetTile(targetPos);
                if (target != filledTile && target != wallTile)
                {
                    queue.Enqueue(new Node(targetPos, node));
                }
            }
            if (node.pos.x < tilemap.cellBounds.xMax)
            {
                var targetPos = new Vector3Int(node.pos.x + 1, node.pos.y, 0);
                var target    = tilemap.GetTile(targetPos);
                if (target != filledTile && target != wallTile)
                {
                    queue.Enqueue(new Node(targetPos, node));
                }
            }
            if (node.pos.y > tilemap.cellBounds.yMin)
            {
                var targetPos = new Vector3Int(node.pos.x, node.pos.y - 1, 0);
                var target    = tilemap.GetTile(targetPos);
                if (target != filledTile && target != wallTile)
                {
                    queue.Enqueue(new Node(targetPos, node));
                }
            }
            if (node.pos.y < tilemap.cellBounds.yMax)
            {
                var targetPos = new Vector3Int(node.pos.x, node.pos.y + 1, 0);
                var target    = tilemap.GetTile(targetPos);
                if (target != filledTile && target != wallTile)
                {
                    queue.Enqueue(new Node(targetPos, node));
                }
            }

            tilemap.SetTile(node.pos, filledTile);

            if (first)
            {
                tilemap.SetTile(node.pos, sourceTile);
                first = false;
            }
        }
    }