/****
* Classes
****/
// Bullet class
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
var dx = this.targetX - this.x;
var dy = this.targetY - this.y;
var angle = Math.atan2(dy, dx);
this.x += Math.cos(angle) * this.speed;
this.y += Math.sin(angle) * this.speed;
};
});
// Piggy class
var Piggy = Container.expand(function () {
var self = Container.call(this);
var piggyGraphics = self.attachAsset('piggy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.hitPoints = Math.floor(Math.random() * 3) + 1; // Random hit points between 1 and 3
self.update = function () {
self.y += self.speed;
};
});
// Assets will be automatically created and loaded by the LK engine
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
// Add flame effect to the spaceship
var flameGraphics = self.attachAsset('flame', {
anchorX: 0.5,
anchorY: 0
});
flameGraphics.y = playerGraphics.height / 2; // Position the flame at the bottom of the spaceship
self.speed = 5;
self.update = function () {
// Player movement logic
if (dragNode) {
self.x = dragNode.x;
self.y = dragNode.y;
// Calculate the angle between the player and the dragNode
var dx = dragNode.x - self.x;
var dy = dragNode.y - self.y;
var angle = Math.atan2(dy, dx);
// Rotate the player to face the dragNode
self.rotation = angle;
// Animate the flame to make it look like the spaceship is moving
flameGraphics.scale.y = 1 + Math.sin(LK.ticks / 10) * 0.1; // Add a small oscillation to the flame's scale
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 100;
var bullets = [];
var piggies = [];
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
game.update = function () {
// Player movement logic
if (dragNode) {
dragNode.x = dragNode.x;
dragNode.y = dragNode.y;
}
// Bullet logic
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
if (bullet.y < 0) {
bullet.destroy();
bullets.splice(i, 1);
} else {
for (var j = piggies.length - 1; j >= 0; j--) {
var piggy = piggies[j];
if (bullet.intersects(piggy)) {
bullet.destroy();
bullets.splice(i, 1);
piggy.hitPoints--;
if (piggy.hitPoints <= 0) {
piggy.destroy();
piggies.splice(j, 1);
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
}
break;
}
}
}
}
// Piggy logic
for (var i = piggies.length - 1; i >= 0; i--) {
var piggy = piggies[i];
if (piggy.y > 2732) {
piggy.destroy();
piggies.splice(i, 1);
} else if (piggy.intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn piggies
if (LK.ticks % 120 == 0) {
var piggy = new Piggy();
piggy.x = Math.random() * 2048;
piggy.y = 0;
piggies.push(piggy);
game.addChild(piggy);
}
};
var dragNode = null;
game.down = function (x, y, obj) {
dragNode = player;
};
game.up = function (x, y, obj) {
dragNode = null;
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullet.targetX = x;
bullet.targetY = y;
bullets.push(bullet);
game.addChild(bullet);
};