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($app$, window, document)
{ "use strict";
////////////////////////////////////////////////////////////////////////////////
// Class "Controller"
////////////////////////////////////////////////////////////////////////////////

var START_STOP = "startStop",
    PAUSE      = "pause";

/** 
 *  @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)
{ this.v_plane   = p_plane;
  this.v_snake   = p_snake;
  this.v_delta_t = 1000/p_init.speed;
  this.v_paused  = false;
  
  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)
         { p_snake.turn(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)
  );
}

Controller.prototype =
{ /**
   *  Starts the game. 
   *  
   *  @method
   *  @name $app$.game.snake.Controller#startGame
   */  
  startGame:
    function()
    { var l_plane = this.v_plane,
          l_snake = this.v_snake;
     
      function o_movement()
      { if (l_snake.move())
        { this.stopGame(); }
        else if (!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;
      }
    },
  
  /**
   *  Pauses the game. 
   *  
   *  @method
   *  @name $app$.game.snake.Controller#pauseGame
   */  
  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;
      }
    },  
 
  /**
   *  Stops the game. 
   *  
   *  @method
   *  @name $app$.game.snake.Controller#stopGame
   */  
  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;
      }
    }
};

$app$.game.snake.Controller = Controller;

////////////////////////////////////////////////////////////////////////////////
// End of Class "Controller"
////////////////////////////////////////////////////////////////////////////////
}(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:28:44 GMT+0100 (MEZ) using the DocStrap template.