Exemplo n.º 1
0
    void LateUpdate()
    {
        UpdateMove();
        if (move)
        {
            return;
        }

        RaycastHit hit;
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
        {
            PathfindingNode node = hit.transform.GetComponent <PathfindingNode>();

            if (node == null || node.isLock && node.target == null)
            {
                return;
            }

            if (Input.GetMouseButtonDown(0))
            {
                if (node.target != null && end == null)
                {
                    if (start != null)
                    {
                        start.isPlayer = false;
                    }
                    start                    = node;
                    node.isPlayer            = true;
                    node.cost                = -1;
                    node.mesh.material.color = pathColor;
                }
                else if (end != null)
                {
                    target = start.target;
                    BuildUnitPath();
                    index = 0;
                    move  = true;
                }
            }
            else if (Input.GetMouseButtonDown(1) && start != null)
            {
                start.isPlayer = false;
                FieldUpdate();
                start = null;
                end   = null;
                hash  = 0;
            }

            if (hash != node.GetInstanceID())
            {
                if (last != null && !last.isPlayer)
                {
                    last.mesh.material.color = defaultColor;
                }
                if (!node.isPlayer)
                {
                    node.mesh.material.color = cursorColor;
                }

                if (start != null && end != null)
                {
                    FieldUpdate();
                    end = null;
                }

                if (start != null && node != null)
                {
                    end = node;

                    path = Pathfinding.Find(start, node, map, width, height);

                    if (path == null)
                    {
                        FieldUpdate();
                        start = null;
                        end   = null;
                        hash  = 0;
                        return;
                    }

                    for (int i = 0; i < path.Count; i++)
                    {
                        path[i].mesh.material.color = pathColor;
                    }
                }
            }

            last = node;
            hash = node.GetInstanceID();
        }
    }