summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/AspectBuilder.cs
blob: d749795e003210e48e54f1e39d11d189ecd6c588 (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
60
61
62
63
using System;
using System.Collections.Specialized;
using MonoGame.Extended.Collections;

namespace MonoGame.Extended.Entities
{
    public class AspectBuilder
    {
        public AspectBuilder()
        {
            AllTypes = new Bag<Type>();
            ExclusionTypes = new Bag<Type>();
            OneTypes = new Bag<Type>();
        }

        public Bag<Type> AllTypes { get; }
        public Bag<Type> ExclusionTypes { get; }
        public Bag<Type> 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<Type> types, ref BitVector32 bits)
        {
            foreach (var type in types)
            {
                var id = componentManager.GetComponentTypeId(type);
                bits[1 << id] = true;
            }
        }
    }
}