Alternative Languages with PlayN
One of the cool things about PlayN is that depending on the target, you can write code in other JVM languages and seemlessly use the code in your applications. In previous posts, I've experimented with using Groovy with the Java target but in this post I will code instead with Mirah. Mirah is a language for the JVM with a Ruby inpsired syntax. One of the major advantages of Mirah is that it can can be used without needing any runtime dependencies. It can also generate Java source code. In order to build Mirah code in a PlayN project, we'll need to add the appropriate plugin to the pom.xml (core project).
<plugin>
<groupId>org.mirah.maven</groupId>
<artifactId>maven-mirah-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals><goal>compile</goal></goals>
</execution>
</executions>
</plugin>
We also need to create a directory to hold the source. Following Maven conventions, the default location for the files is src/main/mirah. Below is the Mirah source code for the Hello World that PlayN generates.
package com.html5gamebook.core
import "playn.core.PlayN"
import "playn.core.Game"
import "playn.core.Image"
import "playn.core.ImageLayer"
class MirahExample
implements Game
interface java.lang.Override
def init:void
# create and add background image layer
bgImage = PlayN.assets().getImage("images/bg.png")
bgLayer = PlayN.graphics().createImageLayer(bgImage)
PlayN.graphics().rootLayer().add(bgLayer)
end
interface java.lang.Override
def paint(alpha:float):void
# the background automatically paints itself, so no need to do anything here!
end
interface java.lang.Override
def update(delta:float):void
end
interface java.lang.Override
def updateRate:int
return 25
end
end
The above code works in the Java target and generates a class file that when decompiled, looks wonky but is valid Java code. To get the same code working in the HTML5 or Android targets, we need to do a little more work. The main reason for this is that the GWT compiler in particular doesn't understand Mirah so you'll need to compile it to Java first and allow the GWT and Android targets to use the generated source.
As always check out the code here.