Clean up the main.js code and add a title screen.

This commit is contained in:
Aaron Mueller 2012-06-24 14:30:33 +02:00
parent e597f19d0e
commit a4d01fefdd
4 changed files with 84 additions and 77 deletions

View file

@ -5,57 +5,48 @@ ig.module(
'impact.entity'
)
.defines(function(){
EntityBall = ig.Entity.extend({
name: 'ball',
size: {x: 24, y: 24},
collides: ig.Entity.COLLIDES.ACTIVE,
type: ig.Entity.TYPE.B,
animSheet: new ig.AnimationSheet( 'media/ball.png', 24, 24 ),
bounciness: 1,
maxVel: {x: 1000, y: 1000},
EntityBall = ig.Entity.extend({
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.addAnim( 'idle', 1, [0] );
this.randomVel();
},
name: 'ball',
ready: function() {
this.startPos = {x: this.pos.x, y: this.pos.y};
},
size: {x: 24, y: 24},
collides: ig.Entity.COLLIDES.ACTIVE,
type: ig.Entity.TYPE.B,
collideWith: function( other, axis ) {
if (other.name == 'paddle') {
// the horizontal speed of the ball multiplied
// every time it hits a paddle
this.vel.x *= 1.15;
animSheet: new ig.AnimationSheet( 'media/ball.png', 24, 24 ),
// the paddles y movement is added to the ball,
// so that players can give the ball a spin
this.vel.y += other.vel.y/2;
}
},
bounciness: 1,
maxVel: {x: 1000, y: 1000},
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.addAnim( 'idle', 1, [0] );
this.randomVel();
},
ready: function() {
this.startPos = {x: this.pos.x, y: this.pos.y};
},
collideWith: function( other, axis ) {
if(other.name == 'paddle') {
// the horizontal speed of the ball multiplied
// every time it hits a paddle
this.vel.x *= 1.15;
// the paddles y movement is added to the ball,
// so that players can give the ball a spin
this.vel.y += other.vel.y/2;
}
},
reset: function() {
this.randomVel();
this.pos.x = this.startPos.x;
this.pos.y = this.startPos.y;
},
randomVel: function() {
this.vel.x = (Math.random()*(100)) + 100;
if(Math.random() > .5) this.vel.x *= -1;
this.vel.y = (Math.random()*20) + 50;
}
reset: function() {
this.randomVel();
this.pos.x = this.startPos.x;
this.pos.y = this.startPos.y;
},
randomVel: function() {
this.vel.x = Math.random()*100+100;
if (Math.random() > .5) this.vel.x *= -1;
this.vel.y = Math.random()*20+50;
}
});
});
});

View file

@ -22,7 +22,6 @@ EntityGoal = ig.Entity.extend({
other.reset();
}
}
});
});

View file

@ -1,5 +1,5 @@
ig.module(
'game.main'
ig.module(
'game.main'
)
.requires(
'impact.game',
@ -12,35 +12,52 @@ ig.module(
'game.levels.level1'
)
.defines(function(){
RunningGame = ig.Game.extend({
font: new ig.Font( 'media/04b03.font.png' ),
MyGame = ig.Game.extend({
// Load a font
font: new ig.Font( 'media/04b03.font.png' ),
init: function() {
ig.input.bind( ig.KEY.UP_ARROW, 'up' );
ig.input.bind( ig.KEY.DOWN_ARROW, 'down' );
init: function() {
ig.input.bind(ig.KEY.ESC, 'mainmenu');
ig.input.bind(ig.KEY.UP_ARROW, 'up');
ig.input.bind(ig.KEY.DOWN_ARROW, 'down');
// vim goodness
ig.input.bind(ig.KEY.K, 'up');
ig.input.bind(ig.KEY.J, 'down');
this.loadLevel( LevelLevel1 );
},
update: function() {
// Update all entities and backgroundMaps
this.parent();
// Add your own, additional update code here
},
draw: function() {
// Draw all entities and backgroundMaps
this.parent();
}
// Load the level
this.loadLevel( LevelLevel1 );
},
update: function() {
if (ig.input.pressed('mainmenu')) ig.system.setGame(StartScreen);
this.parent(); // Update entries and background
},
draw: function() {
this.parent(); // Draw entries and background
}
});
StartScreen = ig.Game.extend({
background: new ig.Image('media/main_screen.png'),
init: function() {
ig.input.bind(ig.KEY.ENTER, 'start');
ig.input.bind(ig.KEY.SPACE, 'start');
},
update: function() {
if (ig.input.pressed('start')) ig.system.setGame(RunningGame);
this.parent();
},
draw: function() {
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 );
});
// Start the Game with 60fps, a resolution of 640x400 (16:10), scaled
// up by a factor of 2
ig.main( '#canvas', MyGame, 60, 640, 400, 1 );
});

BIN
dev/media/main_screen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB