diff options
author | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
---|---|---|
committer | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
commit | ced3d03bdb3ce866d832e03fb212865140905a9a (patch) | |
tree | 2a16c2063a46d3c354ce1585029dda3124f6ad93 /V3/Objects/IdGenerator.cs | |
parent | 0394dccaf06e1009e591a6ff4d645895574724c1 (diff) | |
download | V3-ced3d03bdb3ce866d832e03fb212865140905a9a.tar.gz V3-ced3d03bdb3ce866d832e03fb212865140905a9a.tar.bz2 |
Diffstat (limited to 'V3/Objects/IdGenerator.cs')
-rw-r--r-- | V3/Objects/IdGenerator.cs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/V3/Objects/IdGenerator.cs b/V3/Objects/IdGenerator.cs new file mode 100644 index 0000000..08976ca --- /dev/null +++ b/V3/Objects/IdGenerator.cs @@ -0,0 +1,59 @@ +using System; + +namespace V3.Objects +{ + /// <summary> + /// Static helper class that generates unique IDs for game objects. + /// </summary> + // ReSharper disable once ClassNeverInstantiated.Global + public sealed class IdGenerator + { + private static int sCurrentId; + + private static int? sIdOnce; + + private IdGenerator() + { + throw new NotImplementedException(); + } + + /// <summary> + /// Get an ID for a new game object. + /// </summary> + /// <returns>the ID to use for a new game object</returns> + public static int GetNextId() + { + int id; + if (sIdOnce.HasValue) + { + id = sIdOnce.Value; + ClearIdOnce(); + } + else + { + id = sCurrentId; + sCurrentId++; + } + + return id; + } + + /// <summary> + /// Sets the ID to use only for the next object that is created. + /// </summary> + /// <param name="id">the id for the next object</param> + public static void SetIdOnce(int id) + { + sIdOnce = id; + } + + /// <summary> + /// Clear the ID stored by SetIdOnce that is used only for the + /// next object. + /// </summary> + public static void ClearIdOnce() + { + sIdOnce = null; + } + } +} |