/****
* 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.target.hit();
self.destroy();
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
});
// 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: 2048,
y: 2732
}, {
x: 1024,
y: 2732
}, {
x: 1024,
y: 0
}, {
x: 0,
y: 0
}];
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();
enemies.splice(enemies.indexOf(self), 1);
}
};
});
//<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 = 300;
self.fireRate = 60; // Fire every 60 frames
self.fireCooldown = 0;
self.update = function () {
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;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var towers = [];
var bullets = [];
var enemies = [];
// Create and position towers
function createTowers() {
var tower1 = new Tower();
tower1.x = 500;
tower1.y = 1366;
game.addChild(tower1);
towers.push(tower1);
var tower2 = new Tower();
tower2.x = 1500;
tower2.y = 1366;
game.addChild(tower2);
towers.push(tower2);
}
// Spawn enemies at intervals
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = Math.random() * 2732;
game.addChild(enemy);
enemies.push(enemy);
}
// Create a road for the enemies to follow
function createRoad() {
for (var i = 0; i < 2732; i += 100) {
var roadBlock = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: i
});
game.addChild(roadBlock);
}
}
createRoad();
// Game update loop
game.update = function () {
// Update towers
towers.forEach(function (tower) {
tower.update();
enemies.forEach(function (enemy) {
if (tower.canFire() && Math.abs(tower.x - enemy.x) < 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();
}
};
// Initialize game elements
createTowers(); ===================================================================
--- original.js
+++ change.js
@@ -138,8 +138,21 @@
enemy.y = Math.random() * 2732;
game.addChild(enemy);
enemies.push(enemy);
}
+// Create a road for the enemies to follow
+function createRoad() {
+ for (var i = 0; i < 2732; i += 100) {
+ var roadBlock = LK.getAsset('road', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 1024,
+ y: i
+ });
+ game.addChild(roadBlock);
+ }
+}
+createRoad();
// Game update loop
game.update = function () {
// Update towers
towers.forEach(function (tower) {
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.