/****
* Classes
****/
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
var leftWing = self.attachAsset('leftWing', {
anchorX: 0.5,
anchorY: 0.5,
flipX: 1
});
var rightWing = self.attachAsset('rightWing', {
anchorX: 0.5,
anchorY: 0.5
});
leftWing.x = -enemyGraphics.width / 2.3 - leftWing.width / 2.3;
rightWing.x = enemyGraphics.width / 2.3 + rightWing.width / 2.3;
self.speed = 4 + waveNumber * enemySpeedIncrement; // Initialize enemy speed with progression
self.update = function () {
var dx = player.x - self.x;
var dy = player.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
leftWing.y = Math.sin(LK.ticks / 5) * 10;
rightWing.y = Math.sin(LK.ticks / 5) * 10;
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed + Math.sin(LK.ticks / 5) * 5;
var dx = self.x - player.x;
var dy = self.y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < enemyGraphics.width / 2 + player.width / 2) {
if (!game.gameOverTriggered) {
game.gameOverTriggered = true;
LK.setTimeout(function () {
LK.getSound('gameover').play();
}, 500);
var scaleInterval = LK.setInterval(function () {
enemyGraphics.scale.x += 0.8;
enemyGraphics.scale.y += 0.8;
leftWing.scale.x += 0.8;
leftWing.scale.y += 0.8;
rightWing.scale.x += 0.8;
rightWing.scale.y += 0.8;
// Update the position of wings relative to the size of the enemy
leftWing.x -= 60;
rightWing.x += 60;
// Bring the enemy to the top layer of z axis
game.setChildIndex(self, game.children.length - 1);
if (enemyGraphics.scale.x >= 2048 / enemyGraphics.width && enemyGraphics.scale.y >= 2732 / enemyGraphics.height) {
LK.clearInterval(scaleInterval);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
}, 100);
}
}
// Check if enemy is outside of the circle
if (!isInsideCircle(self, circle)) {
// Make enemy and wings darker
enemyGraphics.alpha = 0.5;
leftWing.alpha = 0.5;
rightWing.alpha = 0.5;
} else {
// Restore enemy and wings brightness
enemyGraphics.alpha = 1;
leftWing.alpha = 1;
rightWing.alpha = 1;
}
};
});
// Create a player class
var Player = Container.expand(function () {
var self = Container.call(this);
var rope = self.attachAsset('rope', {
anchorX: 0.5,
anchorY: 1
});
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
rope.y = -playerGraphics.height / 2;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var smallCircle = self.attachAsset('circle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.1,
scaleY: 0.1
});
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4 + waveNumber * enemySpeedIncrement; // Initialize power-up speed with progression
self.update = function () {
var dx = player.x - self.x;
var dy = player.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
// Add a scale effect to the powerup to make it seem cartoony
self.scale.x = 1 + Math.sin(LK.ticks / 10) * 0.1;
self.scale.y = 1 + Math.sin(LK.ticks / 10) * 0.1;
self.rotation = Math.sin(LK.ticks / 10) * 0.1;
var dx = self.x - player.x;
var dy = self.y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.width / 2 + player.width / 2 && !game.gameOverTriggered) {
self.destroy();
circle.scale.x *= 1.25;
circle.scale.y *= 1.25;
LK.getSound('powerup').play();
}
// Check if power up is outside of the circle
if (!isInsideCircle(self, circle)) {
// Keep power up brightness
powerUpGraphics.alpha = 1;
smallCircle.alpha = 1;
} else {
// Keep power up brightness
powerUpGraphics.alpha = 1;
smallCircle.alpha -= 0.1;
if (smallCircle.alpha <= 0) {
smallCircle.alpha = 0;
}
}
};
});
/****
* Initialize Game
****/
// Add player to the game
//<Assets used in the game will automatically appear here>
//<Write entity 'classes' with empty functions for important behavior here>
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
game.gameOverTriggered = false;
function isInsideCircle(object, circle) {
var dx = object.x - circle.x;
var dy = object.y - circle.y;
var distance = Math.sqrt(dx * dx + dy * dy);
return distance < circle.width / 2;
}
game.down = function (x, y, obj) {
if (!game.gameOverTriggered) {
console.log("Screen was pressed at", x, y);
LK.getSound('zap').play();
for (var i = game.children.length - 1; i >= 0; i--) {
if (game.children[i] instanceof Enemy && isInsideCircle(game.children[i], circle)) {
game.children[i].rotation = Math.PI;
game.children[i].speed = 25;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
game.children[i].update = function () {
this.y += this.speed;
if (this.y > 2732 + 50) {
this.destroy();
}
};
} else if (game.children[i] instanceof PowerUp && isInsideCircle(game.children[i], circle)) {
var smoke = LK.getAsset('smoke', {
anchorX: 0.5,
anchorY: 0.5,
x: game.children[i].x,
y: game.children[i].y
});
game.addChild(smoke);
LK.getSound('smoke').play();
var smokeAnimation = LK.setInterval(function () {
smoke.alpha -= 0.1;
smoke.scale.x += 0.1;
smoke.scale.y += 0.1;
if (smoke.alpha <= 0) {
smoke.destroy();
LK.clearInterval(smokeAnimation);
}
}, 100);
game.children[i].destroy();
}
}
circle.scale.x *= 0.90;
circle.scale.y *= 0.90;
LK.effects.flashScreen(0xffff00, 200); // Flash yellow for 500ms
}
};
function spawnPowerUp() {
var powerUp = new PowerUp();
if (player) {
var side = Math.floor(Math.random() * 4);
if (side == 0) {
// Top
powerUp.x = Math.random() * 2048;
powerUp.y = -50;
} else if (side == 1) {
// Right
powerUp.x = 2048 + 50;
powerUp.y = Math.random() * 2732;
} else if (side == 2) {
// Bottom
powerUp.x = Math.random() * 2048;
powerUp.y = 2732 + 50;
} else {
// Left
powerUp.x = -50;
powerUp.y = Math.random() * 2732;
}
game.addChild(powerUp);
}
}
// Power-ups will now spawn between waves
// Spawn enemies from outside the screen
function spawnEnemy() {
var enemy = new Enemy();
var side = Math.floor(Math.random() * 4);
if (side == 0) {
// Top
enemy.x = Math.random() * 2048;
enemy.y = -50;
} else if (side == 1) {
// Right
enemy.x = 2048 + 50;
enemy.y = Math.random() * 2732;
} else if (side == 2) {
// Bottom
enemy.x = Math.random() * 2048;
enemy.y = 2732 + 50;
} else {
// Left
enemy.x = -50;
enemy.y = Math.random() * 2732;
}
game.addChild(enemy);
}
// Variables to manage waves and power-up spawning
var waveNumber = 0;
var enemiesPerWave = 5;
var enemiesSpawned = 0;
var waveInterval = 5000; // 5 seconds between waves
var enemySpeedIncrement = 0.25; // Speed increment per wave
var enemiesPerWaveIncrement = 1; // Additional enemies per wave
function startNextWave() {
waveNumber++;
enemiesSpawned = 0;
enemiesPerWave += enemiesPerWaveIncrement; // Increase number of enemies per wave
Enemy.prototype.speed += enemySpeedIncrement; // Increase enemy speed
PowerUp.prototype.speed += enemySpeedIncrement; // Increase power-up speed
LK.getSound('mosquito').play(); // Play mosquito sound every time a wave of enemies is spawned
spawnEnemiesInWave();
}
function spawnEnemiesInWave() {
if (enemiesSpawned < enemiesPerWave) {
spawnEnemy();
enemiesSpawned++;
LK.setTimeout(spawnEnemiesInWave, 500); // 0.5 seconds between enemy spawns
} else {
// Spawn power-up after the wave
LK.setTimeout(spawnPowerUp, 1000);
// Start the next wave after a delay
LK.setTimeout(startNextWave, waveInterval);
}
}
// Create a circle in the center of the screen
var circle = new Container();
var circleGraphics = circle.attachAsset('circle', {
anchorX: 0.5,
anchorY: 0.5
});
// Add dim effect to the circle
circleGraphics.alpha = 0.9;
game.addChild(circle);
circle.x = 2048 / 2;
circle.y = 2732 / 2;
// Initialize score text
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Update score text whenever the score changes
LK.on('scoreChange', function () {
scoreTxt.setText(LK.getScore());
});
// Start the first wave
startNextWave();
// Add player to the game
var player = new Player();
game.addChild(player);
player.x = 2048 / 2; // Center player horizontally
player.y = 2732 / 2; // Center player vertically;
player.update = function () {
if (this.rope) {
this.rope.height = this.y;
}
// Add pendulum effect
this.x = 2048 / 2 + Math.sin(LK.ticks / 60) * 30;
};