示例#1
0
    // Update is called once per frame
    void Update()
    {
        //Show the cell size to player when he hovers over the tile
        Vector3    mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3Int cell          = MapController.instance.grid.WorldToCell(mousePosition);

        CellInfo.text = "<b>X = " + cell.x + " : " + "Y = " + cell.y + "</b>";

        pointerGO.transform.position = MapController.CellToWorld(cell.x, cell.y);
    }
示例#2
0
 public void SetPositionInCell(Vector3Int cPos)
 {
     SetPosition(mapController.CellToWorld(cPos));
 }
    private IEnumerator SimulationRoutine()
    {
        List <GameObject> runners = new List <GameObject>();
        int   maxLength           = -1;
        float scale          = 3;
        float scaleIncrement = 1f / results.Count * 2;

        foreach (List <Vector2Int> result in results)
        {
            //Instantiate the runners
            GameObject runner = Instantiate(runnerPrefab, MapController.instance.grid.transform);
            runner.transform.position = MapController.CellToWorld(MapController.instance.StartX, MapController.instance.StartY);

            //Change the size and sorting order so you can see them all (smallest one is rendered on top)
            runner.transform.localScale = new Vector3(scale - (scaleIncrement * results.IndexOf(result)), scale - (scaleIncrement * results.IndexOf(result)), 1f);
            SpriteRenderer runnerRenderer = runner.GetComponent <SpriteRenderer>();
            runnerRenderer.color        = algorithms[results.IndexOf(result)].runnerColor;
            runnerRenderer.sortingOrder = results.IndexOf(result);

            runners.Add(runner);

            //Initializing maximum length of the simulation
            if (result.Count > maxLength)
            {
                maxLength = result.Count;
            }
        }

        yield return(new WaitForSeconds(0.2f));

        for (int i = 0; i < maxLength; i++)
        {
            foreach (List <Vector2Int> result in results)
            {
                if (i < result.Count)
                {
                    runners[results.IndexOf(result)].transform.position = MapController.CellToWorld(result[i].x, result[i].y);

                    GameObject highlightGO = Instantiate(highlightPrefab, MapController.instance.grid.transform);
                    highlightGO.transform.position = MapController.CellToWorld(result[i].x, result[i].y);

                    Color highlightColor = algorithms[results.IndexOf(result)].runnerColor;
                    highlightColor.a = 0.5f;
                    SpriteRenderer highlightGoRenderer = highlightGO.GetComponent <SpriteRenderer>();
                    highlightGoRenderer.color        = highlightColor;
                    highlightGoRenderer.sortingOrder = -1;
                }
            }
            yield return(new WaitForSeconds(0.1f));
        }

        yield return(new WaitForSeconds(0.3f));

        foreach (List <Vector2Int> path in shortestPaths)
        {
            foreach (Vector2Int CellLocation in path)
            {
                GameObject runner = Instantiate(runners[shortestPaths.IndexOf(path)], MapController.instance.grid.transform);
                runner.transform.position = MapController.CellToWorld(CellLocation.x, CellLocation.y);
            }
        }

        yield return(new WaitForSeconds(1f));

        //Show game completed popup
        GameCompletedPopup.SetActive(true);


        //Record the results
        List <string> algNames    = new List <string>();
        List <int>    numOfFields = new List <int>();

        foreach (PathfindingAlgorithmBase algorithm in algorithms)
        {
            algNames.Add(algorithm.algorithmName);
        }
        foreach (List <Vector2Int> result in results)
        {
            numOfFields.Add(result.Count);
        }
        Debug.Log(OptionsController.instance.MapSize);

        RunResult runResult = new RunResult(OptionsController.instance.MapSize, OptionsController.instance.NumberOfObstacles, algNames, numOfFields, timeSpent);

        ResultsHolder.runResults.Add(runResult);
    }
