|
| 1 | +package ecs |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/bits" |
| 5 | +) |
| 6 | + |
| 7 | +// MaskTotalBits is the size of a [Mask] in bits. |
| 8 | +// It is the maximum number of component types that may exist in any [World]. |
| 9 | +// |
| 10 | +// Use build tag `tiny` to reduce all masks to 64 bits. |
| 11 | +const MaskTotalBits = 256 |
| 12 | + |
| 13 | +// Mask is a 256 bit bitmask. |
| 14 | +// It is also a [Filter] for including certain components. |
| 15 | +// |
| 16 | +// Use [All] to create a mask for a list of component IDs. |
| 17 | +// A mask can be further specified using [Mask.Without] or [Mask.Exclusive]. |
| 18 | +// |
| 19 | +// Use build tag `tiny` to reduce all masks to 64 bits. |
| 20 | +type Mask struct { |
| 21 | + bits [4]uint64 // 4x 64 bits of the mask |
| 22 | +} |
| 23 | + |
| 24 | +// Matches the mask as filter against another mask. |
| 25 | +func (b Mask) Matches(bits *Mask) bool { |
| 26 | + return bits.Contains(&b) |
| 27 | +} |
| 28 | + |
| 29 | +// All creates a new Mask from a list of IDs. |
| 30 | +// Matches all entities that have the respective components, and potentially further components. |
| 31 | +// |
| 32 | +// See also [Mask.Without] and [Mask.Exclusive] |
| 33 | +func All(ids ...ID) Mask { |
| 34 | + var mask Mask |
| 35 | + for _, id := range ids { |
| 36 | + mask.Set(id, true) |
| 37 | + } |
| 38 | + return mask |
| 39 | +} |
| 40 | + |
| 41 | +// Get reports whether the bit at the given index [ID] is set. |
| 42 | +func (b *Mask) Get(bit ID) bool { |
| 43 | + idx := bit.id / 64 |
| 44 | + offset := bit.id - (64 * idx) |
| 45 | + mask := uint64(1 << offset) |
| 46 | + return b.bits[idx]&mask == mask |
| 47 | +} |
| 48 | + |
| 49 | +// Set sets the state of the bit at the given index. |
| 50 | +func (b *Mask) Set(bit ID, value bool) { |
| 51 | + idx := bit.id / 64 |
| 52 | + offset := bit.id - (64 * idx) |
| 53 | + if value { |
| 54 | + b.bits[idx] |= (1 << offset) |
| 55 | + } else { |
| 56 | + b.bits[idx] &= ^(1 << offset) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Not returns the inversion of this mask. |
| 61 | +func (b *Mask) Not() Mask { |
| 62 | + return Mask{ |
| 63 | + bits: [4]uint64{^b.bits[0], ^b.bits[1], ^b.bits[2], ^b.bits[3]}, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// IsZero returns whether no bits are set in the mask. |
| 68 | +func (b *Mask) IsZero() bool { |
| 69 | + return b.bits[0] == 0 && b.bits[1] == 0 && b.bits[2] == 0 && b.bits[3] == 0 |
| 70 | +} |
| 71 | + |
| 72 | +// Reset the mask setting all bits to false. |
| 73 | +func (b *Mask) Reset() { |
| 74 | + b.bits = [4]uint64{0, 0, 0, 0} |
| 75 | +} |
| 76 | + |
| 77 | +// Contains reports if the other mask is a subset of this mask. |
| 78 | +func (b *Mask) Contains(other *Mask) bool { |
| 79 | + return b.bits[0]&other.bits[0] == other.bits[0] && |
| 80 | + b.bits[1]&other.bits[1] == other.bits[1] && |
| 81 | + b.bits[2]&other.bits[2] == other.bits[2] && |
| 82 | + b.bits[3]&other.bits[3] == other.bits[3] |
| 83 | +} |
| 84 | + |
| 85 | +// ContainsAny reports if any bit of the other mask is in this mask. |
| 86 | +func (b *Mask) ContainsAny(other *Mask) bool { |
| 87 | + return b.bits[0]&other.bits[0] != 0 || |
| 88 | + b.bits[1]&other.bits[1] != 0 || |
| 89 | + b.bits[2]&other.bits[2] != 0 || |
| 90 | + b.bits[3]&other.bits[3] != 0 |
| 91 | +} |
| 92 | + |
| 93 | +// And returns the bitwise AND of two masks. |
| 94 | +func (b *Mask) And(other *Mask) Mask { |
| 95 | + return Mask{ |
| 96 | + bits: [4]uint64{ |
| 97 | + b.bits[0] & other.bits[0], |
| 98 | + b.bits[1] & other.bits[1], |
| 99 | + b.bits[2] & other.bits[2], |
| 100 | + b.bits[3] & other.bits[3], |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Or returns the bitwise OR of two masks. |
| 106 | +func (b *Mask) Or(other *Mask) Mask { |
| 107 | + return Mask{ |
| 108 | + bits: [4]uint64{ |
| 109 | + b.bits[0] | other.bits[0], |
| 110 | + b.bits[1] | other.bits[1], |
| 111 | + b.bits[2] | other.bits[2], |
| 112 | + b.bits[3] | other.bits[3], |
| 113 | + }, |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// Xor returns the bitwise XOR of two masks. |
| 118 | +func (b *Mask) Xor(other *Mask) Mask { |
| 119 | + return Mask{ |
| 120 | + bits: [4]uint64{ |
| 121 | + b.bits[0] ^ other.bits[0], |
| 122 | + b.bits[1] ^ other.bits[1], |
| 123 | + b.bits[2] ^ other.bits[2], |
| 124 | + b.bits[3] ^ other.bits[3], |
| 125 | + }, |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// TotalBitsSet returns how many bits are set in this mask. |
| 130 | +func (b *Mask) TotalBitsSet() int { |
| 131 | + return bits.OnesCount64(b.bits[0]) + bits.OnesCount64(b.bits[1]) + bits.OnesCount64(b.bits[2]) + bits.OnesCount64(b.bits[3]) |
| 132 | +} |
| 133 | + |
| 134 | +// Equals returns whether two masks are equal. |
| 135 | +func (b *Mask) Equals(other *Mask) bool { |
| 136 | + return b.bits == other.bits |
| 137 | +} |
0 commit comments