Skip to content

Commit

Permalink
Location and object engines reworked
Browse files Browse the repository at this point in the history
Locations redefined to allow the same location be placement and beacon
Reworked LocationManager to avoid duplicates
Reworked GPSRObjectManager to avoid duplicates
Robustness enhanced
  • Loading branch information
kyordhel committed Mar 23, 2015
1 parent d8c2c5d commit ab6d3a8
Show file tree
Hide file tree
Showing 17 changed files with 569 additions and 517 deletions.
70 changes: 0 additions & 70 deletions Beacon.cs

This file was deleted.

77 changes: 63 additions & 14 deletions Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public class Category : INameable
/// <summary>
/// Stores the default placement default location for objects in the category
/// </summary>
protected Placement defaultLocation;
protected SpecificLocation defaultLocation;

/// <summary>
/// Stores the list of objects in the category
/// </summary>
protected List<GPSRObject> objects;
protected Dictionary<string, GPSRObject> objects;

#endregion

Expand All @@ -31,24 +31,29 @@ public class Category : INameable
/// Initializes a new instance of the <see cref="GPSRCmdGen.Category"/> class.
/// </summary>
/// <remarks>Intended for serialization purposes</remarks>
public Category() : this("Unknown objects", Placement.TrashBin){
public Category() : this("Unknown objects", null){
}

/// <summary>
/// Initializes a new instance of the <see cref="GPSRCmdGen.Category"/> class.
/// </summary>
/// <param name="name">The name of the category.</param>
/// <param name="defaultLocation">The default placement location for objects in the category.</param>
public Category(string name, Placement defaultLocation){
public Category(string name, SpecificLocation defaultLocation){
this.Name = name;
this.defaultLocation = defaultLocation;
this.objects = new List<GPSRObject> ();
this.objects = new Dictionary<string, GPSRObject>();
}

#endregion

#region Properties

/// <summary>
/// Returns the number of objects in the category
/// </summary>
public int ObjectCount { get { return this.objects.Count; } }

/// <summary>
/// Gets the name of the Category
/// </summary>
Expand All @@ -68,7 +73,7 @@ public string LocationString {
}
set {
if (this.defaultLocation == null)
this.defaultLocation = new Placement(value);
this.defaultLocation = new SpecificLocation(value, true, false);
else
this.defaultLocation.Name = value;
}
Expand All @@ -90,7 +95,7 @@ public string RoomString
set
{
if(this.defaultLocation == null)
this.defaultLocation = new Placement("unknown");
this.defaultLocation = new SpecificLocation("unknown", true, false);
this.defaultLocation.Room = new Room(value);
}
}
Expand All @@ -99,7 +104,7 @@ public string RoomString
/// Gets or sets the default location for objects in the category
/// </summary>
[XmlIgnore]
public Placement DefaultLocation {
public SpecificLocation DefaultLocation {
get{ return this.defaultLocation;}
set {
this.defaultLocation = value;
Expand All @@ -111,9 +116,9 @@ public Placement DefaultLocation {
/// </summary>
/// <remarks>Use for (de)serialization purposes only</remarks>
[XmlElement("object")]
public List<GPSRObject> Objects
public GPSRObject[] Objects
{
get { return this.objects; }
get { return new List<GPSRObject>(this.objects.Values).ToArray(); }
set {
if(value == null) return;
foreach (GPSRObject o in value)
Expand All @@ -130,10 +135,14 @@ public List<GPSRObject> Objects
/// </summary>
/// <param name="item">The ibject to add to the category</param>
public void AddObject(GPSRObject item){
if (this.Objects.Contains (item))
if (item == null)
return;
item.Category = this;
this.Objects.Add (item);
if ((item.Category != null) && (item.Category != this))
item.Category.RemoveObject(item);
if (!this.objects.ContainsKey(item.Name))
this.objects.Add(item.Name, item);
if (item.Category != this)
item.Category = this;
}

/// <summary>
Expand Down Expand Up @@ -176,13 +185,53 @@ public void AddObject(string name, GPSRObjectType type, DifficultyDegree tier){
this.AddObject (o);
}

/// <summary>
/// Gets a value indicating if the category contains an object with the given name
/// </summary>
/// <param name="objectName">The name of the object to look for</param>
/// <returns>true if the category contains a object with the given name, false otherwise</returns>
public bool Contains(string objectName)
{
return this.objects.ContainsKey(objectName);
}

/// <summary>
/// Removes all objects
/// </summary>
public void Clear()
{
this.objects.Clear();
}

/// <summary>
/// Removes the given GPSRObject from the Category
/// </summary>
/// <param name="item">The GPSRObject to remove</param>
/// <returns>true if the GPSRObject was in the collection, false otherwise</returns>
private bool RemoveObject(GPSRObject item)
{
if (item == null)
return false;
return RemoveObject(item.Name);
}

/// <summary>
/// Removes the given GPSRObject from the room
/// </summary>
/// <param name="objectName">The name of the GPSRObject to remove</param>
/// <returns>true if the GPSRObject was in the collection, false otherwise</returns>
private bool RemoveObject(string objectName)
{
return this.objects.Remove(objectName);
}

/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="GPSRCmdGen.Category"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="GPSRCmdGen.Category"/>.</returns>
public override string ToString ()
{
return string.Format ("{0} [{2} Objects | {1} ]", Name, DefaultLocation.Name, Objects.Count);
return string.Format ("{0} [{2} Objects | {1} ]", Name, DefaultLocation.Name, ObjectCount);
}

#endregion
Expand Down
6 changes: 6 additions & 0 deletions DifficultyDegree.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Xml.Serialization;

namespace GPSRCmdGen
{
Expand All @@ -10,25 +11,30 @@ public enum DifficultyDegree
/// <summary>
/// The difficulty degree is unkwnon
/// </summary>
[XmlEnum("unknown")]
Unknown = -1,
/// <summary>
/// Performing the task is trivial
/// </summary>
[XmlEnum("none")]
None = 0,
/// <summary>
/// Solving the task requires a minimum effort
/// A task using an element with this attribute is easy to solve
/// </summary>
[XmlEnum("easy")]
Easy = 1,
/// <summary>
/// Solving the task requires a moderate effort
/// A task using an element with this attribute is not so easy to solve
/// </summary>
[XmlEnum("moderate")]
Moderate = 3,
/// <summary>
/// Solving the task requires a high effort
/// A task using an element with this attribute is hard to solve
/// </summary>
[XmlEnum("high")]
High = 5
}
}
Expand Down
41 changes: 29 additions & 12 deletions ExampleFilesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,42 @@ namespace GPSRCmdGen
/// <summary>
/// Example files generator.
/// </summary>
public static class ExampleFilesGenerator
{
public static class ExampleFilesGenerator
{
/// <summary>
/// Adds the location of the objects in the given category list to the given rooms list
/// </summary>
/// <param name="rooms">The list of locations grouped by room</param>
/// <param name="categories">The list of objects grouped by category</param>
private static void AddObjectLocations(List<Room> rooms, List<Category> categories)
{
foreach (Category cat in categories)
{
int ix = -1;
for (int i = 0; i < rooms.Count; ++i)
{
if (rooms[i].Name == cat.RoomString) ix = i;
}
if (ix == -1)
{
ix = rooms.Count;
rooms.Add(new Room(cat.RoomString));
}
rooms[ix].AddLocation(cat.DefaultLocation);
}
}

/// <summary>
/// Writes down a set of example data files with information gathered from the Factory
/// </summary>
public static void GenerateExampleFiles(){
GestureContainer gestures = new GestureContainer(Factory.GetDefaultGestures());
CategoryContainer categories = new CategoryContainer(Factory.GetDefaultObjects().Categories);
RoomContainer rooms = new RoomContainer (Factory.GetDefaultLocations ().Rooms);
NameContainer names = new NameContainer (Factory.GetDefaultNames ());
foreach (Category cat in categories.Categories) {
if (!rooms.Rooms.Contains (cat.DefaultLocation.Room))
rooms.Rooms.Add (cat.DefaultLocation.Room);
int rix = rooms.Rooms.IndexOf(cat.DefaultLocation.Room);
rooms.Rooms[rix].AddPlacement(cat.DefaultLocation);
}
CategoryContainer categories = new CategoryContainer(Factory.GetDefaultObjects());
RoomContainer rooms = new RoomContainer (Factory.GetDefaultLocations ());
NameContainer names = new NameContainer (Factory.GetDefaultNames ());
QuestionsContainer questions = new QuestionsContainer(Factory.GetDefaultQuestions());


AddObjectLocations(rooms.Rooms, categories.Categories);
SaveGrammars ();
WriteDatafiles (gestures, categories, rooms, names, questions);
}
Expand Down
Loading

0 comments on commit ab6d3a8

Please sign in to comment.