void Start()
    {
        mapController = GameObject.Find("Map").GetComponent <MapController>();
        ec            = GameObject.Find("Map").GetComponent <EnemyController>();

        row    = mapController.row;
        column = mapController.column;

        startPoint = new Vector2Int(mapController.CellToArray(ec.enemyStart).x, mapController.CellToArray(ec.enemyStart).y);
        endPoint   = new Vector2Int(mapController.CellToArray(ec.home).x, mapController.CellToArray(ec.home).y);


        //drawer = GameObject.Find("DrawToolkit").GetComponent<DrawToolkit>();

        SetMap(mapController.mapArray, row, column);

        //Debug.Log(startPoint + " to " + endPoint);

        defaultPath = new Path(FindPath(startPoint, endPoint));

        //PrintPath(FindPath(startPoint, endPoint));

        //DrawEnemyPath();

        //GetPathDirection(startPoint, endPoint);
    }
Example #2
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();
        //}
    }