Skip to content

Commit 3107977

Browse files
committed
feat operations filter in system and observer
1 parent 453a6fc commit 3107977

5 files changed

Lines changed: 112 additions & 21 deletions

File tree

examples/src/main/java/com/github/elebras1/flecs/examples/ObserverExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void main(String[] args) {
4848
e3.set(new Velocity(2.0f, 2.0f));
4949
e3.remove(Velocity.class);
5050

51-
world.observer().with(Position.class).with(Velocity.class).filter().event(EcsOnAdd).each((entityId) ->
51+
world.observer().with(Position.class).with(Velocity.class).inOut().event(EcsOnAdd).each((entityId) ->
5252
System.out.println("Position added to entity with Velocity: " + entityId)
5353
);
5454

src/main/java/com/github/elebras1/flecs/Field.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.elebras1.flecs;
22

3-
import java.lang.foreign.Arena;
43
import java.lang.foreign.MemorySegment;
54

65
public class Field<T> {

src/main/java/com/github/elebras1/flecs/FlecsObserver.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.elebras1.flecs;
22

33
public class FlecsObserver {
4-
4+
55
private final World world;
66
private final long observerId;
77

@@ -14,10 +14,6 @@ public long id() {
1414
return this.observerId;
1515
}
1616

17-
public World world() {
18-
return this.world;
19-
}
20-
2117
public void enable() {
2218
flecs_h.ecs_enable(this.world.nativeHandle(), this.observerId, true);
2319
}

src/main/java/com/github/elebras1/flecs/ObserverBuilder.java

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.elebras1.flecs;
22

3+
import com.github.elebras1.flecs.util.FlecsConstants;
4+
35
import java.lang.foreign.Arena;
46
import java.lang.foreign.MemorySegment;
57
import java.lang.foreign.ValueLayout;
@@ -101,47 +103,98 @@ public ObserverBuilder with(long relationId, long objectId) {
101103
return this;
102104
}
103105

104-
public ObserverBuilder without(long componentId) {
105-
this.with(componentId);
106-
106+
public ObserverBuilder in() {
107107
if (this.termCount == 0) {
108-
throw new IllegalStateException("No term to apply 'without' modifier to");
108+
throw new IllegalStateException("No term to apply 'in' modifier to");
109109
}
110110

111-
MemorySegment queryDesc = ecs_observer_desc_t.query(this.desc);
111+
MemorySegment queryDesc = ecs_system_desc_t.query(this.desc);
112112
long termsOffset = ecs_query_desc_t.terms$offset();
113113
long termOffset = termsOffset + ((this.termCount - 1) * TERM_SIZE);
114114

115115
MemorySegment term = queryDesc.asSlice(termOffset, TERM_SIZE);
116-
long operOffset = ecs_term_t.oper$offset();
116+
long inoutOffset = ecs_term_t.inout$offset();
117117

118-
term.set(ValueLayout.JAVA_SHORT, operOffset, (short) EcsNot);
118+
term.set(ValueLayout.JAVA_INT, inoutOffset, EcsIn);
119119

120120
return this;
121121
}
122122

123-
public <T> ObserverBuilder without(Class<T> componentClass) {
124-
long componentId = this.world.componentRegistry().getComponentId(componentClass);
125-
return this.without(componentId);
123+
public ObserverBuilder out() {
124+
if (this.termCount == 0) {
125+
throw new IllegalStateException("No term to apply 'out' modifier to");
126+
}
127+
128+
MemorySegment queryDesc = ecs_system_desc_t.query(this.desc);
129+
long termsOffset = ecs_query_desc_t.terms$offset();
130+
long termOffset = termsOffset + ((this.termCount - 1) * TERM_SIZE);
131+
132+
MemorySegment term = queryDesc.asSlice(termOffset, TERM_SIZE);
133+
long inoutOffset = ecs_term_t.inout$offset();
134+
135+
term.set(ValueLayout.JAVA_INT, inoutOffset, EcsOut);
136+
137+
return this;
126138
}
127139

128-
public ObserverBuilder filter() {
140+
public ObserverBuilder inOut() {
129141
if (this.termCount == 0) {
130-
throw new IllegalStateException("No term to apply 'filter' modifier to");
142+
throw new IllegalStateException("No term to apply 'inout' modifier to");
131143
}
132144

133-
MemorySegment queryDesc = ecs_observer_desc_t.query(this.desc);
145+
MemorySegment queryDesc = ecs_system_desc_t.query(this.desc);
134146
long termsOffset = ecs_query_desc_t.terms$offset();
135147
long termOffset = termsOffset + ((this.termCount - 1) * TERM_SIZE);
136148

137149
MemorySegment term = queryDesc.asSlice(termOffset, TERM_SIZE);
138150
long inoutOffset = ecs_term_t.inout$offset();
139151

140-
term.set(ValueLayout.JAVA_SHORT, inoutOffset, (short) EcsIn);
152+
term.set(ValueLayout.JAVA_INT, inoutOffset, EcsInOut);
153+
154+
return this;
155+
}
156+
157+
public ObserverBuilder operator(int operator) {
158+
if (this.termCount == 0) {
159+
throw new IllegalStateException("No term to apply 'operator' modifier to");
160+
}
161+
162+
long termsOffset = ecs_query_desc_t.terms$offset();
163+
long termOffset = termsOffset + ((this.termCount - 1) * TERM_SIZE);
141164

165+
MemorySegment term = this.desc.asSlice(termOffset, TERM_SIZE);
166+
ecs_term_t.oper(term, (short) operator);
142167
return this;
143168
}
144169

170+
public ObserverBuilder and() {
171+
return this.operator(FlecsConstants.EcsAnd);
172+
}
173+
174+
public ObserverBuilder or() {
175+
return this.operator(FlecsConstants.EcsOr);
176+
}
177+
178+
public ObserverBuilder not() {
179+
return this.operator(FlecsConstants.EcsNot);
180+
}
181+
182+
public ObserverBuilder optional() {
183+
return this.operator(FlecsConstants.EcsOptional);
184+
}
185+
186+
public ObserverBuilder andFrom() {
187+
return this.operator(FlecsConstants.EcsAndFrom);
188+
}
189+
190+
public ObserverBuilder orFrom() {
191+
return this.operator(FlecsConstants.EcsOrFrom);
192+
}
193+
194+
public ObserverBuilder notFrom() {
195+
return this.operator(FlecsConstants.EcsNotFrom);
196+
}
197+
145198
public ObserverBuilder yieldExisting() {
146199
ecs_observer_desc_t.yield_existing(this.desc, true);
147200
return this;

src/main/java/com/github/elebras1/flecs/SystemBuilder.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.elebras1.flecs;
22

3+
import com.github.elebras1.flecs.util.FlecsConstants;
4+
35
import java.lang.foreign.Arena;
46
import java.lang.foreign.MemorySegment;
57
import java.lang.foreign.ValueLayout;
@@ -171,6 +173,47 @@ public SystemBuilder inOut() {
171173
return this;
172174
}
173175

176+
public SystemBuilder operator(int operator) {
177+
if (this.termCount == 0) {
178+
throw new IllegalStateException("No term to apply 'operator' modifier to");
179+
}
180+
181+
long termsOffset = ecs_query_desc_t.terms$offset();
182+
long termOffset = termsOffset + ((this.termCount - 1) * TERM_SIZE);
183+
184+
MemorySegment term = this.desc.asSlice(termOffset, TERM_SIZE);
185+
ecs_term_t.oper(term, (short) operator);
186+
return this;
187+
}
188+
189+
public SystemBuilder and() {
190+
return this.operator(FlecsConstants.EcsAnd);
191+
}
192+
193+
public SystemBuilder or() {
194+
return this.operator(FlecsConstants.EcsOr);
195+
}
196+
197+
public SystemBuilder not() {
198+
return this.operator(FlecsConstants.EcsNot);
199+
}
200+
201+
public SystemBuilder optional() {
202+
return this.operator(FlecsConstants.EcsOptional);
203+
}
204+
205+
public SystemBuilder andFrom() {
206+
return this.operator(FlecsConstants.EcsAndFrom);
207+
}
208+
209+
public SystemBuilder orFrom() {
210+
return this.operator(FlecsConstants.EcsOrFrom);
211+
}
212+
213+
public SystemBuilder notFrom() {
214+
return this.operator(FlecsConstants.EcsNotFrom);
215+
}
216+
174217
public <T> SystemBuilder write(Class<T> componentClass) {
175218
long componentId = this.world.componentRegistry().getComponentId(componentClass);
176219
return this.with(componentId).out();

0 commit comments

Comments
 (0)