Source: Controller.js

/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/**
 *  @author    Wolfgang Kowarschick
 *  @copyright 2013, Wolfgang Kowarschick
 *  <br/>
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted under the terms of the 
 *  Creative Commons License Attribution-NonCommercial-ShareAlike 3.0 Unported    
 *  (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/). 
 */

(function($wk$, $app$, window, document)
{ "use strict";

  // import
  var WKcSignaler = $wk$.observer.WKcSignaler,
      Snake       = $app$.game.snake.Snake,
      
      START_STOP  = "startStop",
      PAUSE       = "pause";

////////////////////////////////////////////////////////////////////////////////
// Class "Controller"
////////////////////////////////////////////////////////////////////////////////

/** 
 *  @class
 *  @name $app$.game.snake.Controller
 *  
 *  @param {Object} p_init 
 *         Should contain the attribute
 *         <code>keys</code>  (the keys to control the move direction of
 *                             the snake) and
 *         <code>speed</code> (the speed of the snake).
 *  @param {$app$.game.snake.Plane} p_plane 
 *         The plane where the snake has been placed. 
 *  @param {$app$.game.snake.Snake} p_snake 
 *         The snake itself. 
 */

function Controller(p_init, p_plane, p_snake)
{ WKcSignaler.call(this);  // Call the constructor of the super class.

  this.v_plane     = p_plane;
  this.v_snake     = p_snake;
  this.v_delta_t   = 1000/p_init.speed;
  this.v_paused    = false,
  this.v_key_queue = [];
  
  document.addEventListener
  ( "keypress",
    (function(p_event)
     { var l_key    = p_init.keys[p_event.keyCode],
           l_action = p_init.menu[String.fromCharCode(p_event.charCode)];
       if (this.v_timer) // If the game is running ....
       { if (l_key)
         { // Handle at most one key press event per snake move.
           this.v_key_queue.push(l_key); 
         } 
         else
         { if (l_action === START_STOP)
           { this.stopGame(); }
           else if (l_action === PAUSE)
           { this.pauseGame(); }
         }
       }
       else
       { if (l_action === START_STOP || l_action === PAUSE)
         { this.startGame(); }   
       }
     }
    ).bind(this),
    true
  );
  
  p_snake.addObserver(Snake.KILLED, this.stopGame.bind(this));
}

// Inherit all attributes and methods from "WKcSignaler".
Controller.prototype = Object.create(WKcSignaler.prototype);

/**
 *  Starts the game. 
 *  
 *  @method
 *  @name $app$.game.snake.Controller#startGame
 */  
Controller.prototype.startGame =
function()
{ var l_plane = this.v_plane,
      l_snake = this.v_snake;
     
  function o_movement()
  { var l_key = this.v_key_queue.shift();
    if (l_key)
    { this.v_snake.turn(l_key) };
    l_snake.move();
    if (this.v_timer && !this.v_timer_food && l_plane.foodNr === 0)
    { this.v_plane.addFood(); }
  }
    
  // If game is not running, start it.
  if (!this.v_timer)
  { if (!this.v_paused)
    { this.v_snake.reset(); }
    this.v_timer  = window.setInterval(o_movement.bind(this), this.v_delta_t);
    this.v_paused = false;
    
    //  Inform all observers about the change of the state.
    this.signal(Controller.STARTED);
  }
};
  
/**
 *  Pauses the game. 
 *  
 *  @method
 *  @name $app$.game.snake.Controller#pauseGame
 */  
Controller.prototype.pauseGame =
function()
{ // if the game is running, pause it.
  if (this.v_timer)
  { this.v_paused = true;
    window.clearInterval(this.v_timer);
    delete this.v_timer;

    //  Inform all observers about the change of the state.
    this.signal(Controller.PAUSED);
  }
};
 
  /**
   *  Stops the game. 
   *  
   *  @method
   *  @name $app$.game.snake.Controller#stopGame
   */  
Controller.prototype.stopGame =
function()
{ // if the game is running, stop it.
  if (this.v_timer || this.v_paused)
  { this.v_paused = false;
    window.clearInterval(this.v_timer);
    delete this.v_timer;

    //  Inform all observers about the change of the state.
    this.signal(Controller.STOPPED);
  }
};

// The states of the controller.
Controller.STARTED = "started";
Controller.PAUSED  = "paused";
Controller.STOPPED = "stopped";
  
$app$.game.snake.Controller = Controller;

////////////////////////////////////////////////////////////////////////////////
// End of Class "Controller"
////////////////////////////////////////////////////////////////////////////////
}(this.$wk$, this.$app$, this.window, this.document));
W. Kowarschick © 2013 (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/)
Documentation generated by JSDoc 3.3.0-dev on Sun Dec 15 2013 11:49:53 GMT+0100 (MEZ) using the DocStrap template.