/*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 Paddle = $wk$.Paddle, // Import
Collision = $wk$.Collision, // Import
i, n,
self = this,
l_init = INIT,
l_init_canvas = l_init.canvas,
l_init_paddle = l_init.paddle,
l_init_game = l_init.game,
l_paddles = [],
l_context;
// 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 = l_context = this.v_canvas.getContext("2d");
// Initialize the paddle objects.
this.v_paddles = l_paddles;
for (i = 0, n = l_init_game.paddlesNr; i < n ; i++)
{ l_paddles[i] = new Paddle(l_context, l_init_paddle, i); }
// Initialize the collision object.
this.v_collision = new Collision({left: 0,
right: l_init_canvas.width,
top: 0,
bottom: l_init_canvas.height
},
l_paddles
);
// React to key down and key up events.
document.onkeydown = this.o_start_paddle_moving.bind(this);
document.onkeyup = this.o_stop_paddle_moving.bind(this);
// 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_paddles = this.v_paddles;
this.v_context.clearRect(0, 0, l_init_canvas.width, l_init_canvas.height);
for (i in l_paddles)
{ l_paddles[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_fps = this.initGame.fps,
l_paddles = this.v_paddles;
// collision handling
this.v_collision.handleCollision();
// Move the paddles.
for (i in l_paddles)
{ l_paddles[i].move(1000/l_fps); }
// Redraw the canvas.
this.m_draw();
},
// An event observer:
// It is called whenever a "start paddle moving event" is signaled.
o_start_paddle_moving:
function (p_event)
{ var l_keys = this.initGame.keys[p_event.keyCode];
if (l_keys)
{ this.v_paddles[l_keys[0]].start(l_keys[1]); }
},
// An event observer:
// It is called whenever a "stop paddle moving event" is signaled.
o_stop_paddle_moving:
function (p_event)
{ var l_keys = this.initGame.keys[p_event.keyCode];
if (l_keys)
{ this.v_paddles[l_keys[0]].stop(); }
}
//----------------------------------------------------------------------------
// 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));