Hi Guys,
while I don't have to show new fancy images right now,
I have made some technical progress I want to share.
Backgrounds in Diskophoros stages can be animated. For that purpose the X and Y position of background assets can be described relative to the game time passed.
Just like this:
"xPos": "0-t*5",
"yPos": "60 + sin(t*2)*2",
This feature has been in the game for some time now. In the past I used MVEL,
a powerful expression language library for java, to compile these formulas after game startup and evaluate them passing the elapsed time as a parameter.
I disliked this dependecy, as MVEL was far to powerfull for my usecase
and made it possible to insert code through stage description files,
which may be a security issue in the future if users want to design their own stages.
I decided to create my own Formula representation with a really simple interface:
public interface Formula {
float eval(float t);
}
Which is implemented by a few classes: Constant, Time, BinaryOperator and UnaryOperator.
Now I needed a parser which could parse Strings into Formular objects.
Last time I wrote a parser, I wrote it in Haskell using the parsec library and was very happy with it.
parsec lets users write parsers for complex structures by combining simple parsers (decimal number, constant string, etc.)
using combinators (choice, between, sequence, etc.).
I fortunately found a similar parser library for Java:
https://github.com/jparsec/jparsecjparsec already offers a calculator parsing tutorial:
https://github.com/jparsec/jparsec/wiki/TutorialThe tutorial was easy to port to my formula representation and to expand with some custom functions (sin, abs, t).
The final parser fits into 57 lines of Java code and I am really happy with the result.
If you have to write a parser next time and its to complex to use regex, but not quite complex enough to justify using a grammar based generator like ANTLR, I recommend checking out a parser combinator library.