Permalink
Browse files

AdventureSample - remove parallel arrays in Objects, use ObjectRef[]

  • Loading branch information...
bobbymcr committed May 13, 2018
1 parent b10327f commit 4417f16cf5b28c4d8bbe81e084c5152341e19f4d
@@ -20,7 +20,7 @@ public ObjectRef(int id, string name, string tag, int room)
public string Tag { get; private set; }
public int RawRoom { get; private set; }
public int RawRoom { get; set; }
public int Room
{
@@ -10,20 +10,14 @@ internal sealed class Objects
{
private const int NumberOfObjects = 17;
private readonly string[] objectNames;
private readonly string[] objectTags;
private readonly int[] objectRooms;
private readonly ObjectRef[] objects;
public Objects()
{
this.objectRooms = new int[NumberOfObjects + 1];
this.objectNames = new string[NumberOfObjects + 1];
this.objectTags = new string[NumberOfObjects + 1];
this.InitObjects();
this.objects = InitObjects();
}
public bool Carrying(int id) => this.objectRooms[id] == -1;
public bool Carrying(int id) => this.Ref(id).RawRoom == -1;
public IEnumerable<string> Carrying()
{
@@ -67,19 +61,19 @@ public ObjectRef Find(string noun)
return null;
}
public ObjectRef Ref(int id) => new ObjectRef(id, this.objectNames[id], this.objectTags[id], this.objectRooms[id]);
public ObjectRef Ref(int id) => this.objects[id];
public void Show(int id, int room) => this.objectRooms[id] = room;
public void Show(int id, int room) => this.Ref(id).RawRoom = room;
public void Hide(int id) => this.Show(id, 0);
public void Take(int id) => this.Drop(id, -1);
public void Drop(int id, int room) => this.objectRooms[id] = room;
public void Drop(int id, int room) => this.Ref(id).RawRoom = room;
private void InitObjects()
private static ObjectRef[] InitObjects()
{
ObjectRef[] d = new ObjectRef[]
return new ObjectRef[]
{
new ObjectRef(0, "AN OLD DIARY", "DIA", 1),
new ObjectRef(1, "A SMALL BOX", "BOX", 1),
@@ -99,15 +93,6 @@ private void InitObjects()
new ObjectRef(15, "A GLOWING RUBY", "RUB", 0),
new ObjectRef(16, "A PAIR OF RUBBER GLOVES", "GLO", 19)
};
for (int i = 0; i < NumberOfObjects; ++i)
{
ObjectRef obj = d[i];
this.objectNames[i] = obj.Name;
this.objectTags[i] = obj.Tag;
this.objectRooms[i] = obj.RawRoom;
}
}
}
}

0 comments on commit 4417f16

Please sign in to comment.