void ResetMap() { //先只清空寻路 if (m_aStar.isInit) { m_aStar.Clear(); return; } //再次点击清空地图设置 if (m_player != null) { m_player.state = GridState.Default; } m_player = null; if (m_destination != null) { m_destination.state = GridState.Default; } m_destination = null; foreach (var grid in m_obstacleDic.Values) { grid.state = GridState.Default; } m_obstacleDic.Clear(); }
//初始化网格 void InitMap() { if (m_map != null) { return; } Int2 offset = new Int2(50 + gridSize / 2, 50 + gridSize / 2); m_mapSize = new Int2((Screen.width - 100) / gridSize, (Screen.height - 200) / gridSize); m_map = new UIGridController[m_mapSize.x, m_mapSize.y]; for (int i = 0; i < m_mapSize.x; i++) { for (int j = 0; j < m_mapSize.y; j++) { UIGridController grid = Instantiate(gridPrefab, gridParent); grid.rectTransform.sizeDelta = new Vector2(gridSize, gridSize); grid.rectTransform.anchoredPosition = new Vector2(gridSize * i + offset.x, gridSize * j + offset.y); grid.rectTransform.localScale = Vector3.one; grid.gameObject.SetActive(true); grid.Init(new Int2(i, j), isShowGridHint, OnGridClicked); m_map[i, j] = grid; } } }
//单击某个网格,设置对应状态 void OnGridClicked(UIGridController grid) { if (m_settingState == GridState.Player) { grid.state = GridState.Player; if (m_player != null) { m_player.state = GridState.Default; } m_player = grid; SetHint(); m_settingState = GridState.Default; } else if (m_settingState == GridState.Destination) { grid.state = GridState.Destination; if (m_destination != null) { m_destination.state = GridState.Default; } m_destination = grid; SetHint(); m_settingState = GridState.Default; } else if (m_settingState == GridState.Obstacle) { if (grid.state == GridState.Default) { grid.state = GridState.Obstacle; m_obstacleDic[grid.position] = grid; } else if (grid.state == GridState.Obstacle) { grid.state = GridState.Default; m_obstacleDic.Remove(grid.position); } } }
public UIGridController <TItem> GetController <TItem>() where TItem : UIGridItem, new() { UIGridController <TItem> controller = m_controller as UIGridController <TItem>; return(controller); }