0

私は現在、Adventure Game Creator Framework を作成しており、これまでのところ次のクラスがあります。

// Base class that represents a single episode from a complete game.
public abstract class Episode : IEpisode
{
    public RoomList Rooms {get; }
    public ArtefactList Artefacts {get; }

    public Episode()
    {
        Rooms = new RoomList();
        Artefacts = new ArtefactList();
    }
}

// This is a list of all objects in the episode.
public ArtefactList : List<IArtefact>
{
    public IArtefact Create( UInt32 id, String text )
    {
        IArtefact art = new Artefact( id, text );

        base.Add( art );

        return art;
    }
}

// This is a list of all rooms in the episode.
public RoomList : List<IRoom> 
{   
    public IRoom Create( UInt32 id, String text )
    {
        IRoom rm = new Room( id, text );

        base.Add( rm );

        return rm;
    }
}

public class Room : IRoom
{
    public UInt32 Id { get; set; }
    public String Text { get; set; }

    public IList<IArtefact> Artefacts
    {
        get
        {
            return ???what??? // How do I access the Artefacts property from
                                // the base class:
                                //  (Room --> RoomList --> Episode)
        }
    }   
}

public class Artefact : IArtefact
{
    public UInt32 Id { get; set; }
    public String Text { get; set; }
    public IRoom CurrentRoom { get; set; }

}

public interface IArtefact
{
    UInt32 Id { get; set; }
    String Text { get; set; }
    IRoom CurrentRoom { get; set; }
}

public interface IRoom
{
    UInt32 Id { get; set; }
    String Text { get; set; }
    IList<IArtefact> Artefacts {get; }
}

私が知りたいのは、クラスが、オブジェクト グラフ全体への参照を渡すことなく、クラスRoomのカプセル化されArtefactsたプロパティにアクセスする方法、つまり--> -->です。EpisodeEpisodeEpisodeRoomsListRoom

4

1 に答える 1

1

ルームとアーティファクトの間には 1 対多の関係があります。したがって、RoomList.Create メソッドでこの関係を初期化する必要があります。

public IRoom Create( UInt32 id, String text , ArtefactList artefacts)
{
    IRoom rm = new Room( id, text , artefacts);
    base.Add( rm );
    return rm;
}

ルームを作成するときは、次のようにします。

var episode = new Episode();
episode.Rooms.Create(1, "RoomText", episode.Artefacts);

Artefact には IRoom インスタンスが必要なため、ArtefactList.Create についても同じことを行う必要があります。

于 2013-08-11T14:12:56.987 に答える