0

トラックを生成するトラック ビルダー (私の将来のレーシング ゲーム用) を開発しています。ビルド後、トラックはゲームエンジンなどで処理されます。Track は次のような要素のコレクションです。

  • ショート/ロングストレート
  • 低速/中速/高速コーナー

各要素には 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 が静的であるため、このようにできないことはわかっています。問題を下書きしたかっただけです。どうすればこれを達成できますか?

4

1 に答える 1

0

トラックの順序付けのロジックを構築のロジックから分離します。完全なトラックの構築を担当するエンティティに移動します。次に、ファクトリに便利な名前を付けます。

public abstract class TrackElementFactory
{
    public abstract TrackElement CreateFastCurve();
    public abstract TrackElement CreateLongStraight();
}

public class TrackArranger
{
     public Track CreateAwesomeTrack()
     {
          var factory = //get from somewhere
          var track = new Track();
          track.Add(factory.CreateFastCurve());
          track.Add(factory.CreateLongStraight());
          ....
          return track;
     }
}

また、TrackElement の抽象化に対するこの変更に満足する可能性があります。これにより、実装をよりクリーンにすることができます。

public abstract class TrackElement
{
   public abstract double Length 
   { 
        get;
   }
}
于 2012-05-01T04:13:44.720 に答える