/****
* 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 = -2;
  self.update = function () {
    self.x += self.speed;
    // Update enemy logic
  };
});
// Troop class
var Troop = Container.expand(function () {
  var self = Container.call(this);
  var troopGraphics = self.attachAsset('troop', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = 2;
  self.update = function () {
    self.x += self.speed;
    // Update troop logic
  };
});
//<Assets used in the game will automatically appear here>
// Village class
var Village = Container.expand(function () {
  var self = Container.call(this);
  var villageGraphics = self.attachAsset('village', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.health = 100;
  self.update = function () {
    // Update village logic
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x000000 //Init game with black background 
});
/****
* Game Code
****/
// Initialize arrays and variables
var villages = [];
var troops = [];
var enemies = [];
// Create and position the village
var village = new Village();
village.x = 1024;
village.y = 1366;
game.addChild(village);
villages.push(village);
// Function to spawn a troop
function spawnTroop() {
  var troop = new Troop();
  troop.x = village.x;
  troop.y = village.y;
  game.addChild(troop);
  troops.push(troop);
}
// Function to spawn an enemy
function spawnEnemy() {
  var enemy = new Enemy();
  enemy.x = 2048;
  enemy.y = Math.random() * 2732;
  game.addChild(enemy);
  enemies.push(enemy);
}
// Handle game updates
game.update = function () {
  // Update all troops
  for (var i = troops.length - 1; i >= 0; i--) {
    troops[i].update();
    // Check for collision with enemies
    for (var j = enemies.length - 1; j >= 0; j--) {
      if (troops[i].intersects(enemies[j])) {
        // Destroy both troop and enemy
        troops[i].destroy();
        enemies[j].destroy();
        troops.splice(i, 1);
        enemies.splice(j, 1);
        break;
      }
    }
  }
  // Update all enemies
  for (var i = enemies.length - 1; i >= 0; i--) {
    enemies[i].update();
    // Check for collision with village
    if (enemies[i].intersects(village)) {
      // Reduce village health
      village.health -= 10;
      // Destroy enemy
      enemies[i].destroy();
      enemies.splice(i, 1);
      // Check if village is destroyed
      if (village.health <= 0) {
        LK.showGameOver();
      }
    }
  }
  // Spawn new enemies periodically
  if (LK.ticks % 60 == 0) {
    spawnEnemy();
  }
};
// Handle touch events to spawn troops
game.down = function (x, y, obj) {
  spawnTroop();
}; /****
* 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 = -2;
  self.update = function () {
    self.x += self.speed;
    // Update enemy logic
  };
});
// Troop class
var Troop = Container.expand(function () {
  var self = Container.call(this);
  var troopGraphics = self.attachAsset('troop', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = 2;
  self.update = function () {
    self.x += self.speed;
    // Update troop logic
  };
});
//<Assets used in the game will automatically appear here>
// Village class
var Village = Container.expand(function () {
  var self = Container.call(this);
  var villageGraphics = self.attachAsset('village', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.health = 100;
  self.update = function () {
    // Update village logic
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x000000 //Init game with black background 
});
/****
* Game Code
****/
// Initialize arrays and variables
var villages = [];
var troops = [];
var enemies = [];
// Create and position the village
var village = new Village();
village.x = 1024;
village.y = 1366;
game.addChild(village);
villages.push(village);
// Function to spawn a troop
function spawnTroop() {
  var troop = new Troop();
  troop.x = village.x;
  troop.y = village.y;
  game.addChild(troop);
  troops.push(troop);
}
// Function to spawn an enemy
function spawnEnemy() {
  var enemy = new Enemy();
  enemy.x = 2048;
  enemy.y = Math.random() * 2732;
  game.addChild(enemy);
  enemies.push(enemy);
}
// Handle game updates
game.update = function () {
  // Update all troops
  for (var i = troops.length - 1; i >= 0; i--) {
    troops[i].update();
    // Check for collision with enemies
    for (var j = enemies.length - 1; j >= 0; j--) {
      if (troops[i].intersects(enemies[j])) {
        // Destroy both troop and enemy
        troops[i].destroy();
        enemies[j].destroy();
        troops.splice(i, 1);
        enemies.splice(j, 1);
        break;
      }
    }
  }
  // Update all enemies
  for (var i = enemies.length - 1; i >= 0; i--) {
    enemies[i].update();
    // Check for collision with village
    if (enemies[i].intersects(village)) {
      // Reduce village health
      village.health -= 10;
      // Destroy enemy
      enemies[i].destroy();
      enemies.splice(i, 1);
      // Check if village is destroyed
      if (village.health <= 0) {
        LK.showGameOver();
      }
    }
  }
  // Spawn new enemies periodically
  if (LK.ticks % 60 == 0) {
    spawnEnemy();
  }
};
// Handle touch events to spawn troops
game.down = function (x, y, obj) {
  spawnTroop();
};