F# XNA Game Ported to Windows Phone

The previous post talks about an F# Windows Phone game. Here is the same game ported to the Windows Phone.

The port includes many fixes to the original game plus some other improvements. Most of the original code ported very easily and was up and running in about an hour on the Windows Phone Emulator.

However many parts of the code were optimized so the game runs well on the small device. Particle effects are toned down considerably and collision detection is made much more efficient.

A new camera component was created for the mobile form factor. The cross-hair/camera angle is controlled by dragging the thumb in the red sqaure on the left (i will add a joy-stick graphic later). The firing button is on the right side.

(Note: The non-phone version of the code has a bug in the camera code which has been fixed but I have not posted to codeplex yet).

The game runs surprisingly well both on the emulator and on the phone. Interestingly, the phone version of the game has much more logic than the non-phone version as there is a predator-prey system in the works (the red planes start to chase blue plane when they are near each other).

A very interesting part is that the state machines for both the predator and the prey are written as F# asynchronous computations. Here is the state machine for the predator, written as recursively called functions:

let stateMachine = MailboxProcessor.Start(fun inbox ->

let rec patrolling() =
 async {
  let! msg = inbox.Receive()
  match msg with
  | Prepare prey ->
    prey.Post (AboutToPrey inbox)
    return! prepareToChase()
  | _ -> return! patrolling()

and prepareToChase() =
 async {
  let! msg = inbox.Receive()
  match msg with
  | OkToPrey mdl ->
    prey <- Some mdl
    return! chasing()
  | Abort -> return! patrolling()
  | _ -> return! prepareToChase()

and chasing() =
 async {
  let! msg = inbox.Receive()
  match msg with
  | Abort -> 
    match prey with Some p -> (p.Handle:MailboxProcessor).Post(AbortingChase) | _ -> ()
    prey <- None
    return! patrolling() 
  | _ -> return! chasing()


As can be seen, the states are patrolling, prepareToChase and chasing. The transitions are done by posting messages to the mailbox processor. So far, I have not seen any adverse effects from using mailbox processors on the phone, in the game.


1 thought on “F# XNA Game Ported to Windows Phone

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s