/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { creations: [], lastCreation: {} }); /**** * Classes ****/ var BuildingBlock = Container.expand(function () { var self = Container.call(this); var blockGraphics = self.attachAsset('buildingBlock', { anchorX: 0.5, anchorY: 0.5 }); self.blockSize = 100; self.blockType = 'standard'; self.gridX = 0; self.gridY = 0; self.setColor = function (color) { blockGraphics.tint = color; }; self.setSize = function (size) { self.blockSize = size; blockGraphics.width = size; blockGraphics.height = size; }; self.setType = function (type) { self.blockType = type; }; self.setGridPosition = function (x, y) { self.gridX = x; self.gridY = y; }; self.down = function (x, y, obj) { if (currentMode === 'build') { selectedBlock = self; originalPosition = { x: self.x, y: self.y }; isDragging = true; } else if (currentMode === 'play') { // In play mode, interacting with blocks could trigger game mechanics // For simple MVP, we just acknowledge the interaction console.log("Interacted with block at", self.gridX, self.gridY); LK.getSound('place').play(); // Play sound on interaction } else if (currentMode === 'study') { var index = blocks.indexOf(self); if (index > -1) { blocks.splice(index, 1); self.destroy(); LK.getSound('delete').play(); } } }; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('playerCharacter', { anchorX: 0.5, anchorY: 0.5 }); self.velocity = { x: 0, y: 0 }; self.onGround = false; self.speed = 10; self.jumpPower = 20; self.gravity = 1; self.update = function () { if (self.isAI) { // AI logic to move in any direction and walk on the ground if (Math.random() < 0.01) { // Randomly change direction self.velocity.x = (Math.random() < 0.5 ? -1 : 1) * self.speed; } if (self.onGround && Math.random() < 0.01) { // Randomly jump if on ground self.velocity.y = -self.jumpPower; } } if (currentMode !== 'play') { return; } // Apply gravity self.velocity.y += self.gravity; // Apply velocity self.x += self.velocity.x; self.y += self.velocity.y; // Add movement effect if (self.velocity.x !== 0 || self.velocity.y !== 0) { if (!self.lastWasMoving || self.lastY <= self.y && self.velocity.y < 0) { LK.getSound('footstep').play(); // Play footstep sound when player starts moving or moves uphill } LK.effects.flashObject(self, 0xFFFFFF, 100); self.lastWasMoving = true; } else { self.lastWasMoving = false; } // Check collisions with blocks self.checkCollisions(); // Screen boundaries if (self.x < 40) { self.x = 40; } if (self.x > 2048 - 40) { self.x = 2048 - 40; } if (self.y > 2732 - 60) { self.y = 2732 - 60; self.velocity.y = 0; self.onGround = true; } }; self.checkCollisions = function () { self.onGround = false; for (var i = 0; i < blocks.length; i++) { var block = blocks[i]; if (self.intersects(block)) { // Determine collision side (simple version) var dx = self.x - block.x; var dy = self.y - block.y; var width = (self.width + block.width) / 2; var height = (self.height + block.height) / 2; var crossWidth = width * dy; var crossHeight = height * dx; if (Math.abs(dx) <= width && Math.abs(dy) <= height) { if (crossWidth > crossHeight) { if (crossWidth > -crossHeight) { // Bottom collision self.y = block.y + block.height / 2 + self.height / 2; self.velocity.y = 0; } else { // Left collision self.x = block.x - block.width / 2 - self.width / 2; self.velocity.x = 0; } } else { if (crossWidth > -crossHeight) { // Right collision self.x = block.x + block.width / 2 + self.width / 2; self.velocity.x = 0; } else { // Top collision self.y = block.y - block.height / 2 - self.height / 2; self.velocity.y = 0; self.onGround = true; } } } } } }; self.jump = function () { if (self.onGround && currentMode === 'play') { self.velocity.y = -self.jumpPower; self.onGround = false; } }; self.moveLeft = function () { if (currentMode === 'play') { self.velocity.x = -self.speed; } }; self.moveRight = function () { if (currentMode === 'play') { self.velocity.x = self.speed; } }; self.stopMoving = function () { self.velocity.x = 0; }; return self; }); var UIButton = Container.expand(function (iconAsset, callback) { var self = Container.call(this); var buttonGraphics = self.attachAsset(iconAsset, { anchorX: 0.5, anchorY: 0.5 }); self.active = false; self.callback = callback; self.setActive = function (active) { self.active = active; buttonGraphics.alpha = active ? 1.0 : 0.6; }; self.down = function (x, y, obj) { tween(buttonGraphics, { scaleX: 0.9, scaleY: 0.9 }, { duration: 100 }); }; self.up = function (x, y, obj) { tween(buttonGraphics, { scaleX: 1.0, scaleY: 1.0 }, { duration: 100 }); if (self.callback) { self.callback(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ // Function to start the game function startGame() { switchMode('play'); } // Function to add a new player function addPlayer() { var newPlayer = new Player(); LK.getSound('place').play(); // Play sound when a new player is added newPlayer.isAI = true; newPlayer.x = Math.random() * 2048; newPlayer.y = Math.random() * 2732; game.addChild(newPlayer); } // Game variables var currentMode = 'build'; var gridSize = 100; var blocks = []; var isDragging = false; var selectedBlock = null; var originalPosition = { x: 0, y: 0 }; var player = null; var buildModeButton = null; var playModeButton = null; var deleteButton = null; var saveButton = null; var loadButton = null; var modeTitleText = null; var controlsText = null; var blockColorIndex = 0; var blockColors = [0x3498db, 0xe74c3c, 0xf1c40f, 0x2ecc71, 0x9b59b6, 0x1abc9c, 0xd35400, 0x34495e]; // Setup UI function setupUI() { // Mode title text modeTitleText = new Text2('BUILD MODE', { size: 60, fill: 0xFFFFFF }); modeTitleText.anchor.set(0.5, 0); LK.gui.top.addChild(modeTitleText); modeTitleText.y = 30; // Controls text controlsText = new Text2('Tap to place blocks\nDrag blocks to move them', { size: 40, fill: 0xFFFFFF }); controlsText.anchor.set(0.5, 1); LK.gui.bottom.addChild(controlsText); controlsText.y = -30; // Build mode button buildModeButton = new UIButton('buildModeIcon', function () { switchMode('build'); }); buildModeButton.x = 2048 - 200; buildModeButton.y = 80; game.addChild(buildModeButton); buildModeButton.setActive(true); // Play mode button playModeButton = new UIButton('playModeIcon', function () { switchMode('play'); }); playModeButton.x = 2048 - 120; playModeButton.y = 80; game.addChild(playModeButton); // Delete button deleteButton = new UIButton('deleteIcon', function () { if (currentMode === 'build' && selectedBlock) { var index = blocks.indexOf(selectedBlock); if (index > -1) { blocks.splice(index, 1); selectedBlock.destroy(); selectedBlock = null; LK.getSound('delete').play(); } } }); deleteButton.x = 180; deleteButton.y = 80; game.addChild(deleteButton); // Save button saveButton = new UIButton('saveIcon', saveCreation); saveButton.x = 2048 - 200; saveButton.y = 160; game.addChild(saveButton); // Load button loadButton = new UIButton('loadIcon', loadCreation); loadButton.x = 2048 - 120; loadButton.y = 160; game.addChild(loadButton); // Add Player button var addPlayerButton = new UIButton('playerCharacter', function () { addPlayer(); }); addPlayerButton.x = 2048 - 280; addPlayerButton.y = 80; game.addChild(addPlayerButton); // Study mode button var studyModeButton = new UIButton('buildModeIcon', function () { switchMode('study'); }); studyModeButton.x = 2048 - 440; studyModeButton.y = 80; game.addChild(studyModeButton); // Start Game button var startGameButton = new UIButton('playModeIcon', function () { startGame(); }); startGameButton.x = 2048 - 360; startGameButton.y = 80; game.addChild(startGameButton); } // Create player character function createPlayer() { player = new Player(); player.x = 2048 / 2; player.y = 2732 / 2; game.addChild(player); } // Switch between build and play modes function switchMode(mode) { if (currentMode === mode) { return; } currentMode = mode; LK.getSound('switch').play(); if (mode === 'build') { modeTitleText.setText('BUILD MODE'); controlsText.setText('Tap to place blocks\nDrag blocks to move them'); buildModeButton.setActive(true); playModeButton.setActive(false); // Reset player position if (player) { player.x = 2048 / 2; player.y = 2732 / 2; player.velocity = { x: 0, y: 0 }; } } else if (mode === 'study') { modeTitleText.setText('STUDY MODE'); controlsText.setText('Tap blocks to delete them'); buildModeButton.setActive(false); playModeButton.setActive(false); // Deselect any selected block selectedBlock = null; isDragging = false; } else { modeTitleText.setText('PLAY MODE'); controlsText.setText('Tap left/right side to move\nTap player to jump'); buildModeButton.setActive(false); playModeButton.setActive(true); // Deselect any selected block selectedBlock = null; isDragging = false; } } // Create a new block at the specified grid position function createBlock(gridX, gridY, color) { var x = gridX * gridSize + gridSize / 2; var y = gridY * gridSize + gridSize / 2; // Check if a block already exists at this position for (var i = 0; i < blocks.length; i++) { if (blocks[i].gridX === gridX && blocks[i].gridY === gridY) { return null; } } var block = new BuildingBlock(); block.x = x; block.y = y; block.setGridPosition(gridX, gridY); if (color !== undefined) { block.setColor(color); } else { block.setColor(blockColors[blockColorIndex]); blockColorIndex = (blockColorIndex + 1) % blockColors.length; } blocks.push(block); game.addChild(block); LK.getSound('place').play(); return block; } // Save the current creation to storage function saveCreation() { var blockData = blocks.map(function (block) { return { x: block.gridX, y: block.gridY, color: block.children[0].tint }; }); var creation = { name: "Creation " + (storage.creations.length + 1), blocks: blockData, date: Date.now() }; if (typeof storage.lastCreation === 'undefined' || storage.lastCreation === "undefined") { storage.lastCreation = {}; } storage.lastCreation = creation; storage.creations.push(creation); // Show saved message var savedText = new Text2('Creation Saved!', { size: 60, fill: 0xFFFFFF }); savedText.anchor.set(0.5, 0.5); savedText.x = 2048 / 2; savedText.y = 2732 / 2; game.addChild(savedText); tween(savedText, { alpha: 0 }, { duration: 1500, onFinish: function onFinish() { savedText.destroy(); } }); } // Load a creation from storage function loadCreation() { if (!storage.lastCreation) { return; } // Clear existing blocks for (var i = blocks.length - 1; i >= 0; i--) { blocks[i].destroy(); } blocks = []; // Load saved blocks var saved = storage.lastCreation; if (saved.blocks && Array.isArray(saved.blocks)) { saved.blocks.forEach(function (blockData) { createBlock(blockData.x, blockData.y, blockData.color); }); } // Show loaded message var loadedText = new Text2('Creation Loaded!', { size: 60, fill: 0xFFFFFF }); loadedText.anchor.set(0.5, 0.5); loadedText.x = 2048 / 2; loadedText.y = 2732 / 2; game.addChild(loadedText); tween(loadedText, { alpha: 0 }, { duration: 1500, onFinish: function onFinish() { loadedText.destroy(); } }); } // Convert screen position to grid position function screenToGrid(x, y) { return { x: Math.floor(x / gridSize), y: Math.floor(y / gridSize) }; } // Initialize game function initGame() { setupUI(); createPlayer(); // Create ground for (var i = 0; i < 10; i++) { var ground = new BuildingBlock(); ground.x = i * 200 + 100; ground.y = 2732 - 50; ground.setGridPosition(i, Math.floor(2732 / gridSize) - 1); ground.children[0].width = 200; ground.children[0].height = 50; ground.children[0].tint = 0x27ae60; blocks.push(ground); game.addChild(ground); } // Start background music only if there are players if (game.children.some(function (child) { return child instanceof Player; })) { LK.playMusic('bgmusic'); // Play crowd sound if there are multiple players if (game.children.filter(function (child) { return child instanceof Player; }).length > 1) { LK.getSound('crowd').play(); } } } // Handle game input game.down = function (x, y, obj) { if (currentMode === 'build' && !isDragging && !selectedBlock) { var gridPos = screenToGrid(x, y); // Don't create blocks too high (leave space for UI) if (gridPos.y > 2) { createBlock(gridPos.x, gridPos.y); } } else if (currentMode === 'play') { // Control the player in play mode if (x < 2048 / 2) { player.moveLeft(); } else { player.moveRight(); } // Jump if clicked on player if (obj === player) { player.jump(); } } }; game.up = function (x, y, obj) { if (currentMode === 'build' && isDragging) { isDragging = false; if (selectedBlock) { // Snap to grid var gridPos = screenToGrid(selectedBlock.x, selectedBlock.y); // Check if position is occupied var isOccupied = false; for (var i = 0; i < blocks.length; i++) { if (blocks[i] !== selectedBlock && blocks[i].gridX === gridPos.x && blocks[i].gridY === gridPos.y) { isOccupied = true; break; } } if (isOccupied || gridPos.y <= 2) { // Return to original position selectedBlock.x = originalPosition.x; selectedBlock.y = originalPosition.y; } else { // Set to new grid position selectedBlock.x = gridPos.x * gridSize + gridSize / 2; selectedBlock.y = gridPos.y * gridSize + gridSize / 2; selectedBlock.setGridPosition(gridPos.x, gridPos.y); } selectedBlock = null; } } else if (currentMode === 'play') { player.stopMoving(); } }; game.move = function (x, y, obj) { if (currentMode === 'build' && isDragging && selectedBlock) { selectedBlock.x = x; selectedBlock.y = y; } }; game.update = function () { // Update game state if (player) { player.update(); } }; // Initialize the game initGame();
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
creations: [],
lastCreation: {}
});
/****
* Classes
****/
var BuildingBlock = Container.expand(function () {
var self = Container.call(this);
var blockGraphics = self.attachAsset('buildingBlock', {
anchorX: 0.5,
anchorY: 0.5
});
self.blockSize = 100;
self.blockType = 'standard';
self.gridX = 0;
self.gridY = 0;
self.setColor = function (color) {
blockGraphics.tint = color;
};
self.setSize = function (size) {
self.blockSize = size;
blockGraphics.width = size;
blockGraphics.height = size;
};
self.setType = function (type) {
self.blockType = type;
};
self.setGridPosition = function (x, y) {
self.gridX = x;
self.gridY = y;
};
self.down = function (x, y, obj) {
if (currentMode === 'build') {
selectedBlock = self;
originalPosition = {
x: self.x,
y: self.y
};
isDragging = true;
} else if (currentMode === 'play') {
// In play mode, interacting with blocks could trigger game mechanics
// For simple MVP, we just acknowledge the interaction
console.log("Interacted with block at", self.gridX, self.gridY);
LK.getSound('place').play(); // Play sound on interaction
} else if (currentMode === 'study') {
var index = blocks.indexOf(self);
if (index > -1) {
blocks.splice(index, 1);
self.destroy();
LK.getSound('delete').play();
}
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('playerCharacter', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = {
x: 0,
y: 0
};
self.onGround = false;
self.speed = 10;
self.jumpPower = 20;
self.gravity = 1;
self.update = function () {
if (self.isAI) {
// AI logic to move in any direction and walk on the ground
if (Math.random() < 0.01) {
// Randomly change direction
self.velocity.x = (Math.random() < 0.5 ? -1 : 1) * self.speed;
}
if (self.onGround && Math.random() < 0.01) {
// Randomly jump if on ground
self.velocity.y = -self.jumpPower;
}
}
if (currentMode !== 'play') {
return;
}
// Apply gravity
self.velocity.y += self.gravity;
// Apply velocity
self.x += self.velocity.x;
self.y += self.velocity.y;
// Add movement effect
if (self.velocity.x !== 0 || self.velocity.y !== 0) {
if (!self.lastWasMoving || self.lastY <= self.y && self.velocity.y < 0) {
LK.getSound('footstep').play(); // Play footstep sound when player starts moving or moves uphill
}
LK.effects.flashObject(self, 0xFFFFFF, 100);
self.lastWasMoving = true;
} else {
self.lastWasMoving = false;
}
// Check collisions with blocks
self.checkCollisions();
// Screen boundaries
if (self.x < 40) {
self.x = 40;
}
if (self.x > 2048 - 40) {
self.x = 2048 - 40;
}
if (self.y > 2732 - 60) {
self.y = 2732 - 60;
self.velocity.y = 0;
self.onGround = true;
}
};
self.checkCollisions = function () {
self.onGround = false;
for (var i = 0; i < blocks.length; i++) {
var block = blocks[i];
if (self.intersects(block)) {
// Determine collision side (simple version)
var dx = self.x - block.x;
var dy = self.y - block.y;
var width = (self.width + block.width) / 2;
var height = (self.height + block.height) / 2;
var crossWidth = width * dy;
var crossHeight = height * dx;
if (Math.abs(dx) <= width && Math.abs(dy) <= height) {
if (crossWidth > crossHeight) {
if (crossWidth > -crossHeight) {
// Bottom collision
self.y = block.y + block.height / 2 + self.height / 2;
self.velocity.y = 0;
} else {
// Left collision
self.x = block.x - block.width / 2 - self.width / 2;
self.velocity.x = 0;
}
} else {
if (crossWidth > -crossHeight) {
// Right collision
self.x = block.x + block.width / 2 + self.width / 2;
self.velocity.x = 0;
} else {
// Top collision
self.y = block.y - block.height / 2 - self.height / 2;
self.velocity.y = 0;
self.onGround = true;
}
}
}
}
}
};
self.jump = function () {
if (self.onGround && currentMode === 'play') {
self.velocity.y = -self.jumpPower;
self.onGround = false;
}
};
self.moveLeft = function () {
if (currentMode === 'play') {
self.velocity.x = -self.speed;
}
};
self.moveRight = function () {
if (currentMode === 'play') {
self.velocity.x = self.speed;
}
};
self.stopMoving = function () {
self.velocity.x = 0;
};
return self;
});
var UIButton = Container.expand(function (iconAsset, callback) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset(iconAsset, {
anchorX: 0.5,
anchorY: 0.5
});
self.active = false;
self.callback = callback;
self.setActive = function (active) {
self.active = active;
buttonGraphics.alpha = active ? 1.0 : 0.6;
};
self.down = function (x, y, obj) {
tween(buttonGraphics, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(buttonGraphics, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 100
});
if (self.callback) {
self.callback();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Function to start the game
function startGame() {
switchMode('play');
}
// Function to add a new player
function addPlayer() {
var newPlayer = new Player();
LK.getSound('place').play(); // Play sound when a new player is added
newPlayer.isAI = true;
newPlayer.x = Math.random() * 2048;
newPlayer.y = Math.random() * 2732;
game.addChild(newPlayer);
}
// Game variables
var currentMode = 'build';
var gridSize = 100;
var blocks = [];
var isDragging = false;
var selectedBlock = null;
var originalPosition = {
x: 0,
y: 0
};
var player = null;
var buildModeButton = null;
var playModeButton = null;
var deleteButton = null;
var saveButton = null;
var loadButton = null;
var modeTitleText = null;
var controlsText = null;
var blockColorIndex = 0;
var blockColors = [0x3498db, 0xe74c3c, 0xf1c40f, 0x2ecc71, 0x9b59b6, 0x1abc9c, 0xd35400, 0x34495e];
// Setup UI
function setupUI() {
// Mode title text
modeTitleText = new Text2('BUILD MODE', {
size: 60,
fill: 0xFFFFFF
});
modeTitleText.anchor.set(0.5, 0);
LK.gui.top.addChild(modeTitleText);
modeTitleText.y = 30;
// Controls text
controlsText = new Text2('Tap to place blocks\nDrag blocks to move them', {
size: 40,
fill: 0xFFFFFF
});
controlsText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(controlsText);
controlsText.y = -30;
// Build mode button
buildModeButton = new UIButton('buildModeIcon', function () {
switchMode('build');
});
buildModeButton.x = 2048 - 200;
buildModeButton.y = 80;
game.addChild(buildModeButton);
buildModeButton.setActive(true);
// Play mode button
playModeButton = new UIButton('playModeIcon', function () {
switchMode('play');
});
playModeButton.x = 2048 - 120;
playModeButton.y = 80;
game.addChild(playModeButton);
// Delete button
deleteButton = new UIButton('deleteIcon', function () {
if (currentMode === 'build' && selectedBlock) {
var index = blocks.indexOf(selectedBlock);
if (index > -1) {
blocks.splice(index, 1);
selectedBlock.destroy();
selectedBlock = null;
LK.getSound('delete').play();
}
}
});
deleteButton.x = 180;
deleteButton.y = 80;
game.addChild(deleteButton);
// Save button
saveButton = new UIButton('saveIcon', saveCreation);
saveButton.x = 2048 - 200;
saveButton.y = 160;
game.addChild(saveButton);
// Load button
loadButton = new UIButton('loadIcon', loadCreation);
loadButton.x = 2048 - 120;
loadButton.y = 160;
game.addChild(loadButton);
// Add Player button
var addPlayerButton = new UIButton('playerCharacter', function () {
addPlayer();
});
addPlayerButton.x = 2048 - 280;
addPlayerButton.y = 80;
game.addChild(addPlayerButton);
// Study mode button
var studyModeButton = new UIButton('buildModeIcon', function () {
switchMode('study');
});
studyModeButton.x = 2048 - 440;
studyModeButton.y = 80;
game.addChild(studyModeButton);
// Start Game button
var startGameButton = new UIButton('playModeIcon', function () {
startGame();
});
startGameButton.x = 2048 - 360;
startGameButton.y = 80;
game.addChild(startGameButton);
}
// Create player character
function createPlayer() {
player = new Player();
player.x = 2048 / 2;
player.y = 2732 / 2;
game.addChild(player);
}
// Switch between build and play modes
function switchMode(mode) {
if (currentMode === mode) {
return;
}
currentMode = mode;
LK.getSound('switch').play();
if (mode === 'build') {
modeTitleText.setText('BUILD MODE');
controlsText.setText('Tap to place blocks\nDrag blocks to move them');
buildModeButton.setActive(true);
playModeButton.setActive(false);
// Reset player position
if (player) {
player.x = 2048 / 2;
player.y = 2732 / 2;
player.velocity = {
x: 0,
y: 0
};
}
} else if (mode === 'study') {
modeTitleText.setText('STUDY MODE');
controlsText.setText('Tap blocks to delete them');
buildModeButton.setActive(false);
playModeButton.setActive(false);
// Deselect any selected block
selectedBlock = null;
isDragging = false;
} else {
modeTitleText.setText('PLAY MODE');
controlsText.setText('Tap left/right side to move\nTap player to jump');
buildModeButton.setActive(false);
playModeButton.setActive(true);
// Deselect any selected block
selectedBlock = null;
isDragging = false;
}
}
// Create a new block at the specified grid position
function createBlock(gridX, gridY, color) {
var x = gridX * gridSize + gridSize / 2;
var y = gridY * gridSize + gridSize / 2;
// Check if a block already exists at this position
for (var i = 0; i < blocks.length; i++) {
if (blocks[i].gridX === gridX && blocks[i].gridY === gridY) {
return null;
}
}
var block = new BuildingBlock();
block.x = x;
block.y = y;
block.setGridPosition(gridX, gridY);
if (color !== undefined) {
block.setColor(color);
} else {
block.setColor(blockColors[blockColorIndex]);
blockColorIndex = (blockColorIndex + 1) % blockColors.length;
}
blocks.push(block);
game.addChild(block);
LK.getSound('place').play();
return block;
}
// Save the current creation to storage
function saveCreation() {
var blockData = blocks.map(function (block) {
return {
x: block.gridX,
y: block.gridY,
color: block.children[0].tint
};
});
var creation = {
name: "Creation " + (storage.creations.length + 1),
blocks: blockData,
date: Date.now()
};
if (typeof storage.lastCreation === 'undefined' || storage.lastCreation === "undefined") {
storage.lastCreation = {};
}
storage.lastCreation = creation;
storage.creations.push(creation);
// Show saved message
var savedText = new Text2('Creation Saved!', {
size: 60,
fill: 0xFFFFFF
});
savedText.anchor.set(0.5, 0.5);
savedText.x = 2048 / 2;
savedText.y = 2732 / 2;
game.addChild(savedText);
tween(savedText, {
alpha: 0
}, {
duration: 1500,
onFinish: function onFinish() {
savedText.destroy();
}
});
}
// Load a creation from storage
function loadCreation() {
if (!storage.lastCreation) {
return;
}
// Clear existing blocks
for (var i = blocks.length - 1; i >= 0; i--) {
blocks[i].destroy();
}
blocks = [];
// Load saved blocks
var saved = storage.lastCreation;
if (saved.blocks && Array.isArray(saved.blocks)) {
saved.blocks.forEach(function (blockData) {
createBlock(blockData.x, blockData.y, blockData.color);
});
}
// Show loaded message
var loadedText = new Text2('Creation Loaded!', {
size: 60,
fill: 0xFFFFFF
});
loadedText.anchor.set(0.5, 0.5);
loadedText.x = 2048 / 2;
loadedText.y = 2732 / 2;
game.addChild(loadedText);
tween(loadedText, {
alpha: 0
}, {
duration: 1500,
onFinish: function onFinish() {
loadedText.destroy();
}
});
}
// Convert screen position to grid position
function screenToGrid(x, y) {
return {
x: Math.floor(x / gridSize),
y: Math.floor(y / gridSize)
};
}
// Initialize game
function initGame() {
setupUI();
createPlayer();
// Create ground
for (var i = 0; i < 10; i++) {
var ground = new BuildingBlock();
ground.x = i * 200 + 100;
ground.y = 2732 - 50;
ground.setGridPosition(i, Math.floor(2732 / gridSize) - 1);
ground.children[0].width = 200;
ground.children[0].height = 50;
ground.children[0].tint = 0x27ae60;
blocks.push(ground);
game.addChild(ground);
}
// Start background music only if there are players
if (game.children.some(function (child) {
return child instanceof Player;
})) {
LK.playMusic('bgmusic');
// Play crowd sound if there are multiple players
if (game.children.filter(function (child) {
return child instanceof Player;
}).length > 1) {
LK.getSound('crowd').play();
}
}
}
// Handle game input
game.down = function (x, y, obj) {
if (currentMode === 'build' && !isDragging && !selectedBlock) {
var gridPos = screenToGrid(x, y);
// Don't create blocks too high (leave space for UI)
if (gridPos.y > 2) {
createBlock(gridPos.x, gridPos.y);
}
} else if (currentMode === 'play') {
// Control the player in play mode
if (x < 2048 / 2) {
player.moveLeft();
} else {
player.moveRight();
}
// Jump if clicked on player
if (obj === player) {
player.jump();
}
}
};
game.up = function (x, y, obj) {
if (currentMode === 'build' && isDragging) {
isDragging = false;
if (selectedBlock) {
// Snap to grid
var gridPos = screenToGrid(selectedBlock.x, selectedBlock.y);
// Check if position is occupied
var isOccupied = false;
for (var i = 0; i < blocks.length; i++) {
if (blocks[i] !== selectedBlock && blocks[i].gridX === gridPos.x && blocks[i].gridY === gridPos.y) {
isOccupied = true;
break;
}
}
if (isOccupied || gridPos.y <= 2) {
// Return to original position
selectedBlock.x = originalPosition.x;
selectedBlock.y = originalPosition.y;
} else {
// Set to new grid position
selectedBlock.x = gridPos.x * gridSize + gridSize / 2;
selectedBlock.y = gridPos.y * gridSize + gridSize / 2;
selectedBlock.setGridPosition(gridPos.x, gridPos.y);
}
selectedBlock = null;
}
} else if (currentMode === 'play') {
player.stopMoving();
}
};
game.move = function (x, y, obj) {
if (currentMode === 'build' && isDragging && selectedBlock) {
selectedBlock.x = x;
selectedBlock.y = y;
}
};
game.update = function () {
// Update game state
if (player) {
player.update();
}
};
// Initialize the game
initGame();