/****
* Classes
****/
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.createAsset('hero', 'Hero character', .5, .5);
self.hitPoints = 3;
self.move = function () {};
self.shoot = function () {};
self.dashToPointAndBack = function (targetX, targetY, onCompletion) {
if (self.isDashing) {
return;
}
self.isDashing = true;
var originalX = 2048 * 0.1;
var originalY = 2732 / 2;
var dashSpeed = 30;
var returnSpeed = 10;
var dashComplete = false;
self.isMovingRight = false;
self.move = function () {
if (!dashComplete) {
self.isMovingRight = targetX - self.x > 0;
}
if (!dashComplete) {
if (Math.abs(self.x - targetX) > dashSpeed || Math.abs(self.y - targetY) > dashSpeed) {
self.x += (targetX - self.x > 0 ? 1 : -1) * dashSpeed;
self.y += (targetY - self.y > 0 ? 1 : -1) * dashSpeed;
} else {
dashComplete = true;
self.isMovingRight = false;
}
} else {
if (Math.abs(self.x - originalX) > returnSpeed || Math.abs(self.y - originalY) > returnSpeed) {
self.x += (originalX - self.x > 0 ? 1 : -1) * returnSpeed;
self.y += (originalY - self.y > 0 ? 1 : -1) * returnSpeed;
} else {
self.x = originalX;
self.y = originalY;
self.move = function () {};
if (typeof onCompletion === 'function') {
onCompletion();
}
self.isDashing = false;
}
}
};
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.createAsset('enemy', 'Enemy character', .5, .5);
self.speed = -15;
self.move = function () {
self.x += self.speed;
self.y += Math.random() > 0.5 ? 10 : -10;
};
self.attack = function () {};
});
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet', .5, .5);
self.move = function () {};
});
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('enemyBullet', 'Enemy Bullet', .5, .5);
self.move = function () {};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Create a background container to hold the green field asset
var backgroundContainer = new Container();
var greenFieldBackground = backgroundContainer.createAsset('greenField', 'Beautiful green field with grass and a bit of sky', 0, 0);
greenFieldBackground.width = 2048;
greenFieldBackground.height = 2732;
game.addChild(backgroundContainer);
backgroundContainer.zIndex = -1;
// Display for hit points
var hitPointsDisplay = new Text2('Hit Points: 3', {
size: 50,
fill: "#ffffff"
});
hitPointsDisplay.anchor.set(0, 0);
LK.gui.topLeft.addChild(hitPointsDisplay);
// Initialize Score display
var scoreDisplay = new Text2('Score: 0', {
size: 50,
fill: "#ffffff"
});
scoreDisplay.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreDisplay);
var heroes = [];
var enemies = [];
var heroBullets = [];
var enemyBullets = [];
var hero = game.addChild(new Hero());
hero.x = 2048 * 0.1;
hero.y = 2732 / 2;
heroes.push(hero);
game.on('down', function (obj) {
var event = obj.event;
var position = event.getLocalPosition(game);
if (!hero.intersects({
x: position.x,
y: position.y,
width: 1,
height: 1
})) {
hero.dashToPointAndBack(position.x, position.y, function () {
console.log('Hero has returned to the original position.');
});
}
});
var enemy = game.addChild(new Enemy());
enemy.x = 2048 - enemy.width / 2;
enemy.y = 2732 - enemy.height / 2;
enemies.push(enemy);
var startTime = Date.now();
LK.on('tick', function () {
// Update Score display
var elapsedSeconds = Math.floor((Date.now() - startTime) / 1000);
scoreDisplay.setText('Score: ' + elapsedSeconds);
// Spawn enemies every second
var spawnInterval = Math.max(20, 60 - Math.floor(LK.ticks / 1800));
if (LK.ticks % spawnInterval == 0) {
var newEnemy = game.addChild(new Enemy());
newEnemy.x = 2048;
newEnemy.y = Math.random() * 2732;
enemies.push(newEnemy);
}
heroes.forEach(function (hero) {
hero.move();
enemies.forEach(function (enemy, enemyIndex) {
if (hero.intersects(enemy) && hero.isMovingRight) {
enemy.destroy();
enemies.splice(enemyIndex, 1);
}
});
});
enemies.forEach(function (enemy, index) {
enemy.move();
enemy.attack();
if (enemy.x < 0) {
hero.hitPoints--;
hitPointsDisplay.setText('Hit Points: ' + hero.hitPoints);
enemy.destroy();
enemies.splice(index, 1);
if (hero.hitPoints <= 0) {
LK.showGameOver();
}
}
});
heroBullets.forEach(function (bullet) {
bullet.move();
});
enemyBullets.forEach(function (bullet) {
bullet.move();
});
});