Пример #1
0
    // Update is called once per frame
    void Update()
    {
        // nameDisplay.text = objectName
        cameraMovement = CameraController.GetComponent <CameraMovement>();
        cursorMovement = PointerMove.GetComponent <CursorMovement>();
        isHover        = cursorMovement.getIsOverPlayer();

        selectPlayer = Pointer.GetComponent <SelectPlayer>();
        isSelected   = selectPlayer.getIsSelected();

        if (isSelected)
        {
            PlayerUnit    = selectPlayer.GetSelectedObject();
            playerControl = PlayerUnit.GetComponent <PlayerController>();
            isMoving      = playerControl.getIsMoving();
        }

        showUnitInfo(isSelected, isHover);
        showUnitCommand(isSelected, isMoving);
        if (isSelected && !isMoving)
        {
            unitCommand();
        }
    }
Пример #2
0
    public void GeneratePathTo(int x, int y)
    {
        SelectPlayer sp     = Pointer.GetComponent <SelectPlayer>();
        GameObject   player = sp.GetSelectedObject();

        player.GetComponent <PlayerMovement>().currentPath = null;

        Dictionary <Node, float> distance = new Dictionary <Node, float>();
        Dictionary <Node, Node>  prev     = new Dictionary <Node, Node>();

        List <Node> unvisited = new List <Node>();

        Node source = graph[
            (int)(player.transform.position.x + 4.5f),
            (int)(player.transform.position.z + 4.5f)];

        Node target = graph[
            x,
            y];

        distance[source] = 0;
        prev[source]     = null;

        foreach (Node vertex in graph)
        {
            if (vertex != source)
            {
                distance[vertex] = Mathf.Infinity;
                prev[vertex]     = null;
            }

            unvisited.Add(vertex);
        }

        while (unvisited.Count > 0)
        {
            Node u = null;

            foreach (Node possibleU in unvisited)
            {
                if (u == null || distance[possibleU] < distance[u])
                {
                    u = possibleU;
                }
            }

            if (u == target)
            {
                break;
            }

            unvisited.Remove(u);

            foreach (Node v in u.edges)
            {
                float alt = distance[u] + 1;
                if (alt < distance[v])
                {
                    distance[v] = alt;
                    prev[v]     = u;
                }
            }
        }

        if (prev[target] == null)
        {
            return;
        }

        currentPath = new List <Node>();
        Node curr = target;

        while (curr != null)
        {
            currentPath.Add(curr);
            curr = prev[curr];
        }

        currentPath.Reverse();

        player.GetComponent <PlayerMovement>().currentPath = currentPath;
    }