//生成战斗小组 private void GenerateBattleTeam(int battleUnitCount) { int teamCount = 2; //创建两支队伍 for (int i = 0; i < teamCount; ++i) { //添加到地图中 AddBattleTeam(BattleTeamManager.Instance.CreateBattleTeam()); } if (battleUnitCount > battleMap.BornCount) { UtilityHelper.LogWarning(string.Format("Generate battle units warning.Not enough born points. {0}/{1}", battleUnitCount, battleMap.BornCount)); battleUnitCount = battleMap.BornCount; } //为两支队伍添加战斗单位 for (int i = 0; i < teamCount; ++i) { BattleTeam battleTeam = teams[i]; if (battleTeam != null) { for (int j = 0; j < battleUnitCount; ++j) { //创建战斗单位 BattleUnit battleUnit = BattleUnitManager.Instance.CreateUnit(); //加入队伍 battleTeam.AddBattleUnit(battleUnit); //设置敌队 battleUnit.enemyTeam = teams[1 - i]; } } } }
//点击了技能按钮 private void OnClickedSkillItem() { //获取当前点击对象 string btnName = EventSystem.current.currentSelectedGameObject.name; int skillIdx = -1; if (int.TryParse(btnName.Replace(EGameConstL.STR_SkillBtn, string.Empty), out skillIdx)) { SO_BattleSkill skill = battleUnit.battleUnitAttribute.battleSkills[skillIdx]; if (skill != null) { if (battleUnit.battleUnitAttribute.energy >= skill.energyCost && BattleFieldRenderer.Instance != null) { BattleFieldRenderer.Instance.BattleUnitUseSkill(battleUnit, skill); } else { UtilityHelper.LogWarning(string.Format("能量不足:{0}/{1}", battleUnit.battleUnitAttribute.energy, skill.energyCost)); } } else { UtilityHelper.LogError("Skill item error ->" + btnName); } } else { UtilityHelper.LogError("Skill item name error ->" + btnName); } }
/// <summary> /// 尝试使用道具 /// </summary> /// <param name="itemID"></param> /// <param name="useCount"></param> /// <returns>使用数量</returns> public int TryUseItem(int itemID, int useCount, ref int finalCount) { if (items == null || items.Count == 0) { UtilityHelper.LogError(string.Format("Use item failed. Do not have this item -> {0}", itemID)); return(0); } for (int i = items.Count - 1; i >= 0; --i) { if (items[i].item.itemID == itemID) { //使用道具 if (items[i].count < useCount) { UtilityHelper.LogWarning(string.Format("Use item warning. {0}/{1}", useCount, items[i].count)); useCount = items[i].count; } //使用道具 items[i].count -= useCount; //判断是否还有剩余 if (items[i].count <= 0) { items[i].Reset(); } finalCount = items[i].count; return(useCount); } } UtilityHelper.LogError(string.Format("Use item failed. Do not have this item -> {0}", itemID)); return(0); }
//运行英雄动作 public IEnumerator RunHeroAction(BattleUnitActionEvent heroAction) { if (heroAction == null) { yield break; } switch (heroAction.battleUnitActionType) { case BattleUnitActionType.EnterBattleField: yield return(PlayEnterBattleFieldAction(heroAction as BattleUnitEnterBattleFieldAction)); break; case BattleUnitActionType.ChangeTarget: yield return(PlayChangeTargetAction(heroAction as BattleUnitChangeTargetAction)); break; case BattleUnitActionType.MoveToTarget: yield return(PlayMotionAction(heroAction as BattleUnitMotionAction)); break; case BattleUnitActionType.UseSkill: yield return(PlaySkillAction(heroAction as BattleUnitSkillAction)); break; case BattleUnitActionType.AttributeUpdate: yield return(PlayAttributeUpdateAction(heroAction as BattleUnitAttributeUpdate)); break; case BattleUnitActionType.ManualOperate: yield return(PlayManualAction(heroAction as BattleUnitManualAction)); break; case BattleUnitActionType.PickupItem: yield return(PlayPickupItemAction(heroAction as BattleUnitPickupItemAction)); break; case BattleUnitActionType.UseItem: yield return(PlayUseItemAction(heroAction as BattleUnitUseItemAction)); break; case BattleUnitActionType.Warning: UtilityHelper.LogWarning(heroAction.ToString()); yield return(EGameConstL.WaitForTouchScreen); break; default: break; } yield return(null); }
/// <summary> /// 尝试添加道具 /// </summary> /// <param name="itemID"></param> /// <param name="addition"></param> /// <returns>是否成功</returns> public bool TryAddItem(int itemID, int addition, ref int finalCount) { if (items == null) { items = new List <PackageItem>(); } BattleUnitPickupItemAction action = null; PackageItem emptyItem = null; for (int i = 0; i < items.Count; i++) { if (items[i].item != null && items[i].item.itemID == itemID) { items[i].count += addition; finalCount = items[i].count; return(true); } else if (items[i].item == null) { emptyItem = items[i]; } } //找到了一个空位 if (emptyItem != null) { emptyItem.item = PackageItemManager.Instance.GetItem(itemID); if (emptyItem.item != null) { emptyItem.count += addition; finalCount = emptyItem.count; return(true); } } //背包已满 if (items.Count >= capacity) { UtilityHelper.LogWarning(string.Format("Add item failed. Package if full : {0} -> {1}/{2}", itemID, items.Count, capacity)); return(false); } //添加一个道具 emptyItem = PackageItem.CreateInstance(); emptyItem.item = PackageItemManager.Instance.GetItem(itemID); if (emptyItem.item != null) { items.Add(emptyItem); } emptyItem.count += addition; finalCount = emptyItem.count; return(true); }
//运行英雄动作 public IEnumerator RunHeroAction(BattleUnitAction heroAction) { if (heroAction == null) { yield break; } //进入战场 if (heroAction.enterBattleFieldAction != null) { yield return(PlayEnterBattleFieldAction(heroAction.enterBattleFieldAction)); } //恢复能量 if (heroAction.attributeUpdate != null) { yield return(PlayAttributeUpdateAction(heroAction.attributeUpdate)); } //切换目标 if (heroAction.changeTargetAction != null) { yield return(PlayChangeTargetAction(heroAction.changeTargetAction)); } //移动 if (heroAction.motionAction != null) { yield return(PlayMotionAction(heroAction.motionAction)); } //使用技能 if (heroAction.skillAction != null) { yield return(PlaySkillAction(heroAction.skillAction)); } //手动操作 if (heroAction.manualAction != null) { yield return(PlayManualAction(heroAction.manualAction)); } //警告动作 if (heroAction.warningAction != null) { UtilityHelper.LogWarning(heroAction.ToString()); yield return(EGameConstL.WaitForTouchScreen); } yield return(null); }
//注册 public RegEventResult AddEventHandler(object _ref, GameEventHandler _handler, int _time) { #if UNITY_EDITOR //编辑器模式需要做一个重复检查 for (int i = 0; i < eventInfos.Count; i++) { if (eventInfos[i].handler == _handler && eventInfos[i].listener == _ref) { eventInfos[i].time = _time; UtilityHelper.LogWarning(string.Format("Add event --> {0} <-- handler repeatedly !", eventKey)); return(RegEventResult.Failed); } } #endif eventInfos.Add(new EventInfo(_ref, _handler, _time)); gameEvent += _handler; return(RegEventResult.Success); }
//注册 public RegEventResult AddEventHandler(string listener, GameEventHandler handler, int time) { #if UNITY_EDITOR //编辑器模式需要做一个重复检查 for (int i = 0; i < eventInfos.Count; i++) { if (eventInfos[i].handler == handler && string.Compare(eventInfos[i].listener, listener, true) == 0) { eventInfos[i].time = time; UtilityHelper.LogWarning(string.Format("Add event --> {0} <-- handler repeatedly !", eventKey)); return(RegEventResult.Failed); } } #endif eventInfos.Add(new EventInfo(listener, handler, time)); gameEvent += handler; return(RegEventResult.Success); }
//开始战斗 private void Fight() { battleState = BattleState.Fighting; Debug.Log(string.Format("<color=#ff0000> {0} battle fight. </color>", this.ToString())); List <BattleAction> actions = new List <BattleAction>(); BattleUnit actionUnit = null; do { //没有连接渲染器,则一步一更新 //连接了渲染器,则一直更新直到结束 actionUnit = actionQueue.Dequeue(); if (actionUnit == null) { battleState = BattleState.End; break; } if (actionUnit.CanAction) { HeroActionState state = actionUnit.BattleAction(actions); switch (state) { case HeroActionState.BattleEnd: battleState = BattleState.End; break; case HeroActionState.Error: battleState = BattleState.Exception; break; case HeroActionState.Warn: UtilityHelper.LogWarning(string.Format("Warning: battle action state warning -> {0}", actionUnit.ToString())); break; case HeroActionState.WaitForPlayerChoose: battleState = BattleState.WaitForPlayer; break; default: break; } //追加动作 AppendBattleActions(actions); //考虑是否在放回队列 CalculateNextAction(actionUnit); } ++actionCount; if (actionCount >= EGameConstL.BattleFieldMaxActions) { battleState = BattleState.Exception; } } while (battleFieldRenderer == null && battleState != BattleState.End && battleState != BattleState.Exception); //连接了渲染器,一步一更新 if (battleFieldRenderer != null) { Debug.Log("Play actions"); if (battleState == BattleState.End || battleState == BattleState.Exception) { battleFieldRenderer.PlayBattle(BattleEnd); } else if (battleState == BattleState.WaitForPlayer) { battleFieldRenderer.PlayBattle(null); } else { battleFieldRenderer.PlayBattle(Fight); } } else { //没有连接渲染器,自动战斗 BattleEnd(); } }
//搜索目标 private TargetSearchResult SearchTarget(List <BattleAction> actions) { //按照距离排序敌人 UtilityObjs.battleUnits.Clear(); //只考虑可以行动的 for (int i = 0; i < enemyTeam.battleUnits.Count; ++i) { if (enemyTeam.battleUnits[i].CanAction) { UtilityObjs.battleUnits.Add(enemyTeam.battleUnits[i]); } } //天下无敌了,还有谁?? if (UtilityObjs.battleUnits.Count == 0) { return(TargetSearchResult.Inexistence); } //结果类型 TargetSearchResult searchResult = TargetSearchResult.InRange; //按照距离排序 UtilityObjs.battleUnits.Sort(delegate(BattleUnit b1, BattleUnit b2) { return(mapGrid.Distance(b1.mapGrid) - mapGrid.Distance(b2.mapGrid)); }); //暂时不添加复杂的逻辑,只选择直线距离最近的 BattleUnit newTarget = null; GridUnit newTargetGrid = null; for (int i = 0; i < UtilityObjs.battleUnits.Count; ++i) { //如果当前目标就在范围内 if (mapGrid.Distance(UtilityObjs.battleUnits[i].mapGrid) <= 1) { //设置目标,但是不需要移动 newTarget = UtilityObjs.battleUnits[i]; toTargetPath.Clear(); targetGrid = null; searchResult = TargetSearchResult.InRange; break; } //目标不在周围需要移动 newTargetGrid = battleField.battleMap.GetEmptyGrid(mapGrid, UtilityObjs.battleUnits[i].mapGrid, toTargetPath, mobility); if (newTargetGrid == null) { //UtilityHelper.LogWarning(battleUnitID + "找不到空格子了,看看下一个吧"); continue; } else { newTarget = UtilityObjs.battleUnits[i]; searchResult = TargetSearchResult.NeedMove; break; } } if (newTarget == null) { UtilityHelper.LogWarning("确实找不到了"); targetBattleUnit = null; targetGrid = null; toTargetPath.Clear(); if (actions != null) { //创建一个warning BattleHeroWarningAction action = new BattleHeroWarningAction(this, "No target:" + battleUnitID); actions.Add(action); } return(TargetSearchResult.Inexistence); } //目标不一致,切换目标 if (targetBattleUnit != newTarget) { //切换目标 BattleHeroChangeTargetAction action = new BattleHeroChangeTargetAction(this); action.lastTargetUnit = targetBattleUnit; action.newTargetUnit = newTarget; //设置当前目标以及格子 targetBattleUnit = newTarget; actions.Add(action); } //移动的格子重新设置 targetGrid = newTargetGrid; return(searchResult); }
//开始战斗 private void Fight() { battleState = BattleState.Fighting; BattleUnit actionUnit = null; do { //连接渲染器,则一步一更新 //没有连接渲染器,则一直计算直到结束 actionUnit = actionQueue.Dequeue(); if (actionUnit == null) { battleState = BattleState.End; break; } if (actionUnit.CanAction) { BattleUnitAction battleUnitAction = BattleUnitAction.Create(actionUnit); HeroActionState state = actionUnit.BattleAction(); switch (state) { case HeroActionState.Normal: battleState = BattleState.Fighting; break; case HeroActionState.WaitForPlayerChoose: battleState = BattleState.WaitForPlayer; break; case HeroActionState.Error: battleState = BattleState.Exception; break; case HeroActionState.Warn: battleState = BattleState.Exception; UtilityHelper.LogWarning(string.Format("Warning: battle action state warning -> {0}", actionUnit.ToString())); break; default: break; } } //考虑是否在放回队列 CalculateNextAction(actionUnit); if (battleActions.Count > EGameConstL.BattleFieldMaxActions) { UtilityHelper.LogError("Battle actions overflow max limit."); battleState = BattleState.Exception; } else { //只在这种情况下做战斗结束的判断 if (!actionUnit.CanAction || actionUnit.targetBattleUnit == null || !actionUnit.targetBattleUnit.CanAction) { CheckBattleEnd(); } } } while (battleFieldRenderer == null && battleState != BattleState.End && battleState != BattleState.Exception); //连接了渲染器,一步一表现 if (battleFieldRenderer != null) { if (battleState == BattleState.WaitForPlayer) { battleFieldRenderer.PlayBattle(null); } else { battleFieldRenderer.PlayBattle(Run); } } else { Run(); } }