ericbabe
Posts: 11927
Joined: 3/23/2005 Status: offline
|
Here is the code for a unit attacking another unit, and the code for generating the results of an attack: private void AttackPiece(GameTime gameTime, Context context, int apCost, Piece targetPiece, Random rand, int attackNo) { CohPiece attacker = ThisPiece; CohPiece defender = targetPiece as CohPiece; Player attackerPlayer = context.PlayerManager[attacker.PlayerName]; Player defenderPlayer = context.PlayerManager[defender.PlayerName]; if (attacker == null || defender == null) return; // attack strength String modfDesc = ""; bool frontal; int att; int def; GetAttackStats(context, targetPiece, apCost, out frontal, out att, out def, ref modfDesc, false); // reveal attacker attacker.Hider.Unhide(); // AI player can modify attack roll with caps now // omitted... // get attack roll int roll1, roll2; int baseRoll = MakeAttackRoll(context.GameScene.HexGame, rand, context.PlayerManager[attacker.PlayerName], out roll1, out roll2); int modfRoll = baseRoll + att; // zero roll bonus context.NextRollBonus = 0; // notify for attack String msg = ""; if (modfRoll >= def) { // unhide the defender defender.Hider.Unhide(); msg = String.Format(modfRoll != baseRoll ? Resources.CohAttacker_AttackPiece__0__rolls__4__modified_by__2__and_hits__1__whose_defense_is__3__ : Resources.CohAttacker_AttackPiece__0__rolls__4__and_hits__1__whose_defense_is__3__, attacker.GetName(), defender.GetName(), modfRoll - baseRoll, def, baseRoll); } else { if (defender.Hider.Hidden) { if (attackNo == 0) msg = String.Format(/*ss.Get*/(Resources.CohAttacker_AttackLocation__0__does_not_reveal_any_units___1__), attacker.GetName(), baseRoll); } else { msg = String.Format(modfRoll != baseRoll ? Resources.CohAttacker_AttackPiece__0__rolls__4__modified_by__2__and_misses__1__whose_defense_is__3__ : Resources.CohAttacker_AttackPiece__0__rolls__4__and_misses__1__whose_defense_is__3__, attacker.GetName(), defender.GetName(), modfRoll - baseRoll, def, baseRoll); } } // add modifiers to message if (!defender.Hider.Hidden) { msg += @" ("; msg += modfDesc; msg += @")"; } CombatSubject.Report(msg, modfRoll >= def ? ReportColors.HitColor : ReportColors.Standard); if (!defender.Hider.Hidden) { // is piece hidden damage revealed? if (defender.GetDamage() != null && defender.HiddenDamage && (defender.GetDamage().FlankDefense != 0 || defender.GetDamage().FlankDefenseIsArmored != 0 || defender.GetDamage().FrontDefense != 0 || defender.GetDamage().FlankDefenseIsArmored != 0)) { defender.HiddenDamage = false; CombatSubject.Report(String.Format(/*ss.Get*/(Resources.CohAttacker_AttackPiece__0__damage_is_revealed___1_), defender.GetName(), defender.GetDamage().Name)); } if (attacker.GetDamage() != null && attacker.HiddenDamage && (attacker.GetDamage().Attack != 0 || attacker.GetDamage().AttackVsArmor != 0 || attacker.GetDamage().Range != 0 || attacker.GetDamage().MinRange != 0)) { attacker.HiddenDamage = false; CombatSubject.Report(String.Format(/*ss.Get*/(Resources.CohAttacker_AttackPiece__0__damage_is_revealed___1_), attacker.GetName(), attacker.GetDamage().Name)); } } // roll on flying text FlyingTextManager.AddFloatFade(gameTime, String.Format(Resources.CohAttacker_AttackPiece_Roll___0_, baseRoll), Color.White, attacker, new Vector2(0, -30.0f * attackNo), roll1, roll2); // get roll result if (modfRoll >= def) { // hit // give damage marker bool instantKill = (modfRoll >= def + 4); CohDamage damage; instantKill |= GiveDamage(defender, out damage); // is defender dead bool defenderIsDead = instantKill || defender.Hp() <= 0; // hey controller context.GameScene.Controller.AddAttackSequence(attacker, defender, defender.MapPos, defenderIsDead ? CohController.HitResult.Destroy : CohController.HitResult.Hit, new Vector2(0, -30.0f * attackNo), damage.Name); // reveal hit piece defender.Hider.Unhide(); // destroy if necessary if (defenderIsDead) { PerformCombatKill(context, attacker, defender); } else { HeadlineSubject.Report(String.Format(/*ss.Get*/(Resources.CohAttacker_AttackPiece__0__is_damaged), defender.GetName())); } } else { if (!defender.Hider.Hidden) { // miss context.GameScene.Controller.AddAttackSequence(attacker, defender, defender.MapPos, CohController.HitResult.Miss, new Vector2(0, -30.0f * attackNo), null); HeadlineSubject.Report(String.Format(/*ss.Get*/(Resources.CohAttacker_AttackPiece__0__misses), attacker.GetName())); } } // call event if (PieceAttackingHandler != null) PieceAttackingHandler(gameTime, attacker, defender, defender.MapPos); } and the code for generating the attack roll: private static int MakeAttackRoll(Application.HexGame game, Random rand, Player player, out int roll1, out int roll2) { if (PreferenceManager.Instance.AreUsingDiceCamera && player.IsHuman && !NetworkFacade.Game.IsActiveGame) { try { game.SuspendUpdate = true; _DiceForm = new DiceForm { TopMost = true }; _DiceForm.ShowDialog(); roll1 = 0; roll2 = 0; game.SuspendUpdate = false; return _DiceForm.RollResult; } catch (Exception) { CombatSubject.Report(Resources.CohAttacker_MakeAttackRoll_Dice_Camera_failed_to_initialize_); game.SuspendUpdate = false; return Random2D6(rand, out roll1, out roll2); } } else return Random2D6(rand, out roll1, out roll2); } Here is the code to generate Random2D6: private static int Random2D6(Random rand, out int roll1, out int roll2) { roll1 = rand.Next(6) + 1; roll2 = rand.Next(6) + 1; return roll1 + roll2; }
< Message edited by ericbabe -- 5/13/2012 4:26:24 PM >
_____________________________
|