aboutsummaryrefslogtreecommitdiff
path: root/V3/Objects/IdGenerator.cs
blob: 08976ca99ae3523be9d84b5179743a19d6dd999b (plain)
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
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;
        }
    }
}