From acea7b2e728787a0d83bbf83c8c1f042d2c32e7e Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Mon, 3 Jun 2024 10:15:45 +0800 Subject: + plugins project --- .../MonoGame.Extended.Entities/AspectBuilder.cs | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/AspectBuilder.cs (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/AspectBuilder.cs') diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/AspectBuilder.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/AspectBuilder.cs new file mode 100644 index 0000000..d749795 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/AspectBuilder.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Specialized; +using MonoGame.Extended.Collections; + +namespace MonoGame.Extended.Entities +{ + public class AspectBuilder + { + public AspectBuilder() + { + AllTypes = new Bag(); + ExclusionTypes = new Bag(); + OneTypes = new Bag(); + } + + public Bag AllTypes { get; } + public Bag ExclusionTypes { get; } + public Bag OneTypes { get; } + + public AspectBuilder All(params Type[] types) + { + foreach (var type in types) + AllTypes.Add(type); + + return this; + } + + public AspectBuilder One(params Type[] types) + { + foreach (var type in types) + OneTypes.Add(type); + + return this; + } + + public AspectBuilder Exclude(params Type[] types) + { + foreach (var type in types) + ExclusionTypes.Add(type); + + return this; + } + + public Aspect Build(ComponentManager componentManager) + { + var aspect = new Aspect(); + Associate(componentManager, AllTypes, ref aspect.AllSet); + Associate(componentManager, OneTypes, ref aspect.OneSet); + Associate(componentManager, ExclusionTypes, ref aspect.ExclusionSet); + return aspect; + } + + // ReSharper disable once ParameterTypeCanBeEnumerable.Local + private static void Associate(ComponentManager componentManager, Bag types, ref BitVector32 bits) + { + foreach (var type in types) + { + var id = componentManager.GetComponentTypeId(type); + bits[1 << id] = true; + } + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0