I am taking a break from the server code to give me sometime to think about how I want to resolve some of the issues that Rob N. @ Bivio software pointed out to me. In the interim, I’ve moved back to the making games theme.
LunarLander – Rough draft Project page
This will be the third time I’ve implemented a Lunar lander simulation game; the first was in C++ using inline ASM and mode 13H as my graphics interface. Fun stuff. The second was in Java in the late 90’s and not so fun ( My last line of Java was 2000 for a reason). Now comes the third iteration, implemeneted in Javascript using the Canvas tag. Overall the experience wasn’t so bad, I was initially frustrated annoyed at the lack of a simple putPixel like mechanism, but canvasContext2d.arc or rect with a size/width of 1 pixel is a relatively good substitute.
I decided to go with a run loop pattern instead of a tickless/event driven mechanism. Sure, a tickless system is more efficient ( avoiding unecessary redraws ) but it’s also a tad more complicated and prone to error. Also, I am relying HEAVILY on JIT javascript interpreters to make the MainLoop class efficient. Normally you want to minimize as much as possible unnecessary function calls but MainLoop’s logic handler has a stack and list mechanism that it iterates over, every tick.
The list is a constant actions function, every tick the logic handler calls each element which is an anonymous function. In Lunar lander I used this to split off the status board in two, key state’s being handled by an anonymous function, while the other stats are handled by the Game logic class itself.
The stack on the other hand will be used for staggered events, one anonymous function being popped off and executed per tick. Some in-efficient idea’s for using it would be for animation effects. Push 10 actions onto the stack, each action drawing say an explosion image frame in progressing fashion.
LunarLander’s logic add’s a hook onto MainLoop’s constant action list and from there its pretty straight forward. Every tick, add a gravity value amount to the Y delta velocity, then add this to the current Y position. If Y is > then the height of the screen, then the lander has crashed/landed.