forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Room.cs
187 lines (163 loc) · 5.23 KB
/
Room.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace GPSRCmdGen
{
/// <summary>
/// Represents a room within the house
/// </summary>
[Serializable, XmlRoot("room")]
public class Room : Location
{
#region Variables
/// <summary>
/// Stores the list of placements in this room
/// </summary>
protected Dictionary<string, SpecificLocation> locations;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GPSRCmdGen.Room"/> class.
/// </summary>
/// <remarks>Intended for serialization purposes only</remarks>
public Room() : this(String.Empty) { }
/// <summary>
/// Initializes a new instance of the <see cref="GPSRCmdGen.Room"/> class.
/// </summary>
/// <param name="name">The name of the location.</param>
/// <param name="isPlacement">Flag indicating whether the location is
/// suitable for placing objects.</param>
public Room(string name) : base(name) {
this.locations = new Dictionary<string, SpecificLocation>();
}
#endregion
#region Properties
/// <summary>
/// Gets a value indicating whether the location is suitable for placing a person.
/// </summary>
[XmlIgnore]
public override bool IsBeacon { get { return false; } set { } }
/// <summary>
/// Gets a value indicating whether the location is suitable for placing objects.
/// </summary>
[XmlIgnore]
public override bool IsPlacement { get { return false; } set { } }
/// <summary>
/// Gets or sets the list of placements in the room.
/// </summary>
/// <remarks>Use for (de)serialization purposes only</remarks>
[XmlElement("location")]
public SpecificLocation[] Locations
{
get { return new List<SpecificLocation>(this.locations.Values).ToArray(); }
set
{
if (value == null) return;
foreach (SpecificLocation location in value)
AddLocation(location);
}
}
/// <summary>
/// Returns the number of locations in the room
/// </summary>
public int LocationCount { get { return this.locations.Count; } }
#endregion
#region Methods
/// <summary>
/// Adds an beacon to the room
/// </summary>
/// <param name="name">The name of the beacon to add.</param>
public void AddBeacon(string name)
{
SpecificLocation b = new SpecificLocation(name, false, true);
this.AddLocation(b);
}
/// <summary>
/// Adds a specific location to the room
/// </summary>
/// <param name="item">The specific location to add to the room.</param>
public void AddLocation(SpecificLocation item)
{
if(item == null)
return;
if ((item.Room != null) && (item.Room != this))
item.Room.RemoveLocation(item);
if (this.locations.ContainsKey(item.Name))
{
this.locations[item.Name].IsBeacon |= item.IsBeacon;
this.locations[item.Name].IsPlacement |= item.IsPlacement;
}
else
this.locations.Add(item.Name, item);
if (item.Room != this)
item.Room = this;
}
/// <summary>
/// Adds a specific location to the room
/// </summary>
/// <param name="name">The name of the location.</param>
/// <param name="placement">Indicates whether the location is
/// suitable for placing objects.</param>
/// <param name="beacon">Indicates whether the location is
/// suitable for placing objects.</param>
public void AddLocation(string name, bool placement, bool beacon)
{
AddLocation(new SpecificLocation(name, placement, beacon));
}
/// <summary>
/// Adds an placement to the room
/// </summary>
/// <param name="name">The name of the placement to add.</param>
public void AddPlacement(string name)
{
SpecificLocation p = new SpecificLocation(name, true, false);
this.AddLocation(p);
}
/// <summary>
/// Gets a value indicating if the room contains a location with the given name
/// </summary>
/// <param name="locationName">The name of the room to look for</param>
/// <returns>true if the collection contains a room with the given name, false otherwise</returns>
public bool Contains(string locationName)
{
return this.locations.ContainsKey(locationName);
}
/// <summary>
/// Removes all locations
/// </summary>
public void Clear()
{
this.locations.Clear();
}
/// <summary>
/// Removes the given location from the room
/// </summary>
/// <param name="item">The room to remove</param>
/// <returns>true if the room was in the collection, false otherwise</returns>
private bool RemoveLocation(SpecificLocation item)
{
if (item == null)
return false;
return RemoveLocation(item.Name);
}
/// <summary>
/// Removes the given location from the room
/// </summary>
/// <param name="locationName">The name of the room to remove</param>
/// <returns>true if the room was in the collection, false otherwise</returns>
private bool RemoveLocation(string locationName)
{
return this.locations.Remove(locationName);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="GPSRCmdGen.Room"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="GPSRCmdGen.Room"/>.</returns>
public override string ToString()
{
return string.Format("{0} ({1} locations)", Name, LocationCount);
}
#endregion
}
}