Skip to content

Commit d5d459f

Browse files
committed
feat : standelone scope
1 parent fd1aac2 commit d5d459f

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.elebras1.flecs.examples;
2+
3+
import com.github.elebras1.flecs.Entity;
4+
import com.github.elebras1.flecs.World;
5+
import com.github.elebras1.flecs.examples.components.*;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class StandaloneViewExample {
11+
public static void main(String[] args) {
12+
try (World world = new World()) {
13+
// Register components
14+
world.component(Position.class);
15+
world.component(Velocity.class);
16+
world.component(Inventory.class);
17+
18+
List<Entity> entities = new ArrayList<>();
19+
20+
// Create entities with initial data (Records)
21+
for (int i = 0; i < 20; i++) {
22+
long entityId = world.entity("Entity_" + i);
23+
Entity entity = world.obtainEntity(entityId);
24+
entity.set(new Position(i * 10.0f, i * 5.0f));
25+
entity.set(new Velocity(1.0f, 0.5f));
26+
entities.add(entity);
27+
}
28+
29+
// getMutView() requires an active scope to function properly.
30+
world.scope(() -> {
31+
for(Entity entity : entities) {
32+
PositionView positionView = entity.getMutView(Position.class);
33+
VelocityView velocityView = entity.getMutView(Velocity.class);
34+
System.out.println("Entity: " + entity.id() + " Position(" + positionView.x() + ", " + positionView.y() + ") Velocity(" + velocityView.dx() + ", " + velocityView.dy() + ")");
35+
}
36+
});
37+
}
38+
}
39+
}

examples/src/main/java/com/github/elebras1/flecs/examples/ComponentViewExample.java renamed to examples/src/main/java/com/github/elebras1/flecs/examples/ViewExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.github.elebras1.flecs.examples.components.*;
66
import com.github.elebras1.flecs.util.FlecsConstants;
77

8-
public class ComponentViewExample {
8+
public class ViewExample {
99

1010
public static void main(String[] args) {
1111
try (World world = new World()) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ public void disableRest() {
769769
}
770770
}
771771

772+
public void scope(Runnable action) {
773+
ScopedValue.where(FlecsContext.CURRENT_CACHE, new FlecsContext.ViewCache()).run(action);
774+
}
775+
772776
@Override
773777
public void close() {
774778
if (!this.closed) {

0 commit comments

Comments
 (0)