/****
* Classes
****/
// Bullet class representing the bullets fired by the player
var Bullet = Container.expand(function () {
  var self = Container.call(this);
  var bulletGraphics = self.attachAsset('bullet', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = -10; // Speed at which the bullet moves
  self.update = function () {
    self.y += self.speed;
  };
});
//<Assets used in the game will automatically appear here>
// Target class representing the targets to be shot
var Target = Container.expand(function () {
  var self = Container.call(this);
  var targetGraphics = self.attachAsset('target', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = 2; // Speed at which the target moves
  self.update = function () {
    self.x += self.speed;
    if (self.x > 2048 || self.x < 0) {
      self.speed *= -1; // Reverse direction when hitting screen edges
    }
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x000000 //Init game with black background 
});
/****
* Game Code
****/
// Initialize variables
var targets = [];
var bullets = [];
var score = 0;
// Create score display
var scoreTxt = new Text2('Score: 0', {
  size: 100,
  fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn a new target
function spawnTarget() {
  var newTarget = new Target();
  newTarget.x = Math.random() * 2048;
  newTarget.y = 200;
  targets.push(newTarget);
  game.addChild(newTarget);
}
// Function to handle shooting
function shoot(x, y) {
  var newBullet = new Bullet();
  newBullet.x = x;
  newBullet.y = 2732 - 100; // Start from the bottom of the screen
  bullets.push(newBullet);
  game.addChild(newBullet);
}
// Game update loop
game.update = function () {
  // Update targets
  for (var i = targets.length - 1; i >= 0; i--) {
    targets[i].update();
    if (targets[i].y < -50) {
      targets[i].destroy();
      targets.splice(i, 1);
    }
  }
  // Update bullets
  for (var j = bullets.length - 1; j >= 0; j--) {
    bullets[j].update();
    if (bullets[j].y < -50) {
      bullets[j].destroy();
      bullets.splice(j, 1);
    }
  }
  // Check for collisions
  for (var k = bullets.length - 1; k >= 0; k--) {
    for (var l = targets.length - 1; l >= 0; l--) {
      if (bullets[k].intersects(targets[l])) {
        bullets[k].destroy();
        targets[l].destroy();
        bullets.splice(k, 1);
        targets.splice(l, 1);
        score += 1;
        scoreTxt.setText('Score: ' + score);
        break;
      }
    }
  }
  // Spawn new targets periodically
  if (LK.ticks % 120 === 0) {
    spawnTarget();
  }
};
// Handle touch events for shooting
game.down = function (x, y, obj) {
  shoot(x, y);
}; /****
* Classes
****/
// Bullet class representing the bullets fired by the player
var Bullet = Container.expand(function () {
  var self = Container.call(this);
  var bulletGraphics = self.attachAsset('bullet', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = -10; // Speed at which the bullet moves
  self.update = function () {
    self.y += self.speed;
  };
});
//<Assets used in the game will automatically appear here>
// Target class representing the targets to be shot
var Target = Container.expand(function () {
  var self = Container.call(this);
  var targetGraphics = self.attachAsset('target', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speed = 2; // Speed at which the target moves
  self.update = function () {
    self.x += self.speed;
    if (self.x > 2048 || self.x < 0) {
      self.speed *= -1; // Reverse direction when hitting screen edges
    }
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x000000 //Init game with black background 
});
/****
* Game Code
****/
// Initialize variables
var targets = [];
var bullets = [];
var score = 0;
// Create score display
var scoreTxt = new Text2('Score: 0', {
  size: 100,
  fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn a new target
function spawnTarget() {
  var newTarget = new Target();
  newTarget.x = Math.random() * 2048;
  newTarget.y = 200;
  targets.push(newTarget);
  game.addChild(newTarget);
}
// Function to handle shooting
function shoot(x, y) {
  var newBullet = new Bullet();
  newBullet.x = x;
  newBullet.y = 2732 - 100; // Start from the bottom of the screen
  bullets.push(newBullet);
  game.addChild(newBullet);
}
// Game update loop
game.update = function () {
  // Update targets
  for (var i = targets.length - 1; i >= 0; i--) {
    targets[i].update();
    if (targets[i].y < -50) {
      targets[i].destroy();
      targets.splice(i, 1);
    }
  }
  // Update bullets
  for (var j = bullets.length - 1; j >= 0; j--) {
    bullets[j].update();
    if (bullets[j].y < -50) {
      bullets[j].destroy();
      bullets.splice(j, 1);
    }
  }
  // Check for collisions
  for (var k = bullets.length - 1; k >= 0; k--) {
    for (var l = targets.length - 1; l >= 0; l--) {
      if (bullets[k].intersects(targets[l])) {
        bullets[k].destroy();
        targets[l].destroy();
        bullets.splice(k, 1);
        targets.splice(l, 1);
        score += 1;
        scoreTxt.setText('Score: ' + score);
        break;
      }
    }
  }
  // Spawn new targets periodically
  if (LK.ticks % 120 === 0) {
    spawnTarget();
  }
};
// Handle touch events for shooting
game.down = function (x, y, obj) {
  shoot(x, y);
};