Options
All
  • Public
  • Public/Protected
  • All
Menu

interface game Game

All the game logic need to take place in these methods

Here is an example of it using a class pattern:

function Game() {
  var self = this;
  var _score = 0;
  var _highestRandomNumber = 0;

  self.color = "#FF00FF";
  //called once the seed is ready and the rng (random number generator is initialised)
  //can be used to setup the game, consider as frame zero
  self.start = function(rng){
    console.log("Game: start");
  }


  //optional
  //called just before update, its main purpose it to allow to implement input yourself by recording it
  //the recordInput callback passed in allow you to set the input yourself
  // this way : recordInput("<custom name>",<value>); 
  //note that the input name must have been registered before hand in the init function
  self.preUpdate = function(recordInput){
    recordInput("jumping",true); //this set the jumping input to always be true
  }


  //called just after start (after delta milliseconds have passed) and every so often after that
  //rng is the random generator
  //input allow you to access player's input (it automatically record the input for score submission)
  //elapsed is current duration of the game (from the call to "start") it is basically the accumulation of each delta so far
  //delta is a fixed delta (60fps by default)
  self.update = function(rng,input,elapsed,delta){
    if(input.jumping){
      self.color = "#FF0000";
    }else{
      self.color= "#FF00FF";
    }
    var rand = Math.floor(rng.next() * 1000000);
    _highestRandomNumber = Math.max(_highestRandomNumber,rand);

    _score += input.touch.currentTouches.length;

    if(elapsed > 10000){
      console.log("Game : last update, quiting after " + (elapsed/1000)  +" s");
      return {score:_score,data:{highestRandomNumber:_highestRandomNumber}};  
    }
  }

  //optional
  // self.finish = function(elapsed){
  //  // can cleanup if necessary
  // }
}

Summary

Properties

finish

finish: function

This method will be executed when the game is over after the last update

param

elapsed is an interger representing the number of milliseconds since the start of the game

Type declaration

    • (elapsed: number): void
    • Parameters

      • elapsed: number

      Returns void

Optional preUpdate

preUpdate: function

This is called just before every update and allow you to register your own input if required

param

recordInput is a function that can be called to set manually the value of registered input (InputConfiguration)

Type declaration

    • (recordInput: function): void
    • Parameters

      • recordInput: function
          • (name: string, value: any): void
          • Parameters

            • name: string
            • value: any

            Returns void

      Returns void

start

start: function

Called at the start of the game

param

rng is the seeded random generator that can be used in start to setup some random setup before the first update

Type declaration

    • (rng: RNG): void
    • Parameters

      Returns void

update

update: function

The update function that drive the game logic

In order to terminate the game, that function need to return a GameResult object

param

rng is the seeded random generator whose next number is given by calling rng.next()

param

input is an object whose keys are the configured input in InputConfiguration

param

elapsed is an interger representing the number of milliseconds since the start of the game

param

delta is an interger representing the number of milliseconds since the last frame of the game (fixed to 16ms)

returns

_ update need to return an GameResult object to declare game over else do not return or return undefined/null

Type declaration