//创建格子 public void GenerateGrid() { //创建一个网格根节点 GridRoot = new GameObject("GridRoot"); GridRoot.transform.position = Vector2.zero; //根据摄像机的scale,以及网格总数,获取单元网格的大小 Camera cam = Camera.main; perUnitScale = (cam.orthographicSize * 2 * cam.aspect - margin) / RowCount; //设置网格根节点的起始点 Vector2 InitOffset = new Vector2(-perUnitScale * (RowCount - 1) / 2f, -perUnitScale * (ColumnCount - 1) / 2f); GridRoot.transform.position = InitOffset; //生成网格与所有绿色格子,并关掉所有的绿色格子 GreenCells = new Cell[TotalGrids]; Vector2 pos = Vector2.zero; for (int i = 0; i < TotalGrids; i++) { pos.y = i / RowCount * perUnitScale; pos.x = i % RowCount * perUnitScale; GameObject grid = Instantiate(GridPrefab); grid.transform.parent = GridRoot.transform; grid.transform.localPosition = pos; grid.transform.localPosition += Vector3.forward; grid.transform.localScale *= perUnitScale; grid.GetComponent <Cell> ().SetIndex(i % RowCount, i / RowCount); GreenCells[i] = Instantiate(PlayerGreenPrefab).GetComponent <Cell> (); GreenCells[i].transform.parent = GridRoot.transform; GreenCells[i].transform.localPosition = pos; GreenCells[i].transform.localScale *= perUnitScale; GreenCells[i].SetIndex(i % RowCount, i / RowCount); GreenCells[i].DisableCell(); } //放置红色格子 Vector2Int index_red = new Vector2Int(Random.Range(0, RowCount), Random.Range(0, RowCount)); VirusRed = Instantiate(VirusRedPrefab).GetComponent <Cell> (); VirusRed.transform.parent = GridRoot.transform; VirusRed.transform.localPosition = new Vector2(index_red.x * perUnitScale, index_red.y * perUnitScale); VirusRed.transform.localScale *= perUnitScale; VirusRed.SetIndex(index_red); VirusSpawner = VirusRed.GetComponent <SpawnGreyVirus> (); GreyCells = new List <Cell> (); virusCount = 1; Steps = 0; WaitForInput = true; }
//更新红色格子的位置 protected void UpdateVirusRed() { VirusRed.PrepareStep(); if (IfPosGreen(VirusRed.nextIndex) || IfPosGrey(VirusRed.nextIndex)) { VirusRed.Reset(); } else { VirusSpawner.SpawnGreyCellOnCurrentPos(); VirusRed.Step(); } }