/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Initialize Game ****/ // No sandbox drag-and-drop classes needed for Free Fire shooter var game = new LK.Game({ backgroundColor: 0xf0f0f0 }); /**** * Game Code ****/ // --- Free Fire Top-Down Shooter Game State --- var player = null; var enemies = []; var bullets = []; var score = 0; var gameOver = false; var lastTouchX = 0; var lastTouchY = 0; var shootCooldown = 0; var enemySpawnCooldown = 0; // --- UI --- var titleTxt = new Text2('Free Fire', { size: 90, fill: 0x222222 }); titleTxt.anchor.set(0.5, 0); LK.gui.top.addChild(titleTxt); var scoreTxt = new Text2('0', { size: 70, fill: 0x3A8EE6 }); scoreTxt.anchor.set(0.5, 0); scoreTxt.y = 0; LK.gui.top.addChild(scoreTxt); var infoTxt = new Text2('Drag to move. Tap to shoot!', { size: 60, fill: 0x444444 }); infoTxt.anchor.set(0.5, 0); infoTxt.y = 0; LK.gui.bottom.addChild(infoTxt); // --- Background --- var bgGrid = LK.getAsset('bgGrid', { anchorX: 0, anchorY: 0, x: 0, y: 0 }); game.addChild(bgGrid); // --- Player --- function createPlayer() { var p = new Container(); var gfx = p.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); p.x = 2048 / 2; p.y = 2732 - 400; p.radius = gfx.width / 2; p.width = gfx.width; p.height = gfx.height; p.lastX = p.x; p.lastY = p.y; return p; } player = createPlayer(); game.addChild(player); // --- Enemy --- function createEnemy() { var e = new Container(); var gfx = e.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); // Spawn at random X at top e.x = 200 + Math.random() * (2048 - 400); e.y = -gfx.height; e.radius = gfx.width / 2; e.width = gfx.width; e.height = gfx.height; e.speed = 6 + Math.random() * 4; e.lastY = e.y; return e; } // --- Bullet --- function createBullet(x, y, dx, dy) { var b = new Container(); var gfx = b.attachAsset('goal', { anchorX: 0.5, anchorY: 0.5 }); b.x = x; b.y = y; b.radius = gfx.width / 2; b.width = gfx.width; b.height = gfx.height; b.dx = dx; b.dy = dy; b.lastY = b.y; return b; } // --- Reset Game --- function resetGame() { // Remove all enemies and bullets for (var i = 0; i < enemies.length; ++i) enemies[i].destroy(); for (var i = 0; i < bullets.length; ++i) bullets[i].destroy(); enemies = []; bullets = []; player.x = 2048 / 2; player.y = 2732 - 400; player.lastX = player.x; player.lastY = player.y; score = 0; scoreTxt.setText(score); gameOver = false; shootCooldown = 0; enemySpawnCooldown = 0; } // --- Game move handler (drag to move, tap to shoot) --- game.move = function (x, y, obj) { if (gameOver) return; // Drag to move player player.x = x; player.y = y; lastTouchX = x; lastTouchY = y; }; game.down = function (x, y, obj) { if (gameOver) { resetGame(); return; } // Shoot bullet towards touch var dx = x - player.x; var dy = y - player.y; var len = Math.sqrt(dx * dx + dy * dy); if (len > 0) { dx /= len; dy /= len; var bullet = createBullet(player.x, player.y, dx * 32, dy * 32); bullets.push(bullet); game.addChild(bullet); shootCooldown = 10; } }; game.up = function (x, y, obj) { // No dragNode logic needed }; // --- Game update loop --- game.update = function () { if (gameOver) return; // --- Bullets update --- for (var i = bullets.length - 1; i >= 0; --i) { var b = bullets[i]; b.lastY = b.y; b.x += b.dx; b.y += b.dy; // Remove if off screen if (b.x < -100 || b.x > 2048 + 100 || b.y < -100 || b.y > 2732 + 100) { b.destroy(); bullets.splice(i, 1); continue; } } // --- Enemies update --- for (var i = enemies.length - 1; i >= 0; --i) { var e = enemies[i]; e.lastY = e.y; e.y += e.speed; // Remove if off screen if (e.y > 2732 + 200) { e.destroy(); enemies.splice(i, 1); continue; } // Check collision with player if (Math.abs(e.x - player.x) < (e.width + player.width) / 2 * 0.8 && Math.abs(e.y - player.y) < (e.height + player.height) / 2 * 0.8) { // Game over LK.effects.flashScreen(0xe64a3a, 1000); gameOver = true; infoTxt.setText('Game Over! Tap to restart.'); LK.showGameOver(); return; } } // --- Bullet/Enemy collision --- for (var i = bullets.length - 1; i >= 0; --i) { var b = bullets[i]; for (var j = enemies.length - 1; j >= 0; --j) { var e = enemies[j]; if (Math.abs(b.x - e.x) < (b.width + e.width) / 2 * 0.8 && Math.abs(b.y - e.y) < (b.height + e.height) / 2 * 0.8) { // Destroy both b.destroy(); e.destroy(); bullets.splice(i, 1); enemies.splice(j, 1); score += 1; scoreTxt.setText(score); break; } } } // --- Spawn enemies --- enemySpawnCooldown--; if (enemySpawnCooldown <= 0) { var enemy = createEnemy(); enemies.push(enemy); game.addChild(enemy); enemySpawnCooldown = 40 + Math.floor(Math.random() * 30); } // --- Shoot cooldown --- if (shootCooldown > 0) shootCooldown--; }; // --- Start game --- resetGame(); // --- Prevent elements in top-left 100x100 px --- bgGrid.x = 0; bgGrid.y = 0; // --- End of file ---;
===================================================================
--- original.js
+++ change.js
@@ -3,62 +3,11 @@
****/
var tween = LK.import("@upit/tween.v1");
/****
-* Classes
-****/
-// Draggable base class for all sandbox elements
-var Draggable = Container.expand(function () {
- var self = Container.call(this);
- self.isDragging = false;
- // Called when user presses down on the element
- self.down = function (x, y, obj) {
- self.isDragging = true;
- self.dragOffsetX = x - self.x;
- self.dragOffsetY = y - self.y;
- dragNode = self;
- };
- // Called when user releases the element
- self.up = function (x, y, obj) {
- self.isDragging = false;
- dragNode = null;
- };
- return self;
-});
-// Player class
-var Player = Draggable.expand(function () {
- var self = Draggable.call(this);
- var gfx = self.attachAsset('player', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.type = 'player';
- return self;
-});
-// Obstacle class
-var Obstacle = Draggable.expand(function () {
- var self = Draggable.call(this);
- var gfx = self.attachAsset('obstacle', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.type = 'obstacle';
- return self;
-});
-// Goal class
-var Goal = Draggable.expand(function () {
- var self = Draggable.call(this);
- var gfx = self.attachAsset('goal', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.type = 'goal';
- return self;
-});
-
-/****
* Initialize Game
****/
+// No sandbox drag-and-drop classes needed for Free Fire shooter
var game = new LK.Game({
backgroundColor: 0xf0f0f0
});