// heavily based/copied from/on the quintus platformer example.
// vim:tw=120:

window.addEventListener("load",function() {

var Q = window.Q = Quintus({ development: false, imagePath: "/images/crushed/" })
        .include("Sprites, Scenes, Input, 2D, Anim, Touch, UI")
        .setup({ maximize: true })
        .controls().touch()

Q.Sprite.extend("Player",{

  init: function(p) {

    this._super(p, {
      sheet: "astronaut",
      x: 290,
      y: 96
    });

    this.add('2d, platformerControls');
    this.on("hit.sprite",function(collision) {
      if(collision.obj.isA("Rocket")) {

        // the rocket flies away
        var rocket = collision.obj;
        rocket.add("tween");
        rocket.animate({ x: 100, y: -500, angle: 360 });

        // FIXME following the rocket does not work
        Q.stage().unfollow();
        Q.stage().viewport.follow(rocket);

        // the player disappears
        this.destroy();

        // and the invite is displayed
        $("#einladung").show("slow");
      }
    });

  }

});


Q.Sprite.extend("Rocket", {
  init: function(p) {
    this._super(p, { sheet: 'rocket' });
  }
});

Q.Sprite.extend("Enemy", {
  init: function(p, q) {
    this._super(p, q);
    this.add('2d, aiBounce');

    this.on("bump.left,bump.right,bump.bottom",function(collision) {
      if(collision.obj.isA("Player")) {
        Q.stageScene("endGame", 1);
        collision.obj.destroy();
      }
    });

    this.on("bump.top",function(collision) {
      if(collision.obj.isA("Player")) {
        this.destroy();
        collision.obj.p.vy = -300;
      }
    });
  }
});

Q.Enemy.extend("Neingeist",{
  init: function(p) {
    this._super(p, { sheet: 'neingeist', vx: 100 });
  }
});

Q.Enemy.extend("Starbug",{
  init: function(p) {
    this._super(p, { sheet: 'starbug', vx: 90 });
  }
});

Q.Enemy.extend("Martin",{
  init: function(p) {
    this._super(p, { sheet: 'martin', vx: 80 });
  }
});

Q.scene("level1", function(stage) {

  stage.insert(new Q.Repeater({ asset: "background.png", speedX: 0.5, speedY: 0.5 }));

  stage.collisionLayer(new Q.TileLayer({
                             tileW: 24,
                             tileH: 24,
                             dataAsset: 'level.json',
                             sheet:     'tiles' }));


  // player
  var player = stage.insert(new Q.Player());

  // viewport
  stage.add("viewport").follow(player);
  stage.viewport.scale = 2;

  // enemies
  stage.insert(new Q.Starbug({ x: 584, y: 0 }));
  stage.insert(new Q.Neingeist({ x: 620, y: 0 }));
  stage.insert(new Q.Martin({ x: 530, y: 0 }));

  stage.insert(new Q.Rocket({ x: 122, y: 32 }));
});

Q.scene('endGame',function(stage) {
  var container = stage.insert(new Q.UI.Container({
    x: Q.width/2, y: Q.height/2, fill: "rgba(0,0,0,0.5)"
  }));

  var button = container.insert(new Q.UI.Button({ x: 0, y: 0, asset: "nochmal.png" }))

  button.on("click",function() {
    Q.clearStages();
    Q.stageScene('level1');
  });

  container.fit(16);
});

Q.load("sprites.png, sprites.json, level.json, tiles.png, background.png, nochmal.png", function() {
  Q.sheet("tiles","tiles.png", { tilew: 24, tileh: 24 });
  Q.compileSheets("sprites.png","sprites.json");

  Q.stageScene("level1");
  $(loading).hide();
});

});
