/****
* Classes
****/
// Bullet class for projectiles fired by towers
var Bullet = Container.expand(function (startX, startY, target) {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.target = target;
self.x = startX;
self.y = startY;
self.update = function () {
var dx = self.target.x - self.x;
var dy = self.target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 10) {
self.destroy();
self.target.hit();
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
// Calculate the angle to the target
var angle = Math.atan2(dy, dx);
// Rotate the bullet towards the target
bulletGraphics.rotation = angle + 90;
}
};
});
// Enemy class for the creatures attacking the realm
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.health = 100;
self.path = [{
x: 2010,
y: 2600
}, {
x: 1000,
y: 2600
}, {
x: 1000,
y: 200
}, {
x: 0,
y: 200
}];
self.pathIndex = 0;
self.update = function () {
var target = self.path[self.pathIndex];
var dx = target.x - self.x;
var dy = target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
self.x = target.x;
self.y = target.y;
self.pathIndex++;
if (self.pathIndex >= self.path.length) {
// Enemy reached the end, game over logic
LK.showGameOver();
}
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
self.hit = function () {
self.health -= 50;
if (self.health <= 0) {
self.destroy();
if (enemies.indexOf(self) != -1) {
enemies.splice(enemies.indexOf(self), 1);
money += 10; // Add 10 money for killed monster
}
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Tower class to represent defense towers
var Tower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('tower', {
anchorX: 0.5,
anchorY: 0.5
});
self.range = 600;
self.fireRate = 60; // Fire every 60 frames
self.fireCooldown = 0;
// The turret's firing range is hidden and not displayed on the game screen
self.update = function () {
if (self.fireCooldown < 0) {
self.fireCooldown = 0;
}
if (self.fireCooldown > 0) {
self.fireCooldown--;
}
};
self.canFire = function () {
return self.fireCooldown === 0;
};
self.fire = function (target) {
if (self.canFire()) {
var bullet = new Bullet(self.x, self.y, target);
game.addChild(bullet);
bullets.push(bullet);
self.fireCooldown = self.fireRate;
} else if (self.fireCooldown > 0) {
self.fireCooldown--;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000,
//Init game with black background
fps: 60
});
/****
* Game Code
****/
game.down = function (x, y, obj) {
if (money >= 100) {
var tower = new Tower();
tower.x = x;
tower.y = y;
game.addChild(tower);
towers.push(tower);
money -= 100;
}
};
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
game.addChild(background);
// Initialize arrays and variables
var towers = [];
var bullets = [];
var enemies = [];
var money = 100;
// Create and position towers
function createTowers() {
// No initial towers
}
// Spawn enemies at intervals
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = 2010;
enemy.y = 200;
game.addChild(enemy);
enemies.push(enemy);
}
// Create a road for the enemies to follow
function createRoad(x, y) {
if (Array.isArray(x)) {
for (var i = x[0]; i < x[1]; i += 100) {
var roadBlock = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0.5,
x: i,
y: y
});
game.addChild(roadBlock);
}
} else {
for (var i = y[0]; i < y[1]; i += 100) {
var roadBlock = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0.5,
x: x,
y: i
});
game.addChild(roadBlock);
}
}
}
createRoad(2000, [200, 2601]);
createRoad([1000, 2000], 2601);
createRoad(1000, [200, 2601]);
createRoad([0, 1000], 200);
// Game update loop
game.update = function () {
// Update towers
towers.forEach(function (tower) {
tower.update();
enemies.forEach(function (enemy) {
var dx = tower.x - enemy.x;
var dy = tower.y - enemy.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (tower.canFire() && distance < tower.range) {
tower.fire(enemy);
}
});
});
// Update bullets
bullets.forEach(function (bullet) {
bullet.update();
});
// Update enemies
enemies.forEach(function (enemy) {
enemy.update();
});
// Spawn enemies every 120 frames
if (LK.ticks % 120 === 0) {
spawnEnemy();
}
// Update the money text
moneyText.setText('Money: ' + money);
};
// Initialize game elements
createTowers();
// Create a text object to display the amount of money
var moneyText = new Text2('Money: ' + money, {
size: 50,
fill: 0xFFFFFF
});
moneyText.anchor.set(0, 1); // Sets anchor to the bottom left corner of the text.
// Add the money text to the GUI overlay at the bottom left corner of the screen
LK.gui.bottomLeft.addChild(moneyText); /****
* Classes
****/
// Bullet class for projectiles fired by towers
var Bullet = Container.expand(function (startX, startY, target) {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.target = target;
self.x = startX;
self.y = startY;
self.update = function () {
var dx = self.target.x - self.x;
var dy = self.target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 10) {
self.destroy();
self.target.hit();
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
// Calculate the angle to the target
var angle = Math.atan2(dy, dx);
// Rotate the bullet towards the target
bulletGraphics.rotation = angle + 90;
}
};
});
// Enemy class for the creatures attacking the realm
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.health = 100;
self.path = [{
x: 2010,
y: 2600
}, {
x: 1000,
y: 2600
}, {
x: 1000,
y: 200
}, {
x: 0,
y: 200
}];
self.pathIndex = 0;
self.update = function () {
var target = self.path[self.pathIndex];
var dx = target.x - self.x;
var dy = target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
self.x = target.x;
self.y = target.y;
self.pathIndex++;
if (self.pathIndex >= self.path.length) {
// Enemy reached the end, game over logic
LK.showGameOver();
}
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
self.hit = function () {
self.health -= 50;
if (self.health <= 0) {
self.destroy();
if (enemies.indexOf(self) != -1) {
enemies.splice(enemies.indexOf(self), 1);
money += 10; // Add 10 money for killed monster
}
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Tower class to represent defense towers
var Tower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('tower', {
anchorX: 0.5,
anchorY: 0.5
});
self.range = 600;
self.fireRate = 60; // Fire every 60 frames
self.fireCooldown = 0;
// The turret's firing range is hidden and not displayed on the game screen
self.update = function () {
if (self.fireCooldown < 0) {
self.fireCooldown = 0;
}
if (self.fireCooldown > 0) {
self.fireCooldown--;
}
};
self.canFire = function () {
return self.fireCooldown === 0;
};
self.fire = function (target) {
if (self.canFire()) {
var bullet = new Bullet(self.x, self.y, target);
game.addChild(bullet);
bullets.push(bullet);
self.fireCooldown = self.fireRate;
} else if (self.fireCooldown > 0) {
self.fireCooldown--;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000,
//Init game with black background
fps: 60
});
/****
* Game Code
****/
game.down = function (x, y, obj) {
if (money >= 100) {
var tower = new Tower();
tower.x = x;
tower.y = y;
game.addChild(tower);
towers.push(tower);
money -= 100;
}
};
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
game.addChild(background);
// Initialize arrays and variables
var towers = [];
var bullets = [];
var enemies = [];
var money = 100;
// Create and position towers
function createTowers() {
// No initial towers
}
// Spawn enemies at intervals
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = 2010;
enemy.y = 200;
game.addChild(enemy);
enemies.push(enemy);
}
// Create a road for the enemies to follow
function createRoad(x, y) {
if (Array.isArray(x)) {
for (var i = x[0]; i < x[1]; i += 100) {
var roadBlock = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0.5,
x: i,
y: y
});
game.addChild(roadBlock);
}
} else {
for (var i = y[0]; i < y[1]; i += 100) {
var roadBlock = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0.5,
x: x,
y: i
});
game.addChild(roadBlock);
}
}
}
createRoad(2000, [200, 2601]);
createRoad([1000, 2000], 2601);
createRoad(1000, [200, 2601]);
createRoad([0, 1000], 200);
// Game update loop
game.update = function () {
// Update towers
towers.forEach(function (tower) {
tower.update();
enemies.forEach(function (enemy) {
var dx = tower.x - enemy.x;
var dy = tower.y - enemy.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (tower.canFire() && distance < tower.range) {
tower.fire(enemy);
}
});
});
// Update bullets
bullets.forEach(function (bullet) {
bullet.update();
});
// Update enemies
enemies.forEach(function (enemy) {
enemy.update();
});
// Spawn enemies every 120 frames
if (LK.ticks % 120 === 0) {
spawnEnemy();
}
// Update the money text
moneyText.setText('Money: ' + money);
};
// Initialize game elements
createTowers();
// Create a text object to display the amount of money
var moneyText = new Text2('Money: ' + money, {
size: 50,
fill: 0xFFFFFF
});
moneyText.anchor.set(0, 1); // Sets anchor to the bottom left corner of the text.
// Add the money text to the GUI overlay at the bottom left corner of the screen
LK.gui.bottomLeft.addChild(moneyText);
No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat. Goblin. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat. Tower with ballista. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
One bow arrow facing upwards. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
texture of the road made of large stones like tiles for a game. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Top view. Texture for 2D games. Green meadow.