/****
* Classes
****/
var Base = Container.expand(function () {
var self = Container.call(this);
var baseGraphics = self.attachAsset('base', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 500;
self.update = function () {
if (self.health <= 0) {
LK.showGameOver();
}
};
self.takeDamage = function (amount) {
self.health -= amount;
// Play base hit sound
LK.getSound('baseHit').play();
};
});
var Boss = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 200;
self.speed = 0.5;
self.update = function () {
// Update logic for boss
// Move straight down
self.y += self.speed;
};
self.takeDamage = function (amount) {
self.health -= amount;
// Play boss hit sound
LK.getSound('bossHit').play();
if (self.health <= 0) {
self.die();
}
};
self.die = function () {
// Handle boss death
for (var i = bullets.length - 1; i >= 0; i--) {
if (bullets[i].intersects(self)) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Play boss death sound
LK.getSound('bossDeath').play();
// Update score
score += 10;
scoreTxt.setText('Score: ' + score);
self.destroy();
};
});
// 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 () {
self.y -= self.speed;
if (self.y < 0 || self.y > 2732) {
self.destroy();
}
};
});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 50;
self.speed = 1;
self.update = function () {
// Update logic for enemy
// Move straight down
self.y += self.speed;
};
self.takeDamage = function (amount) {
self.health -= amount;
// Play enemy hit sound
LK.getSound('enemyHit').play();
if (self.health <= 0) {
self.die();
}
};
self.die = function () {
// Handle enemy death
for (var i = bullets.length - 1; i >= 0; i--) {
if (bullets[i].intersects(self)) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Play enemy death sound
LK.getSound('enemyDeath').play();
// Update score
score += 1;
scoreTxt.setText('Score: ' + score);
self.destroy();
};
});
// HealthBar class
var HealthBar = Container.expand(function () {
var self = Container.call(this);
var healthBarGraphics = self.attachAsset('healthBar', {
anchorX: 0.5,
anchorY: 0.5,
height: 15
});
self.update = function () {
// Update logic for health bar
// Adjust width of health bar based on health of parent
healthBarGraphics.width = self.parent.health;
};
self.interactive = false; // Disable hitbox for health bar
});
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.speed = 5;
self.update = function () {
// Update logic for hero
// Move hero left if it's not at the edge of the screen
if (self.x > 0) {
self.x -= self.speed;
}
// Move hero right if it's not at the edge of the screen
if (self.x < 2048) {
self.x += self.speed;
}
};
self.takeDamage = function (amount) {
self.health -= amount;
// Play player damage sound
LK.getSound('playerDamage').play();
if (self.health <= 0) {
self.die();
}
};
self.die = function () {
// Handle hero death
LK.showGameOver();
};
});
var SuperBoss = Container.expand(function () {
var self = Container.call(this);
var superBossGraphics = self.attachAsset('superBoss', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 500;
self.speed = 0.3;
self.update = function () {
// Update logic for super boss
// Move straight down
self.y += self.speed;
};
self.takeDamage = function (amount) {
self.health -= amount;
// Play boss hit sound
LK.getSound('bossHit').play();
if (self.health <= 0) {
self.die();
}
};
self.die = function () {
// Handle super boss death
for (var i = bullets.length - 1; i >= 0; i--) {
if (bullets[i].intersects(self)) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Play boss death sound
LK.getSound('bossDeath').play();
// Update score
score += 50;
scoreTxt.setText('Score: ' + score);
self.destroy();
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Add a background to the game
var background = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
width: 2048,
height: 2732
}));
LK.playMusic('backgroundMusic', {
loop: true
});
// Initialize score counter
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Attach health bar to hero
var heroHealthBar = hero.addChild(new HealthBar());
heroHealthBar.y = 75; // Position health bar below hero
var base = game.addChild(new Base());
base.x = 2048 / 2;
base.y = 2732 - 50;
// Attach health bar to base
var baseHealthBar = base.addChild(new HealthBar());
baseHealthBar.y = 0; // Position health bar above base
// Initialize enemies array
var enemies = [];
// Initialize bullets array
var bullets = [];
// Function to spawn enemies
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = 0;
// Attach health bar to enemy
var enemyHealthBar = enemy.addChild(new HealthBar());
enemyHealthBar.y = -75; // Position health bar slightly above enemy
enemies.push(enemy);
game.addChild(enemy);
}
// Function to shoot bullets
function shootBullet() {
var bullet = new Bullet();
bullet.x = hero.x;
bullet.y = hero.y;
bullets.push(bullet);
game.addChild(bullet);
}
// Handle touch events for shooting
game.down = function (x, y, obj) {
hero.x = x;
shootBullet();
};
// Update game logic
game.update = function () {
// Update hero
hero.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].intersects(hero)) {
if (enemies[i] instanceof Boss) {
hero.takeDamage(20); // Boss does double damage
} else if (enemies[i] instanceof SuperBoss) {
hero.takeDamage(100); // SuperBoss does 50 damage
} else {
hero.takeDamage(10);
}
enemies[i].destroy();
enemies.splice(i, 1);
} else if (enemies[i].intersects(base)) {
if (enemies[i] instanceof Boss) {
base.takeDamage(20); // Boss does double damage
} else if (enemies[i] instanceof SuperBoss) {
base.takeDamage(100); // SuperBoss does 50 damage
} else {
base.takeDamage(10);
}
enemies[i].destroy();
enemies.splice(i, 1);
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j]) {
bullets[j].update();
}
for (var k = enemies.length - 1; k >= 0; k--) {
if (enemies[k].health > 0 && bullets[j] && bullets[j].intersects(enemies[k])) {
enemies[k].takeDamage(20);
if (bullets[j]) {
bullets[j].destroy();
}
bullets.splice(j, 1);
break;
}
}
}
if (LK.ticks % enemySpawnInterval == 0) {
spawnEnemy();
}
// Increase spawn rate over time
if (enemySpawnInterval > 80 && LK.ticks % 600 == 0) {
enemySpawnInterval -= 10;
if (enemySpawnInterval < 80) {
enemySpawnInterval = 80;
}
}
// Spawn boss enemy periodically
if (LK.ticks % 1200 == 0) {
var boss = new Boss();
boss.x = Math.random() * 2048;
boss.y = 0;
var bossHealthBar = boss.addChild(new HealthBar());
bossHealthBar.y = -125;
enemies.push(boss);
game.addChild(boss);
}
// Spawn SuperBoss when score reaches 100
if (score >= 100 && !superBossSpawned) {
var superBoss = new SuperBoss();
superBoss.x = Math.random() * 2048;
superBoss.y = 0;
var superBossHealthBar = superBoss.addChild(new HealthBar());
superBossHealthBar.y = -150;
enemies.push(superBoss);
game.addChild(superBoss);
superBossSpawned = true;
}
// Spawn powerups periodically
};
game.move = function (x, y, obj) {
hero.x = x;
};
// Variable to track if SuperBoss has spawned
var superBossSpawned = false;
// Variable to track enemy spawn interval
var enemySpawnInterval = 120;