Gondwana Game Engine Development (master) - 2.6.1
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
CollisionGroupRegistry.cs
1using Newtonsoft.Json;
2
4
9public sealed class CollisionGroupRegistry
10{
11 [JsonProperty("groups")]
12 private readonly Dictionary<string, int> _groups;
13
14 [JsonProperty("nextBit")]
15 private int _nextBit;
16
22 {
23 _groups = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
24
25 Define("WorldStatic");
26 Define("Actors");
27 Define("Projectiles");
28 Define("Triggers");
29 }
30
36#pragma warning disable IDE0051 // Used by Newtonsoft.Json via reflection.
37 [JsonConstructor]
38 private CollisionGroupRegistry(Dictionary<string, int> groups, int nextBit)
39 {
40 _groups = new Dictionary<string, int>(groups ?? throw new ArgumentNullException(nameof(groups)),
41 StringComparer.OrdinalIgnoreCase);
42 _nextBit = nextBit;
43 }
44
45#pragma warning restore IDE0051
46
53 public int Define(string name)
54 {
55 if (string.IsNullOrWhiteSpace(name))
56 throw new ArgumentException("Group name cannot be empty.", nameof(name));
57
58 if (_groups.TryGetValue(name, out var existing))
59 return existing;
60
61 if (_nextBit >= 31)
62 throw new InvalidOperationException("Max 31 collision groups exceeded for int masks.");
63
64 int value = 1 << _nextBit++;
65 _groups.Add(name, value);
66 return value;
67 }
68
74 public int Get(string name)
75 {
76 if (!_groups.TryGetValue(name, out var value))
77 throw new KeyNotFoundException($"Collision group '{name}' not defined.");
78
79 return value;
80 }
81
88 public int GetMask(IEnumerable<string> names)
89 {
90 ArgumentNullException.ThrowIfNull(names);
91
92 int mask = CollisionMasks.None;
93 foreach (var name in names)
94 mask |= Get(name);
95
96 return mask;
97 }
98
103 public IReadOnlyCollection<string> GetGroupNames() => _groups.Keys.ToArray();
104
108 public int WorldStatic => _groups["WorldStatic"];
109
113 public int Actors => _groups["Actors"];
114
118 public int Projectiles => _groups["Projectiles"];
119
123 public int Triggers => _groups["Triggers"];
124}
int GetMask(IEnumerable< string > names)
Combines previously defined collision groups into one bit mask. An empty collection resolves to Colli...
int WorldStatic
Gets the bit flag value for the WorldStatic collision group.
int Get(string name)
Gets the bit flag value for a previously defined collision group.
int Projectiles
Gets the bit flag value for the Projectiles collision group.
IReadOnlyCollection< string > GetGroupNames()
Gets a read-only collection of all defined collision group names.
int Define(string name)
Defines a new collision group with the specified name, or returns the existing group value if already...
int Actors
Gets the bit flag value for the Actors collision group.
int Triggers
Gets the bit flag value for the Triggers collision group.
CollisionGroupRegistry()
Initializes a new instance of the CollisionGroupRegistry class with predefined collision groups: Worl...