分配后的财产价值错误 - C#

时间:2015-01-31 22:46:26

标签: c# .net

我偶然发现了这个问题,我在我的Character类中为属性CurrentHp赋值,并且值不正确且与右手边不匹配。

Character类继承自GameEntity,两个类的代码如下所示:

GameEntity

public abstract class GameEntity
{
    public int BaseDmgMin { get; set; }
    public int BaseDmgMax { get; set; }
    public virtual int CurrentHp { get; set; }
    public int MaxHp { get; set; }

    public bool IsDead()
    {
        if (CurrentHp < 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public abstract int TakeDamage(int damage);
    public abstract int Attack();
}

字符

public class Character : GameEntity
{
    public Character(String CharacterName, int Exp, int Gold) : this()
   {
        this.CharacterName = CharacterName;
        this.Exp = Exp;
        this.Gold = Gold;

    }

    public Character(){
        Inventory = new Inventory();
    }

    public Guid CharacterID { get; set; }
    public string CharacterName { get; set; }
    public int Exp { get; set; }
    public int Gold { get; set; }
    public Weapon Weapon { get; set; }
    public Shield Shield { get; set; }
    public Inventory Inventory { get; set; }
    public int Level { get; set; }
    public DateTime LastHealthUpdate { get; set; }
    public bool InFight { get; set; }
    public override int CurrentHp
    {
        get
        {
            if (!InFight)
            {
                var timeHealth = TimeHealth();
                if (timeHealth > 0)
                {
                    LastHealthUpdate = DateTime.Now;
                    base.CurrentHp = (base.CurrentHp + timeHealth) > MaxHp ? MaxHp : base.CurrentHp + timeHealth;
                } 
            }

            return base.CurrentHp;
        }
        set{ base.CurrentHp = value; }
    }

    public void UpdateHealth()
    {
       var a = CurrentHp;
    }

    public override int Attack()
    {
        Random r = new Random();
        int hitDamage = r.Next(BaseDmgMin, BaseDmgMax);
        if (Weapon != null)
            hitDamage += Weapon.Damage;

        return hitDamage;
    }

    public int TimeHealth()
    {
        var diffMilli = DateTime.Now - LastHealthUpdate;
        var intervals = (int)diffMilli.TotalMinutes / 5;
        return intervals;
    }

    public int GoldLoss()
    {
        int gold = Gold;
        Random r = new Random();
        int multiplier = r.Next(30, 50);
        gold = (gold * multiplier) / 100;
        Gold -= gold;

        return gold;
    }

    public int ExpLoss()
    {
        var expLoss = Convert.ToInt32(Exp * 0.9);
        Exp -= expLoss;
        return expLoss;
    }

    public void GoldGain(int gold) {
        this.Gold += gold;
    }

    public void ExpGain(int exp) {
        this.Exp += exp;
    }

    public override int TakeDamage(int damage)
    {
        if (Shield != null)
            damage = (damage - Shield.Armor > 0) ? damage - Shield.Armor : 0;

        CurrentHp = (CurrentHp - damage > 0) ? CurrentHp - damage : 0;
        return damage;
    }


}

当我单步执行代码时,我会看到以下内容:

1:CurrentHp的值为0,但值应为25。 http://imgur.com/4gQp0hm

2:CurrentHp现在具有值100,而不再对正在执行的CurrentHp赋值。 http://imgur.com/KvS3WCu

我错过了什么或我做错了什么? 我很感激能得到的所有帮助:)

0 个答案:

没有答案