Skip to content

Commit 881f191

Browse files
committed
set component in field and iter
1 parent a5ef011 commit 881f191

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ public static void main(String[] args) {
7272

7373
float newX = pos.x() + vel.dx() * it.deltaTime();
7474
float newY = pos.y() + vel.dy() * it.deltaTime();
75-
long entityId = it.entity(i);
76-
Entity entity = world.obtainEntity(entityId);
77-
entity.set(new Position(newX, newY));
78-
// Note: System.out.printf removed to avoid synchronization issues with multi-threading
75+
positions.set(i, new Position(newX, newY));
7976
}
8077
});
8178

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

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

3+
import java.lang.foreign.Arena;
34
import java.lang.foreign.MemorySegment;
45

56
public class Field<T> {
@@ -34,4 +35,20 @@ public T get(int i) {
3435

3536
return this.component.read(elementSegment);
3637
}
38+
39+
public void set(int i, T componentData) {
40+
if (!this.isSet()) {
41+
throw new IllegalStateException("Field is not set.");
42+
}
43+
if (i < 0 || i >= this.count) {
44+
throw new IndexOutOfBoundsException("Index " + i + " out of bounds for count " + this.count);
45+
}
46+
47+
try (Arena tempArena = Arena.ofConfined()) {
48+
long elementOffset = i * this.component.size();
49+
MemorySegment elementSegment = this.memorySegment.asSlice(elementOffset, this.component.size());
50+
51+
this.component.write(elementSegment, componentData, tempArena);
52+
}
53+
}
3754
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,59 @@ public <T> boolean fieldBoolean(Class<T> componentClass, int index, String field
147147
return this.fieldPtr(componentClass, index, fieldName, i, offset).get(ValueLayout.JAVA_BOOLEAN, offset[0]);
148148
}
149149

150+
public <T> byte fieldByte(Class<T> componentClass, int index, String fieldName, int i) {
151+
long[] offset = new long[1];
152+
return this.fieldPtr(componentClass, index, fieldName, i, offset).get(ValueLayout.JAVA_BYTE, offset[0]);
153+
}
154+
155+
public <T> String fieldString(Class<T> componentClass, int index, String fieldName, int i) {
156+
long[] offset = new long[1];
157+
MemorySegment stringPointer = this.fieldPtr(componentClass, index, fieldName, i, offset).get(ValueLayout.ADDRESS.withByteAlignment(1), offset[0]);
158+
if (stringPointer.address() == 0) {
159+
return null;
160+
}
161+
return stringPointer.reinterpret(Long.MAX_VALUE).getString(0);
162+
}
163+
164+
public <T> void setFieldInt(Class<T> componentClass, int index, String fieldName, int i, int value) {
165+
long[] offset = new long[1];
166+
this.fieldPtr(componentClass, index, fieldName, i, offset).set(ValueLayout.JAVA_INT, offset[0], value);
167+
}
168+
169+
public <T> void setFieldFloat(Class<T> componentClass, int index, String fieldName, int i, float value) {
170+
long[] offset = new long[1];
171+
this.fieldPtr(componentClass, index, fieldName, i, offset).set(ValueLayout.JAVA_FLOAT, offset[0], value);
172+
}
173+
174+
public <T> void setFieldDouble(Class<T> componentClass, int index, String fieldName, int i, double value) {
175+
long[] offset = new long[1];
176+
this.fieldPtr(componentClass, index, fieldName, i, offset).set(ValueLayout.JAVA_DOUBLE, offset[0], value);
177+
}
178+
179+
public <T> void setFieldLong(Class<T> componentClass, int index, String fieldName, int i, long value) {
180+
long[] offset = new long[1];
181+
this.fieldPtr(componentClass, index, fieldName, i, offset).set(ValueLayout.JAVA_LONG, offset[0], value);
182+
}
183+
184+
public <T> void setFieldShort(Class<T> componentClass, int index, String fieldName, int i, short value) {
185+
long[] offset = new long[1];
186+
this.fieldPtr(componentClass, index, fieldName, i, offset).set(ValueLayout.JAVA_SHORT, offset[0], value);
187+
}
188+
189+
public <T> void setFieldBoolean(Class<T> componentClass, int index, String fieldName, int i, boolean value) {
190+
long[] offset = new long[1];
191+
this.fieldPtr(componentClass, index, fieldName, i, offset).set(ValueLayout.JAVA_BOOLEAN, offset[0], value);
192+
}
193+
194+
public <T> void setFieldByte(Class<T> componentClass, int index, String fieldName, int i, byte value) {
195+
long[] offset = new long[1];
196+
this.fieldPtr(componentClass, index, fieldName, i, offset).set(ValueLayout.JAVA_BYTE, offset[0], value);
197+
}
198+
199+
public <T> void setFieldString(Class<T> componentClass, int index, String fieldName, int i, String value) {
200+
// TODO
201+
}
202+
150203
public long event() {
151204
return ecs_iter_t.event(this.nativeIter);
152205
}

0 commit comments

Comments
 (0)