Skip to content

Instantly share code, notes, and snippets.

@clinuxrulz
Last active October 21, 2018 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clinuxrulz/089d3179e73efb66a19d0e87634b341f to your computer and use it in GitHub Desktop.
Save clinuxrulz/089d3179e73efb66a19d0e87634b341f to your computer and use it in GitHub Desktop.
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