/****
* Classes
****/
// Enemy class
var Enemy = Container.expand(function () {
  var self = Container.call(this);
  var enemyGraphics = self.attachAsset('enemy', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = 5;
  self.update = function () {
    self.y += self.speed;
    if (self.y > 2732) {
      self.destroy();
    }
  };
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
  var self = Container.call(this);
  var heroGraphics = self.attachAsset('hero', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = 10;
  self.update = function () {
    // Hero update logic
  };
  self.moveLeft = function () {
    self.x -= self.speed;
  };
  self.moveRight = function () {
    self.x += self.speed;
  };
  self.attack = function () {
    var bullet = new HeroBullet();
    bullet.x = self.x;
    bullet.y = self.y - heroGraphics.height / 2;
    game.addChild(bullet);
    heroBullets.push(bullet);
  };
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
  var self = Container.call(this);
  var bulletGraphics = self.attachAsset('heroBullet', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = -15;
  self.update = function () {
    self.y += self.speed;
    if (self.y < 0) {
      self.destroy();
    }
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x000000 //Init game with black background 
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var enemies = [];
var heroBullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
  size: 150,
  fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Spawn enemies
function spawnEnemy() {
  var enemy = new Enemy();
  enemy.x = Math.random() * 2048;
  enemy.y = -enemy.height;
  game.addChild(enemy);
  enemies.push(enemy);
}
// Update game state
game.update = function () {
  hero.update();
  for (var i = enemies.length - 1; i >= 0; i--) {
    enemies[i].update();
    if (enemies[i].intersects(hero)) {
      LK.effects.flashScreen(0xff0000, 1000);
      LK.showGameOver();
    }
  }
  for (var j = heroBullets.length - 1; j >= 0; j--) {
    heroBullets[j].update();
    for (var k = enemies.length - 1; k >= 0; k--) {
      if (heroBullets[j].intersects(enemies[k])) {
        enemies[k].destroy();
        heroBullets[j].destroy();
        enemies.splice(k, 1);
        heroBullets.splice(j, 1);
        score++;
        scoreTxt.setText(score);
        break;
      }
    }
  }
  if (LK.ticks % 60 == 0) {
    spawnEnemy();
  }
};
// Handle touch events
game.down = function (x, y, obj) {
  if (x < 2048 / 2) {
    hero.moveLeft();
  } else {
    hero.moveRight();
  }
};
game.up = function (x, y, obj) {
  hero.attack();
}; /****
* Classes
****/
// Enemy class
var Enemy = Container.expand(function () {
  var self = Container.call(this);
  var enemyGraphics = self.attachAsset('enemy', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = 5;
  self.update = function () {
    self.y += self.speed;
    if (self.y > 2732) {
      self.destroy();
    }
  };
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
  var self = Container.call(this);
  var heroGraphics = self.attachAsset('hero', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = 10;
  self.update = function () {
    // Hero update logic
  };
  self.moveLeft = function () {
    self.x -= self.speed;
  };
  self.moveRight = function () {
    self.x += self.speed;
  };
  self.attack = function () {
    var bullet = new HeroBullet();
    bullet.x = self.x;
    bullet.y = self.y - heroGraphics.height / 2;
    game.addChild(bullet);
    heroBullets.push(bullet);
  };
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
  var self = Container.call(this);
  var bulletGraphics = self.attachAsset('heroBullet', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = -15;
  self.update = function () {
    self.y += self.speed;
    if (self.y < 0) {
      self.destroy();
    }
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x000000 //Init game with black background 
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var enemies = [];
var heroBullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
  size: 150,
  fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Spawn enemies
function spawnEnemy() {
  var enemy = new Enemy();
  enemy.x = Math.random() * 2048;
  enemy.y = -enemy.height;
  game.addChild(enemy);
  enemies.push(enemy);
}
// Update game state
game.update = function () {
  hero.update();
  for (var i = enemies.length - 1; i >= 0; i--) {
    enemies[i].update();
    if (enemies[i].intersects(hero)) {
      LK.effects.flashScreen(0xff0000, 1000);
      LK.showGameOver();
    }
  }
  for (var j = heroBullets.length - 1; j >= 0; j--) {
    heroBullets[j].update();
    for (var k = enemies.length - 1; k >= 0; k--) {
      if (heroBullets[j].intersects(enemies[k])) {
        enemies[k].destroy();
        heroBullets[j].destroy();
        enemies.splice(k, 1);
        heroBullets.splice(j, 1);
        score++;
        scoreTxt.setText(score);
        break;
      }
    }
  }
  if (LK.ticks % 60 == 0) {
    spawnEnemy();
  }
};
// Handle touch events
game.down = function (x, y, obj) {
  if (x < 2048 / 2) {
    hero.moveLeft();
  } else {
    hero.moveRight();
  }
};
game.up = function (x, y, obj) {
  hero.attack();
};