// Define la dimensión de la rejilla y el método para colocar entidades en ella.
Crafty.c('Rejilla', {
   init: function( ) {
      this.attr({
         w: Game.anchoCelda,
         h: Game.altoCelda
     })
   },

   en: function(x, y) {
      this.attr({ x: x * Game.anchoCelda, y: y * Game.altoCelda });
      return this;
   }
 });

//Componente que agrupa los comportamientos comunes,
Crafty.c('Actor', {
   init: function( ) {
      this.requires('2D, Canvas, Rejilla, Color');
    },
});

// Dotamos a los actores de color y personalidad
Crafty.c('MuroNegro', {
   init: function( ) {
      this.requires('Actor, Color, Pared');
      this.color('black');
  },
});

Crafty.c('MuroMarron', {
   init: function( ) {
      this.requires('Actor, Color, Pared');
      this.color('brown');
  },
});

// Esta componente será nuestro jugador, añadimos código para la colisión.
Crafty.c('Politico', {
  init: function() {
    this.requires('Actor, Fourway, Color, Collision')
      .fourway(4)
      .color('rgb(20, 75, 40)')
      .onHit('Pared', this.detenerMov)
      .onHit('Tesoro', this.recogerTesoro)
      
 }, 


  // detiene el movimiento
  detenerMov: function() {
   
    if (this._movement) {
      this.x -= this._movement.x;
      this.y -= this._movement.y;
    }

  },

  
  recogerTesoro: function(datos) {
   
	datos[0].obj.recogido();    //Disparamos el evento recogido del Tesoro

  }

});

// Para ganar el juego nuestro jugador debe recoger todos los tesoros


Crafty.c('Tesoro', {
  init: function() {
    this.requires('Actor, Color')
        .color('yellow');
  },
    
  recogido: function() {
     this.destroy();

     // Si no queda ningún tesoro ya hemos terminado.
     if (!Crafty('Tesoro').length) {  
	 Crafty.scene('Victory');
     }  
  } 
});