junkdog / artemis-odb

A continuation of the popular Artemis ECS framework
BSD 2-Clause "Simplified" License
776 stars 111 forks source link

Relay / Receiver: observe component field writes #488

Closed junkdog closed 7 years ago

junkdog commented 7 years ago

What?

@Relay: component scaffolding

Only primitive types and String are supported.

public class Layer extends Component {
    @Relay // mark primitive fields and strings
    public int order;
}

RelayProcessor: listening in on updated values

// this is the actual contract - the @Relay on Layer::order
// is only validated and serves as documentation; so that
// we don't accidentally forget that we might be running 
// custom code against the component.
@Relay.Receiver(type = Layer.class, field = "order")
public class LayerOrderRelay
        implements RelayProcessor<Layer> {

    // auto-@Wire 
    private ComponentMapper<HudRenderable> hudRenderableMapper;

    @Override // compare to systems' begin/end
    public void begin(IntBag entites) {}

    @Override
    public void onUpdate(int entityId,
                         Layer layer) {

        boolean isHud = hudRenderableMapper.has(entityId);
        if (layer.isUiLayer() != isHud)
            hudRenderableMapper.set(entityId, !isHud);
    }

    @Override // just like systems; these are executed in batch
    public void end(IntBag entites) {}
}
junkdog commented 7 years ago

Nvm, can't properly intercept the entity id when writing to the component field.