public class Skeleton : MonoBehaviour
{
private MonsterBaseState state;
private void Awake()
{
state = new PatrolState<Skeleton>(this);
}
public void GetMonsterInfo()
{
dosomething;
}
}
pubilc class manyMonster:MonoBehaviour
{
private MonsterBaseState state;
private void Awake()
{
state = new PatrolState<Skeleton>(this);
}
}
//and more monster Class
public interface MonsterBaseState
{
void BehaviorForUpdate();
}
public class PatrolState<T> : MonsterBaseState
{
private T cluster;
public PatrolState(T curCluster)
{
cluster = curCluster;
cluster.GetMonsterInfo();//wrong here
}
public void BehaviorForUpdate()
{
}
}
我不能在 PatrolState 類中使用 GetMonsterinfo 方法
想傳骷髏或者其他怪物,所以不用寫很多狀態碼
uj5u.com熱心網友回復:
您可以實作一個新介面,如下所示:
public interface IMonster
{
void GetMonsterInfo();
//void MonsterMethod1();
//void MonsterMethod2();
//..
}
public class Skeleton : MonoBehaviour,IMonster
{
private MonsterBaseState state;
private void Awake()
{
state = new PatrolState<Skeleton>(this);
}
public void GetMonsterInfo()
{
}
}
public interface MonsterBaseState
{
void BehaviorForUpdate();
}
public class PatrolState<T> : MonsterBaseState where T :IMonster
{
private T cluster;
public PatrolState(T curCluster)
{
cluster = curCluster;
cluster.GetMonsterInfo();//wrong here
}
public void BehaviorForUpdate()
{
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372363.html
下一篇:嘗試制作計算器
