@donmc:
I've recently been working on a "layout engine" for my latest game that works similarly to the JavaScript/DOM event implementation.
One way you could do it is follow an interface along the lines of:
1 2 3 4 5 6 7 8 9 10 11 12
| interface Event { }
interface Node { void appendNode(Node n); Iterable<Node> nodes(); boolean handlesEvent(Event e); void removeNode(Node n); } |
In this instance, you create a few "top-level" nodes (assume
DefaultNode is a working implementation of
Node, and that
RootNode is an already instanced object)
1 2 3 4 5 6 7
| Node optionsLayer = new DefaultNode(); Node startupScreen = new DefaultNode(); Node gameScreen = new DefaultNode();
rootNode.appendNode(optionsLayer); rootNode.appendNode(startupScreen); rootNode.appendNode(gameScreen); |
Then, for event handling, you start at the root node, and then iterate through all nodes and their children in a "tree-like" format
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static boolean fireEvent(Event e) { return fireEvent(rootNode, e); }
private static boolean fireEvent(Node currentNode, Event e) { for (Node child : currentNode.nodes()) if (fireEvent(child, e)) return true;
return currentNode.handlesEvent(e); } |
The end result would be as such:
1 2 3 4 5 6 7
| Fire Event: Root Node Node 0 (Options Layer) If Node in Options Layer available to Handle Event, Return True Node 1 (Startup Screen) ... Node 2 (Game Screen) ... |
I've used a derivative of this technique for a simple Real-Time simulation with thousands of objects, and it worked fine. Depending on your implementation of the
Node interface performance will vary - I'd just use a simple ArrayList as the backing until you have a working model that is to slow to work with.