var Explosion = Container.expand(function () {
var self = Container.call(this);
var explosionGraphics = self.createAsset('explosion', 'Explosion Graphics', .5, .5);
self.animationDuration = 10;
self.tickCount = 0;
self.on('tick', function () {
self.tickCount++;
if (self.tickCount > self.animationDuration) {
self.destroy();
}
});
});
var ParallaxBackground = Container.expand(function (layers, speed) {
var self = Container.call(this);
self.layers = layers.map(function (layerInfo) {
var layer = self.createAsset(layerInfo.id, layerInfo.description, 0.5, 0.5);
layer.speed = layerInfo.speed;
self.addChild(layer);
return layer;
});
self.move = function () {
self.layers.forEach(function (layer) {
layer.y += layer.speed;
if (layer.y >= 2732 + 1000) {
layer.y -= 2732 * 2;
}
layer.x = 2048 - layer.width + 1000;
});
};
});
var HealthPickup = Container.expand(function () {
var self = Container.call(this);
self.creationTime = LK.ticks;
var pickupGraphics = self.createAsset('healthPickup', 'Health Pickup', .5, .5);
self.restoreAmount = 10;
self.move = function () {
self.y += 2;
};
self.applyToHero = function (hero) {
hero.health = Math.min(hero.health + self.restoreAmount, hero.healthBar.maxHealth);
hero.healthBar.updateHealth(hero.health);
};
});
var HealthBar = Container.expand(function (maxHealth) {
var self = Container.call(this);
var background = self.createAsset('healthBarBackground', 'Health Bar Background', 0, 0.5);
var foreground = self.createAsset('healthBarForeground', 'Health Bar Foreground', 0, 0.5);
self.setMaxHealth = function (maxHealth) {
self.maxHealth = maxHealth;
self.currentHealth = maxHealth;
};
self.updateHealth = function (currentHealth) {
self.currentHealth = currentHealth;
var scale = currentHealth / self.maxHealth;
foreground.scale.x = scale > 0 ? scale : 0;
};
self.setMaxHealth(maxHealth);
});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.createAsset('hero', 'Hero character', .5, .5);
self.healthBar = new HealthBar(100);
self.healthBar.x = 10;
self.healthBar.y = 10;
LK.gui.topLeft.addChild(self.healthBar);
self.speed = 5;
self.health = 100;
self.healthBar.setMaxHealth(self.health);
self.healthBar.updateHealth(self.health);
self.move = function () {
if (self.y + self.speed > 0) {
if (self.y + self.speed < 2732) {
self.y += self.speed;
}
}
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.createAsset('enemy', 'Enemy character', .5, .5);
self.baseSpeed = -3.6;
self.move = function () {
self.y -= self.baseSpeed;
};
});
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet', .5, .5);
self.speed = 12;
self.move = function () {
self.y -= self.speed;
};
});
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('enemyBullet', 'Enemy Bullet', .5, .5);
self.baseSpeed = -12;
self.move = function () {
self.y -= self.baseSpeed;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var backgroundLayers = [{
id: 'backgroundFar',
description: 'Far Background',
speed: 0.5
}, {
id: 'backgroundMid',
description: 'Mid Background',
speed: 1
}, {
id: 'backgroundNear',
description: 'Near Background',
speed: 1.5
}];
var parallaxBackground = self.addChild(new ParallaxBackground(backgroundLayers, 2));
var hero = self.addChild(new Hero());
var enemies = [];
var heroBullets = [];
var enemyBullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
LK.gui.topCenter.addChild(scoreTxt);
hero.x = 200;
hero.y = 2732 / 2;
LK.on('tick', function () {
var LEFT = 37, UP = 38, RIGHT = 39, DOWN = 40;
LK.stage.on('move', function (obj) {
var touchPos = obj.event.getLocalPosition(self);
hero.x = touchPos.x;
hero.y = touchPos.y;
});
if (hero.x < 0) hero.x = 0;
if (hero.x > 2048 - hero.width) hero.x = 2048 - hero.width;
if (hero.y < 0) hero.y = 0;
if (hero.y > 2732 - hero.height) hero.y = 2732 - hero.height;
parallaxBackground.move();
hero.move();
self.children.forEach(function (child) {
if (child instanceof HealthPickup) {
if (hero.intersects(child)) {
child.applyToHero(hero);
child.destroy();
} else if (child.y > 2732 + 50 || LK.ticks - child.creationTime > 300) {
child.destroy();
}
} else if (child instanceof Explosion && LK.ticks % 5 === 0) {
child.destroy();
}
});
for (var i = 0; i < enemies.length; i++) {
enemies[i].move();
if (hero.intersects(enemies[i])) {
hero.health -= 20;
hero.healthBar.updateHealth(hero.health);
if (hero.health <= 0) {
LK.showGameOver();
}
enemies[i].destroy();
enemies.splice(i, 1);
continue;
}
if (enemies[i].y > 2732 + 50) {
enemies[i].destroy();
enemies.splice(i, 1);
scoreTxt.setText(score);
}
}
for (var i = 0; i < heroBullets.length; i++) {
heroBullets[i].move();
for (var j = 0; j < enemyBullets.length; j++) {
if (heroBullets[i].intersects(enemyBullets[j])) {
heroBullets[i].destroy();
enemyBullets[j].destroy();
heroBullets.splice(i, 1);
enemyBullets.splice(j, 1);
i--;
break;
}
}
for (var j = 0; j < enemies.length; j++) {
if (heroBullets[i] && heroBullets[i].intersects(enemies[j])) {
var explosion = new Explosion();
explosion.x = enemies[j].x;
explosion.y = enemies[j].y;
self.addChild(explosion);
enemies[j].destroy();
if (Math.random() < 0.05) {
var pickup = new HealthPickup();
pickup.x = enemies[j].x;
pickup.y = enemies[j].y - pickup.height;
self.addChild(pickup);
}
enemies.splice(j, 1);
score += 10;
scoreTxt.setText(score);
heroBullets[i].destroy();
heroBullets.splice(i, 1);
i--;
break;
}
}
if (heroBullets[i] && heroBullets[i].y < -50) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
for (var i = 0; i < enemyBullets.length; i++) {
enemyBullets[i].move();
if (hero.intersects(enemyBullets[i])) {
hero.health -= 20;
hero.healthBar.updateHealth(hero.health);
if (hero.health <= 0) {
LK.showGameOver();
}
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
continue;
}
if (enemyBullets[i].y > 2732 + 50) {
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
}
}
});
hero.canShoot = true;
hero.shootCooldown = 12;
hero.lastShotTime = 0;
hero.on('down', function (obj) {
if (hero.canShoot) {
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y;
heroBullets.push(bullet);
self.addChild(bullet);
hero.canShoot = false;
hero.lastShotTime = LK.ticks;
}
});
LK.on('tick', function () {
if (!hero.canShoot && LK.ticks - hero.lastShotTime > hero.shootCooldown) {
hero.canShoot = true;
}
});
var startTime = Date.now();
var enemySpawnInterval = 2000;
var maxEnemies = 5;
var enemySpeedIncreaseTimer = LK.setInterval(function () {
Enemy.prototype.baseSpeed *= 1.05;
EnemyBullet.prototype.baseSpeed *= 1.05;
}, 30000);
var enemySpawnTimer = LK.setInterval(function () {
var currentTime = Date.now();
var elapsedTime = (currentTime - startTime) / 60000;
maxEnemies = 5 + Math.floor(elapsedTime);
if (enemies.length < maxEnemies) {
var enemy = new Enemy();
var enemyWidth = enemy.width;
enemy.x = Math.random() * (2048 - enemyWidth) + enemyWidth / 2;
enemy.y = -50;
enemies.push(enemy);
self.addChild(enemy);
}
}, enemySpawnInterval);
LK.setInterval(function () {
if (Math.random() < 0.1) {
var pickup = new HealthPickup();
pickup.x = Math.random() * (2048 - pickup.width) + pickup.width / 2;
pickup.y = -50;
self.addChild(pickup);
} else {
var enemyIndex = Math.floor(Math.random() * enemies.length);
var bullet = new EnemyBullet();
bullet.x = enemies[enemyIndex].x;
bullet.y = enemies[enemyIndex].y;
enemyBullets.push(bullet);
self.addChild(bullet);
}
}, 2000);
});
Create a plain red health bar Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Create a plain empty health bar Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Create a top down view of a single Earth-like planet Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A background image of some stars in space Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A background image of asteroids in space Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A realistic looking explosion, viewed from above. Bright coloured fire Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a double missile pointing vertically, with fire coming out of the rear Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A health pickup icon Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A cool looking spaceship viewed from above Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A round energy based projectile. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.