/**** 
* Classes
****/ 
// Bomb class
var Bomb = Container.expand(function () {
	var self = Container.call(this);
	var bombGraphics = self.attachAsset('bomb', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.explode = function () {
		// Create explosion effect
		var explosion = new Explosion();
		explosion.x = self.x;
		explosion.y = self.y;
		game.addChild(explosion);
		explosion.init();
		// Flash the screen
		LK.effects.flashScreen(0xff0000, 500);
		// Damage nearby buildings
		for (var i = 0; i < buildings.length; i++) {
			if (self.intersects(buildings[i])) {
				buildings[i].takeDamage(50);
			}
		}
		LK.getSound('explosion').play();
		self.destroy();
	};
});
//<Assets used in the game will automatically appear here>
// Building class
var Building = Container.expand(function () {
	var self = Container.call(this);
	var buildingGraphics = self.attachAsset('building', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create reconstruction animation
	var reconstruction = new BuildingReconstruction();
	reconstruction.x = self.x;
	reconstruction.y = self.y;
	game.addChild(reconstruction);
	reconstruction.init();
	self.health = 100;
	self.update = function () {
		if (self.health <= 0) {
			self.destroy();
		}
	};
	self.takeDamage = function (damage) {
		self.health -= damage;
	};
});
// BuildingReconstruction class
var BuildingReconstruction = Container.expand(function () {
	var self = Container.call(this);
	self.init = function () {
		// Create reconstruction effect with fade in
		self.alpha = 0;
		LK.effects.flashObject(self, 0x00ff00, 500);
		var fadeInInterval = LK.setInterval(function () {
			if (self.alpha < 1) {
				self.alpha += 0.02;
			} else {
				LK.clearInterval(fadeInInterval);
			}
		}, 20);
	};
});
// Explosion class
var Explosion = Container.expand(function () {
	var self = Container.call(this);
	self.init = function () {
		// Create explosion effect
		LK.effects.flashObject(self, 0xff0000, 500);
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize road
var road = game.addChild(LK.getAsset('road', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024,
	y: 2500
}));
// Initialize buildings array
var buildings = [];
// Create buildings
for (var i = 0; i < 10; i++) {
	var building = new Building();
	building.x = 200 * (i + 1);
	building.y = 2000;
	buildings.push(building);
	game.addChild(building);
}
// Initialize bombs array
var bombs = [];
// Handle touch down event to place bombs
game.down = function (x, y, obj) {
	var bomb = new Bomb();
	bomb.x = x;
	bomb.y = y;
	bombs.push(bomb);
	game.addChild(bomb);
	LK.setTimeout(function () {
		bomb.explode();
	}, 1000);
};
// Update game state
game.update = function () {
	for (var i = buildings.length - 1; i >= 0; i--) {
		buildings[i].update();
		if (buildings[i].health <= 0) {
			buildings.splice(i, 1);
		}
	}
	for (var j = bombs.length - 1; j >= 0; j--) {
		if (bombs[j].destroyed) {
			bombs.splice(j, 1);
		}
	}
	// Add new buildings when all are destroyed
	if (buildings.length === 0) {
		LK.setTimeout(function () {
			for (var i = 0; i < 10; i++) {
				var building = new Building();
				building.x = 200 * (i + 1);
				building.y = 2000;
				buildings.push(building);
				game.addChild(building);
			}
		}, 1000);
	}
}; /**** 
* Classes
****/ 
// Bomb class
var Bomb = Container.expand(function () {
	var self = Container.call(this);
	var bombGraphics = self.attachAsset('bomb', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.explode = function () {
		// Create explosion effect
		var explosion = new Explosion();
		explosion.x = self.x;
		explosion.y = self.y;
		game.addChild(explosion);
		explosion.init();
		// Flash the screen
		LK.effects.flashScreen(0xff0000, 500);
		// Damage nearby buildings
		for (var i = 0; i < buildings.length; i++) {
			if (self.intersects(buildings[i])) {
				buildings[i].takeDamage(50);
			}
		}
		LK.getSound('explosion').play();
		self.destroy();
	};
});
//<Assets used in the game will automatically appear here>
// Building class
var Building = Container.expand(function () {
	var self = Container.call(this);
	var buildingGraphics = self.attachAsset('building', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create reconstruction animation
	var reconstruction = new BuildingReconstruction();
	reconstruction.x = self.x;
	reconstruction.y = self.y;
	game.addChild(reconstruction);
	reconstruction.init();
	self.health = 100;
	self.update = function () {
		if (self.health <= 0) {
			self.destroy();
		}
	};
	self.takeDamage = function (damage) {
		self.health -= damage;
	};
});
// BuildingReconstruction class
var BuildingReconstruction = Container.expand(function () {
	var self = Container.call(this);
	self.init = function () {
		// Create reconstruction effect with fade in
		self.alpha = 0;
		LK.effects.flashObject(self, 0x00ff00, 500);
		var fadeInInterval = LK.setInterval(function () {
			if (self.alpha < 1) {
				self.alpha += 0.02;
			} else {
				LK.clearInterval(fadeInInterval);
			}
		}, 20);
	};
});
// Explosion class
var Explosion = Container.expand(function () {
	var self = Container.call(this);
	self.init = function () {
		// Create explosion effect
		LK.effects.flashObject(self, 0xff0000, 500);
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize road
var road = game.addChild(LK.getAsset('road', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024,
	y: 2500
}));
// Initialize buildings array
var buildings = [];
// Create buildings
for (var i = 0; i < 10; i++) {
	var building = new Building();
	building.x = 200 * (i + 1);
	building.y = 2000;
	buildings.push(building);
	game.addChild(building);
}
// Initialize bombs array
var bombs = [];
// Handle touch down event to place bombs
game.down = function (x, y, obj) {
	var bomb = new Bomb();
	bomb.x = x;
	bomb.y = y;
	bombs.push(bomb);
	game.addChild(bomb);
	LK.setTimeout(function () {
		bomb.explode();
	}, 1000);
};
// Update game state
game.update = function () {
	for (var i = buildings.length - 1; i >= 0; i--) {
		buildings[i].update();
		if (buildings[i].health <= 0) {
			buildings.splice(i, 1);
		}
	}
	for (var j = bombs.length - 1; j >= 0; j--) {
		if (bombs[j].destroyed) {
			bombs.splice(j, 1);
		}
	}
	// Add new buildings when all are destroyed
	if (buildings.length === 0) {
		LK.setTimeout(function () {
			for (var i = 0; i < 10; i++) {
				var building = new Building();
				building.x = 200 * (i + 1);
				building.y = 2000;
				buildings.push(building);
				game.addChild(building);
			}
		}, 1000);
	}
};