-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFilters.go
130 lines (109 loc) · 2.78 KB
/
Filters.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package talosecs
func FilterWith[A any]() []A {
slice, length := getComponentsGeneric[A]()
var result = make([]A, length)
for i, c := range slice { // I can't just return slice because it is of type []any, and there is no fast way of conversion to the []A
if compA, ok := c.(A); ok {
result[i] = compA
}
}
return result
}
func FilterWith2[A any, B any]() ([]A, []B) {
sliceA, maxLength := getComponentsGeneric[A]()
var resultA = make([]A, maxLength)
var resultB = make([]B, maxLength)
i := 0
for _, c := range sliceA {
if compA, ok := c.(A); ok {
e := GetEntity(compA)
if compB, ok2 := GetComponent[B](e); ok2 {
resultA[i] = compA
resultB[i] = compB
i++
}
}
}
return resultA[:i], resultB[:i]
}
func FilterWith3[A any, B any, C any]() ([]A, []B, []C) {
sliceA, maxLength := getComponentsGeneric[A]()
var resultA = make([]A, maxLength)
var resultB = make([]B, maxLength)
var resultC = make([]C, maxLength)
i := 0
for _, c := range sliceA {
if compA, ok := c.(A); ok {
e := GetEntity(compA)
if compB, ok2 := GetComponent[B](e); ok2 {
if compC, ok3 := GetComponent[C](e); ok3 {
resultA[i] = compA
resultB[i] = compB
resultC[i] = compC
i++
}
}
}
}
return resultA[:i], resultB[:i], resultC[:i]
}
func FilterW1Excl1[A any, Excl any]() []A {
slice, length := getComponentsGeneric[A]()
var result = make([]A, length)
i := 0
for _, c := range slice {
e := GetEntity(c)
if compA, ok := c.(A); ok && !HasComponent[Excl](e) {
result[i] = compA
i++
}
}
return result[:i]
}
func FilterW2Excl1[A any, B any, Excl any]() ([]A, []B) {
sliceA, maxLength := getComponentsGeneric[A]()
var resultA = make([]A, maxLength)
var resultB = make([]B, maxLength)
i := 0
for _, c := range sliceA {
if compA, ok := c.(A); ok {
e := GetEntity(compA)
if compB, ok2 := GetComponent[B](e); ok2 && !HasComponent[Excl](e) {
resultA[i] = compA
resultB[i] = compB
i++
}
}
}
return resultA[:i], resultB[:i]
}
func FilterW1Excl2[A any, ExclA any, ExclB any]() []A {
slice, length := getComponentsGeneric[A]()
var result = make([]A, length)
i := 0
for _, c := range slice {
e := GetEntity(c)
if compA, ok := c.(A); ok && !HasComponent[ExclA](e) && !HasComponent[ExclB](e) {
result[i] = compA
i++
}
}
return result[:i]
}
func FilterW2Excl2[A any, B any, ExclA any, ExclB any]() ([]A, []B) {
sliceA, maxLength := getComponentsGeneric[A]()
var resultA = make([]A, maxLength)
var resultB = make([]B, maxLength)
i := 0
for _, c := range sliceA {
if compA, ok := c.(A); ok {
e := GetEntity(compA)
if compB, ok2 := GetComponent[B](e); ok2 && !HasComponent[ExclA](e) && !HasComponent[ExclB](e) {
resultA[i] = compA
resultB[i] = compB
i++
}
}
}
return resultA[:i], resultB[:i]
}