using System; using System.Collections.Generic; using System.Runtime.CompilerServices; #pragma warning disable 660,661 namespace Svelto.ECS { /// /// Exclusive Groups guarantee that the GroupID is unique. /// /// The best way to use it is like: /// /// public static class MyExclusiveGroups //(can be as many as you want) /// { /// public static ExclusiveGroup MyExclusiveGroup1 = new ExclusiveGroup(); /// /// public static ExclusiveGroup[] GroupOfGroups = { MyExclusiveGroup1, ...}; //for each on this! /// } /// ///To debug it use in your debug window: Svelto.ECS.Debugger.EGID.GetGroupNameFromId(groupID) public sealed class ExclusiveGroup { public const uint MaxNumberOfExclusiveGroups = 2 << 20; public ExclusiveGroup() { _group = ExclusiveGroupStruct.Generate(); } public ExclusiveGroup(string recognizeAs) { _group = ExclusiveGroupStruct.Generate(); _knownGroups.Add(recognizeAs, _group); } public ExclusiveGroup(ExclusiveGroupBitmask bitmask) { _group = ExclusiveGroupStruct.Generate((byte) bitmask); } public ExclusiveGroup(ushort range) { _group = ExclusiveGroupStruct.GenerateWithRange(range); #if DEBUG _range = range; #endif } public static implicit operator ExclusiveGroupStruct(ExclusiveGroup group) { return group._group; } public static ExclusiveGroupStruct operator+(ExclusiveGroup @group, uint b) { #if DEBUG if (@group._range == 0) throw new ECSException($"Adding values to a not ranged ExclusiveGroup: {@group.id}"); if (b >= @group._range) throw new ECSException($"Using out of range group: {@group.id} + {b}"); #endif return group._group + b; } public uint id => _group.id; //todo document the use case for this method public static ExclusiveGroupStruct Search(string holderGroupName) { if (_knownGroups.ContainsKey(holderGroupName) == false) throw new Exception("Named Group Not Found ".FastConcat(holderGroupName)); return _knownGroups[holderGroupName]; } public override string ToString() { return _group.ToString(); } static readonly Dictionary _knownGroups = new Dictionary(); #if DEBUG readonly ushort _range; #endif readonly ExclusiveGroupStruct _group; } }