So, enough people asked me that I finally got off my butt and put it up in contrib:
http://code.google.com/p/jmonkeyplatform-contributions/source/detail?r=961
I called it Zay-ES as a play on “ES” and Dr. Zaius… because he’s a “monkey”. If you do not know who Dr. Zaius is then please turn in your geek card at the door on your way out.
So this is my ES.
Is it perfect? No. Do I use it to run my game/server every day 24/day? Yes.
Hopefully it can get beaten into shape. I think the core is pretty good
Some issues I noticed during cleanup:
- The SQL stuff is kind of hard-coded to hsqldb’s approach at the moment. It’s all JDBC mostly, but the main SqlEntityData class is only setup to load the hsqldb driver and has some specific code for configuring that for optimal use. The system is architected to support multiple database flavors (with ComponentTable subclasses, etc.) but I’ve done enough of this OR mapping in my day job to shudder at the thought.
- I sometime back removed the non-persistent DefaultEntityData class. I need to add another one. (In fact, if SqlEntityData did not fail on lack of JDBC driver it would work as a fine map-based entity handler… but I digress.)
Usage:
Presuming you have the hsqldb jar in your classpath (as well as the two dependencies in the zay-es lib directory)…
// The main entry point to my ES is EntityData... I think it better describes what we're
// doing and avoids the "system" confusion
EntityData ed = new SqlEntityData( "some/path", 100 ); // 100 ms write-delay
// Create an entity and set some stuff using raw access
EntityId anakin = ed.createEntity();
ed.setComponent( anakin, new Name( "Anakin Skywalker");
EntityId c3po = ed.createEntity();
ed.setComponent( c3po, new Name( "C-3PO");
es.setComponent( c3po, new CreatedBy(anakin) );
The Name and CreatedBy components are built into the ES as they were deemed extremely common components.
Now, if you wanted to see and keep track of all of Anakin’s creations:
ComponentFilter filter = FieldFilter.create( CreatedBy.class, "creatorId", anakin );
EntitySet es = ed.getEntities( filter, CreatedBy.class, Name.class );
es.applyChanges(); // not strictly necessary but not a bad idea
System.out.println( "Anakin's creations:" + es );
...later in an update loop or something...
if( es.applyChanges() ) {
addStuff( es.getAddedEntities() );
removeStuff( es.getRemovedEntities() );
updateStuff( es.getChangedEntities() );
}
…and those components are all persistent… so it will remember them next time, also.
For the really curious, you can look at the hsqldb by running a command similar to:
java -cp lib\hsqldb.jar org.hsqldb.util.DatabaseManagerSwing