/****
* Classes
****/
// Boss class
var Boss = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.health = 100;
self.healthBar = self.attachAsset('bossHealthBar', {
anchorX: 0.5,
anchorY: 0
});
self.healthBar.y = -bossGraphics.height / 2 - self.healthBar.height / 2;
self.update = function () {
if (self.y < 2732 * 0.3) {
self.speed += self.acceleration;
self.y += self.speed;
} else {
// Add dynamic movement to the boss
self.y += Math.sin(LK.ticks / 30) * 1;
}
self.healthBar.width = bossGraphics.width * (self.health / 100);
if (self.health <= 0) {
// Spawn a series of enemy bullets that radiate out in a circle
var numBullets = 12;
for (var i = 0; i < numBullets; i++) {
var angle = i / numBullets * 2 * Math.PI;
var bullet = new EnemyBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.direction = {
x: Math.cos(angle),
y: Math.sin(angle)
};
game.addChild(bullet);
enemyBullets.push(bullet);
}
self.destroy();
bosses.splice(bosses.indexOf(self), 1);
}
if (self.y > 2732 + bossGraphics.height) {
self.destroy();
bosses.splice(bosses.indexOf(self), 1);
}
};
self.shoot = function () {
var bullet = new EnemyBullet();
bullet.x = self.x;
bullet.y = self.y + bossGraphics.height / 2;
var dx = lastMousePosition.x - self.x;
var dy = lastMousePosition.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
bullet.direction = {
x: dx / distance,
y: dy / distance
};
game.addChild(bullet);
enemyBullets.push(bullet);
};
});
// 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 = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + enemyGraphics.height) {
self.destroy();
enemies.splice(enemies.indexOf(self), 1);
}
};
self.shoot = function () {
var bullet = new EnemyBullet();
bullet.x = self.x;
bullet.y = self.y + enemyGraphics.height / 2;
// Calculate the direction vector towards the hero's last known position
var dx = lastMousePosition.x - self.x;
var dy = lastMousePosition.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector
bullet.direction = {
x: dx / distance,
y: dy / distance
};
game.addChild(bullet);
enemyBullets.push(bullet);
};
});
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('enemyBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 20;
self.update = function () {
self.x += self.direction.x * self.speed;
self.y += self.direction.y * self.speed;
if (self.y > 2732 + bulletGraphics.height || self.x < -bulletGraphics.width || self.x > 2048 + bulletGraphics.width) {
self.destroy();
enemyBullets.splice(enemyBullets.indexOf(self), 1);
} else if (self.intersects(hero)) {
hero.health -= 10; // decrease hero's health by 10
LK.effects.flashObject(hero, 0xff0000, 500); // flash hero red for 500ms
self.destroy();
enemyBullets.splice(enemyBullets.indexOf(self), 1);
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
self.health = 100;
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.healthBar = self.attachAsset('healthBar', {
anchorX: 0.5,
anchorY: 0
});
self.healthBar.y = -heroGraphics.height / 2 - self.healthBar.height / 2;
self.speed = 10;
self.update = function () {
// Hero update logic
var dx = lastMousePosition.x - self.x;
var dy = lastMousePosition.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > self.speed) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
self.healthBar.width = heroGraphics.width * (self.health / 100);
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -1.5;
self.acceleration = -5;
self.update = function () {
self.speed += self.acceleration * 0.1; // Increase speed by acceleration factor
self.y += self.speed;
if (self.y < -bulletGraphics.height) {
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
} else {
for (var i = bosses.length - 1; i >= 0; i--) {
if (self.intersects(bosses[i])) {
bosses[i].health -= 10;
LK.effects.flashObject(bosses[i], 0xff0000, 500); // flash boss red for 500ms
LK.getSound('hit').play(); // Play hit sound effect
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
break;
}
}
}
};
});
// Particle class
var Particle = Container.expand(function () {
var self = Container.call(this);
var particleGraphics = self.attachAsset('particle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
particleGraphics.blendMode = 1;
self.alpha = 1;
self.update = function () {
self.y += self.speed;
self.alpha -= 0.0167;
if (self.alpha <= 0) {
self.alpha = 1;
self.y = hero.y + hero.height / 2 + (Math.random() * 100 - 50);
self.x = hero.x + (Math.random() * 100 - 50);
}
};
});
// PowerUp class
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1.5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + powerUpGraphics.height) {
self.destroy();
powerUps.splice(powerUps.indexOf(self), 1);
} else if (self.intersects(hero)) {
activatePowerUp();
self.destroy();
powerUps.splice(powerUps.indexOf(self), 1);
}
};
});
// Star class
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = Math.random() * 5 + 1; // Random speed for parallax effect
// Make stars that are moving slower smaller and more transparent
starGraphics.scale.set(self.speed / 6);
starGraphics.alpha = Math.min(self.speed / 6, 0.5);
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + starGraphics.height) {
self.y = -starGraphics.height;
self.x = Math.random() * 2048; // Random x position
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Spawn a new power-up
function spawnPowerUp() {
var powerUp = new PowerUp();
powerUp.x = Math.random() * 2048;
powerUp.y = -powerUp.height;
game.addChild(powerUp);
powerUps.push(powerUp);
}
// Activate power-up
function activatePowerUp() {
hero.shoot = function () {
var bullet1 = new HeroBullet();
var bullet2 = new HeroBullet();
bullet1.x = hero.x - 60;
bullet2.x = hero.x + 60;
bullet1.y = bullet2.y = hero.y - hero.height / 2;
game.addChild(bullet1);
game.addChild(bullet2);
heroBullets.push(bullet1);
heroBullets.push(bullet2);
};
LK.setTimeout(function () {
hero.shoot = function () {
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y - hero.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
};
}, 5000);
}
// Check for game over condition
function checkGameOver() {
if (hero.health <= 0) {
// Flash screen red for 1 second (1000ms) to show we are dead.
LK.effects.flashScreen(0xff0000, 1000);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver(); // Calling this will destroy the 'Game' and reset entire game state.
}
}
// Initialize arrays and variables
var hero;
var heroBullets = [];
var enemyBullets = [];
var bosses = [];
var enemies = [];
var score = 0;
var scoreTxt;
var powerUps = [];
var particles = [];
var lastMousePosition = {
x: 0,
y: 0
};
// Initialize game elements
function initGame() {
// Create star field
for (var i = 0; i < 100; i++) {
var star = new Star();
star.x = Math.random() * 2048; // Random x position
star.y = Math.random() * 2732; // Random y position
game.addChild(star);
}
// Create a container for particles
var particleContainer = new Container();
game.addChild(particleContainer);
// Create hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Create particles
for (var i = 0; i < 20; i++) {
var particle = new Particle();
particle.x = hero.x;
particle.y = hero.y + hero.height / 2;
particleContainer.addChild(particle);
particles.push(particle);
}
// Create score text
scoreTxt = new Text2('0', {
size: 200,
fill: "#ffffff",
font: "'GillSans-Bold',Impact,'Arial Black',Tahoma"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Set up game update loop
game.update = function () {
// Update hero
hero.update();
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
}
// Update particles
for (var i = particles.length - 1; i >= 0; i--) {
particles[i].update();
}
// Spawn particles every 5 ticks
if (LK.ticks % 5 == 0) {
var particle = new Particle();
particle.x = hero.x;
particle.y = hero.y + hero.height / 2;
particleContainer.addChild(particle);
particles.push(particle);
}
// Hero auto-fires bullets at a faster rate (50% increase)
if (LK.ticks % 40 == 0) {
hero.shoot();
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (LK.ticks % 120 == 0) {
enemies[j].shoot();
}
}
// Update power-ups
for (var k = powerUps.length - 1; k >= 0; k--) {
powerUps[k].update();
}
// Check for collisions and game over condition
checkCollisions();
checkGameOver();
// Check if hero intersects with any enemy
for (var i = 0; i < enemies.length; i++) {
if (hero.intersects(enemies[i])) {
// Flash screen red for 1 second (1000ms) to show we are dead.
LK.effects.flashScreen(0xff0000, 1000);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver(); // Calling this will destroy the 'Game' and reset entire game state.
break;
}
}
// Update bosses
for (var i = bosses.length - 1; i >= 0; i--) {
bosses[i].update();
if (LK.ticks % 40 == 0) {
bosses[i].shoot();
}
}
// Check for collisions between hero bullets and bosses
for (var i = heroBullets.length - 1; i >= 0; i--) {
for (var j = bosses.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(bosses[j])) {
// Decrease boss's health
bosses[j].health -= 10;
// Destroy bullet
heroBullets[i].destroy();
heroBullets.splice(i, 1);
// Destroy boss if its health is 0
if (bosses[j].health <= 0) {
bosses[j].destroy();
bosses.splice(j, 1);
}
break;
}
}
}
// Spawn enemies
if (LK.ticks % 90 == 0) {
spawnEnemy();
}
// Spawn power-ups
if (LK.ticks % 600 == 0) {
spawnPowerUp();
}
};
// Set up game controls
game.down = function (x, y, obj) {
// hero.shoot(); removed this line
};
game.move = function (x, y, obj) {
lastMousePosition = {
x: x,
y: y
};
};
}
// Check for collisions between hero bullets and enemies
function checkCollisions() {
for (var i = heroBullets.length - 1; i >= 0; i--) {
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
// Destroy enemy and bullet
enemies[j].destroy();
heroBullets[i].destroy();
LK.getSound('hit').play(); // Play hit sound effect
enemies.splice(j, 1);
heroBullets.splice(i, 1);
// Update score
score += 10;
scoreTxt.setText(score);
// Spawn a boss enemy for every enemy killed
if (bosses.length == 0) {
var boss = new Boss();
boss.x = 2048 / 2;
boss.y = -boss.height;
game.addChild(boss);
bosses.push(boss);
}
break;
}
}
}
}
// Spawn a new enemy
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -enemy.height;
game.addChild(enemy);
enemies.push(enemy);
}
// Initialize the game
initGame();