示例#4
0
    // Update is called once per frame
    void Update()
    {
        if (stageController.pausing)
        {
            return;
        }


        Vector3    lPos   = Input.mousePosition;
        Vector3Int cPos   = mapController.WorldToCell(Camera.main.ScreenToWorldPoint(lPos));
        Vector3    newPos = mapController.CellToWorld(cPos);

        //如果所选地点不在场景内,不管
        if (mapController.CellOutOfBound(cPos))
        {
            mouseMark.GetComponent <RectTransform>().anchoredPosition = new Vector2(5000, 5000);
            return;
        }
        //如果在场景内
        else
        {
            //获取所选塔
            selectedTurret = ui.selectedTurret;
            //如果已经选取了塔
            if (selectedTurret != -1)
            {
                mouseMark.GetComponent <RectTransform>().anchoredPosition = Utils.GetUIPosition(newPos);
                float r     = TurretInfo.GetTurretInfo(selectableTurrets[selectedTurret]).range[0];
                float range = Utils.LengthLocalToUI(r * 2 * GameObject.Find("Map").GetComponent <MapController>().cellLocalSize);

                rangeMark.GetComponent <RectTransform>().sizeDelta = new Vector2(range, range);

                //能够造塔
                if (CanCreateTurret(newPos))
                {
                    mouseMark.GetComponent <Image>().color = Color.green;
                    //单击左键建造
                    if (Input.GetMouseButtonDown(0))
                    {
                        CreateTurret(newPos);

                        Vector3Int aPos = mapController.CellToArray(cPos);
                        mapController.mapArray[aPos.x, aPos.y] = Utils.TURRET;    //地图中此位置标记为TURRET

                        //敌人改变默认路径
                        GameObject.Find("Map").GetComponent <PathFinder>().RefreshDefaultPath();
                    }
                }
                //不能造塔
                else
                {
                    mouseMark.GetComponent <Image>().color = Color.red;

                    if (Input.GetMouseButtonDown(0))
                    {
                        AudioManager.instance.PlaySound("se_invalid");
                    }
                }
            }
            //没选中塔
            else
            {
                mouseMark.GetComponent <RectTransform>().anchoredPosition = new Vector2(5000, 5000);
                if (Input.GetMouseButtonDown(0))
                {
                    Vector3      wPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    TurretScript ts   = GetTurret(wPos);

                    if (ts != null && !GameObject.Find("TurretUpdateUI").GetComponent <TurretUpdateUIController>().Selected())
                    {
                        GameObject.Find("TurretUpdateUI").GetComponent <TurretUpdateUIController>().ShowTurretInfo(ts);
                    }
                }
            }
        }


        /*
         * //单机左键:摆放新炮塔或查看已摆放炮塔升级界面
         * if (Input.GetMouseButtonDown(0))
         * {
         *  selectedTurret = ui.selectedTurret;
         *
         *  //若已经选中炮塔:放置炮塔
         *  if(selectedTurret != -1)
         *  {
         *      //获得鼠标点击位置
         *      Vector3 lPos = Input.mousePosition;
         *      Vector3Int cPos = mapController.WorldToCell(Camera.main.ScreenToWorldPoint(lPos));
         *      Vector3 newPos = mapController.CellToWorld(cPos);
         *
         *      //如果所选地点不在场景内,不管
         *      if (mapController.CellOutOfBound(cPos)) return;
         *
         *
         *      //如果能放置,则根据选取的角色放置炮塔
         *      if (CanCreateTurret(newPos))
         *      {
         *          CreateTurret(newPos);
         *
         *          Vector3Int aPos = mapController.CellToArray(cPos);
         *          mapController.mapArray[aPos.x, aPos.y] = Utils.TURRET;    //地图中此位置标记为TURRET
         *
         *          //敌人改变默认路径
         *          GameObject.Find("Map").GetComponent<PathFinder>().RefreshDefaultPath();
         *          //Debug.Log("place a turret");
         *      }
         *      else
         *      {
         *          //AudioSource.PlayClipAtPoint(AudioManager.se_invalid,new Vector3(0,0,0));
         *          //AudioManager.instance.PlaySound("se_invalid");
         *      }
         *
         *      return;
         *  }
         *  //否则查看升级界面
         *  else
         *  {
         *
         *
         *  }
         *
         *
         * }*/

        //右键取消选择
        //if(Input.GetMouseButtonDown(1))
        //{
        //    CancelSelect();
        //}
    }