dominion-dev / dominion-ecs-java

Insanely fast ECS (Entity Component System) for Java
https://dominion-dev.github.io
MIT License
288 stars 17 forks source link

withState() doesn't seem to be working #160

Closed timjbrown closed 1 year ago

timjbrown commented 1 year ago

This is an enum I'm using to categorize entities.

package components;

public enum EntityType {
    CUE, BALL, POCKET, WALL;
}

When I do

List<With1<EntityType>> cueResult = GameWorld.get().getDominion()
                .findEntitiesWith(EntityType.class)
                .stream().collect(Collectors.toList());

cueResult has size() 18 and contains With1[comp=CUE, entity=Entity={id=|0:5:0|-> [CUE, java.awt.Color[r=255,g=255,b=255], models.Transform2@3d75a2cf, colliders.ColliderComponent@788dcd3c, bodies.Body@55dc666e, components.TagCue@6ef8985a], stateId=|1:0:0|, enabled=true}]

When I do

List<With1<EntityType>> cueResult = GameWorld.get().getDominion()
                .findEntitiesWith(EntityType.class)
                .withState(EntityType.CUE)
                .stream().collect(Collectors.toList());

cueResult has size 0

Either I don't understand what withState() is intended to do or there is a bug.

In the meantime I've just used a tag class to accomplish the same goal

With1<TagCue> cueResult = GameWorld.get().getDominion().findEntitiesWith(TagCue.class).stream().findFirst().orElse(null);
enricostara commented 1 year ago

Hi @timjbrown , did you set the EntityType.CUE as state as well? If you add an enum type object as a component is fine, but if you want to check a particular value you have to set the enum value as a state: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md#class-entity

timjbrown commented 1 year ago

No it wasn't clear to me how states worked. I thought it referred to the state of components that already exist not an extra idea of state separate on top of that. What would you say is the canonical way to do this in Dominion? To get the CUE ball in this example? By setState() and withState()?

enricostara commented 1 year ago

Ok, this is the general rule: add a component -> you can query to find all entities that have that specific component type; set a state -> you can filter all entities, which you have already found by component type, looking for a particular state (enum value)