双方或多方借用第三方进行引用,用来降低耦合。
public class MiddleBase
{
public Transform pos;
public float bloodCount;
public virtual void ReduceBlood(float Reduce)//虚方法
{
bloodCount -= Reduce;
}
}
public class MiddleNpc:MiddleBase
{
public override void ReduceBlood(float Reduce)
{
//base.ReduceBlood(Reduce);
bloodCount-=2*Reduce;
}
}
public class MiddlePlayer: MiddleBase
{
}
public class TestMiddle : MonoBehaviour
{
public void CaculateBlood(MiddleBase attacker ,MiddleBase attacked)
{
if (Vector3.Distance(attacker.pos.position, attacked.pos.position) < 10)
{
attacked.ReduceBlood(10);
}
}
// Start is called before the first frame update
void Start()
{
MiddleNpc middleNpc = new MiddleNpc();
MiddlePlayer middlePlayer = new MiddlePlayer();
//玩家攻击npc
CaculateBlood(middlePlayer, middleNpc);
//npc攻击玩家
CaculateBlood(middleNpc, middlePlayer);
}
// Update is called once per frame
void Update()
{
}
}
|