本文整理汇总了C#中IUnit.IsAlive方法的典型用法代码示例。如果您正苦于以下问题:C# IUnit.IsAlive方法的具体用法?C# IUnit.IsAlive怎么用?C# IUnit.IsAlive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IUnit
的用法示例。
在下文中一共展示了IUnit.IsAlive方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestLifePoints
private void TestLifePoints(IUnit unit)
{
Assert.AreEqual(unit.DefaultLifePoints, unit.LifePoints);
Assert.IsTrue(unit.IsAlive());
Assert.IsTrue(unit.DecreaseLifePoints());
Assert.AreEqual(unit.DefaultLifePoints - 1, unit.LifePoints);
// Removes life points until the unit is dead:
while(unit.LifePoints > 0) {
Assert.IsTrue(unit.IsAlive());
Assert.IsTrue(unit.DecreaseLifePoints());
}
Assert.IsFalse(unit.IsAlive());
}
示例2: Combat
/// <summary>
/// Computes a fight between the currently selected unit
/// and the best one from the selected destination.
/// </summary>
/// <param name="unit">The unit who fight.</param>
/// <returns>A constant from the CombatResult enumeration to describe the result.</returns>
private CombatResult Combat(IUnit unit)
{
IUnit enemy = GetBestUnit();
Random randCombat = new Random();
Random rand = new Random();
int nbRound = 3 + randCombat.Next((Math.Max(unit.LifePoints, enemy.LifePoints)) + 2);
int n = 0;
while(nbRound > n && unit.IsAlive() && enemy.IsAlive()) {
double ratioLife = (double)unit.LifePoints / (double)unit.DefaultLifePoints;
double ratioLifeDef = (double)enemy.LifePoints / (double)enemy.DefaultLifePoints;
double attaUnit = (double)unit.Attack * (double)ratioLife;
double defUnitdef = (double)enemy.Defense * (double)ratioLifeDef;
double ratioAttDef = (double)(attaUnit / defUnitdef);
double ratioChanceDef = 0;
if(ratioAttDef > 1) {
// Advantage for the attacker.
ratioChanceDef = (1 / ratioAttDef) / 2;
ratioChanceDef = (0.5 - ratioChanceDef) + 0.5;
} else if(ratioAttDef == 1) {
// Draw: 50% chances to win.
ratioChanceDef = 0.5;
} else {
// Advantage for the defender.
ratioChanceDef = ratioAttDef / 2;
}
double ratioCombat = (double)((double)rand.Next(100) / 100);
if(ratioCombat <= ratioChanceDef) {
enemy.DecreaseLifePoints();
} else {
unit.DecreaseLifePoints();
}
n++;
}
// Computes the result of the fight:
if(!unit.IsAlive()) {
this.game.Map.RemoveUnit(unit, this.selectedPosition);
this.lastMoveInfo = this.player.Name + " lost the fight.";
this.lastCombatResult = CombatResult.LOSE;
} else if(!enemy.IsAlive()) {
this.game.Map.RemoveUnit(enemy, this.destination);
this.lastMoveInfo = this.player.Name + " won the fight.";
this.lastCombatResult = CombatResult.WIN;
} else {
this.lastMoveInfo = "The fight ended with a draw";
this.lastCombatResult = CombatResult.DRAW;
}
return this.lastCombatResult;
}