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: 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},

    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;
    }
  });
});

