If you want to run the game you have to
- Install latest NetBeans & Java bundle (I recommend to download the JavaEE bundle so that you can run the web version below too) .
- Download project from GitHub.
- Open project in NetBeans and run it.
The main challenge during this rewrite was to use the original Java game engine (backend) and write an HTML/Javascript front end.
I first looked at some web frameworks like Play, but did not use them since for my simple app, learning how to use the framework would take longer than to use vanilla Javascript.
Next, I had to find out what protocol to use for communication between the back and front ends. The most logical choice seemed websockets. However, App Engine was not supporting websockets, so I decided to use HTTP.
The downside of HTTP is that it works on the request-response model, i.e. communication only occurs if there is a request from the front end to the backend. This meant that I had to move the game timer out from the Java backend to the Javascript front end. My attempt to make it work on App Engine failed again which made me question the usefulness of App Engine.
I used the gson library to convert objects to json strings and back. I sent json strings between the back and front ends. Since the front end and backend was completely decoupled (i.e. they were separate processes, one working in the JVM, the other in the browser), I had to pass the whole game state as a giant json string from the back end to the front end. I captured mouse events on the HTML/Javascipt side and send them as a json string to Java. This meant that instead of the controller setting the mouse event handler in the view, the controller's job was to respond to requests and convert json strings back and forth, i.e it was an adapter.
Doing web app development was a little frustrating from the debugging point of view because
- I could not set breapoints in JavaScript within NetBeans. I had to use the browsers debugging tools. I made use of a lot of print() statements in Java and alert() calls in JavaScript.
- Sometimes the code changes would not be reflected on the app instantly and I lost time wondering what was wrong. A browser refresh usually resolves it.
- Install latest NetBeans & JavaEE bundle (Note that JavaSE is not enough because it lacks webserver)
- Download latest gson jar file.
- Download project from GitHub.
- Open project in NetBeans.
- Point gson to the jar file you downloaded in step 2.
- Run it. The game will launch in your default web browser.
In the future I plan to
- Use websockets and move the game timer back to Java backend.
- Use Play framework to gain experience with using web frameworks.
- Buy a VPS and host my game there.
- Polish the game (sounds, graphics, levels).
- Port it to Android.
No comments:
Post a Comment