uberpong/dev/lib/game/entities/ball.js

62 lines
1.1 KiB
JavaScript
Raw Normal View History

ig.module(
'game.entities.ball'
)
.requires(
'impact.entity'
)
.defines(function(){
2012-06-24 10:50:26 +02:00
EntityBall = ig.Entity.extend({
2012-06-24 10:50:26 +02:00
name: 'ball',
size: {x: 24, y: 24},
2012-06-24 10:50:26 +02:00
collides: ig.Entity.COLLIDES.ACTIVE,
type: ig.Entity.TYPE.B,
animSheet: new ig.AnimationSheet( 'media/ball.png', 24, 24 ),
2012-06-24 10:50:26 +02:00
bounciness: 1,
maxVel: {x: 1000, y: 1000},
2012-06-24 10:50:26 +02:00
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.addAnim( 'idle', 1, [0] );
2012-06-24 10:50:26 +02:00
this.randomVel();
},
2012-06-24 10:50:26 +02:00
ready: function() {
this.startPos = {x: this.pos.x, y: this.pos.y};
},
2012-06-24 10:50:26 +02:00
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;
2012-06-24 10:50:26 +02:00
// 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;
}
},
2012-06-24 10:50:26 +02:00
reset: function() {
this.randomVel();
2012-06-24 10:50:26 +02:00
this.pos.x = this.startPos.x;
this.pos.y = this.startPos.y;
},
2012-06-24 10:50:26 +02:00
randomVel: function() {
this.vel.x = (Math.random()*(100)) + 100;
if(Math.random() > .5) this.vel.x *= -1;
this.vel.y = (Math.random()*20) + 50;
}
2012-06-24 10:50:26 +02:00
});
2012-06-24 10:50:26 +02:00
});