uberpong/dev/lib/game/main.js

236 lines
6.3 KiB
JavaScript
Raw Permalink Normal View History

2013-05-02 22:41:31 +02:00
ig.module(
'game.main'
)
.requires(
//'impact.debug.menu',
'impact.game',
'impact.font',
'game.entities.ball',
'game.entities.paddle-enemy',
'game.entities.paddle-player',
2013-05-02 22:41:31 +02:00
'game.entities.namefield',
'game.levels.level1'
)
2013-05-02 22:41:31 +02:00
.defines(function() {
RunningGame = ig.Game.extend({
2013-05-02 22:41:31 +02:00
font: new ig.Font('media/lcddot.font.png'),
2013-05-16 21:53:35 +02:00
2013-04-18 21:36:49 +02:00
pauseDialogAlpha: 0,
2013-05-02 22:41:31 +02:00
pauseDialog: new ig.AnimationSheet('media/pause_screen.png', 624, 384),
2013-04-18 21:36:49 +02:00
pauseDialogAnim: null,
2013-04-11 21:12:27 +02:00
showPause: false,
2013-05-16 21:53:35 +02:00
2013-04-11 21:34:22 +02:00
clearColor: null,
init: function() {
2013-04-03 21:51:11 +02:00
ig.input.unbindAll();
ig.global.lifes = 4; // increase this to 3
ig.global.score = 0;
2013-04-11 21:12:27 +02:00
// Menu navigation
2013-04-03 21:51:11 +02:00
ig.input.bind(ig.KEY.ESC, 'escape');
ig.input.bind(ig.KEY.ENTER, 'enter');
2013-04-11 21:12:27 +02:00
// Game navigation
ig.input.bind(ig.KEY.UP_ARROW, 'up');
ig.input.bind(ig.KEY.DOWN_ARROW, 'down');
2012-07-06 17:09:07 +02:00
ig.input.bind(ig.KEY.T, 'force-top');
ig.input.bind(ig.KEY.B, 'force-bottom');
2013-04-03 21:51:11 +02:00
// vim goodness
ig.input.bind(ig.KEY.K, 'up');
ig.input.bind(ig.KEY.J, 'down');
2013-04-03 21:51:11 +02:00
// Gamer style
ig.input.bind(ig.KEY.W, 'up');
ig.input.bind(ig.KEY.S, 'down');
2013-04-18 21:36:49 +02:00
// init graphics
this.pauseDialogAnim = new ig.Animation( this.pauseDialog, 0, [0] )
// init music
2013-05-16 21:53:35 +02:00
ig.music.add('media/sounds/DST-AngryRobotIII.ogg');
ig.music.add('media/sounds/DST-2ndBallad.ogg');
ig.music.random = true;
ig.music.loop = true;
ig.music.volume = 0.3;
ig.music.play();
// Load the level
2012-07-03 22:26:28 +02:00
this.loadLevel(LevelLevel1);
},
update: function() {
2013-04-03 21:51:11 +02:00
// User is in the pause menu
2013-04-11 21:12:27 +02:00
if (this.showPause) {
2013-04-11 21:49:31 +02:00
if (ig.input.pressed('escape')) {
2013-04-03 21:51:11 +02:00
ig.system.setGame(StartScreen);
2013-04-11 21:12:27 +02:00
return;
2013-04-03 21:51:11 +02:00
}
2013-04-11 21:49:31 +02:00
if (ig.input.pressed('enter')) {
2013-04-11 21:12:27 +02:00
this.showPause = false;
2013-04-18 21:36:49 +02:00
this.pauseDialogAlpha = 0;
2013-04-11 21:12:27 +02:00
return;
2013-04-03 21:51:11 +02:00
}
}
// The game is over
if (ig.global.lifes == 0) {
2013-04-11 21:49:31 +02:00
ig.system.setGame(ScoreboardScreen);
2012-07-03 22:26:28 +02:00
}
// The User want back to the main menu inside a running game
2013-04-03 21:51:11 +02:00
if (ig.input.pressed('escape')) {
2013-04-11 21:12:27 +02:00
this.showPause = true;
2012-07-03 22:26:28 +02:00
}
// The game is running
2013-04-11 21:49:31 +02:00
if (!this.showPause) this.parent();
},
draw: function() {
2013-04-11 21:34:22 +02:00
ig.system.context.clearRect( 0 ,0, ig.system.realWidth, ig.system.realHeight );
this.parent(); // Draw entries and background
this.font.draw(ig.global.score, 390, 8, ig.Font.ALIGN.RIGHT);
2013-04-03 21:51:11 +02:00
2013-04-11 21:12:27 +02:00
if (this.showPause) {
2013-04-18 21:36:49 +02:00
if(this.pauseDialogAlpha < 1) {
this.pauseDialogAlpha += .1;
if(this.pauseDialogAlpha > 1) this.pauseDialogAlpha = 1;
this.pauseDialogAnim.alpha = this.pauseDialogAlpha;
}
this.pauseDialogAnim.draw(0, 0);
2013-04-03 21:51:11 +02:00
}
}
});
2013-04-11 21:49:31 +02:00
ScoreboardScreen = ig.Game.extend({
scoreoid: {
api_key: 'd1011f8f6776a10f7a5d87eaa86c43c8d2ffb9dc',
game_id: '642fc6c1e8',
response: 'json'
},
2013-05-16 22:42:54 +02:00
2013-05-16 22:04:56 +02:00
font: new ig.Font( 'media/lcddot.font.png' ),
2013-05-16 22:27:15 +02:00
fontSmall: new ig.Font( 'media/lcddot.small.font.png' ),
2013-05-16 22:42:54 +02:00
fontSmallGrey: new ig.Font( 'media/lcddot.small.grey.font.png' ),
2013-05-16 22:27:15 +02:00
backgroundInput: new ig.Image('media/highscore_input_screen.png'),
2013-05-16 22:04:56 +02:00
background: new ig.Image('media/highscore_screen.png'),
2013-05-02 22:41:31 +02:00
nameField: null,
2013-05-16 22:04:56 +02:00
clearColor: null,
2013-04-11 21:49:31 +02:00
init: function() {
2013-05-16 22:04:05 +02:00
ig.input.bind(ig.KEY.ENTER, 'submit-score');
ig.input.bind(ig.KEY.ESC, 'main-menu');
this.nameField = ig.game.spawnEntity(EntityNameField, 363, 219);
this.loadScores();
2013-04-11 21:49:31 +02:00
},
2013-04-11 21:49:31 +02:00
update: function() {
if (ig.input.pressed('main-menu')) {
ig.system.setGame(StartScreen);
}
2013-05-16 22:04:05 +02:00
if (ig.input.pressed('submit-score') && this.nameField !== null) {
2013-05-16 22:04:05 +02:00
this.saveScore(this.nameField.name(), ig.global.score, function(context) {
setTimeout(function() { context.loadScores(); }, 2000);
});
this.nameField.kill();
this.nameField = null;
2013-05-16 22:04:05 +02:00
}
2013-04-11 21:49:31 +02:00
this.parent();
},
loadScores: function() {
ig.global.scores = undefined;
2013-05-16 22:56:23 +02:00
data = $.extend({}, {
order: 'desc',
order_by: 'score',
limit: 10
}, this.scoreoid);
$.post('https://www.scoreoid.com/api/getBestScores', data, function(data) {
ig.global.scores = data;
});
},
2013-05-16 22:04:05 +02:00
saveScore: function(username, score, cb) {
2013-05-16 22:56:23 +02:00
data = $.extend({}, {
score: score,
username: username
}, this.scoreoid);
$.post('https://www.scoreoid.com/api/createScore', data, cb(this));
},
2013-04-11 21:49:31 +02:00
draw: function() {
2013-05-16 22:04:56 +02:00
ig.system.context.clearRect( 0 ,0, ig.system.realWidth, ig.system.realHeight );
2013-04-11 21:49:31 +02:00
this.parent();
2013-05-16 22:27:15 +02:00
if (this.nameField !== null) this.backgroundInput.draw(0, 0);
else this.background.draw(0, 0);
// Draw the current highscore if its loaded
2013-05-16 22:42:54 +02:00
var num = 1;
if (ig.global.scores !== undefined) {
var ypos = 50;
2013-05-16 22:42:54 +02:00
var font = this.fontSmall;
for (var i=0; i<ig.global.scores.length; i++) {
var score = ig.global.scores[i];
2013-05-16 22:42:54 +02:00
if(num <= 3) font = this.fontSmall;
else font = this.fontSmallGrey;
font.draw(num, 60, ypos, ig.Font.ALIGN.CENTER);
font.draw(score.Score.score, 210, ypos, ig.Font.ALIGN.RIGHT);
font.draw(score.Player.username, 250, ypos, ig.Font.ALIGN.LEFT);
2013-05-16 22:27:15 +02:00
ypos += 30;
2013-05-16 22:42:54 +02:00
num++;
}
}
2013-04-25 22:20:07 +02:00
if (this.nameField !== null) this.nameField.draw();
2013-05-16 22:14:30 +02:00
this.font.draw(ig.global.score, 524, 112, ig.Font.ALIGN.RIGHT);
2013-04-11 21:49:31 +02:00
}
});
StartScreen = ig.Game.extend({
background: new ig.Image('media/main_screen.png'),
clearColor: null,
init: function() {
2013-04-03 21:51:11 +02:00
ig.input.bind(ig.KEY.ENTER, 'start-game');
ig.input.bind(ig.KEY.H, 'highscore'); // XXX: Just for debug purpose
},
update: function() {
2013-04-11 21:12:27 +02:00
if (ig.input.pressed('start-game')) {
ig.system.setGame(RunningGame);
}
// XXX: Just for debug purpose
if (ig.input.pressed('highscore')) {
2013-05-16 22:42:54 +02:00
ig.global.score = Math.floor(Math.random()*1000)*1;
ig.system.setGame(ScoreboardScreen);
}
this.parent();
},
draw: function() {
ig.system.context.clearRect( 0 ,0, ig.system.realWidth, ig.system.realHeight );
this.parent();
this.background.draw(0, 0);
}
});
// Start the Game with 60fps, a resolution of 640x400 (16:10), scaled
// up by a factor of 2
ig.main( '#canvas', StartScreen, 60, 624, 384, 1 );
});