Skip to content

Commit 22194db

Browse files
committed
feat : entity view
1 parent d5d459f commit 22194db

5 files changed

Lines changed: 53 additions & 16 deletions

File tree

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

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

33
import com.github.elebras1.flecs.Entity;
4+
import com.github.elebras1.flecs.EntityView;
45
import com.github.elebras1.flecs.World;
56
import com.github.elebras1.flecs.examples.components.*;
67
import com.github.elebras1.flecs.util.FlecsConstants;
@@ -13,24 +14,27 @@ public static void main(String[] args) {
1314
world.component(Velocity.class);
1415
world.component(Inventory.class);
1516

16-
for (int i = 0; i < 10; i++) {
17-
long entityId = world.entity("Entity_" + i);
18-
Entity entity = world.obtainEntity(entityId);
19-
entity.set(new Position(i * 10.0f, i * 5.0f));
20-
entity.set(new Velocity(1.0f, 0.5f));
21-
int[] elements = new int[10];
22-
for(int j = 0; j < elements.length; j++) {
23-
elements[j] = j;
17+
// obtainEntityView() requires an active scope to function properly.
18+
world.scope(() -> {
19+
for (int i = 0; i < 10; i++) {
20+
long entityId = world.entity("Entity_" + i);
21+
EntityView entityView = world.obtainEntityView(entityId);
22+
entityView.set(new Position(i * 10.0f, i * 5.0f));
23+
entityView.set(new Velocity(1.0f, 0.5f));
24+
int[] elements = new int[10];
25+
for(int j = 0; j < elements.length; j++) {
26+
elements[j] = j;
27+
}
28+
entityView.set(new Inventory(elements));
2429
}
25-
entity.set(new Inventory(elements));
26-
}
30+
});
2731

2832
world.system("MovementSystem").kind(FlecsConstants.EcsOnUpdate).with(Position.class).with(Velocity.class).multiThreaded().each(entityId -> {
29-
Entity entity = world.obtainEntity(entityId);
33+
EntityView entityView = world.obtainEntityView(entityId);
3034

31-
PositionView positionView = entity.getMutView(Position.class);
32-
VelocityView velocityView = entity.getMutView(Velocity.class);
33-
InventoryView inventoryView = entity.getMutView(Inventory.class);
35+
PositionView positionView = entityView.getMutView(Position.class);
36+
VelocityView velocityView = entityView.getMutView(Velocity.class);
37+
InventoryView inventoryView = entityView.getMutView(Inventory.class);
3438

3539
float currentX = positionView.x();
3640
float currentY = positionView.y();

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99

1010
public class Entity {
1111

12-
private final World world;
13-
private final long id;
12+
private World world;
13+
private long id;
1414

1515
Entity(World world, long id) {
1616
this.world = world;
1717
this.id = id;
1818
}
1919

20+
protected void setWorld(World world) {
21+
this.world = world;
22+
}
23+
24+
protected void setId(long id) {
25+
this.id = id;
26+
}
27+
2028
public long id() {
2129
return id;
2230
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.elebras1.flecs;
2+
3+
public class EntityView extends Entity {
4+
5+
EntityView(World world, long id) {
6+
super(world, id);
7+
}
8+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ public class FlecsContext {
88

99
public static class ViewCache {
1010
private final Map<Class<?>, ComponentView> views;
11+
private final EntityView entityView;
1112

1213
public ViewCache() {
1314
this.views = new IdentityHashMap<>(8);
15+
this.entityView = new EntityView(null, 0);
1416
}
1517

1618
public ComponentView get(Class<?> componentClass) {
@@ -27,5 +29,9 @@ public ComponentView get(Class<?> componentClass) {
2729

2830
return view;
2931
}
32+
33+
public EntityView getEntityView() {
34+
return this.entityView;
35+
}
3036
}
3137
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ public Entity obtainEntity(long entityId) {
184184
return new Entity(this, entityId);
185185
}
186186

187+
public EntityView obtainEntityView(long entityId) {
188+
if(entityId <= 0) {
189+
throw new IllegalArgumentException("Invalid entity ID: " + entityId);
190+
}
191+
192+
EntityView entityView = FlecsContext.CURRENT_CACHE.get().getEntityView();
193+
entityView.setWorld(this);
194+
entityView.setId(entityId);
195+
return entityView;
196+
}
197+
187198
MemorySegment getComponentBuffer(long size) {
188199
return this.defaultBuffers.componentBuffer().ensure(size);
189200
}

0 commit comments

Comments
 (0)