Source: main.js

/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/**
 *  @module $wk$
 *  @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$, window, document)
{ "use strict";

////////////////////////////////////////////////////////////////////////////////
// Class "Main"
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
// Constructor 
//------------------------------------------------------------------------------

// The class "Main". 
function Main()
{ var Ball          = $wk$.Ball,      // Import
      Collision     = $wk$.Collision, // Import
      i,
      self          = this,
      l_init        = INIT,
      l_init_canvas = l_init.canvas,
      l_init_ball   = l_init.ball,
      l_init_game   = l_init.game,
      l_balls       = [];
      
  // Save the init object so that methods can access them.
  this.initCanvas = l_init_canvas;
  this.initGame   = l_init_game;

  // Initialize the canvas.
  this.v_canvas        = document.getElementById("d_canvas");
  this.v_canvas.width  = l_init_canvas.width;
  this.v_canvas.height = l_init_canvas.height;
  this.v_context       = this.v_canvas.getContext("2d");

  // Initialize the balls objects.
  this.v_balls = l_balls;     

  for (i = 0; i < l_init_game.numberOfBalls; i++)
  { l_balls[i] = new Ball(this.v_context, l_init_ball); }

  // Initialize the collision object.
  this.v_collision = new Collision({left:   0, 
                                    right:  l_init_canvas.width, 
                                    top:    0,
                                    bottom: l_init_canvas.height
                                   },
                                   l_balls
                                  );

  // Detect mouse clicks on balls.
  this.v_canvas.onmousedown 
    = function(p_event) { self.o_mouse_mousedown(p_event); };

  // Start the timer for redrawing the canvas every 1000/FPS milliseconds.
  this.v_timer 
    = window.setInterval(function(){ self.o_redraw(); }, 1000/l_init_game.fps);
}

//------------------------------------------------------------------------------
// End of Constructor
//------------------------------------------------------------------------------

Main.prototype =
{ //----------------------------------------------------------------------------
  // Methods (View)
  //----------------------------------------------------------------------------

  // Clears the canvas and draws all sprites.
  m_draw:
    function()
    { var i             = null,
          l_init_canvas = this.initCanvas,
          l_balls       = this.v_balls;
      this.v_context.clearRect(0, 0, l_init_canvas.width, l_init_canvas.height);
      for (i in l_balls)
      { l_balls[i].draw(); }
    },

  // An event observer: 
  // It is called every 1000/FPS milliseconds, but only when the game has been started.
  o_redraw:
    function() 
    { var i           = null,
          l_balls     = this.v_balls;
    
      // collision handling
      this.v_collision.handleCollision();

      // Move the ball.
      l_balls = this.v_balls;
      for (i in l_balls)
      { l_balls[i].move(1000/this.initGame.fps); }

      // Redraw the canvas.
      this.m_draw();
    },
  
  //----------------------------------------------------------------------------
  // Methods (Controller)
  //----------------------------------------------------------------------------

  // Whenever a ball has been mousedowned: Do something.
  o_mouse_mousedown:
    function(p_event)
    { var l_ball_index,
          l_mouse_pos = this.v_canvas.mousePos(p_event);

      if (l_mouse_pos)
      { l_ball_index = this.v_collision.handleMouseClick(l_mouse_pos);
        if (l_ball_index)
        { // Stop the ball moving around.
          /*
          var l_ball = this.v_balls[l_ball_index];
         
          l_ball.vx = 0;
          l_ball.vy = 0;
          */
          // Delete the ball.
          this.v_balls.splice(l_ball_index, 1);     
        }
      }
    },

  //----------------------------------------------------------------------------
  // End of Methods
  //----------------------------------------------------------------------------
};

//Create the main object, after the HTML page has been loaded.
window.onload = function(){ new Main(); };

////////////////////////////////////////////////////////////////////////////////
// End of Class "Main"
////////////////////////////////////////////////////////////////////////////////

}(this.$wk$, 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 Tue Nov 12 2013 11:29:08 GMT+0100 (MEZ) using the DocStrap template.