//根据点击位置获取点中的格子 private GridUnitRenderer GetGridClicked(Vector3 clickedWorldPos) { //转换空间到格子组织节点(GridUnits)的空间 clickedWorldPos = gridUnitsRoot.transform.InverseTransformPoint(clickedWorldPos); //初步判定所在行列 int row = Mathf.FloorToInt((clickedWorldPos.y - EGameConstL.Map_GridOffsetY * 0.5f) / -EGameConstL.Map_GridOffsetY); int column = Mathf.FloorToInt((clickedWorldPos.x + EGameConstL.Map_GridWidth * 0.5f - ((row & 1) == (EGameConstL.Map_FirstRowOffset ? 1 : 0) ? 0f : (EGameConstL.Map_GridWidth * 0.5f))) / EGameConstL.Map_GridWidth); int testRow = 0; int testColumn = 0; //二次判定,判定周围格子 GridUnitRenderer clickedGrid = null; float minDis = Mathf.Infinity; for (int r = -1; r <= 1; ++r) { for (int c = -1; c <= 1; ++c) { testRow = row + r; testColumn = column + c; if (testRow < 0 || testRow >= battleField.battleMap.mapHeight || testColumn < 0 || testColumn >= battleField.battleMap.mapWidth) { continue; } float distance = UtilityHelper.CalcDistanceInXYAxis(clickedWorldPos, battleField.battleMap.mapGrids[testColumn, testRow].localPosition); if (distance < minDis && distance < EGameConstL.Map_HexRadius) { minDis = distance; clickedGrid = battleField.battleMap.mapGrids[testColumn, testRow].gridUnitRenderer; } } } return(clickedGrid); }
//刷新格子 private void RefreshBattleMapGrids() { if (battleField == null) { UtilityHelper.LogError("Prepare battle map failed. No battle data."); return; } for (int r = 0; r < battleField.battleMap.mapHeight; ++r) { for (int c = 0; c < battleField.battleMap.mapWidth; ++c) { GridUnit gridUnitData = battleField.battleMap.mapGrids[c, r]; if (gridUnitData != null) { //创建一个用于显示的格子对象 GridUnitRenderer gridUnitRenderer = GetUnusedGridUnitRenderer(); if (gridUnitRenderer != null) { gridUnitData.ConnectRenderer(gridUnitRenderer); } } } } }
/// <summary> /// 测试选中区域 /// </summary> private void TestSelectedRange(int radius) { //如果点击了鼠标左键 if (Input.GetMouseButtonDown(0)) { //计算点击位置 Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition); clickedWorldPos.z = 0; //判断是否有格子被点中? GridUnitRenderer clicked = GetGridClicked(clickedWorldPos); //点中了格子 if (clicked != null) { ClearRendererType(); List <GridUnit> rangeGrids = new List <GridUnit>(); //测试区域 battleField.battleMap.GetRangeGrids(clicked.gridUnit.row, clicked.gridUnit.column, radius, rangeGrids); foreach (var item in rangeGrids) { battleField.battleMap.mapGrids[item.column, item.row].gridUnitRenderer.GridRenderType = GridRenderType.Range; } } } }
//创建格子 private GridUnitRenderer CreateGridUnitRenderer() { var clone = ClonePrefab("prefabs/battlemapunits/gridunit.unity3d", "gridunit"); GridUnitRenderer renderer = clone.GetComponent <GridUnitRenderer>(); clone.transform.SetParent(gridUnitsRoot); clone.transform.SetUnused(false, EGameConstL.STR_Grid); renderer.Init(); gridRenderersPool.Add(renderer); return(renderer); }
/// <summary> /// 清空状态 /// </summary> /// <param name="resetOperationState">是否将操作状态设置为select(等待操作状态)</param> /// <param name="resetManualBattleUnitState">是否重置带操作战斗单位(行动中状态)</param> private void CleanState(bool resetOperationState, bool resetManualBattleUnitState) { //清除区域显示效果 SetCircularRangeRenderStateActive(false, GridRenderType.MoveRange); SetCircularRangeRenderStateActive(false, GridRenderType.SkillReleaseRange); SetCircularRangeRenderStateActive(false, GridRenderType.SkillEffectRange); //还原所有战斗单位的状态 if (usedManualReleaseAnalysisor != null) { //可以被选中的 for (int i = 0; i < usedManualReleaseAnalysisor.suitableUnits.Count; ++i) { usedManualReleaseAnalysisor.suitableUnits[i].battleUnitRenderer.UpdateRenderState(BattleUnitRenderState.Normal); } //不可以被选中的 for (int i = 0; i < usedManualReleaseAnalysisor.distanceLimit.Count; ++i) { usedManualReleaseAnalysisor.distanceLimit[i].battleUnitRenderer.UpdateRenderState(BattleUnitRenderState.Normal); } //队伍不合适的 for (int i = 0; i < usedManualReleaseAnalysisor.teamLimit.Count; ++i) { usedManualReleaseAnalysisor.teamLimit[i].battleUnitRenderer.UpdateRenderState(BattleUnitRenderState.Normal); } //无效的 for (int i = 0; i < usedManualReleaseAnalysisor.battleUnitInvalid.Count; ++i) { usedManualReleaseAnalysisor.battleUnitInvalid[i].battleUnitRenderer.UpdateRenderState(BattleUnitRenderState.Normal); } } //是否重置操作状态 if (resetOperationState) { manualOperationState = ManualOperationState.Select; } //是否重置手动操作的英雄 if (resetManualBattleUnitState && manualOperatingBattleUnitRenderer != null) { manualOperatingBattleUnitRenderer.UpdateRenderState(BattleUnitRenderState.Action); } //重置已选中项 selectedBattleUnitRenderer = null; selectedGridUnitRenderer = null; }
//更新战场点击的情况 private void UpdateBattleFieldTouched() { //如果点击了鼠标左键 if (Input.GetMouseButtonDown(0)) { //计算点击位置 Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition); clickedWorldPos.z = 0; //判断是否有格子被点中? GridUnitRenderer clicked = GetGridClicked(clickedWorldPos); if (clicked != null && clicked.gridUnit != null) { OnBattleUnitAndGridTouched(clicked.gridUnit, clicked.gridUnit.battleUnit); } } }
/// <summary> /// 测试选中区域 /// </summary> private void TestSelectedRange(int radius) { //如果点击了鼠标左键 if (Input.GetMouseButtonDown(0)) { //计算点击位置 Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition); clickedWorldPos.z = 0; //判断是否有格子被点中? GridUnitRenderer clicked = GetGridClicked(clickedWorldPos); //点中了格子 if (clicked != null) { SetRangeHighlightActive(true, clicked.gridUnit.row, clicked.gridUnit.column, radius); } } }
public void ClickedBattleField(Vector3 screenPosition) { //计算点击位置 Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(screenPosition); clickedWorldPos.z = 0; //判断是否有格子被点中? GridUnitRenderer clicked = GetGridClicked(clickedWorldPos); if (clicked != null && clicked.gridUnit != null) { OnBattleUnitAndGridTouched(clicked.gridUnit, clicked.gridUnit.battleUnit); } else { //点到了地图外,关闭所有弹出层界面 UIViewManager.Instance.HideViews(UIViewLayer.Popup); } }
/// <summary> /// 测试点中的格子 /// </summary> private void TestSelected() { //如果点击了鼠标左键 if (Input.GetMouseButtonDown(0)) { //计算点击位置 Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition); clickedWorldPos.z = 0; //判断是否有格子被点中? GridUnitRenderer clicked = GetGridClicked(clickedWorldPos); //点中了格子 if (clicked != null) { if (selectedGrid != null) { if (selectedGrid.Equals(clicked)) { //重复点中相同的格子 Debug.Log("点中了相同的格子"); return; } else { selectedGrid.GridRenderType = GridRenderType.Normal; } } selectedGrid = clicked; selectedGrid.GridRenderType = GridRenderType.Selected; } //没有点中格子,但是当前有选中,取消选中 else if (selectedGrid != null) { selectedGrid.GridRenderType = GridRenderType.Normal; selectedGrid = null; } } }
//点击了技能,在以固定地点为原点的技能情况下 private void OnBattleUnitAndGridTouched_StateSkill_GridUnitTarget(GridUnit gridTouched, BattleUnit battleUnitTouched) { //如果当前有点中的格子 if (selectedGridUnitRenderer != null && (selectedGridUnitRenderer.Equals(gridTouched.gridUnitRenderer) || usedManualReleaseAnalysisor.battleSkill.releaseRadius <= 0)) { //如果当前的释放距离为0,表示无需二次点击确认,直接释放 if (usedManualReleaseAnalysisor.battleSkill.releaseRadius <= 0) { ManualSkill(manualOperatingBattleUnitRenderer.battleUnit.mapGrid); return; } //点中了重复的格子,判断是否有目标 bool hasTarget = false; for (int i = 0; i < usedManualReleaseAnalysisor.suitableUnits.Count; ++i) { if (usedManualReleaseAnalysisor.suitableUnits[i].mapGrid.Distance(gridTouched) <= usedManualReleaseAnalysisor.battleSkill.effectRadius) { hasTarget = true; break; } } //有目标,则释放 if (hasTarget) { ManualSkill(gridTouched); } else { UtilityHelper.Log("范围内没有目标"); } } else { //这个格子不在范围内 if (manualOperatingBattleUnitRenderer.battleUnit.mapGrid.Distance(gridTouched) <= usedManualReleaseAnalysisor.battleSkill.releaseRadius) { if (selectedGridUnitRenderer != null) { //取消上一个范围显示 SetCircularRangeRenderStateActive(false, GridRenderType.SkillEffectRange); //取消可被攻击单位的显示 for (int i = 0; i < usedManualReleaseAnalysisor.suitableUnits.Count; ++i) { usedManualReleaseAnalysisor.suitableUnits[i].battleUnitRenderer.UpdateRenderState(BattleUnitRenderState.Normal); } } //设置新的范围显示 selectedGridUnitRenderer = gridTouched.gridUnitRenderer; SetCircularRangeRenderStateActive( true, GridRenderType.SkillEffectRange, gridTouched.row, gridTouched.column, usedManualReleaseAnalysisor.battleSkill.effectRadius ); //设置新的可被攻击单位的显示 for (int i = 0; i < usedManualReleaseAnalysisor.suitableUnits.Count; ++i) { if (usedManualReleaseAnalysisor.suitableUnits[i].mapGrid.Distance(gridTouched) <= usedManualReleaseAnalysisor.battleSkill.effectRadius) { usedManualReleaseAnalysisor.suitableUnits[i].battleUnitRenderer.UpdateRenderState(BattleUnitRenderState.Selected); } } } else { UtilityHelper.Log("超出区域"); UIViewManager.Instance.ShowView(UIViewName.BattleFieldUnitInfo, gridTouched, battleUnitTouched); } } }
private void TestNearestGrid() { //如果点击了鼠标左键 if (Input.GetMouseButtonDown(0)) { //计算点击位置 Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition); clickedWorldPos.z = 0; //判断是否有格子被点中? GridUnitRenderer clicked = GetGridClicked(clickedWorldPos); //点中了格子 if (clicked != null) { if (from == null || from.Equals(clicked)) { from = clicked; from.gridUnit.battleUnit = new BattleUnit(); from.AppendGridRenderType(GridRenderType.Start); } else if (to == null || to.Equals(clicked)) { to = clicked; to.gridUnit.battleUnit = new BattleUnit(); to.AppendGridRenderType(GridRenderType.End); //求最近的格子 var nearest = battleField.battleMap.GetEmptyGrid(from.gridUnit, to.gridUnit, path, -1); if (nearest == null) { UtilityHelper.LogError("Can not find out nearest grid."); from.ResetGridRenderType(); to.ResetGridRenderType(); to.gridUnit.battleUnit = null; from.gridUnit.battleUnit = null; from = null; to = null; } else { nearestGrid = nearest.gridUnitRenderer; nearestGrid.AppendGridRenderType(GridRenderType.Range); SetPathHighlightActive(true, path.ToArray()); } } else { from.ResetGridRenderType(); to.ResetGridRenderType(); nearestGrid.ResetGridRenderType(); to.gridUnit.battleUnit = null; from.gridUnit.battleUnit = null; from = null; to = null; nearestGrid = null; SetPathHighlightActive(false, null); path.Clear(); } } //没有点中格子 else { } } }
private void TestNavigation() { //如果点击了鼠标左键 if (Input.GetMouseButtonDown(0)) { //计算点击位置 Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition); clickedWorldPos.z = 0; //判断是否有格子被点中? GridUnitRenderer clicked = GetGridClicked(clickedWorldPos); //点中了格子 if (clicked != null) { if (clicked.gridUnit.GridType == GridType.Obstacle) { //点中了障碍物! Debug.Log("Clicked obstacle."); return; } if (from == null) { //当前还没有选择起始地点 from = clicked; from.AppendGridRenderType(GridRenderType.Start); } else if (to == null) { //两次点中了起点 if (from.Equals(clicked)) { return; } //当前没有选择终点 to = clicked; to.AppendGridRenderType(GridRenderType.End); UtilityHelper.TimerStart(); int navTimes = 999; int count = navTimes; while (count > 0) { //有起点有终点,开始导航 if (MapNavigator.Instance.Navigate(battleField.battleMap, from.gridUnit, to.gridUnit, path, searched)) { } else { //没有找到路径 Debug.LogError("Navitation failed. No path."); return; } --count; } SetPathHighlightActive(true, path.ToArray()); UtilityHelper.Log(string.Format("Nav times:{0}, timeCost{1:00}", navTimes, UtilityHelper.TimerEnd())); } else { from.ResetGridRenderType(); from = null; to.ResetGridRenderType(); to = null; foreach (var item in searched) { battleField.battleMap.mapGrids[item.column, item.row].gridUnitRenderer.ResetGridRenderType(); } foreach (var item in path) { battleField.battleMap.mapGrids[item.column, item.row].gridUnitRenderer.ResetGridRenderType(); } } } //没有点中格子 else { if (from != null) { from.ResetGridRenderType(); from = null; } if (to != null) { to.ResetGridRenderType(); to = null; } foreach (var item in searched) { battleField.battleMap.mapGrids[item.column, item.row].gridUnitRenderer.ResetGridRenderType(); } foreach (var item in path) { battleField.battleMap.mapGrids[item.column, item.row].gridUnitRenderer.ResetGridRenderType(); } } } }
private void TestNearestGrid() { //如果点击了鼠标左键 if (Input.GetMouseButtonDown(0)) { //计算点击位置 Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition); clickedWorldPos.z = 0; //判断是否有格子被点中? GridUnitRenderer clicked = GetGridClicked(clickedWorldPos); //点中了格子 if (clicked != null) { if (from == null || from.Equals(clicked)) { from = clicked; from.gridUnit.battleUnit = new BattleUnit(); from.GridRenderType = GridRenderType.Start; } else if (to == null || to.Equals(clicked)) { to = clicked; to.gridUnit.battleUnit = new BattleUnit(); to.GridRenderType = GridRenderType.End; //求最近的格子 var nearest = battleField.battleMap.GetEmptyGrid(from.gridUnit, to.gridUnit, path, -1); if (nearest == null) { UtilityHelper.LogError("Can not find out nearest grid."); from.GridRenderType = GridRenderType.Normal; to.GridRenderType = GridRenderType.Normal; to.gridUnit.battleUnit = null; from.gridUnit.battleUnit = null; from = null; to = null; } else { nearestGrid = nearest.gridUnitRenderer; nearestGrid.GridRenderType = GridRenderType.Range; foreach (var item in path) { if (item.Equals(to.gridUnit) || item.Equals(nearestGrid.gridUnit)) { continue; } item.gridUnitRenderer.GridRenderType = GridRenderType.Path; } } } else { from.GridRenderType = GridRenderType.Normal; to.GridRenderType = GridRenderType.Normal; nearestGrid.GridRenderType = GridRenderType.Normal; to.gridUnit.battleUnit = null; from.gridUnit.battleUnit = null; from = null; to = null; nearestGrid = null; foreach (var item in path) { item.gridUnitRenderer.GridRenderType = GridRenderType.Normal; } path.Clear(); } } //没有点中格子 else { } } }