在C#中
初始版本
namespace _013_桥接模式
{
/// <summary>
/// 玩家抽象类
/// </summary>
public abstract class AbstractPlayer
{
protected IWeapon _weapon;//武器引用
public void SetWeapon(IWeapon weapon)
{
_weapon = weapon;
}
//抽象方法使用武器
public abstract void UseWeaponAttack();
}
}
namespace _013_桥接模式
{
//抽象武器
public abstract class IWeapon
{
public string Name { set; get; } //武器名称
public float attackValue { set; get; } //攻击力
public abstract void Attack();//攻击
//加入怪物的引用
public Monster monster { set; get; }
/*public Monster Monsters { set;get;}=null;
//设置攻击目标
public void SetAttackTarget(Monster monster)
{
Monsters=monster;
}*/
protected IWeapon(string name, float attackValue)
{
Name = name;
this.attackValue = attackValue;
}
}
}
namespace _013_桥接模式
{
/// <summary>
/// 玩家接口
/// </summary>
public interface IPlayer
{
void UseWeaponAttack();
}
}
namespace _013_桥接模式
{
public class Mage: AbstractPlayer
{
public override void UseWeaponAttack()
{
Console.WriteLine($"法师使用{_weapon.Name}武器进行攻击!");
if (_weapon != null)
{
_weapon.Attack();
}
}
}
}
namespace _013_桥接模式
{
public class Warrior:AbstractPlayer
{
//使用武器
public override void UseWeaponAttack()
{
//面向抽象编程,或者面向接口编程
if (_weapon != null)
{
Console.WriteLine($"战士使用{_weapon.Name}武器进行攻击!");
_weapon.Attack();
}
}
}
}
namespace _013_桥接模式
{
public class Sword:IWeapon
{
public Sword(string name, float attackValue) : base(name, attackValue)
{
}
public override void Attack()
{
//武器特效逻辑
Console.WriteLine($"{Name}大剑攻击!,发出咣当咣当的声音");
//调用敌人的减血方法
if(monster!=null)
{
//击中敌人后的逻辑
Console.WriteLine($"敌人掉了{attackValue}滴血!");
monster.TakeDamage(attackValue);
}
}
}
}
namespace _013_桥接模式
{
public class Bow:IWeapon
{
public Bow(string name, float attackValue) : base(name, attackValue)
{
}
public override void Attack()
{
//武器特效逻辑
Console.WriteLine($"{Name}搭弓射箭!,发出嗖嗖的声音");
//击中敌人后的逻辑
Console.WriteLine($"射中敌人,对方很高兴的掉了{attackValue}滴血!");
//调用敌人的减血方法 if(monster!=null)
{
//击中敌人后的逻辑
Console.WriteLine($"敌人掉了{attackValue}滴血!");
monster.TakeDamage(attackValue);
}
}
}
}
namespace _013_桥接模式
{
/// <summary>
/// 怪物类
/// </summary>
public class Monster
{
public string Name { get; set; }
public float Hp { get; set; }
public Monster(string name, float hp)
{
Name = name;
Hp = hp;
}
public void TakeDamage(float damage)
{
if(damage<Hp)
{
Hp-=damage;
}
else
{
Hp = 0;
}
}
}
}
namespace _013_桥接模式
{
internal class Program
{
static void Main(string[] args)
{
Warrior warrior = new Warrior();
IWeapon sword = new Sword("风霜小剑",30f);
IWeapon bow =new Bow("射日长弓",15f);
Monster monster = new Monster("石头鬼",100f);
warrior.SetWeapon(sword);
sword.monster = monster;
// sword.SetAttackTarget(monster);//设置攻击目标
warrior.UseWeaponAttack();
}
}
}
最终版本
实现玩家之间,玩家和怪物之间相互攻击
新增角色类
namespace _013_桥接模式
{
/// <summary>
/// 角色基类
/// </summary>
public abstract class ICharacter
{
public string Name { get; set; }
public float CurrentHP { get; set; }//当前血量
public float MaxHP { get; set; } //最大血量
//攻击方法
public abstract void Attack(ICharacter character);
//掉血方法
public void TankDamage(float damage)
{
if(CurrentHP > damage)
CurrentHP -= damage;
else
CurrentHP = 0;
}
}
}
namespace _013_桥接模式
{
/// <summary>
/// 玩家抽象类
/// </summary>
public abstract class AbstractPlayer:ICharacter
{
public AbstractPlayer(string name,float maxHP)
{
Name = name;
MaxHP = maxHP;
CurrentHP = MaxHP;
}
}
}
namespace _013_桥接模式
{
public class Warrior : AbstractPlayer
{
private IWeapon _weapon;//武器引用
public Warrior(string name, float maxHP) : base(name, maxHP)
{
}
public void SetWeapon(IWeapon weapon)
{
_weapon = weapon;
}
public override void Attack(ICharacter character)
{
//面向抽象编程,或者面向接口编程
if (_weapon != null)
{
Console.WriteLine($"{Name}使用{_weapon.Name}武器进行攻击!");
_weapon.Attack();
character.TankDamage(_weapon.attackValue);
Console.WriteLine($"{character.Name}掉了{_weapon.attackValue}滴血");
}
}
}
}
namespace _013_桥接模式
{
/// <summary>
/// 怪物类
/// </summary>
public abstract class Monster : ICharacter
{
public float AILevel { get; set; }
public Monster(string name, float maxHP, float aILevel=1)
{
Name = name;
MaxHP = maxHP;
CurrentHP = maxHP;
AILevel = aILevel;
}
}
}
namespace _013_桥接模式
{
/// <summary>
/// 石头怪
/// </summary>
public class StoneMonster : Monster
{
public StoneMonster(string name, float maxHP, float aILevel = 1) : base(name, maxHP, aILevel)
{
}
public override void Attack(ICharacter character)
{
Console.WriteLine($"{Name}对{character.Name}发起攻击");
Console.WriteLine($"{character.Name}掉了10f血");
character.TankDamage(10);
}
}
}
namespace _013_桥接模式
{
//抽象武器
public abstract class IWeapon
{
public string Name { set; get; } //武器名称
public float attackValue { set; get; } //攻击力
public abstract void Attack();//攻击
protected IWeapon(string name, float attackValue)
{
Name = name;
this.attackValue = attackValue;
}
}
}
namespace _013_桥接模式
{
public class Bow:IWeapon
{
private Monster monster;
public Bow(string name, float attackValue) : base(name, attackValue)
{
}
public override void Attack()
{
//武器特效逻辑
Console.WriteLine($"{Name}搭弓射箭!,发出嗖嗖的声音");
//击中敌人后的逻辑
Console.WriteLine($"射中敌人,对方很高兴的掉了{attackValue}滴血!");
Console.WriteLine($"敌人掉了{attackValue}滴血!");
}
}
}
namespace _013_桥接模式
{
public class Sword:IWeapon
{
public Sword(string name, float attackValue) : base(name, attackValue)
{
}
private Monster monster;
public override void Attack()
{
//武器特效逻辑
Console.WriteLine($"{Name}大剑攻击!,发出咣当咣当的声音");
//调用敌人的减血方法
if(monster!=null)
{
//击中敌人后的逻辑
Console.WriteLine($"敌人掉了{attackValue}滴血!");
Console.WriteLine($"掉了{attackValue}滴血");
}
}
}
}
namespace _013_桥接模式
{
internal class Program
{
static void Main(string[] args)
{
Warrior warrior = new Warrior("战士",100f);
IWeapon sword = new Sword("风霜小剑",30f);
IWeapon bow =new Bow("射日长弓",15f);
Monster monster = new StoneMonster("石头鬼",100f,1);
warrior.SetWeapon(sword);
warrior.Attack(monster);
monster.Attack(warrior);
}
}
}