/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the House
var House = Container.expand(function () {
var self = Container.call(this);
var houseGraphics = self.attachAsset('house', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.takeDamage = function (amount) {
self.health -= amount;
if (self.health <= 0) {
LK.showGameOver();
}
};
});
// Class for the Intruder
var Intruder = Container.expand(function () {
var self = Container.call(this);
var intruderGraphics = self.attachAsset('intruder', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.y += self.speed;
if (self.intersects(house)) {
house.takeDamage(10);
self.destroy();
}
};
});
// Class for the Trap
var Trap = Container.expand(function () {
var self = Container.call(this);
var trapGraphics = self.attachAsset('trap', {
anchorX: 0.5,
anchorY: 0.5
});
self.activate = function (intruder) {
intruder.destroy();
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize house
var house = game.addChild(new House());
house.x = 2048 / 2;
house.y = 2732 - 200;
// Array to keep track of intruders
var intruders = [];
// Array to keep track of traps
var traps = [];
// Function to spawn intruders
function spawnIntruder() {
var intruder = new Intruder();
intruder.x = Math.random() * 2048;
intruder.y = 0;
intruders.push(intruder);
game.addChild(intruder);
}
// Function to place a trap
function placeTrap(x, y) {
var trap = new Trap();
trap.x = x;
trap.y = y;
traps.push(trap);
game.addChild(trap);
}
// Handle game updates
game.update = function () {
// Update intruders
for (var i = intruders.length - 1; i >= 0; i--) {
intruders[i].update();
if (intruders[i].y > 2732) {
intruders[i].destroy();
intruders.splice(i, 1);
}
}
// Check for trap activation
for (var j = traps.length - 1; j >= 0; j--) {
for (var k = intruders.length - 1; k >= 0; k--) {
if (traps[j].intersects(intruders[k])) {
traps[j].activate(intruders[k]);
traps[j].destroy();
traps.splice(j, 1);
break;
}
}
}
// Spawn intruders periodically
if (LK.ticks % 120 == 0) {
spawnIntruder();
}
};
// Handle placing traps on touch
game.down = function (x, y, obj) {
placeTrap(x, y);
};