0
我正在開發軌道生成器(用於我未來的賽車遊戲),這是生成軌道。構建完成後,跟蹤由遊戲引擎處理等。田徑是元素的集合,可以是:工廠系列從屬元素
- 短/長直
- 慢/中/快角落
每一個元素都有一個length屬性,它應該同時建立生成軌道的元件。元素依賴於在生成之前的元素(慢速之後有一個快速轉角有點奇怪)。
這是第一次我去「ifing」的解決方案:
public class ElementType
{
...
public double GenerateLength()
{
switch (m_TypeEnum)
{
case ElementTypeEnum.SlowSpeedCorner:
return Parameters.SlowCornerAvgLength+Generator.GetDeviation;
...
case ElementTypeEnum.LongStraight:
default:
return Parameters.LongStraightAvgLength + Generator.GetDeviation(Parameters.LongStraightLengthMaxDeviation);
}
}
...
public IList<ElementType> GetPossibleSuccessors()
{
switch (m_TypeEnum)
{
case ElementTypeEnum.SlowSpeedCorner:
return new List<ElementType>() { new ElementType(ElementTypeEnum.SlowSpeedCorner), new ElementType(ElementTypeEnum.ShortStraight), new ElementType(ElementTypeEnum.LongStraight) };
...
case ElementTypeEnum.LongStraight:
default:
return new List<ElementType>() { new ElementType(ElementTypeEnum.SlowSpeedCorner), new ElementType(ElementTypeEnum.MediumSpeedCorner), new ElementType(ElementTypeEnum.FastSpeedCorner) };
}
}
}
public enum ElementTypeEnum : int
{
LongStraight = 1,
SlowSpeedCorner = 2,
}
和發電機是立足於方法:
public static TrackElement GenerateElement(TrackElement Predecessor)
{
ElementType type = SelectElementType(Predecessor);
double length = type.GenerateLength();
return new TrackElement(type, length);
}
private static ElementType SelectElementType(TrackElement Predecessor)
{
IList<ElementType> successors = Predecessor.Type.GetPossibleSuccessors();
int possibleSuccessors = successors.Count;
int selected = Generator.GetInt(possibleSuccessors);
return successors[selected];
}
但你可以猜到,這是一齣戲當在發動機中消耗它時 - 因爲每個屬性都會產生太多的「ifing」。讓我感動的元素不同類別的基礎上:
public abstract class TrackElement
{
public TrackElement(double Length)
{
m_length = Length;
}
protected abstract static double GenerateLength();
public sealed double Length
{
get
{
return m_length;
}
}
}
但現在我有使用與建設軌道問題提供類:
public static TrackElement GenerateElement(TrackElement Predecessor)
{
??? type = SelectElementType(Predecessor);
double length = type.GenerateLength();
return new ???(length);
}
當然,我知道,我不能做這樣的,因爲GenerateLength是靜態的,我只想起草我的問題。我怎樣才能做到這一點?