From ced3d03bdb3ce866d832e03fb212865140905a9a Mon Sep 17 00:00:00 2001 From: Thomas Leyh Date: Sun, 24 Jul 2016 08:14:18 +0200 Subject: Add project files. --- V3/Objects/IdGenerator.cs | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 V3/Objects/IdGenerator.cs (limited to 'V3/Objects/IdGenerator.cs') 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 +{ + /// + /// Static helper class that generates unique IDs for game objects. + /// + // ReSharper disable once ClassNeverInstantiated.Global + public sealed class IdGenerator + { + private static int sCurrentId; + + private static int? sIdOnce; + + private IdGenerator() + { + throw new NotImplementedException(); + } + + /// + /// Get an ID for a new game object. + /// + /// the ID to use for a new game object + public static int GetNextId() + { + int id; + if (sIdOnce.HasValue) + { + id = sIdOnce.Value; + ClearIdOnce(); + } + else + { + id = sCurrentId; + sCurrentId++; + } + + return id; + } + + /// + /// Sets the ID to use only for the next object that is created. + /// + /// the id for the next object + public static void SetIdOnce(int id) + { + sIdOnce = id; + } + + /// + /// Clear the ID stored by SetIdOnce that is used only for the + /// next object. + /// + public static void ClearIdOnce() + { + sIdOnce = null; + } + } +} -- cgit v1.2.1