Skip to content

Commit fd1aac2

Browse files
committed
remove old reference of direct component acess from iter
1 parent c328f16 commit fd1aac2

2 files changed

Lines changed: 13 additions & 35 deletions

File tree

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.elebras1.flecs.*;
44
import com.github.elebras1.flecs.examples.components.Minister;
5+
import com.github.elebras1.flecs.examples.components.MinisterView;
56
import com.github.elebras1.flecs.util.FlecsConstants;
67

78
import java.util.Random;
@@ -18,22 +19,18 @@ public static void main(String[] args) {
1819
world.obtainEntity(world.entity("Min_" + i)).set(new Minister("M-" + i, "default.png", rnd.nextFloat() * 50, 2020, 0));
1920
}
2021

21-
world.system("LoyaltySystem")
22-
.with(Minister.class)
23-
.kind(FlecsConstants.EcsOnUpdate)
24-
.multiThreaded(true)
25-
.iter(it -> {
26-
int count = it.count();
27-
for (int i = 0; i < count; i++) {
28-
float loyalty = it.fieldFloat(Minister.class, 0, "loyalty", i);
29-
30-
float newLoyalty = Math.min(loyalty + 10.0f, 100.0f);
31-
String newImg = newLoyalty > 50 ? "happy.png" : "angry.png";
32-
33-
it.setFieldFloat(Minister.class, 0, "loyalty", i, newLoyalty);
34-
it.setFieldString(Minister.class, 0, "imageFileName", i, newImg);
35-
}
36-
});
22+
world.system("LoyaltySystem").with(Minister.class).kind(FlecsConstants.EcsOnUpdate).multiThreaded(true).iter(it -> {
23+
Field<Minister> ministerField = it.field(Minister.class, 0);
24+
for (int i = 0; i < it.count(); i++) {
25+
MinisterView ministerView = ministerField.getMutView(i);
26+
27+
float newLoyalty = Math.min(ministerView.loyalty() + 10.0f, 100.0f);
28+
String newImg = newLoyalty > 50 ? "happy.png" : "angry.png";
29+
30+
ministerView.loyalty(newLoyalty);
31+
ministerView.imageFileName(newImg);
32+
}
33+
});
3734

3835
for (int f = 0; f < 5; f++) {
3936
world.progress(0.016f);

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ public static void main(String[] args) {
5151
System.out.println("--- Benchmark 4: iter() + Field<T>.getMutView(i) ---");
5252
runBenchmark("iter+Field+getMutView", () -> benchmarkIterFieldGetMutView(query));
5353

54-
System.out.println("--- Benchmark 5: iter() + fieldFloat() (native) ---");
55-
runBenchmark("iter+native", () -> benchmarkIterNative(query));
56-
5754
System.out.println();
5855
System.out.println("=== Benchmark Complete ===");
5956
}
@@ -120,22 +117,6 @@ private static float benchmarkIterFieldGetMutView(Query query) {
120117
return sum[0];
121118
}
122119

123-
private static float benchmarkIterNative(Query query) {
124-
final float[] sum = {0.0f};
125-
126-
query.iter(iter -> {
127-
for (int i = 0; i < iter.count(); i++) {
128-
float posX = iter.fieldFloat(Position.class, 0, "x", i);
129-
float posY = iter.fieldFloat(Position.class, 0, "y", i);
130-
float velDx = iter.fieldFloat(Velocity.class, 1, "dx", i);
131-
float velDy = iter.fieldFloat(Velocity.class, 1, "dy", i);
132-
sum[0] += posX + posY + velDx + velDy;
133-
}
134-
});
135-
136-
return sum[0];
137-
}
138-
139120
private static void runBenchmark(String name, java.util.function.Supplier<Float> benchmark) {
140121
System.out.print(" Warming up... ");
141122
for (int i = 0; i < WARMUP_ITERATIONS; i++) {

0 commit comments

Comments
 (0)