uberpong/dev/lib/game/entities/ball.js
2013-04-11 21:51:11 +02:00

68 lines
1.7 KiB
JavaScript

ig.module(
'game.entities.ball'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityBall = ig.Entity.extend({
hitSounds: [
new ig.Sound('media/sounds/hit1.ogg'),
new ig.Sound('media/sounds/hit2.ogg'),
new ig.Sound('media/sounds/hit3.ogg'),
new ig.Sound('media/sounds/hit4.ogg'),
],
wallSound: new ig.Sound('media/sounds/wall.ogg'),
name: 'ball',
size: {x: 26, y: 26},
offset: {x: 7, y: 7},
_wmDrawBox: false,
collides: ig.Entity.COLLIDES.ACTIVE,
type: ig.Entity.TYPE.B,
animSheet: new ig.AnimationSheet( 'media/ball.png', 40, 40 ),
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;
this.hitSounds[Math.floor(Math.random()*4)].play();
}
},
handleMovementTrace: function(res) {
if (res.collision.y) this.wallSound.play();
this.parent(res);
},
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;
}
});
});