Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static <A> nz.sodium.Cell<Option<A>> watchEntityComponentOp(
nz.sodium.Cell<Option<EcsReadOnlySceneContext>> cSceneCtxOp,
nz.sodium.Stream<EcsSceneChanges> sSceneChanges,
int entityId,
EcsComponent<A> ecsComponent
) {
nz.sodium.StreamLoop<Option<A>> slEntityComponentOp = new sodium.StreamLoop<>();
nz.sodium.Cell<Option<A>> cEntityComponentOp =
slEntityComponentOp
.hold(
cSceneCtxOp.sample()
.bind(
(EcsReadOnlySceneContext sceneCtx) ->
sceneCtx.getComponent(entityId, ecsComponent)
)
);
slEntityComponentOp.loop(
sodium.Stream.filterOptional(
sSceneChanges.snapshot(
cEntityComponentOp,
cSceneCtxOp,
(EcsSceneChanges sceneChanges, Option<A> entityComponentOp, Option<EcsReadOnlySceneContext> sceneCtxOp) -> {
Option<Option<A>> _nextEntityComponentOpOp = Option.none();
for (EcsSceneChange sceneChange : sceneChanges.changes()) {
Option<Option<A>> lastEntityComponentOpOp = _nextEntityComponentOpOp;
_nextEntityComponentOpOp = sceneChange.match(new EcsSceneChange.CasesAdapter<Option<Option<A>>>(lastEntityComponentOpOp) {
@Override
public Option<Option<A>> newScene() {
return sceneCtxOp.map((EcsReadOnlySceneContext sceneCtx) -> sceneCtx.getComponent(entityId, ecsComponent));
}
@Override
public Option<Option<A>> setComponents(int entityId2, EcsComponentValue[] components) {
if (entityId2 != entityId) {
return lastEntityComponentOpOp;
}
for (EcsComponentValue componentValue : components) {
for (A component : componentValue.value(ecsComponent)) {
return Option.some(Option.some(component));
}
}
return lastEntityComponentOpOp;
}
@Override
public Option<Option<A>> unsetComponents(int entityId2, EcsComponentType[] componentTypes) {
if (entityId2 != entityId) {
return lastEntityComponentOpOp;
}
for (EcsComponentType componentType : componentTypes) {
if (componentType.equals(ecsComponent.type())) {
return Option.some(Option.none());
}
}
return lastEntityComponentOpOp;
}
});
}
return fj.data.Java8.Option_Optional(_nextEntityComponentOpOp);
}
)
)
);
return cEntityComponentOp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.