//刷新格子 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); } } } } }
//根据点击位置获取点中的格子 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 GridUnitRenderer CreateGridUnitRenderer() { var clone = ClonePrefab("resources/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 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); } }
//点击了技能,在以固定地点为原点的技能情况下 private void OnBattleUnitAndGridTouched_StateSkill_GridUnitTarget(GridUnit gridTouched, BattleUnit battleUnitTouched) { //如果当前有点中的格子 if (selectedGridUnitRenderer != null && (selectedGridUnitRenderer.Equals(gridTouched.gridUnitRenderer) || usedManualReleaseAnalysisor.battleSkill.GetReleaseRadius(usedManualReleaseAnalysisor.releaser.mapGrid) <= 0)) { //如果当前的释放距离为0,表示无需二次点击确认,直接释放 if (usedManualReleaseAnalysisor.battleSkill.GetReleaseRadius(usedManualReleaseAnalysisor.releaser.mapGrid) <= 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.GetReleaseRadius(usedManualReleaseAnalysisor.releaser.mapGrid)) { 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); } } }