uberpong/dev/lib/game/entities/ball.js
Aaron Mueller b4c3d98984 Refactorthe score and make it global ...
Still full of bugs and crap ...
2013-04-18 21:55:26 +02:00

72 lines
1.9 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) {
if (other.name == 'paddle-enemy' || other.name == 'paddle-player') {
// 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;
// TODO: Moooar points on speed balls
// TODO: Moar points on edge hits
if (other.name == 'paddle-player') ig.global.score += 20;
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;
}
});
});