/**** * Plugins ****/ 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 ****/ 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
@@ -64,313 +64,213 @@
/****
* Game Code
****/
-// Basic shapes for sandbox elements
-// --- Global state ---
-var sandboxElements = []; // All placed elements
-var dragNode = null; // Currently dragged element
-var mode = 'edit'; // 'edit' or 'play'
-var playerStart = {
- x: 400,
- y: 1366
-}; // Default player start
-var goalStart = {
- x: 1648,
- y: 1366
-}; // Default goal start
+// --- 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 ---
-// Title
-var titleTxt = new Text2('Me Game Creat', {
+var titleTxt = new Text2('Free Fire', {
size: 90,
fill: 0x222222
});
titleTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(titleTxt);
-// Mode toggle button
-var modeBtn = new Text2('Play', {
+var scoreTxt = new Text2('0', {
size: 70,
- fill: 0xFFFFFF
-});
-modeBtn.anchor.set(0.5, 0.5);
-modeBtn.x = 0;
-modeBtn.y = 0;
-modeBtn.bg = 0x3a8ee6;
-modeBtn.padding = 30;
-LK.gui.topRight.addChild(modeBtn);
-// Add element buttons
-var addPlayerBtn = new Text2('Add Player', {
- size: 60,
fill: 0x3A8EE6
});
-addPlayerBtn.anchor.set(0.5, 0.5);
-addPlayerBtn.y = 0;
-LK.gui.left.addChild(addPlayerBtn);
-var addGoalBtn = new Text2('Add Goal', {
+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: 0x4AD66D
-});
-addGoalBtn.anchor.set(0.5, 0.5);
-addGoalBtn.y = 120;
-LK.gui.left.addChild(addGoalBtn);
-var addObstacleBtn = new Text2('Add Obstacle', {
- size: 60,
- fill: 0xE64A3A
-});
-addObstacleBtn.anchor.set(0.5, 0.5);
-addObstacleBtn.y = 240;
-LK.gui.left.addChild(addObstacleBtn);
-// Info text
-var infoTxt = new Text2('Drag elements. Tap Play to test!', {
- size: 60,
fill: 0x444444
});
infoTxt.anchor.set(0.5, 0);
infoTxt.y = 0;
LK.gui.bottom.addChild(infoTxt);
-// --- Sandbox background grid ---
+// --- Background ---
var bgGrid = LK.getAsset('bgGrid', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
game.addChild(bgGrid);
-// --- Add initial elements ---
-function addInitialElements() {
- // Add player
- var player = new Player();
- player.x = playerStart.x;
- player.y = playerStart.y;
- sandboxElements.push(player);
- game.addChild(player);
- // Add goal
- var goal = new Goal();
- goal.x = goalStart.x;
- goal.y = goalStart.y;
- sandboxElements.push(goal);
- game.addChild(goal);
+// --- 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;
}
-addInitialElements();
-// --- Add element handlers ---
-addPlayerBtn.down = function (x, y, obj) {
- if (mode !== 'edit') return;
- var player = new Player();
- player.x = 400 + Math.random() * 200;
- player.y = 400 + Math.random() * 400;
- sandboxElements.push(player);
- game.addChild(player);
-};
-addGoalBtn.down = function (x, y, obj) {
- if (mode !== 'edit') return;
- var goal = new Goal();
- goal.x = 1648 - Math.random() * 200;
- goal.y = 400 + Math.random() * 400;
- sandboxElements.push(goal);
- game.addChild(goal);
-};
-addObstacleBtn.down = function (x, y, obj) {
- if (mode !== 'edit') return;
- var obs = new Obstacle();
- obs.x = 1024 + (Math.random() - 0.5) * 600;
- obs.y = 1366 + (Math.random() - 0.5) * 800;
- sandboxElements.push(obs);
- game.addChild(obs);
-};
-// --- Mode toggle handler ---
-modeBtn.down = function (x, y, obj) {
- if (mode === 'edit') {
- // Switch to play mode
- if (!getPlayer() || !getGoal()) {
- infoTxt.setText('Need at least 1 Player and 1 Goal!');
- return;
- }
- mode = 'play';
- modeBtn.setText('Edit');
- infoTxt.setText('Drag to move player. Reach the goal!');
- setEditMode(false);
- startPlayMode();
- } else {
- // Switch to edit mode
- mode = 'edit';
- modeBtn.setText('Play');
- infoTxt.setText('Drag elements. Tap Play to test!');
- setEditMode(true);
- stopPlayMode();
- }
-};
-// --- Helper functions ---
-function getPlayer() {
- for (var i = 0; i < sandboxElements.length; ++i) {
- if (sandboxElements[i].type === 'player') return sandboxElements[i];
- }
- return null;
+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;
}
-function getGoal() {
- for (var i = 0; i < sandboxElements.length; ++i) {
- if (sandboxElements[i].type === 'goal') return sandboxElements[i];
- }
- return null;
+// --- 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;
}
-function getObstacles() {
- var arr = [];
- for (var i = 0; i < sandboxElements.length; ++i) {
- if (sandboxElements[i].type === 'obstacle') arr.push(sandboxElements[i]);
- }
- return arr;
+// --- 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;
}
-// Enable/disable dragging for all elements
-function setEditMode(enabled) {
- for (var i = 0; i < sandboxElements.length; ++i) {
- sandboxElements[i].interactive = enabled;
- }
-}
-// Remove all elements except player and goal
-function removeObstacles() {
- for (var i = sandboxElements.length - 1; i >= 0; --i) {
- if (sandboxElements[i].type === 'obstacle') {
- sandboxElements[i].destroy();
- sandboxElements.splice(i, 1);
- }
- }
-}
-// --- Play mode logic ---
-var playState = {
- moving: false,
- vx: 0,
- vy: 0,
- speed: 18,
- lastX: 0,
- lastY: 0,
- lastIntersectGoal: false,
- lastIntersectObs: []
-};
-function startPlayMode() {
- // Reset player to start
- var player = getPlayer();
- var goal = getGoal();
- if (!player || !goal) return;
- player.x = playerStart.x;
- player.y = playerStart.y;
- playState.vx = 0;
- playState.vy = 0;
- playState.moving = false;
- playState.lastIntersectGoal = false;
- playState.lastIntersectObs = [];
- // Obstacles cannot be moved
- setEditMode(false);
- // Player is draggable in play mode
- player.interactive = true;
-}
-function stopPlayMode() {
- // Player and all elements are draggable again
- setEditMode(true);
-}
-// --- Dragging logic (edit mode) ---
-function handleEditMove(x, y, obj) {
- if (dragNode && dragNode.isDragging) {
- dragNode.x = x - dragNode.dragOffsetX;
- dragNode.y = y - dragNode.dragOffsetY;
- }
-}
-// --- Play mode movement (drag to move player) ---
-function handlePlayMove(x, y, obj) {
- var player = getPlayer();
- if (!player) return;
- // Only allow dragging player
- if (dragNode === player) {
- player.x = x;
- player.y = y;
- }
-}
-// --- Game move handler ---
+// --- Game move handler (drag to move, tap to shoot) ---
game.move = function (x, y, obj) {
- if (mode === 'edit') {
- handleEditMove(x, y, obj);
- } else if (mode === 'play') {
- handlePlayMove(x, y, obj);
- }
+ if (gameOver) return;
+ // Drag to move player
+ player.x = x;
+ player.y = y;
+ lastTouchX = x;
+ lastTouchY = y;
};
-// --- Game down handler ---
game.down = function (x, y, obj) {
- if (mode === 'edit') {
- // Find topmost element under pointer
- for (var i = sandboxElements.length - 1; i >= 0; --i) {
- var el = sandboxElements[i];
- var dx = x - el.x;
- var dy = y - el.y;
- var w = el.width || 180;
- var h = el.height || 180;
- if (Math.abs(dx) < w / 2 && Math.abs(dy) < h / 2) {
- dragNode = el;
- el.isDragging = true;
- el.dragOffsetX = x - el.x;
- el.dragOffsetY = y - el.y;
- break;
- }
- }
- } else if (mode === 'play') {
- // Only allow dragging player
- var player = getPlayer();
- if (player) {
- var dx = x - player.x;
- var dy = y - player.y;
- var w = player.width || 180;
- var h = player.height || 180;
- if (Math.abs(dx) < w / 2 && Math.abs(dy) < h / 2) {
- dragNode = player;
- player.isDragging = true;
- player.dragOffsetX = x - player.x;
- player.dragOffsetY = y - player.y;
- }
- }
+ 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 handler ---
game.up = function (x, y, obj) {
- if (dragNode) {
- dragNode.isDragging = false;
- dragNode = null;
- }
+ // No dragNode logic needed
};
// --- Game update loop ---
game.update = function () {
- if (mode === 'play') {
- var player = getPlayer();
- var goal = getGoal();
- var obstacles = getObstacles();
- // Check for win
- var intersectGoal = player && goal && player.intersects(goal);
- if (!playState.lastIntersectGoal && intersectGoal) {
- // Win!
- LK.effects.flashScreen(0x4ad66d, 800);
- LK.showYouWin();
+ 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;
}
- playState.lastIntersectGoal = intersectGoal;
- // Check for collision with obstacles
- for (var i = 0; i < obstacles.length; ++i) {
- var obs = obstacles[i];
- var intersectObs = player && obs && player.intersects(obs);
- if (!playState.lastIntersectObs[i]) {
- if (intersectObs) {
- // Hit obstacle: flash red, reset player
- LK.effects.flashObject(player, 0xe64a3a, 600);
- player.x = playerStart.x;
- player.y = playerStart.y;
- }
+ }
+ // --- 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;
}
- playState.lastIntersectObs[i] = intersectObs;
}
}
-};
-// --- Utility: Remove all elements (for future expansion) ---
-function clearSandbox() {
- for (var i = sandboxElements.length - 1; i >= 0; --i) {
- sandboxElements[i].destroy();
- sandboxElements.splice(i, 1);
+ // --- 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 ---
\ No newline at end of file
+// --- End of file ---;
\ No newline at end of file