2011-02-12 14:55:38 +00:00
|
|
|
// Init Global vars
|
2011-02-13 05:19:58 +00:00
|
|
|
var FPS = 30;
|
2011-02-12 14:55:38 +00:00
|
|
|
var Stages = {} ;
|
|
|
|
var Level = 1 ;
|
|
|
|
var Layouts = {} ;
|
|
|
|
var timers = {} ;
|
|
|
|
var Game = null ;
|
|
|
|
|
|
|
|
// -- Init scene prototype
|
|
|
|
var app = function(opts) {
|
|
|
|
|
2011-02-13 05:19:58 +00:00
|
|
|
this.name = "Wyrian - A Tyrian Arcade Game Clone" ;
|
|
|
|
this.version = '0.2a' ;
|
2011-02-12 14:55:38 +00:00
|
|
|
|
|
|
|
// Init uniqId
|
|
|
|
this.uniqId = 0 ;
|
|
|
|
|
|
|
|
// Default settings
|
|
|
|
var settings = {
|
|
|
|
libs: {}
|
|
|
|
} ;
|
|
|
|
$.extend(true, settings, opts);
|
|
|
|
this.settings = settings ;
|
|
|
|
|
2011-02-12 17:54:57 +00:00
|
|
|
// Set Scene size
|
|
|
|
this.width = $(document).width() ;
|
|
|
|
this.height = $(document).height() ;
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
// Listen for inputs
|
|
|
|
this.input = new this.Input() ;
|
2011-02-13 12:52:18 +00:00
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
// Return object
|
|
|
|
return this;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
// -- Bind init call
|
|
|
|
app.prototype.init = function() {
|
|
|
|
|
|
|
|
var self = this ;
|
|
|
|
|
|
|
|
require({
|
2011-02-13 07:16:43 +00:00
|
|
|
baseUrl: "js/",
|
2011-02-13 12:52:18 +00:00
|
|
|
urlArgs: "bust=" + GameVersion
|
2011-02-12 14:55:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// -- Layout to load
|
|
|
|
self.settings.layers,
|
|
|
|
|
|
|
|
// -- All objects are loaded => can run
|
|
|
|
function() {
|
|
|
|
$(document).trigger('gameLoaded') ;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( typeof this.settings.init == 'function') {
|
|
|
|
this.settings.init() ;
|
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
// -- A log function
|
|
|
|
app.prototype.log = function(txt) {
|
|
|
|
try {
|
|
|
|
console.log(txt);
|
|
|
|
} catch(e) {} ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Call all canvas updates if declared as 'running'
|
2011-02-13 01:13:16 +00:00
|
|
|
app.prototype.loopAnimation = function() {
|
2011-02-12 22:03:47 +00:00
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
// -- Search for elements that have to be updated
|
2011-02-13 12:28:40 +00:00
|
|
|
Game.activeElements = 0 ;
|
2011-02-12 14:55:38 +00:00
|
|
|
for ( var i in Layouts ) {
|
|
|
|
var _layout = Layouts[i] ;
|
|
|
|
if ( _layout && _layout.running ) {
|
|
|
|
_layout.update() ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 07:16:43 +00:00
|
|
|
// -- Get level
|
2011-02-13 12:28:40 +00:00
|
|
|
Level = Math.floor((Game.score||0)/1000) ;
|
2011-02-13 07:16:43 +00:00
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
// -- Create ennemies if needed
|
2011-02-12 14:55:38 +00:00
|
|
|
var numEnnemies = 0 ;
|
2011-02-13 12:52:18 +00:00
|
|
|
if ( Layouts.Ennemies ) {
|
|
|
|
for ( var i in Layouts.Ennemies.els ) {
|
|
|
|
var _el = Layouts.Ennemies.els[i] ;
|
|
|
|
if ( ! _el.deleteAfter ) numEnnemies++ ;
|
|
|
|
}
|
|
|
|
for ( var i = numEnnemies-1 ; i < Level + 3 ; i++ ) {
|
|
|
|
Layouts.Ennemies.createRandom() ;
|
|
|
|
}
|
2011-02-12 14:55:38 +00:00
|
|
|
}
|
2011-02-12 22:03:47 +00:00
|
|
|
|
2011-02-13 05:19:58 +00:00
|
|
|
// -- Init loops counter
|
2011-02-13 12:28:40 +00:00
|
|
|
Game.loops = Game.loops||0 ;
|
2011-02-13 05:19:58 +00:00
|
|
|
|
|
|
|
// -- Init vars
|
2011-02-13 12:28:40 +00:00
|
|
|
if ( Game.loops == 0 ) {
|
2011-02-13 05:19:58 +00:00
|
|
|
this.frameCount = 0;
|
|
|
|
this.fps = 0;
|
|
|
|
this.maxfps = 1 / (FPS / 1000);
|
|
|
|
this.lastTime = new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Calculate time deltas and find fps value
|
|
|
|
var nowTime = new Date();
|
|
|
|
var diffTime = Math.ceil((nowTime.getTime() - this.lastTime.getTime()));
|
|
|
|
if (diffTime >= 1000) {
|
|
|
|
this.fps = this.frameCount;
|
|
|
|
this.frameCount = 0;
|
|
|
|
this.lastTime = nowTime;
|
|
|
|
}
|
|
|
|
this.frameCount++;
|
|
|
|
|
|
|
|
// -- Increase loop counter
|
2011-02-13 12:28:40 +00:00
|
|
|
Game.loops++ ;
|
2011-02-13 05:19:58 +00:00
|
|
|
|
|
|
|
// -- Refresh Scores and HUD informations
|
2011-02-13 12:28:40 +00:00
|
|
|
$('#score span').html(Game.score) ;
|
|
|
|
$('#activeElements span').html(Game.activeElements) ;
|
2011-02-13 05:19:58 +00:00
|
|
|
$('#fpsCounter span').html(this.fps) ;
|
2011-02-12 14:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Bind Inputs
|
|
|
|
app.prototype.Input = function() {
|
|
|
|
|
|
|
|
this.keyboard = {};
|
|
|
|
var that = this;
|
|
|
|
this.mousedown = false;
|
|
|
|
this.keydown = true;
|
|
|
|
|
|
|
|
// this is handling WASD, and arrows keys
|
|
|
|
function update_keyboard(e, val) {
|
|
|
|
if(e.keyCode==40 || e.keyCode==83)
|
|
|
|
that.keyboard['down'] = val;
|
|
|
|
if(e.keyCode==38 || e.keyCode==87)
|
|
|
|
that.keyboard['up'] = val;
|
|
|
|
if(e.keyCode==39 || e.keyCode==68)
|
|
|
|
that.keyboard['right'] = val;
|
|
|
|
if(e.keyCode==37 || e.keyCode==65)
|
|
|
|
that.keyboard['left'] = val;
|
|
|
|
if(e.keyCode==32) {
|
|
|
|
that.keyboard['space'] = val;
|
|
|
|
e.preventDefault() ;
|
|
|
|
e.stopPropagation() ;
|
|
|
|
}
|
|
|
|
if(e.keyCode==17)
|
|
|
|
that.keyboard['ctrl'] = val;
|
|
|
|
if(e.keyCode==13)
|
|
|
|
that.keyboard['enter'] = val;
|
|
|
|
};
|
|
|
|
|
|
|
|
document.ontouchstart = function(event) {
|
|
|
|
that.mousedown = true;
|
|
|
|
}
|
|
|
|
document.ontouchend = function(event) {
|
|
|
|
that.mousedown = false;
|
|
|
|
}
|
|
|
|
document.ontouchmove = function(event) {}
|
|
|
|
|
|
|
|
document.onmousedown = function(event) {
|
|
|
|
that.mousedown = true;
|
|
|
|
}
|
|
|
|
document.onmouseup = function(event) {
|
|
|
|
that.mousedown = false;
|
|
|
|
}
|
|
|
|
document.onclick = function(event) {
|
|
|
|
//that.click(event);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
document.onmousemove = function(event) {
|
|
|
|
that.xmouse = event.clientX;
|
|
|
|
that.ymouse = event.clientY;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
document.onkeydown = function(e) {
|
|
|
|
that.keydown = true;
|
|
|
|
update_keyboard(e, true);
|
|
|
|
};
|
|
|
|
document.onkeyup = function(e) {
|
|
|
|
that.keydown = false;
|
|
|
|
update_keyboard(e, false);
|
|
|
|
};
|
|
|
|
// can be used to avoid key jamming
|
|
|
|
document.onkeypress = function(e) {
|
|
|
|
if ( that.keyboard.space ) {
|
|
|
|
e.preventDefault() ;
|
|
|
|
e.stopPropagation() ;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// make sure that the keyboard is rested when
|
|
|
|
// the user leave the page
|
|
|
|
window.onblur = function (e) {
|
|
|
|
that.keyboard = {}
|
|
|
|
that.keydown = false;
|
|
|
|
that.mousedown = false;
|
|
|
|
}
|
|
|
|
}
|