User prompt
Then if you click the start game button it will dissappear and the kid will swim to shore
User prompt
When you click the character the raft moves down
User prompt
When your character gets teleported to a raft there's a button that can bring the character down by lowering the raft
User prompt
Now If you click your character instead of a start game button it'll make the raft move down with the character ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
After you see the black screen for 3 seconds then you'll be teleported to a raft in the middle of the ocean
User prompt
I can still hear the button click
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'mute')' in or related to this line: 'LK.sounds.mute();' Line Number: 766
User prompt
Please fix the bug: 'TypeError: LK.getSoundVolume is not a function' in or related to this line: 'LK.getSoundVolume(0);' Line Number: 766
User prompt
Please fix the bug: 'TypeError: LK.setMute is not a function' in or related to this line: 'LK.setMute(!self.soundOn);' Line Number: 760
User prompt
Now add a settings button two that says turn sound off and turn sound on
User prompt
After you click the start game button the screen will turn black for 3 seconds
User prompt
And then after you pick your character a button that says start game will be under them
User prompt
When you click on a character a text that says Hello! will appear and then fade away
User prompt
When you click on a character they should smile ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Make the boys some hair that sticks to his head
User prompt
Remove the boys hair
User prompt
And make the boys hair more realistic
User prompt
Give the girl hair
User prompt
Make the characters more realistic fun and cartoony
User prompt
Make the characters more fun cartoony and realistic
User prompt
Now when you click the player button 2 characters appear a girl and a boy click on one to select it
User prompt
Now make a fun and cartoony play button ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Remove everything just a green background
User prompt
Please fix the bug: 'Timeout.tick error: window.addEventListener is not a function' in or related to this line: 'window.addEventListener('keydown', function (e) {' Line Number: 621
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { resources: { wood: 0, stone: 0, food: 0, water: 0 }, crafted: { raft: false, axe: false, firepit: false }, mapExplored: 0 }); /**** * Classes ****/ var CraftingButton = Container.expand(function (itemName, requiredResources) { var self = Container.call(this); self.itemName = itemName; self.requiredResources = requiredResources; var buttonGraphics = self.attachAsset('craftingButton', { anchorX: 0, anchorY: 0 }); self.nameText = new Text2(itemName, { size: 22, fill: 0xFFFFFF }); self.nameText.anchor.set(0.5, 0.5); self.nameText.x = 75; self.nameText.y = 30; self.addChild(self.nameText); self.isCrafted = function () { return storage.crafted[itemName] || false; }; self.canCraft = function () { if (self.isCrafted()) { return false; } for (var resource in self.requiredResources) { if ((storage.resources[resource] || 0) < self.requiredResources[resource]) { return false; } } return true; }; self.updateAppearance = function () { if (self.isCrafted()) { buttonGraphics.alpha = 0.5; } else if (self.canCraft()) { buttonGraphics.alpha = 1; } else { buttonGraphics.alpha = 0.7; } }; self.down = function (x, y, obj) { if (self.canCraft()) { self.craftItem(); } }; self.craftItem = function () { if (self.isCrafted() || !self.canCraft()) { return; } // Consume resources for (var resource in self.requiredResources) { storage.resources[resource] -= self.requiredResources[resource]; } // Mark item as crafted storage.crafted[self.itemName] = true; // Give additional score LK.setScore(LK.getScore() + 10); // Play sound LK.getSound('craft').play(); // Update inventory display updateInventoryDisplay(); // Update crafting menu updateCraftingButtons(); // Special actions based on item if (self.itemName === 'raft' && storage.mapExplored >= 10) { // Player has built a raft and explored enough - they win! LK.showYouWin(); } }; self.update = function () { self.updateAppearance(); }; return self; }); var InventorySlot = Container.expand(function (index, resourceType) { var self = Container.call(this); self.index = index; self.resourceType = resourceType; var slotGraphics = self.attachAsset('inventorySlot', { anchorX: 0, anchorY: 0 }); self.countText = new Text2('0', { size: 30, fill: 0xFFFFFF }); self.countText.anchor.set(1, 1); self.countText.x = 65; self.countText.y = 65; self.addChild(self.countText); self.typeText = new Text2(resourceType, { size: 16, fill: 0xFFFFFF }); self.typeText.anchor.set(0, 0); self.typeText.x = 5; self.typeText.y = 5; self.addChild(self.typeText); self.update = function () { var count = storage.resources[resourceType] || 0; self.countText.setText(count.toString()); }; return self; }); var MapTile = Container.expand(function (tileType) { var self = Container.call(this); self.type = tileType || 'beach'; var assetId; switch (self.type) { case 'beach': assetId = 'beachTile'; break; case 'jungle': assetId = 'jungleTile'; break; case 'water': assetId = 'waterTile'; break; case 'mountain': assetId = 'mountainTile'; break; case 'cave': assetId = 'caveTile'; break; default: assetId = 'beachTile'; } var tileGraphics = self.attachAsset(assetId, { anchorX: 0, anchorY: 0 }); self.isWalkable = self.type !== 'water' && self.type !== 'mountain'; self.down = function (x, y, obj) { if (self.isWalkable && player) { player.moveTowards(self.x + 50, self.y + 50); } }; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('playerShape', { anchorX: 0.5, anchorY: 0.5 }); self.energy = 100; self.maxEnergy = 100; self.speed = 5; self.isMoving = false; self.targetX = 0; self.targetY = 0; self.moveTowards = function (x, y) { self.isMoving = true; self.targetX = x; self.targetY = y; }; self.stop = function () { self.isMoving = false; }; self.update = function () { if (self.isMoving) { var dx = self.targetX - self.x; var dy = self.targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > self.speed) { var ratio = self.speed / distance; self.x += dx * ratio; self.y += dy * ratio; // Reduce energy while moving if (LK.ticks % 30 === 0) { self.energy = Math.max(0, self.energy - 1); } } else { self.x = self.targetX; self.y = self.targetY; self.isMoving = false; } } }; return self; }); var Resource = Container.expand(function (type) { var self = Container.call(this); self.type = type || 'wood'; var assetId; switch (self.type) { case 'wood': assetId = 'resourceWood'; break; case 'stone': assetId = 'resourceStone'; break; case 'food': assetId = 'resourceFood'; break; case 'water': assetId = 'waterResource'; break; default: assetId = 'resourceWood'; } var resourceGraphics = self.attachAsset(assetId, { anchorX: 0.5, anchorY: 0.5 }); self.collected = false; self.down = function (x, y, obj) { if (!self.collected && playerCanReach(self)) { self.collect(); } }; self.collect = function () { if (self.collected) { return; } self.collected = true; LK.getSound('collect').play(); // Add resource to inventory storage.resources[self.type]++; updateInventoryDisplay(); // Visual feedback tween(resourceGraphics, { alpha: 0, scaleX: 0.2, scaleY: 0.2 }, { duration: 500, onFinish: function onFinish() { self.destroy(); // Remove from resources array var index = resources.indexOf(self); if (index !== -1) { resources.splice(index, 1); } } }); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x1AA7EC }); /**** * Game Code ****/ // Game state variables var player; var resources = []; var mapTiles = []; var inventorySlots = []; var craftingButtons = []; var energyBar; var energyBarBackground; var dayNightCycle = 0; // 0-1000 representing time of day var isDragging = false; var gameMapContainer; var uiContainer; var mapWidth = 20; var mapHeight = 20; var tileSize = 100; // Game functions function initGame() { // Set up containers gameMapContainer = new Container(); game.addChild(gameMapContainer); uiContainer = new Container(); game.addChild(uiContainer); // Generate map generateMap(); // Create player player = new Player(); player.x = 150; player.y = 150; gameMapContainer.addChild(player); // Generate resources generateResources(); // Create UI createUI(); // Start playing music LK.playMusic('islandMusic'); // Center map initially centerMapOnPlayer(); } function generateMap() { for (var y = 0; y < mapHeight; y++) { for (var x = 0; x < mapWidth; x++) { var tileType = determineMapTileType(x, y); var tile = new MapTile(tileType); tile.x = x * tileSize; tile.y = y * tileSize; mapTiles.push(tile); gameMapContainer.addChild(tile); } } } function determineMapTileType(x, y) { // Create interesting map with different biomes var centerX = mapWidth / 2; var centerY = mapHeight / 2; var distFromCenter = Math.sqrt(Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2)); // Water around the edges if (x <= 1 || y <= 1 || x >= mapWidth - 2 || y >= mapHeight - 2) { return 'water'; } // Beach near water if (x <= 3 || y <= 3 || x >= mapWidth - 4 || y >= mapHeight - 4) { return 'beach'; } // Mountain range in one area if (x > mapWidth * 0.6 && y < mapHeight * 0.4 && Math.random() < 0.7) { return 'mountain'; } // A few caves scattered around if (distFromCenter > 5 && Math.random() < 0.05) { return 'cave'; } // Jungle in the middle return 'jungle'; } function generateResources() { // Place resources on appropriate tiles for (var i = 0; i < mapTiles.length; i++) { var tile = mapTiles[i]; // Skip non-walkable tiles if (!tile.isWalkable) { continue; } // Determine if we should place a resource here var chance = 0; var resourceType = ''; switch (tile.type) { case 'beach': chance = 0.1; resourceType = Math.random() < 0.8 ? 'food' : 'water'; break; case 'jungle': chance = 0.2; resourceType = Math.random() < 0.7 ? 'wood' : 'food'; break; case 'cave': chance = 0.5; resourceType = 'stone'; break; } if (Math.random() < chance) { var resource = new Resource(resourceType); resource.x = tile.x + 50 + (Math.random() * 20 - 10); resource.y = tile.y + 50 + (Math.random() * 20 - 10); resources.push(resource); gameMapContainer.addChild(resource); } } } function createUI() { // Energy bar background energyBarBackground = LK.getAsset('energyBarBg', { anchorX: 0, anchorY: 0, x: 1598, y: 50 }); uiContainer.addChild(energyBarBackground); // Energy bar energyBar = LK.getAsset('energyBar', { anchorX: 0, anchorY: 0, x: 1598, y: 50 }); uiContainer.addChild(energyBar); // Inventory slots var resourceTypes = ['wood', 'stone', 'food', 'water']; for (var i = 0; i < resourceTypes.length; i++) { var slot = new InventorySlot(i, resourceTypes[i]); slot.x = 1598 + i % 2 * 80; slot.y = 100 + Math.floor(i / 2) * 80; inventorySlots.push(slot); uiContainer.addChild(slot); } // Create crafting buttons createCraftingMenu(); // Score display var scoreText = new Text2('Score: 0', { size: 30, fill: 0xFFFFFF }); scoreText.anchor.set(0, 0); scoreText.x = 120; scoreText.y = 50; uiContainer.addChild(scoreText); // Update function for score scoreText.update = function () { this.setText('Score: ' + LK.getScore()); }; // Instructions var instructionText = new Text2('Collect resources and craft items to escape the island!', { size: 24, fill: 0xFFFFFF }); instructionText.anchor.set(0.5, 0); instructionText.x = 1024; instructionText.y = 50; uiContainer.addChild(instructionText); } function createCraftingMenu() { var craftingItems = [{ name: 'axe', resources: { wood: 3, stone: 2 } }, { name: 'firepit', resources: { wood: 5, stone: 3 } }, { name: 'raft', resources: { wood: 10, stone: 5 } }]; for (var i = 0; i < craftingItems.length; i++) { var item = craftingItems[i]; var button = new CraftingButton(item.name, item.resources); button.x = 1598; button.y = 280 + i * 70; craftingButtons.push(button); uiContainer.addChild(button); } } function updateInventoryDisplay() { for (var i = 0; i < inventorySlots.length; i++) { inventorySlots[i].update(); } } function updateCraftingButtons() { for (var i = 0; i < craftingButtons.length; i++) { craftingButtons[i].update(); } } function updateEnergyBar() { var percentage = player.energy / player.maxEnergy; energyBar.width = 400 * percentage; // Change color based on energy level if (percentage < 0.2) { energyBar.tint = 0xE74C3C; // Red } else if (percentage < 0.5) { energyBar.tint = 0xF39C12; // Orange } else { energyBar.tint = 0x27AE60; // Green } // Check if player is out of energy if (player.energy <= 0 && !gameLost) { gameLost = true; LK.showGameOver(); } } function centerMapOnPlayer() { gameMapContainer.x = 1024 - player.x; gameMapContainer.y = 1366 - player.y; // Constrain map position var minX = 2048 - mapWidth * tileSize; var minY = 2732 - mapHeight * tileSize; if (gameMapContainer.x > 0) { gameMapContainer.x = 0; } if (gameMapContainer.y > 0) { gameMapContainer.y = 0; } if (gameMapContainer.x < minX) { gameMapContainer.x = minX; } if (gameMapContainer.y < minY) { gameMapContainer.y = minY; } } function playerCanReach(object) { var dx = object.x - player.x; var dy = object.y - player.y; var distance = Math.sqrt(dx * dx + dy * dy); return distance < 100; // Player can reach within 100 pixels } function restoreEnergy() { // Restore energy if the player has food and water if (storage.resources.food > 0 && storage.resources.water > 0) { storage.resources.food--; storage.resources.water--; player.energy = Math.min(player.maxEnergy, player.energy + 50); updateInventoryDisplay(); } } function handleMapClick(x, y) { var localPoint = gameMapContainer.toLocal({ x: x, y: y }); // Convert to tile coordinates var tileX = Math.floor(localPoint.x / tileSize); var tileY = Math.floor(localPoint.y / tileSize); // Make sure coordinates are valid if (tileX >= 0 && tileX < mapWidth && tileY >= 0 && tileY < mapHeight) { // Find the tile at these coordinates var tileIndex = tileY * mapWidth + tileX; var tile = mapTiles[tileIndex]; if (tile && tile.isWalkable) { // Move player to center of tile player.moveTowards(tileX * tileSize + tileSize / 2, tileY * tileSize + tileSize / 2); // Mark tile as explored if it wasn't already if (!tile.explored) { tile.explored = true; storage.mapExplored++; LK.setScore(LK.getScore() + 1); } } } } // Game initialization var gameLost = false; initGame(); // Update function called every tick game.update = function () { // Update day/night cycle dayNightCycle = (dayNightCycle + 1) % 1000; // Update player player.update(); // Center map on player centerMapOnPlayer(); // Update energy bar updateEnergyBar(); // Restore energy automatically over time if player has crafted a firepit if (storage.crafted.firepit && LK.ticks % 300 === 0) { player.energy = Math.min(player.maxEnergy, player.energy + 2); } // Make harvesting more efficient if player has axe if (storage.crafted.axe && LK.ticks % 300 === 0 && resources.length > 0) { // Find nearest wood resource and collect it automatically var nearestWood = null; var nearestDistance = Infinity; for (var i = 0; i < resources.length; i++) { if (resources[i].type === 'wood') { var dx = resources[i].x - player.x; var dy = resources[i].y - player.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < nearestDistance && distance < 200) { nearestDistance = distance; nearestWood = resources[i]; } } } if (nearestWood) { nearestWood.collect(); } } }; // Event handlers game.down = function (x, y, obj) { // Check if we clicked a UI element first var targetUI = false; // Check if click was on inventory or crafting UI if (x > 1598) { targetUI = true; } // If not a UI element, handle map click if (!targetUI) { handleMapClick(x, y); } }; // Keyboard shortcuts are not needed on mobile - removed to fix sandbox error // We can use a touch interface or UI button for restoreEnergy functionality instead LK.setTimeout(function () { // Note: window.addEventListener is not available in this sandbox environment }, 1000);
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
resources: {
wood: 0,
stone: 0,
food: 0,
water: 0
},
crafted: {
raft: false,
axe: false,
firepit: false
},
mapExplored: 0
});
/****
* Classes
****/
var CraftingButton = Container.expand(function (itemName, requiredResources) {
var self = Container.call(this);
self.itemName = itemName;
self.requiredResources = requiredResources;
var buttonGraphics = self.attachAsset('craftingButton', {
anchorX: 0,
anchorY: 0
});
self.nameText = new Text2(itemName, {
size: 22,
fill: 0xFFFFFF
});
self.nameText.anchor.set(0.5, 0.5);
self.nameText.x = 75;
self.nameText.y = 30;
self.addChild(self.nameText);
self.isCrafted = function () {
return storage.crafted[itemName] || false;
};
self.canCraft = function () {
if (self.isCrafted()) {
return false;
}
for (var resource in self.requiredResources) {
if ((storage.resources[resource] || 0) < self.requiredResources[resource]) {
return false;
}
}
return true;
};
self.updateAppearance = function () {
if (self.isCrafted()) {
buttonGraphics.alpha = 0.5;
} else if (self.canCraft()) {
buttonGraphics.alpha = 1;
} else {
buttonGraphics.alpha = 0.7;
}
};
self.down = function (x, y, obj) {
if (self.canCraft()) {
self.craftItem();
}
};
self.craftItem = function () {
if (self.isCrafted() || !self.canCraft()) {
return;
}
// Consume resources
for (var resource in self.requiredResources) {
storage.resources[resource] -= self.requiredResources[resource];
}
// Mark item as crafted
storage.crafted[self.itemName] = true;
// Give additional score
LK.setScore(LK.getScore() + 10);
// Play sound
LK.getSound('craft').play();
// Update inventory display
updateInventoryDisplay();
// Update crafting menu
updateCraftingButtons();
// Special actions based on item
if (self.itemName === 'raft' && storage.mapExplored >= 10) {
// Player has built a raft and explored enough - they win!
LK.showYouWin();
}
};
self.update = function () {
self.updateAppearance();
};
return self;
});
var InventorySlot = Container.expand(function (index, resourceType) {
var self = Container.call(this);
self.index = index;
self.resourceType = resourceType;
var slotGraphics = self.attachAsset('inventorySlot', {
anchorX: 0,
anchorY: 0
});
self.countText = new Text2('0', {
size: 30,
fill: 0xFFFFFF
});
self.countText.anchor.set(1, 1);
self.countText.x = 65;
self.countText.y = 65;
self.addChild(self.countText);
self.typeText = new Text2(resourceType, {
size: 16,
fill: 0xFFFFFF
});
self.typeText.anchor.set(0, 0);
self.typeText.x = 5;
self.typeText.y = 5;
self.addChild(self.typeText);
self.update = function () {
var count = storage.resources[resourceType] || 0;
self.countText.setText(count.toString());
};
return self;
});
var MapTile = Container.expand(function (tileType) {
var self = Container.call(this);
self.type = tileType || 'beach';
var assetId;
switch (self.type) {
case 'beach':
assetId = 'beachTile';
break;
case 'jungle':
assetId = 'jungleTile';
break;
case 'water':
assetId = 'waterTile';
break;
case 'mountain':
assetId = 'mountainTile';
break;
case 'cave':
assetId = 'caveTile';
break;
default:
assetId = 'beachTile';
}
var tileGraphics = self.attachAsset(assetId, {
anchorX: 0,
anchorY: 0
});
self.isWalkable = self.type !== 'water' && self.type !== 'mountain';
self.down = function (x, y, obj) {
if (self.isWalkable && player) {
player.moveTowards(self.x + 50, self.y + 50);
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('playerShape', {
anchorX: 0.5,
anchorY: 0.5
});
self.energy = 100;
self.maxEnergy = 100;
self.speed = 5;
self.isMoving = false;
self.targetX = 0;
self.targetY = 0;
self.moveTowards = function (x, y) {
self.isMoving = true;
self.targetX = x;
self.targetY = y;
};
self.stop = function () {
self.isMoving = false;
};
self.update = function () {
if (self.isMoving) {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > self.speed) {
var ratio = self.speed / distance;
self.x += dx * ratio;
self.y += dy * ratio;
// Reduce energy while moving
if (LK.ticks % 30 === 0) {
self.energy = Math.max(0, self.energy - 1);
}
} else {
self.x = self.targetX;
self.y = self.targetY;
self.isMoving = false;
}
}
};
return self;
});
var Resource = Container.expand(function (type) {
var self = Container.call(this);
self.type = type || 'wood';
var assetId;
switch (self.type) {
case 'wood':
assetId = 'resourceWood';
break;
case 'stone':
assetId = 'resourceStone';
break;
case 'food':
assetId = 'resourceFood';
break;
case 'water':
assetId = 'waterResource';
break;
default:
assetId = 'resourceWood';
}
var resourceGraphics = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.down = function (x, y, obj) {
if (!self.collected && playerCanReach(self)) {
self.collect();
}
};
self.collect = function () {
if (self.collected) {
return;
}
self.collected = true;
LK.getSound('collect').play();
// Add resource to inventory
storage.resources[self.type]++;
updateInventoryDisplay();
// Visual feedback
tween(resourceGraphics, {
alpha: 0,
scaleX: 0.2,
scaleY: 0.2
}, {
duration: 500,
onFinish: function onFinish() {
self.destroy();
// Remove from resources array
var index = resources.indexOf(self);
if (index !== -1) {
resources.splice(index, 1);
}
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1AA7EC
});
/****
* Game Code
****/
// Game state variables
var player;
var resources = [];
var mapTiles = [];
var inventorySlots = [];
var craftingButtons = [];
var energyBar;
var energyBarBackground;
var dayNightCycle = 0; // 0-1000 representing time of day
var isDragging = false;
var gameMapContainer;
var uiContainer;
var mapWidth = 20;
var mapHeight = 20;
var tileSize = 100;
// Game functions
function initGame() {
// Set up containers
gameMapContainer = new Container();
game.addChild(gameMapContainer);
uiContainer = new Container();
game.addChild(uiContainer);
// Generate map
generateMap();
// Create player
player = new Player();
player.x = 150;
player.y = 150;
gameMapContainer.addChild(player);
// Generate resources
generateResources();
// Create UI
createUI();
// Start playing music
LK.playMusic('islandMusic');
// Center map initially
centerMapOnPlayer();
}
function generateMap() {
for (var y = 0; y < mapHeight; y++) {
for (var x = 0; x < mapWidth; x++) {
var tileType = determineMapTileType(x, y);
var tile = new MapTile(tileType);
tile.x = x * tileSize;
tile.y = y * tileSize;
mapTiles.push(tile);
gameMapContainer.addChild(tile);
}
}
}
function determineMapTileType(x, y) {
// Create interesting map with different biomes
var centerX = mapWidth / 2;
var centerY = mapHeight / 2;
var distFromCenter = Math.sqrt(Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2));
// Water around the edges
if (x <= 1 || y <= 1 || x >= mapWidth - 2 || y >= mapHeight - 2) {
return 'water';
}
// Beach near water
if (x <= 3 || y <= 3 || x >= mapWidth - 4 || y >= mapHeight - 4) {
return 'beach';
}
// Mountain range in one area
if (x > mapWidth * 0.6 && y < mapHeight * 0.4 && Math.random() < 0.7) {
return 'mountain';
}
// A few caves scattered around
if (distFromCenter > 5 && Math.random() < 0.05) {
return 'cave';
}
// Jungle in the middle
return 'jungle';
}
function generateResources() {
// Place resources on appropriate tiles
for (var i = 0; i < mapTiles.length; i++) {
var tile = mapTiles[i];
// Skip non-walkable tiles
if (!tile.isWalkable) {
continue;
}
// Determine if we should place a resource here
var chance = 0;
var resourceType = '';
switch (tile.type) {
case 'beach':
chance = 0.1;
resourceType = Math.random() < 0.8 ? 'food' : 'water';
break;
case 'jungle':
chance = 0.2;
resourceType = Math.random() < 0.7 ? 'wood' : 'food';
break;
case 'cave':
chance = 0.5;
resourceType = 'stone';
break;
}
if (Math.random() < chance) {
var resource = new Resource(resourceType);
resource.x = tile.x + 50 + (Math.random() * 20 - 10);
resource.y = tile.y + 50 + (Math.random() * 20 - 10);
resources.push(resource);
gameMapContainer.addChild(resource);
}
}
}
function createUI() {
// Energy bar background
energyBarBackground = LK.getAsset('energyBarBg', {
anchorX: 0,
anchorY: 0,
x: 1598,
y: 50
});
uiContainer.addChild(energyBarBackground);
// Energy bar
energyBar = LK.getAsset('energyBar', {
anchorX: 0,
anchorY: 0,
x: 1598,
y: 50
});
uiContainer.addChild(energyBar);
// Inventory slots
var resourceTypes = ['wood', 'stone', 'food', 'water'];
for (var i = 0; i < resourceTypes.length; i++) {
var slot = new InventorySlot(i, resourceTypes[i]);
slot.x = 1598 + i % 2 * 80;
slot.y = 100 + Math.floor(i / 2) * 80;
inventorySlots.push(slot);
uiContainer.addChild(slot);
}
// Create crafting buttons
createCraftingMenu();
// Score display
var scoreText = new Text2('Score: 0', {
size: 30,
fill: 0xFFFFFF
});
scoreText.anchor.set(0, 0);
scoreText.x = 120;
scoreText.y = 50;
uiContainer.addChild(scoreText);
// Update function for score
scoreText.update = function () {
this.setText('Score: ' + LK.getScore());
};
// Instructions
var instructionText = new Text2('Collect resources and craft items to escape the island!', {
size: 24,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 1024;
instructionText.y = 50;
uiContainer.addChild(instructionText);
}
function createCraftingMenu() {
var craftingItems = [{
name: 'axe',
resources: {
wood: 3,
stone: 2
}
}, {
name: 'firepit',
resources: {
wood: 5,
stone: 3
}
}, {
name: 'raft',
resources: {
wood: 10,
stone: 5
}
}];
for (var i = 0; i < craftingItems.length; i++) {
var item = craftingItems[i];
var button = new CraftingButton(item.name, item.resources);
button.x = 1598;
button.y = 280 + i * 70;
craftingButtons.push(button);
uiContainer.addChild(button);
}
}
function updateInventoryDisplay() {
for (var i = 0; i < inventorySlots.length; i++) {
inventorySlots[i].update();
}
}
function updateCraftingButtons() {
for (var i = 0; i < craftingButtons.length; i++) {
craftingButtons[i].update();
}
}
function updateEnergyBar() {
var percentage = player.energy / player.maxEnergy;
energyBar.width = 400 * percentage;
// Change color based on energy level
if (percentage < 0.2) {
energyBar.tint = 0xE74C3C; // Red
} else if (percentage < 0.5) {
energyBar.tint = 0xF39C12; // Orange
} else {
energyBar.tint = 0x27AE60; // Green
}
// Check if player is out of energy
if (player.energy <= 0 && !gameLost) {
gameLost = true;
LK.showGameOver();
}
}
function centerMapOnPlayer() {
gameMapContainer.x = 1024 - player.x;
gameMapContainer.y = 1366 - player.y;
// Constrain map position
var minX = 2048 - mapWidth * tileSize;
var minY = 2732 - mapHeight * tileSize;
if (gameMapContainer.x > 0) {
gameMapContainer.x = 0;
}
if (gameMapContainer.y > 0) {
gameMapContainer.y = 0;
}
if (gameMapContainer.x < minX) {
gameMapContainer.x = minX;
}
if (gameMapContainer.y < minY) {
gameMapContainer.y = minY;
}
}
function playerCanReach(object) {
var dx = object.x - player.x;
var dy = object.y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
return distance < 100; // Player can reach within 100 pixels
}
function restoreEnergy() {
// Restore energy if the player has food and water
if (storage.resources.food > 0 && storage.resources.water > 0) {
storage.resources.food--;
storage.resources.water--;
player.energy = Math.min(player.maxEnergy, player.energy + 50);
updateInventoryDisplay();
}
}
function handleMapClick(x, y) {
var localPoint = gameMapContainer.toLocal({
x: x,
y: y
});
// Convert to tile coordinates
var tileX = Math.floor(localPoint.x / tileSize);
var tileY = Math.floor(localPoint.y / tileSize);
// Make sure coordinates are valid
if (tileX >= 0 && tileX < mapWidth && tileY >= 0 && tileY < mapHeight) {
// Find the tile at these coordinates
var tileIndex = tileY * mapWidth + tileX;
var tile = mapTiles[tileIndex];
if (tile && tile.isWalkable) {
// Move player to center of tile
player.moveTowards(tileX * tileSize + tileSize / 2, tileY * tileSize + tileSize / 2);
// Mark tile as explored if it wasn't already
if (!tile.explored) {
tile.explored = true;
storage.mapExplored++;
LK.setScore(LK.getScore() + 1);
}
}
}
}
// Game initialization
var gameLost = false;
initGame();
// Update function called every tick
game.update = function () {
// Update day/night cycle
dayNightCycle = (dayNightCycle + 1) % 1000;
// Update player
player.update();
// Center map on player
centerMapOnPlayer();
// Update energy bar
updateEnergyBar();
// Restore energy automatically over time if player has crafted a firepit
if (storage.crafted.firepit && LK.ticks % 300 === 0) {
player.energy = Math.min(player.maxEnergy, player.energy + 2);
}
// Make harvesting more efficient if player has axe
if (storage.crafted.axe && LK.ticks % 300 === 0 && resources.length > 0) {
// Find nearest wood resource and collect it automatically
var nearestWood = null;
var nearestDistance = Infinity;
for (var i = 0; i < resources.length; i++) {
if (resources[i].type === 'wood') {
var dx = resources[i].x - player.x;
var dy = resources[i].y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < nearestDistance && distance < 200) {
nearestDistance = distance;
nearestWood = resources[i];
}
}
}
if (nearestWood) {
nearestWood.collect();
}
}
};
// Event handlers
game.down = function (x, y, obj) {
// Check if we clicked a UI element first
var targetUI = false;
// Check if click was on inventory or crafting UI
if (x > 1598) {
targetUI = true;
}
// If not a UI element, handle map click
if (!targetUI) {
handleMapClick(x, y);
}
};
// Keyboard shortcuts are not needed on mobile - removed to fix sandbox error
// We can use a touch interface or UI button for restoreEnergy functionality instead
LK.setTimeout(function () {
// Note: window.addEventListener is not available in this sandbox environment
}, 1000);