Last active
October 21, 2018 00:16
-
-
Save clinuxrulz/089d3179e73efb66a19d0e87634b341f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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