//Add Buff public void addBuff(Role role, Buff buff) { if (role.getBuff(buff.Key) == null) { role.buffs.Add(buff.Copy()); battleResult.Actions.Add(BattleActionView.AddBuff(role, buff)); } }
//计算放招的消耗 public void performSkillCost(Role role, Skill skill) { role.Balls -= skill.RequireBall; //有变化才记录 if (skill.RequireBall != 0) { battleResult.Actions.Add(BattleActionView.AddBall(role, -skill.RequireBall)); } }
//位置转移技能 public static BattleActionView Position(Role role, int prev, int pos) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "POSITION"; action.Result.Value = pos.ToString(); return(action); }
//一系列的战斗序列呈现的函数 //展现招式 public static BattleActionView CastSkill(Role role, Skill skill) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "CASTSKILL"; action.Result.Value = skill.Key + "#" + skill.Level; return(action); }
//展现打中的效果:群体 //TODO //展现属性变化,只需要HP! public static BattleActionView CostHP(Role role, int hp) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "HP"; action.Result.Value = hp.ToString(); return(action); }
//减少BUFF public static BattleActionView RemoveBuff(Role role, Buff buff) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "REMOVEBUFF"; action.Result.Value = buff.Key + "." + buff.Power + "." + buff.Round; return(action); }
//幻影 public static BattleActionView Shadow(Role role, int Pos) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "SHADOW"; action.Result.Value = Pos.ToString(); return(action); }
//增加攻击系数 public static BattleActionView AddAttackRatio(Role role, float attackRatio) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "ATTACKRATIO"; action.Result.Value = attackRatio.ToString(); return(action); }
//死亡 public static BattleActionView Die(Role role) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "DIE"; action.Result.Value = ""; return(action); }
public void powerUp(Role role, float attackRatio) { float ratio = attackRatio; if (ratio > 4.0f) { ratio = 4.0f; } role.attackRatio = ratio; battleResult.Actions.Add(BattleActionView.AddAttackRatio(role, ratio)); }
//天赋:奋不顾身 public static BattleActionView Defend(Role role, Role originalTarget) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "DEFEND"; action.Result.Value = originalTarget.Position.ToString(); return(action); }
//加Ball public static BattleActionView AddBall(Role role, int ball) { BattleActionView action = new BattleActionView(); action.SourceRole = role.Key; action.SourceRoleId = role.Id; action.Result = new BattleActionResult(); action.Result.Type = "BALL"; action.Result.Value = ball.ToString(); return(action); }
//展现打中的效果:单体 public static BattleActionView ShowSkill(Role target, Skill skill) { BattleActionView action = new BattleActionView(); action.SourceRole = target.Key; action.SourceRoleId = target.Id; action.Result = new BattleActionResult(); action.Result.Type = "SHOWSKILL"; action.Result.Value = skill.Key + "#" + skill.Level; return(action); }
//损血效果 //返回true表示人物损血后已经死亡 public bool costHP(Role target, int HP) { target.Hp -= HP; battleResult.Actions.Add(BattleActionView.CostHP(target, HP)); if (target.Hp <= 0) { Die(target); return(true); } return(false); }
public void Die(Role role) { //死亡动画 if (role.Team == 0) { friendRoles[role.Position] = null; } else { enemyRoles[role.Position] = null; } if (role.battleCopy) { RoleManager.removeID(role.Id); } CurrentRoles.Remove(role); battleResult.Actions.Add(BattleActionView.Die(role)); }
//查询战场状态 public static BattleActionView QueryBattleStatus(List <Role> currentRoles) { BattleActionView action = new BattleActionView(); action.Result = new BattleActionResult(); action.Result.Type = "QUERY"; action.Result.Value = "战场状态:\n"; foreach (Role role in currentRoles) { action.Result.Value += "=>" + role.Key + " HP:" + role.Hp + " 气格" + role.Balls + " 位置" + role.Position; action.Result.Value += " 攻击系数" + role.attackRatio; action.Result.Value += " 状态:"; foreach (Buff buff in role.buffs) { action.Result.Value += buff.Key + "." + buff.Power + "." + buff.Round + " "; } action.Result.Value += "\n"; } return(action); }
//返回值: //true:正常执行人物接下来的动作 //false:不执行接下来的动作(晕眩等情形) public bool runBuff(Role role, Buff buff) { bool continueAction = true; if (buff.Key == "中毒") { //1000威力的毒每次掉20%血 int HP = (int)(role.MaxHp * buff.Power / 1000.0f * 0.2f); battleResult.Actions.Add(BattleActionView.Poison(role)); if (costHP(role, HP)) { continueAction = false; } } if (buff.Key == "晕眩") { continueAction = false; } return(continueAction); }
public void changePosition(Role role, int prev, int pos) { role.Position = pos; battleResult.Actions.Add(BattleActionView.Position(role, prev, pos)); }
//Remove Buff public void removeBuff(Role role, Buff buff) { role.buffs.Remove(buff); battleResult.Actions.Add(BattleActionView.RemoveBuff(role, buff)); }
//计算并演算当前技能产生的特效 public void calcEffects(Role sourceRole, Role targetRole, Skill skill, int HP) { foreach (Effect effect in skill.Effects) { #region 召唤系 if (effect.Key == "幻影") { Role[] team = null; if (sourceRole.Team == 0) { team = friendRoles; } else { team = enemyRoles; } for (int i = 0; i < memberCount; i++) { if (team[i] == null) { //召唤一个影子 Role shadow = sourceRole.BattleCopy(); shadow.Name = "幻影" + sourceRole.Name; shadow.Key = "幻影" + sourceRole.Key; shadow.roleType = RoleType.Shadow; powerUp(shadow, shadow.attackRatio * 0.5f); shadow.Position = i; Buff buff = new Buff(); buff.Key = "即死"; buff.Round = 3; buff.skill = skill; shadow.buffs.Add(buff); battleResult.Actions.Add(BattleActionView.Shadow(sourceRole, i)); CurrentRoles.Add(shadow); actionedRoleId.Add(shadow.Id); team[i] = shadow; //battleResult.Actions.Add(BattleActionView.QueryBattleStatus(CurrentRoles)); break; } } } if (effect.Key == "识破") { if (targetRole.roleType == RoleType.Shadow) { Die(targetRole); } } if (effect.Key == "吸收") { if (targetRole.roleType == RoleType.Shadow) { Die(targetRole); powerUp(sourceRole, sourceRole.attackRatio + 0.1f); } } #endregion #region BUFF/DEBUFF类 if (effect.Key == "五行")// && Tools.ProbabilityTest(skill.Level / 1000.0f + 0.2f)) { Buff buff = new Buff(); buff.Key = Buff.debuffs[Tools.GetRandomInt(0, Buff.debuffs.Length) % Buff.debuffs.Length]; buff.Round = 3; buff.BasePower = 1.0f; buff.skill = skill; addBuff(targetRole, buff); } if (effect.Key == "乱心")// && Tools.ProbabilityTest(skill.Level / 1000.0f + 0.2f)) { Buff buff = new Buff(); buff.Key = "蛊惑"; buff.Round = 2; buff.BasePower = 1.0f; buff.skill = skill; addBuff(targetRole, buff); } if (effect.Key == "震晕") // && Tools.ProbabilityTest(0.3+skill.Level / 1000.0f)) { Buff buff = new Buff(); buff.Key = "晕眩"; buff.Round = 3; buff.BasePower = 1.0f; buff.skill = skill; addBuff(targetRole, buff); } #endregion #region 阵法类 if (effect.Key == "阴阳")// && Tools.ProbabilityTest(skill.Level / 1000.0f + 0.2f)) { //前中后三排位置互换 Role[] team = friendRoles; if (targetRole.Team == 1) { team = enemyRoles; } Role[] newTeam = new Role[team.Length]; for (int i = 0; i < newTeam.Length; i++) { newTeam[i] = null; } for (int i = 0; i < team.Length; i++) { if (team[i] == null) { continue; } int prevPos = team[i].Position; int newPos = (team[i].Position + 3) % team.Length; changePosition(team[i], prevPos, newPos); newTeam[newPos] = team[i]; } if (targetRole.Team == 0) { friendRoles = newTeam; } else { enemyRoles = newTeam; } } #endregion #region 吸血类 if (effect.Key == "吸血")// && Tools.ProbabilityTest(skill.Level / 1000.0f + 0.2f)) { int toObsorb = (int)(HP * skill.Level / 1000.0f); if (toObsorb > HP * 0.7) { toObsorb = (int)(HP * 0.7); } costHP(sourceRole, -toObsorb); } #endregion #region 强化类 if (effect.Key == "血誓")// && Tools.ProbabilityTest(skill.Level / 1000.0f + 0.2f)) { float ratio = skill.Level / 1500.0f; if (ratio > 1.0f) { ratio = 1.0f; } float HPRatio = ratio * 0.8f; costHP(sourceRole, (int)(sourceRole.Hp * HPRatio)); powerUp(sourceRole, sourceRole.attackRatio * (1 + ratio)); } if (effect.Key == "战狂")// && Tools.ProbabilityTest(skill.Level / 1000.0f + 0.2f)) { float ratio = (1.0f - sourceRole.Hp / sourceRole.MaxHp) * skill.Level / 250.0f; powerUp(sourceRole, sourceRole.attackRatio * (1.0f + ratio)); } #endregion } }
/// <summary> /// 计算战斗结果 /// </summary> /// <returns></returns> public BattleResult Compute() { //目前先给一个简单的逻辑 //每个回合按角色顺序行动,直接攻击对方随机单位 while (!IsFinished()) { //新的一轮,清空行动记录 actionedRoleId.Clear(); //【显示】查询当前战场状态 battleResult.Actions.Add(BattleActionView.QueryBattleStatus(CurrentRoles)); while (true) { Role sourceRole = this.GetNextActionRole(); if (sourceRole == null) { break; //直到没有角色可以行动了 } if (IsFinished()) { break; } //Buff & Debuff bool continueAction = true; foreach (Buff buff in sourceRole.buffs) { bool status = runBuff(sourceRole, buff); if (!status) { continueAction = false; } } if (!removeBuffs(sourceRole)) { continueAction = false; } if (!continueAction) { actionedRoleId.Add(sourceRole.Id); continue; } //选择攻击对象和技能 ActionSet action = selectAction(sourceRole); //放招动画 & 放招消耗 battleResult.Actions.Add(BattleActionView.CastSkill(sourceRole, action.skill)); performSkillCost(sourceRole, action.skill); //单体攻击 if (action.skill.range == Range.Single) { hitSingleEnemy(sourceRole, action.target, action.skill); } //直线攻击 if (action.skill.range == Range.Line) { int pos = action.target.Position; Role[] team = friendRoles; if (action.target.Team == 1) { team = enemyRoles; } for (int i = pos % eachRowCount; i < memberCount; i += eachRowCount) { if (team[i] != null) { hitSingleEnemy(sourceRole, team[i], action.skill); } } } //群体攻击 if (action.skill.range == Range.All) { Role[] team = friendRoles; if ((sourceRole.Team == 0 && sourceRole.getBuff("蛊惑") == null) || (sourceRole.Team == 1 && sourceRole.getBuff("蛊惑") != null)) { team = enemyRoles; } for (int i = 0; i < team.Length; i++) { if (team[i] != null) { hitSingleEnemy(sourceRole, team[i], action.skill); } } } if (action.skill.range == Range.AllForEffect) { calcEffects(sourceRole, action.target, action.skill, 0); } //对自己使用的技能 if (action.skill.range == Range.Self) { //Buff & Debuff addBuffs(sourceRole, sourceRole, action.skill); //攻击特效 calcEffects(sourceRole, action.target, action.skill, 0); } actionedRoleId.Add(sourceRole.Id); } } return(battleResult); }
//呈现战斗结果 public static string Show(BattleActionView view) { string message = ""; switch (view.Result.Type) { case "CASTSKILL": message = "【" + view.SourceRole + "】施放了技能【" + view.Result.Value + "】"; break; case "SHOWSKILL": message = "【" + view.SourceRole + "】被技能【" + view.Result.Value + "】命中!"; break; case "HP": message = "【" + view.SourceRole + "】的HP减少了【" + view.Result.Value + "】。"; break; case "BALL": message = "【" + view.SourceRole + "】的气力增加了【" + view.Result.Value + "】。"; break; case "MISS": message = "【" + view.SourceRole + "】躲过了攻击。"; break; case "ATTACKRATIO": message = "【" + view.SourceRole + "】的攻击系数上升为【" + view.Result.Value + "】"; break; case "ADDBUFF": message = "【" + view.SourceRole + "】增加了Buff/Debuff【" + view.Result.Value + "】"; break; case "REMOVEBUFF": message = "【" + view.SourceRole + "】减少了Buff/Debuff【" + view.Result.Value + "】"; break; case "POISON": message = "【" + view.SourceRole + "】中毒发作了!"; break; case "SHADOW": message = "【" + view.SourceRole + "】产生了一个幻影在位置【" + view.Result.Value + "】"; break; case "DEFEND": message = "【" + view.SourceRole + "】奋不顾身地跳到位置【" + view.Result.Value + "】,挡下了这一招!"; break; case "POSITION": message = "【" + view.SourceRole + "】的位置转移至【" + view.Result.Value + "】"; break; case "DIE": message = "【" + view.SourceRole + "】从战场上撤退。"; break; case "QUERY": message = view.Result.Value; break; default: break; } return(message); }
//攻击一个单体敌人 public void hitSingleEnemy(Role sourceRole, Role target, Skill skill) { //战斗判定:先给个简单的计算公式 bool isMissed = true; //if (target.Team != sourceRole.Team) Buff piaomiao = target.getBuff("飘渺"); float piaomiaoRatio = 0.0f; if (piaomiao != null) { piaomiaoRatio = piaomiao.Power / 1000.0f * 1.0f; } isMissed = Tools.ProbabilityTest(target.Dodge / 100.0 + piaomiaoRatio); if (!isMissed) { //天赋:奋不顾身 if (skill.range == Range.Single) { Role toDefend = null; foreach (Role role in CurrentRoles) { if (role.Id != target.Id && role.Team == target.Team && role.getTalent("奋不顾身") != null && Tools.ProbabilityTest(0.3f + role.getTalent("奋不顾身").Level / 1000.0f)) { toDefend = role; break; } } if (toDefend != null) { Role originalTarget = target.BattleCopy(); target = toDefend; battleResult.Actions.Add(BattleActionView.Defend(toDefend, originalTarget)); } } int HP = calcAttack(sourceRole, skill);// - action.target.Defence; //命中动画 battleResult.Actions.Add(BattleActionView.ShowSkill(target, skill)); //攻击特效 calcEffects(sourceRole, target, skill, HP); //Buff & Debuff addBuffs(sourceRole, target, skill); //加气格 int ball = Tools.ProbabilityTest(0.5) ? 1 : 0; if (sourceRole.Balls >= 6) { ball = 0; } sourceRole.Balls += ball; if (ball != 0) { battleResult.Actions.Add(BattleActionView.AddBall(sourceRole, ball)); } int targetBall = Tools.ProbabilityTest(0.5) ? 1 : 0; target.Balls += targetBall; if (targetBall != 0) { battleResult.Actions.Add(BattleActionView.AddBall(target, targetBall)); } //损血动画 costHP(target, HP); } else { //闪躲动画 battleResult.Actions.Add(BattleActionView.Miss(target)); } }