uberpong/dev/lib/game/entities/ball.js
2012-06-24 10:50:26 +02:00

62 lines
1.1 KiB
JavaScript

ig.module(
'game.entities.ball'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityBall = ig.Entity.extend({
name: 'ball',
size: {x:48, y:48},
collides: ig.Entity.COLLIDES.ACTIVE,
type: ig.Entity.TYPE.B,
animSheet: new ig.AnimationSheet( 'media/ball.png', 48, 48 ),
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;
}
});
});