/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 8; self.damage = 20; self.owner = null; self.update = function () { self.x += Math.cos(self.rotation) * self.speed; self.y += Math.sin(self.rotation) * self.speed; // Remove if off screen if (self.x < -50 || self.x > 2098 || self.y < -50 || self.y > 2782) { self.destroy(); for (var i = 0; i < bullets.length; i++) { if (bullets[i] === self) { bullets.splice(i, 1); break; } } } }; return self; }); var Cover = Container.expand(function () { var self = Container.call(this); var coverGraphics = self.attachAsset('cover', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.health = 100; self.maxHealth = 100; self.ammo = 30; self.speed = 2; self.lastShot = 0; self.fireRate = 800; self.lastDirectionChange = 0; self.moveDirection = Math.random() * Math.PI * 2; self.takeDamage = function (damage) { self.health -= damage; LK.effects.flashObject(self, 0xff0000, 200); if (self.health <= 0) { self.die(); } }; self.die = function () { LK.setScore(LK.getScore() + 10); self.destroy(); for (var i = 0; i < enemies.length; i++) { if (enemies[i] === self) { enemies.splice(i, 1); break; } } }; self.update = function () { // Simple AI behavior var dx = player.x - self.x; var dy = player.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); // If player is close, shoot if (distance < 400 && Date.now() - self.lastShot > self.fireRate) { self.lastShot = Date.now(); self.rotation = Math.atan2(dy, dx); var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y; bullet.rotation = self.rotation; bullet.owner = self; bullets.push(bullet); game.addChild(bullet); } // Random movement if (Date.now() - self.lastDirectionChange > 2000) { self.moveDirection = Math.random() * Math.PI * 2; self.lastDirectionChange = Date.now(); } var moveX = Math.cos(self.moveDirection) * self.speed; var moveY = Math.sin(self.moveDirection) * self.speed; self.x += moveX; self.y += moveY; // Keep within bounds if (self.x < 40) { self.x = 40; self.moveDirection = Math.PI - self.moveDirection; } if (self.x > 2048 - 40) { self.x = 2048 - 40; self.moveDirection = Math.PI - self.moveDirection; } if (self.y < 40) { self.y = 40; self.moveDirection = -self.moveDirection; } if (self.y > 2732 - 40) { self.y = 2732 - 40; self.moveDirection = -self.moveDirection; } // Check wall collisions for (var i = 0; i < walls.length; i++) { if (self.intersects(walls[i])) { self.x -= moveX; self.y -= moveY; self.moveDirection += Math.PI; break; } } }; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.health = 100; self.maxHealth = 100; self.ammo = 30; self.maxAmmo = 30; self.speed = 4; self.weaponType = 'assault'; // 'assault' or 'shotgun' self.isReloading = false; self.lastShot = 0; self.fireRate = 200; // milliseconds between shots self.moveX = 0; self.moveY = 0; self.takeDamage = function (damage) { self.health -= damage; if (self.health <= 0) { self.health = 0; self.die(); } }; self.die = function () { LK.effects.flashObject(self, 0xff0000, 1000); // Respawn after 3 seconds LK.setTimeout(function () { self.respawn(); }, 3000); }; self.respawn = function () { self.health = self.maxHealth; self.ammo = self.maxAmmo; // Find safe spawn position self.x = Math.random() * (2048 - 200) + 100; self.y = Math.random() * (2732 - 200) + 100; }; self.reload = function () { if (!self.isReloading && self.ammo < self.maxAmmo) { self.isReloading = true; LK.getSound('reload').play(); LK.setTimeout(function () { self.ammo = self.maxAmmo; self.isReloading = false; }, 2000); } }; self.shoot = function () { if (self.ammo > 0 && !self.isReloading && Date.now() - self.lastShot > self.fireRate) { self.ammo--; self.lastShot = Date.now(); LK.getSound('gunshot').play(); // Create bullet var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y; bullet.rotation = self.rotation; bullet.owner = self; bullets.push(bullet); game.addChild(bullet); } }; self.update = function () { // Apply movement self.x += self.moveX; self.y += self.moveY; // Keep within bounds if (self.x < 40) { self.x = 40; } if (self.x > 2048 - 40) { self.x = 2048 - 40; } if (self.y < 40) { self.y = 40; } if (self.y > 2732 - 40) { self.y = 2732 - 40; } // Check wall collisions for (var i = 0; i < walls.length; i++) { if (self.intersects(walls[i])) { self.x -= self.moveX; self.y -= self.moveY; break; } } }; return self; }); var Wall = Container.expand(function () { var self = Container.call(this); var wallGraphics = self.attachAsset('wall', { anchorX: 0.5, anchorY: 0.5 }); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x2a2a2a }); /**** * Game Code ****/ // Game variables var player; var enemies = []; var bullets = []; var walls = []; var covers = []; var gameMode = 'freeForAll'; // 'teamDeathmatch' or 'freeForAll' var maxEnemies = 5; // UI elements var healthBar; var ammoBar; var minimap; var crosshair; var fireButton; var reloadButton; var joystick; var joystickKnob; var scoreText; // Input handling var joystickActive = false; var joystickCenter = { x: 0, y: 0 }; var fireButtonPressed = false; // Initialize player player = game.addChild(new Player()); player.x = 1024; player.y = 1366; // Create map walls for (var i = 0; i < 8; i++) { var wall = game.addChild(new Wall()); wall.x = Math.random() * (2048 - 200) + 100; wall.y = Math.random() * (2732 - 200) + 100; walls.push(wall); } // Create cover objects for (var i = 0; i < 12; i++) { var cover = game.addChild(new Cover()); cover.x = Math.random() * (2048 - 150) + 75; cover.y = Math.random() * (2732 - 100) + 50; covers.push(cover); } // Create UI elements healthBar = LK.gui.bottomLeft.addChild(LK.getAsset('healthBar', { x: 20, y: -80 })); ammoBar = LK.gui.bottomLeft.addChild(LK.getAsset('ammoBar', { x: 20, y: -50 })); scoreText = LK.gui.top.addChild(new Text2('Score: 0', { size: 60, fill: '#ffffff' })); scoreText.anchor.set(0.5, 0); // Crosshair crosshair = game.addChild(LK.getAsset('crosshair', { anchorX: 0.5, anchorY: 0.5 })); crosshair.alpha = 0.7; // Mobile controls joystick = LK.gui.bottomLeft.addChild(LK.getAsset('joystick', { anchorX: 0.5, anchorY: 0.5, x: 100, y: -150 })); joystick.alpha = 0.5; joystickKnob = LK.gui.bottomLeft.addChild(LK.getAsset('joystickKnob', { anchorX: 0.5, anchorY: 0.5, x: 100, y: -150 })); fireButton = LK.gui.bottomRight.addChild(LK.getAsset('fireButton', { anchorX: 0.5, anchorY: 0.5, x: -100, y: -100 })); fireButton.alpha = 0.7; reloadButton = LK.gui.bottomRight.addChild(LK.getAsset('reloadButton', { anchorX: 0.5, anchorY: 0.5, x: -100, y: -200 })); reloadButton.alpha = 0.7; // Minimap minimap = LK.gui.topRight.addChild(LK.getAsset('minimap', { anchorX: 1, anchorY: 0, x: -20, y: 20 })); minimap.alpha = 0.8; // Joystick controls joystick.down = function (x, y, obj) { joystickActive = true; joystickCenter.x = x; joystickCenter.y = y; }; joystick.up = function (x, y, obj) { joystickActive = false; joystickKnob.x = 100; joystickKnob.y = -150; player.moveX = 0; player.moveY = 0; }; joystick.move = function (x, y, obj) { if (joystickActive) { var dx = x - joystickCenter.x; var dy = y - joystickCenter.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > 50) { dx = dx / distance * 50; dy = dy / distance * 50; } joystickKnob.x = 100 + dx; joystickKnob.y = -150 + dy; player.moveX = dx * 0.1; player.moveY = dy * 0.1; } }; // Fire button fireButton.down = function (x, y, obj) { fireButtonPressed = true; player.shoot(); }; fireButton.up = function (x, y, obj) { fireButtonPressed = false; }; // Reload button reloadButton.down = function (x, y, obj) { player.reload(); }; // Game aiming game.move = function (x, y, obj) { // Update crosshair position crosshair.x = x; crosshair.y = y; // Update player rotation to face crosshair var dx = x - player.x; var dy = y - player.y; player.rotation = Math.atan2(dy, dx); }; // Spawn enemies function spawnEnemy() { if (enemies.length < maxEnemies) { var enemy = game.addChild(new Enemy()); enemy.x = Math.random() * (2048 - 80) + 40; enemy.y = Math.random() * (2732 - 80) + 40; enemies.push(enemy); } } // Update UI function updateUI() { // Update health bar var healthPercent = player.health / player.maxHealth; healthBar.scaleX = healthPercent; healthBar.tint = healthPercent > 0.5 ? 0x00ff00 : healthPercent > 0.25 ? 0xffff00 : 0xff0000; // Update ammo bar var ammoPercent = player.ammo / player.maxAmmo; ammoBar.scaleX = ammoPercent; // Update score scoreText.setText('Score: ' + LK.getScore()); } // Game update loop game.update = function () { // Spawn enemies periodically if (LK.ticks % 300 === 0) { spawnEnemy(); } // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { var bullet = bullets[i]; // Check collision with player if (bullet.owner !== player && bullet.intersects(player)) { player.takeDamage(bullet.damage); LK.getSound('hit').play(); bullet.destroy(); bullets.splice(i, 1); continue; } // Check collision with enemies for (var j = 0; j < enemies.length; j++) { if (bullet.owner !== enemies[j] && bullet.intersects(enemies[j])) { enemies[j].takeDamage(bullet.damage); LK.getSound('hit').play(); bullet.destroy(); bullets.splice(i, 1); break; } } // Check collision with walls for (var k = 0; k < walls.length; k++) { if (bullet.intersects(walls[k])) { bullet.destroy(); bullets.splice(i, 1); break; } } // Check collision with covers for (var l = 0; l < covers.length; l++) { if (bullet.intersects(covers[l])) { bullet.destroy(); bullets.splice(i, 1); break; } } } // Auto-fire if fire button is held if (fireButtonPressed) { player.shoot(); } // Update UI updateUI(); // Check game over condition if (player.health <= 0) { LK.showGameOver(); } // Check win condition (score-based) if (LK.getScore() >= 100) { LK.showYouWin(); } }; // Start background music LK.playMusic('battleMusic');
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.damage = 20;
self.owner = null;
self.update = function () {
self.x += Math.cos(self.rotation) * self.speed;
self.y += Math.sin(self.rotation) * self.speed;
// Remove if off screen
if (self.x < -50 || self.x > 2098 || self.y < -50 || self.y > 2782) {
self.destroy();
for (var i = 0; i < bullets.length; i++) {
if (bullets[i] === self) {
bullets.splice(i, 1);
break;
}
}
}
};
return self;
});
var Cover = Container.expand(function () {
var self = Container.call(this);
var coverGraphics = self.attachAsset('cover', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.maxHealth = 100;
self.ammo = 30;
self.speed = 2;
self.lastShot = 0;
self.fireRate = 800;
self.lastDirectionChange = 0;
self.moveDirection = Math.random() * Math.PI * 2;
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xff0000, 200);
if (self.health <= 0) {
self.die();
}
};
self.die = function () {
LK.setScore(LK.getScore() + 10);
self.destroy();
for (var i = 0; i < enemies.length; i++) {
if (enemies[i] === self) {
enemies.splice(i, 1);
break;
}
}
};
self.update = function () {
// Simple AI behavior
var dx = player.x - self.x;
var dy = player.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// If player is close, shoot
if (distance < 400 && Date.now() - self.lastShot > self.fireRate) {
self.lastShot = Date.now();
self.rotation = Math.atan2(dy, dx);
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.rotation = self.rotation;
bullet.owner = self;
bullets.push(bullet);
game.addChild(bullet);
}
// Random movement
if (Date.now() - self.lastDirectionChange > 2000) {
self.moveDirection = Math.random() * Math.PI * 2;
self.lastDirectionChange = Date.now();
}
var moveX = Math.cos(self.moveDirection) * self.speed;
var moveY = Math.sin(self.moveDirection) * self.speed;
self.x += moveX;
self.y += moveY;
// Keep within bounds
if (self.x < 40) {
self.x = 40;
self.moveDirection = Math.PI - self.moveDirection;
}
if (self.x > 2048 - 40) {
self.x = 2048 - 40;
self.moveDirection = Math.PI - self.moveDirection;
}
if (self.y < 40) {
self.y = 40;
self.moveDirection = -self.moveDirection;
}
if (self.y > 2732 - 40) {
self.y = 2732 - 40;
self.moveDirection = -self.moveDirection;
}
// Check wall collisions
for (var i = 0; i < walls.length; i++) {
if (self.intersects(walls[i])) {
self.x -= moveX;
self.y -= moveY;
self.moveDirection += Math.PI;
break;
}
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.maxHealth = 100;
self.ammo = 30;
self.maxAmmo = 30;
self.speed = 4;
self.weaponType = 'assault'; // 'assault' or 'shotgun'
self.isReloading = false;
self.lastShot = 0;
self.fireRate = 200; // milliseconds between shots
self.moveX = 0;
self.moveY = 0;
self.takeDamage = function (damage) {
self.health -= damage;
if (self.health <= 0) {
self.health = 0;
self.die();
}
};
self.die = function () {
LK.effects.flashObject(self, 0xff0000, 1000);
// Respawn after 3 seconds
LK.setTimeout(function () {
self.respawn();
}, 3000);
};
self.respawn = function () {
self.health = self.maxHealth;
self.ammo = self.maxAmmo;
// Find safe spawn position
self.x = Math.random() * (2048 - 200) + 100;
self.y = Math.random() * (2732 - 200) + 100;
};
self.reload = function () {
if (!self.isReloading && self.ammo < self.maxAmmo) {
self.isReloading = true;
LK.getSound('reload').play();
LK.setTimeout(function () {
self.ammo = self.maxAmmo;
self.isReloading = false;
}, 2000);
}
};
self.shoot = function () {
if (self.ammo > 0 && !self.isReloading && Date.now() - self.lastShot > self.fireRate) {
self.ammo--;
self.lastShot = Date.now();
LK.getSound('gunshot').play();
// Create bullet
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.rotation = self.rotation;
bullet.owner = self;
bullets.push(bullet);
game.addChild(bullet);
}
};
self.update = function () {
// Apply movement
self.x += self.moveX;
self.y += self.moveY;
// Keep within bounds
if (self.x < 40) {
self.x = 40;
}
if (self.x > 2048 - 40) {
self.x = 2048 - 40;
}
if (self.y < 40) {
self.y = 40;
}
if (self.y > 2732 - 40) {
self.y = 2732 - 40;
}
// Check wall collisions
for (var i = 0; i < walls.length; i++) {
if (self.intersects(walls[i])) {
self.x -= self.moveX;
self.y -= self.moveY;
break;
}
}
};
return self;
});
var Wall = Container.expand(function () {
var self = Container.call(this);
var wallGraphics = self.attachAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2a2a2a
});
/****
* Game Code
****/
// Game variables
var player;
var enemies = [];
var bullets = [];
var walls = [];
var covers = [];
var gameMode = 'freeForAll'; // 'teamDeathmatch' or 'freeForAll'
var maxEnemies = 5;
// UI elements
var healthBar;
var ammoBar;
var minimap;
var crosshair;
var fireButton;
var reloadButton;
var joystick;
var joystickKnob;
var scoreText;
// Input handling
var joystickActive = false;
var joystickCenter = {
x: 0,
y: 0
};
var fireButtonPressed = false;
// Initialize player
player = game.addChild(new Player());
player.x = 1024;
player.y = 1366;
// Create map walls
for (var i = 0; i < 8; i++) {
var wall = game.addChild(new Wall());
wall.x = Math.random() * (2048 - 200) + 100;
wall.y = Math.random() * (2732 - 200) + 100;
walls.push(wall);
}
// Create cover objects
for (var i = 0; i < 12; i++) {
var cover = game.addChild(new Cover());
cover.x = Math.random() * (2048 - 150) + 75;
cover.y = Math.random() * (2732 - 100) + 50;
covers.push(cover);
}
// Create UI elements
healthBar = LK.gui.bottomLeft.addChild(LK.getAsset('healthBar', {
x: 20,
y: -80
}));
ammoBar = LK.gui.bottomLeft.addChild(LK.getAsset('ammoBar', {
x: 20,
y: -50
}));
scoreText = LK.gui.top.addChild(new Text2('Score: 0', {
size: 60,
fill: '#ffffff'
}));
scoreText.anchor.set(0.5, 0);
// Crosshair
crosshair = game.addChild(LK.getAsset('crosshair', {
anchorX: 0.5,
anchorY: 0.5
}));
crosshair.alpha = 0.7;
// Mobile controls
joystick = LK.gui.bottomLeft.addChild(LK.getAsset('joystick', {
anchorX: 0.5,
anchorY: 0.5,
x: 100,
y: -150
}));
joystick.alpha = 0.5;
joystickKnob = LK.gui.bottomLeft.addChild(LK.getAsset('joystickKnob', {
anchorX: 0.5,
anchorY: 0.5,
x: 100,
y: -150
}));
fireButton = LK.gui.bottomRight.addChild(LK.getAsset('fireButton', {
anchorX: 0.5,
anchorY: 0.5,
x: -100,
y: -100
}));
fireButton.alpha = 0.7;
reloadButton = LK.gui.bottomRight.addChild(LK.getAsset('reloadButton', {
anchorX: 0.5,
anchorY: 0.5,
x: -100,
y: -200
}));
reloadButton.alpha = 0.7;
// Minimap
minimap = LK.gui.topRight.addChild(LK.getAsset('minimap', {
anchorX: 1,
anchorY: 0,
x: -20,
y: 20
}));
minimap.alpha = 0.8;
// Joystick controls
joystick.down = function (x, y, obj) {
joystickActive = true;
joystickCenter.x = x;
joystickCenter.y = y;
};
joystick.up = function (x, y, obj) {
joystickActive = false;
joystickKnob.x = 100;
joystickKnob.y = -150;
player.moveX = 0;
player.moveY = 0;
};
joystick.move = function (x, y, obj) {
if (joystickActive) {
var dx = x - joystickCenter.x;
var dy = y - joystickCenter.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 50) {
dx = dx / distance * 50;
dy = dy / distance * 50;
}
joystickKnob.x = 100 + dx;
joystickKnob.y = -150 + dy;
player.moveX = dx * 0.1;
player.moveY = dy * 0.1;
}
};
// Fire button
fireButton.down = function (x, y, obj) {
fireButtonPressed = true;
player.shoot();
};
fireButton.up = function (x, y, obj) {
fireButtonPressed = false;
};
// Reload button
reloadButton.down = function (x, y, obj) {
player.reload();
};
// Game aiming
game.move = function (x, y, obj) {
// Update crosshair position
crosshair.x = x;
crosshair.y = y;
// Update player rotation to face crosshair
var dx = x - player.x;
var dy = y - player.y;
player.rotation = Math.atan2(dy, dx);
};
// Spawn enemies
function spawnEnemy() {
if (enemies.length < maxEnemies) {
var enemy = game.addChild(new Enemy());
enemy.x = Math.random() * (2048 - 80) + 40;
enemy.y = Math.random() * (2732 - 80) + 40;
enemies.push(enemy);
}
}
// Update UI
function updateUI() {
// Update health bar
var healthPercent = player.health / player.maxHealth;
healthBar.scaleX = healthPercent;
healthBar.tint = healthPercent > 0.5 ? 0x00ff00 : healthPercent > 0.25 ? 0xffff00 : 0xff0000;
// Update ammo bar
var ammoPercent = player.ammo / player.maxAmmo;
ammoBar.scaleX = ammoPercent;
// Update score
scoreText.setText('Score: ' + LK.getScore());
}
// Game update loop
game.update = function () {
// Spawn enemies periodically
if (LK.ticks % 300 === 0) {
spawnEnemy();
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// Check collision with player
if (bullet.owner !== player && bullet.intersects(player)) {
player.takeDamage(bullet.damage);
LK.getSound('hit').play();
bullet.destroy();
bullets.splice(i, 1);
continue;
}
// Check collision with enemies
for (var j = 0; j < enemies.length; j++) {
if (bullet.owner !== enemies[j] && bullet.intersects(enemies[j])) {
enemies[j].takeDamage(bullet.damage);
LK.getSound('hit').play();
bullet.destroy();
bullets.splice(i, 1);
break;
}
}
// Check collision with walls
for (var k = 0; k < walls.length; k++) {
if (bullet.intersects(walls[k])) {
bullet.destroy();
bullets.splice(i, 1);
break;
}
}
// Check collision with covers
for (var l = 0; l < covers.length; l++) {
if (bullet.intersects(covers[l])) {
bullet.destroy();
bullets.splice(i, 1);
break;
}
}
}
// Auto-fire if fire button is held
if (fireButtonPressed) {
player.shoot();
}
// Update UI
updateUI();
// Check game over condition
if (player.health <= 0) {
LK.showGameOver();
}
// Check win condition (score-based)
if (LK.getScore() >= 100) {
LK.showYouWin();
}
};
// Start background music
LK.playMusic('battleMusic');