Skip to content

Commit

Permalink
Walk this way
Browse files Browse the repository at this point in the history
- Update instances using a walker object created at the top level
  • Loading branch information
Jozufozu committed Sep 15, 2024
1 parent ed35c5a commit 5ffddeb
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.ObjIntConsumer;
Expand Down Expand Up @@ -170,6 +171,16 @@ public void translateAndRotate(PoseStack poseStack) {
}

public void updateInstances(PoseStack poseStack) {
// Need to use an anonymous class so it can reference this.
updateInstancesInner(poseStack, new Walker() {
@Override
public void accept(InstanceTree child) {
child.updateInstancesInner(poseStack, this);
}
});
}

private void updateInstancesInner(PoseStack poseStack, Walker walker) {
if (visible) {
poseStack.pushPose();
translateAndRotate(poseStack);
Expand All @@ -179,9 +190,9 @@ public void updateInstances(PoseStack poseStack) {
.setChanged();
}

for (InstanceTree child : children.values()) {
child.updateInstances(poseStack);
}
// Use the bare HashMap.forEach because .values() always allocates a new collection.
// We also don't want to store an array of children because that would statically use a lot more memory.
children.forEach(walker);

poseStack.popPose();
}
Expand Down Expand Up @@ -292,4 +303,14 @@ public void delete() {
public interface ObjIntIntConsumer<T> {
void accept(T t, int i, int j);
}

// Helper interface for writing walking classes.
private interface Walker extends BiConsumer<String, InstanceTree> {
void accept(InstanceTree child);

@Override
default void accept(String name, InstanceTree child) {
accept(child);
}
}
}

0 comments on commit 5ffddeb

Please sign in to comment.