Files
CleanCode/CleanDemo/Player.cs
2026-03-08 11:55:18 +01:00

45 lines
1.3 KiB
C#

public class Player : IAttackable, IDescribable{
public string Name { get; }
public int _health;
public readonly int _baseDamage;
public bool IsDead { get; private set; }
public readonly List<string> _inventory = new List<string>();
public Player(string name, int health, int baseDamage) {
Name = name;
_health = health;
_baseDamage = baseDamage;
}
public void TakeDamage(int amount) {
_health -= amount;
if (_health <= 0)
IsDead = true;
}
public void Heal() {
if (_inventory.Contains("potion")) {
_health += 30;
_inventory.Remove("potion");
Console.WriteLine($"{Name} healed to {_health}");
}
else {
Console.WriteLine("No potion available");
}
}
public int CalculateDamage() {
int totalDamage = _baseDamage;
if (_inventory.Contains("sword")) totalDamage += 10;
return totalDamage;
}
public void AddToInventory(string item) {
_inventory.Add(item);
}
public void Describe() {
Console.WriteLine($"--- {Name} ---");
Console.WriteLine($"HP: {_health}");
Console.WriteLine($"Base DMG: {_baseDamage}");
Console.WriteLine($"Inventory: {string.Join(", ", _inventory)}");
}
}