/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highestDay: 1,
totalHarvests: 0
});
/****
* Classes
****/
var InventorySlot = Container.expand(function () {
var self = Container.call(this);
self.item = null;
self.quantity = 0;
self.selected = false;
var background = self.attachAsset('inventorySlot', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.9,
scaleY: 0.9
});
self.icon = null;
self.itemLabel = new Text2('', {
size: 42,
fill: 0xFF0000
});
self.itemLabel.anchor.set(0.5, 0);
self.itemLabel.y = 90;
self.addChild(self.itemLabel);
self.quantityLabel = new Text2('0', {
size: 40,
fill: 0xFFFFFF
});
self.quantityLabel.anchor.set(1, 1);
self.quantityLabel.x = 60;
self.quantityLabel.y = 60;
self.addChild(self.quantityLabel);
self.setItem = function (itemType, amount) {
self.item = itemType;
self.quantity = amount;
// Remove previous icon if exists
if (self.icon) {
self.removeChild(self.icon);
self.icon = null;
}
// Create new icon
if (itemType) {
var iconAssetId = '';
if (itemType === 'wheatSeed' || itemType === 'cornSeed' || itemType === 'potatoSeed') {
iconAssetId = 'seedIcon';
} else if (itemType === 'water') {
iconAssetId = 'waterIcon';
} else if (itemType === 'fertilizer') {
iconAssetId = 'fertilizerIcon';
} else if (itemType === 'wheat' || itemType === 'corn' || itemType === 'potato') {
iconAssetId = 'foodIcon';
}
if (iconAssetId) {
self.icon = LK.getAsset(iconAssetId, {
anchorX: 0.5,
anchorY: 0.5
});
// Set icon tint based on item type
if (itemType === 'wheatSeed') {
self.icon.tint = 0xFFD700; // Gold
} else if (itemType === 'cornSeed') {
self.icon.tint = 0xFFFF00; // Yellow
} else if (itemType === 'potatoSeed') {
self.icon.tint = 0xCD853F; // Peru
} else if (itemType === 'wheat') {
self.icon.tint = 0xDAA520; // Golden rod
} else if (itemType === 'corn') {
self.icon.tint = 0xF0E68C; // Khaki
} else if (itemType === 'potato') {
self.icon.tint = 0xD2B48C; // Tan
}
self.addChild(self.icon);
}
}
// Update labels
self.itemLabel.setText(itemType ? itemType : '');
self.quantityLabel.setText(amount > 0 ? amount.toString() : '');
};
self.addItems = function (amount) {
if (self.item) {
self.quantity += amount;
self.quantityLabel.setText(self.quantity.toString());
return true;
}
return false;
};
self.removeItems = function (amount) {
if (self.item && self.quantity >= amount) {
self.quantity -= amount;
self.quantityLabel.setText(self.quantity > 0 ? self.quantity.toString() : '');
if (self.quantity <= 0) {
self.setItem(null, 0);
}
return true;
}
return false;
};
self.select = function () {
self.selected = true;
background.tint = 0xAAAAAA;
};
self.deselect = function () {
self.selected = false;
background.tint = 0xFFFFFF;
};
self.down = function (x, y, obj) {
if (game && !self.selected && self.item && self.quantity > 0) {
// Deselect any previously selected slot
if (selectedInventorySlot) {
selectedInventorySlot.deselect();
}
// Select this slot
selectedInventorySlot = self;
self.select();
}
};
return self;
});
var Plot = Container.expand(function () {
var self = Container.call(this);
self.planted = false;
self.growthStage = 0;
self.growthTime = 0;
self.maxGrowthTime = 100; // 5 seconds at 60 FPS
self.cropType = null;
var dirtGraphics = self.attachAsset('dirt', {
anchorX: 0.5,
anchorY: 0.5
});
self.seedling = self.attachAsset('seedling', {
anchorX: 0.5,
anchorY: 0.5,
visible: false
});
self.crop = self.attachAsset('crop', {
anchorX: 0.5,
anchorY: 0.5,
visible: false
});
self.plant = function (cropType) {
if (!self.planted) {
self.planted = true;
self.growthStage = 0;
self.growthTime = 0;
self.cropType = cropType;
self.seedling.visible = true;
self.crop.visible = false;
// Change seedling color based on crop type
if (cropType === 'wheat') {
self.seedling.tint = 0x00FF00; // Green
self.crop.tint = 0xFFD700; // Gold
self.maxGrowthTime = 100; // 5 seconds
} else if (cropType === 'corn') {
self.seedling.tint = 0x32CD32; // Lime Green
self.crop.tint = 0xFFFF00; // Yellow
self.maxGrowthTime = 200; // 7 seconds
} else if (cropType === 'potato') {
self.seedling.tint = 0x556B2F; // Dark Olive Green
self.crop.tint = 0xCD853F; // Peru
self.maxGrowthTime = 240; // 4 seconds
}
LK.getSound('plantSound').play();
return true;
}
return false;
};
self.harvest = function () {
if (self.planted && self.growthStage === 2) {
LK.getSound('harvestSound').play();
var harvestYield = {
type: self.cropType,
quantity: 1
};
// Reset plot state
self.planted = false;
self.growthStage = 0;
self.growthTime = 0;
self.cropType = null;
self.seedling.visible = false;
self.crop.visible = false;
return harvestYield;
}
return null;
};
self.update = function () {
if (self.planted) {
self.growthTime++;
// Handle growth stages
if (self.growthTime >= self.maxGrowthTime && self.growthStage < 2) {
self.growthStage = 2; // Fully grown
self.seedling.visible = false;
self.crop.visible = true;
} else if (self.growthTime >= self.maxGrowthTime / 2 && self.growthStage < 1) {
self.growthStage = 1; // Middle stage
self.seedling.scale.set(1.3, 1.3);
}
}
};
self.down = function (x, y, obj) {
// Will be handled by game's plot interaction logic
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x5DA130 // Green background for farm theme
});
/****
* Game Code
****/
// Game state
var gameDay = 1;
var energy = 200;
var maxEnergy = 200;
var weatherCondition = 'sunny'; // sunny, cloudy, rainy, stormy
var weatherEffects = {
'sunny': {
growthMultiplier: 1.2,
energyDrain: 1.0
},
'cloudy': {
growthMultiplier: 1.0,
energyDrain: 0.8
},
'rainy': {
growthMultiplier: 1.5,
energyDrain: 1.2
},
'stormy': {
growthMultiplier: 0.7,
energyDrain: 1.5
}
};
var selectedInventorySlot = null;
var dayTimer = 0;
var dayLength = 36000; // 60 seconds at 60 FPS
var endOfDayTriggered = false;
// Create farm grid
var gridSize = 6;
var plotSize = 300;
var gridStartX = (2400 - gridSize * plotSize) / 2;
var gridStartY = 400;
var plots = [];
// Create plots
for (var row = 0; row < gridSize; row++) {
for (var col = 0; col < gridSize; col++) {
var plot = new Plot();
plot.x = gridStartX + col * plotSize;
plot.y = gridStartY + row * plotSize;
plot.gridX = col;
plot.gridY = row;
plots.push(plot);
game.addChild(plot);
}
}
// Create inventory system
var inventorySize = 8;
var inventorySlots = [];
var inventoryStartX = (2550 - inventorySize * 240) / 2; // Adjust starting position to account for larger slots
var inventoryStartY = 2400;
// Create inventory slots
for (var i = 0; i < inventorySize; i++) {
var slot = new InventorySlot();
slot.x = inventoryStartX + i * 300; // Increase spacing between slots
slot.y = inventoryStartY;
slot.slotIndex = i;
inventorySlots.push(slot);
game.addChild(slot);
}
// Initialize inventory with starting items
inventorySlots[0].setItem('wheatSeed', 10);
inventorySlots[1].setItem('cornSeed', 10);
inventorySlots[2].setItem('potatoSeed', 10);
inventorySlots[3].setItem('water', 10);
inventorySlots[4].setItem('fertilizer', 10);
// Create UI elements
// Day counter
var dayCounter = new Text2('Day: 1', {
size: 60,
fill: 0xFFFFFF
});
dayCounter.anchor.set(0.5, 0.5);
var dayCounterBg = LK.getAsset('dayCounterBg', {
anchorX: 0.5,
anchorY: 0.5
});
dayCounterBg.x = 120;
dayCounterBg.y = 60;
dayCounter.x = 120;
dayCounter.y = 60;
game.addChild(dayCounterBg);
game.addChild(dayCounter);
// Weather indicator
var weatherIcon = LK.getAsset('weatherIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
weatherIcon.x = 230;
weatherIcon.y = 60;
game.addChild(weatherIcon);
// Energy bar
var energyBarBg = LK.getAsset('energyBarBg', {
anchorX: 0,
anchorY: 0.5
});
energyBarBg.x = 300;
energyBarBg.y = 60;
game.addChild(energyBarBg);
var energyBarFill = LK.getAsset('energyBar', {
anchorX: 0,
anchorY: 0.5
});
energyBarFill.x = 300;
energyBarFill.y = 60;
game.addChild(energyBarFill);
var energyLabel = new Text2('Energy: 100/100', {
size: 40,
fill: 0xFFFFFF
});
energyLabel.anchor.set(0.5, 0.5);
energyLabel.x = 550;
energyLabel.y = 60;
game.addChild(energyLabel);
// Instructions
var instructionsText = new Text2('Select seeds from inventory and tap plots to plant. Harvest when crops are fully grown.', {
size: 50,
fill: 0xFFFFFF
});
instructionsText.anchor.set(0.5, 0);
instructionsText.x = 2048 / 2;
instructionsText.y = 2150;
game.addChild(instructionsText);
// Stats display
var statsText = new Text2('Harvests: 0', {
size: 50,
fill: 0xFFFFFF
});
statsText.anchor.set(0.5, 0);
statsText.x = 2048 / 2;
statsText.y = 200;
game.addChild(statsText);
// Helper functions
function updateEnergyDisplay() {
energyBarFill.scale.x = energy / maxEnergy;
energyLabel.setText("Energy: ".concat(Math.floor(energy), "/").concat(maxEnergy));
}
function updateWeatherDisplay() {
if (weatherCondition === 'sunny') {
weatherIcon.tint = 0xFFFF00; // Yellow
} else if (weatherCondition === 'cloudy') {
weatherIcon.tint = 0xCCCCCC; // Gray
} else if (weatherCondition === 'rainy') {
weatherIcon.tint = 0x4682B4; // Steel Blue
} else if (weatherCondition === 'stormy') {
weatherIcon.tint = 0x191970; // Midnight Blue
}
}
function changeWeather() {
var weatherOptions = ['sunny', 'cloudy', 'rainy', 'stormy'];
var weights = [4, 3, 2, 1];
// Adjust weights based on day number (increase difficulty)
if (gameDay > 5) {
weights = [3, 3, 3, 2];
}
if (gameDay > 10) {
weights = [2, 3, 3, 3];
}
// Create weighted selection array
var selectionArray = [];
for (var i = 0; i < weatherOptions.length; i++) {
for (var j = 0; j < weights[i]; j++) {
selectionArray.push(weatherOptions[i]);
}
}
// Select random weather
var randomIndex = Math.floor(Math.random() * selectionArray.length);
weatherCondition = selectionArray[randomIndex];
LK.getSound('weatherChangeSound').play();
updateWeatherDisplay();
}
function startNewDay() {
gameDay++;
dayCounter.setText("Day: ".concat(gameDay));
energy = maxEnergy;
updateEnergyDisplay();
changeWeather();
dayTimer = 0;
endOfDayTriggered = false;
// Record highest day if applicable
if (gameDay > storage.highestDay) {
storage.highestDay = gameDay;
}
// Add some random seeds based on day progression
var seedChance = 0.5 + gameDay * 0.05;
if (Math.random() < seedChance) {
// Find empty slot
var emptySlot = inventorySlots.find(function (slot) {
return !slot.item;
});
if (emptySlot) {
var seedTypes = ['wheatSeed', 'cornSeed', 'potatoSeed'];
var randomSeed = seedTypes[Math.floor(Math.random() * seedTypes.length)];
var amount = 1 + Math.floor(Math.random() * Math.min(gameDay, 5));
emptySlot.setItem(randomSeed, amount);
}
}
// Add new challenge effects as days progress
if (gameDay > 3) {
// Reduce energy max slightly each day
maxEnergy = Math.max(50, 100 - (gameDay - 3) * 5);
energy = maxEnergy;
updateEnergyDisplay();
}
}
function endDay() {
// Check game over condition
var hasSeeds = inventorySlots.some(function (slot) {
return slot.item && (slot.item === 'wheatSeed' || slot.item === 'cornSeed' || slot.item === 'potatoSeed') && slot.quantity > 0;
});
var hasFood = inventorySlots.some(function (slot) {
return slot.item && (slot.item === 'wheat' || slot.item === 'corn' || slot.item === 'potato') && slot.quantity > 0;
});
var hasGrowingCrops = plots.some(function (plot) {
return plot.planted;
});
if (!hasSeeds && !hasFood && !hasGrowingCrops) {
// Game over - no way to continue
statsText.setText("Game Over! You survived ".concat(gameDay, " days. Total harvests: ").concat(storage.totalHarvests));
LK.showGameOver();
return;
}
// Consume food at end of day
var foodConsumed = false;
for (var _i = 0, _inventorySlots = inventorySlots; _i < _inventorySlots.length; _i++) {
var slot = _inventorySlots[_i];
if (slot.item === 'wheat' || slot.item === 'corn' || slot.item === 'potato') {
slot.removeItems(1);
foodConsumed = true;
break;
}
}
if (!foodConsumed) {
// Game over - no food to eat
statsText.setText("Game Over! You survived ".concat(gameDay, " days but starved. Total harvests: ").concat(storage.totalHarvests));
LK.showGameOver();
return;
}
// Start new day
startNewDay();
}
// Event handlers
game.down = function (x, y, obj) {
// Check if we tapped an inventory slot
for (var i = 0; i < inventorySlots.length; i++) {
var slot = inventorySlots[i];
if (slot.getBounds().contains(x, y)) {
// Deselect previous slot
if (selectedInventorySlot) {
selectedInventorySlot.deselect();
}
// Select new slot if it has items
if (slot.item && slot.quantity > 0) {
selectedInventorySlot = slot;
slot.select();
} else {
selectedInventorySlot = null;
}
return;
}
}
// Check if we tapped a plot
for (var j = 0; j < plots.length; j++) {
var plot = plots[j];
if (plot.getBounds().contains(x, y)) {
// If we have a selected inventory item
if (selectedInventorySlot && selectedInventorySlot.item) {
var item = selectedInventorySlot.item;
// Handle planting
if ((item === 'wheatSeed' || item === 'cornSeed' || item === 'potatoSeed') && !plot.planted) {
if (energy >= 10) {
var cropType = item.replace('Seed', '');
if (plot.plant(cropType)) {
selectedInventorySlot.removeItems(1);
energy -= 10;
updateEnergyDisplay();
}
}
}
// Handle harvesting
else if (plot.planted && plot.growthStage === 2) {
var harvestResult = plot.harvest();
if (harvestResult) {
// Find empty slot or matching crop slot
var targetSlot = inventorySlots.find(function (slot) {
return !slot.item || slot.item === harvestResult.type;
});
if (targetSlot) {
if (!targetSlot.item) {
targetSlot.setItem(harvestResult.type, harvestResult.quantity);
} else {
targetSlot.addItems(harvestResult.quantity);
}
energy -= 5;
updateEnergyDisplay();
// Update harvests counter
storage.totalHarvests++;
statsText.setText("Harvests: ".concat(storage.totalHarvests));
}
}
}
// Handle water
else if (item === 'water' && plot.planted) {
if (energy >= 5) {
selectedInventorySlot.removeItems(1);
plot.growthTime += 60; // Boost growth
energy -= 5;
updateEnergyDisplay();
}
}
// Handle fertilizer
else if (item === 'fertilizer' && plot.planted) {
if (energy >= 5) {
selectedInventorySlot.removeItems(1);
plot.growthTime += 120; // Bigger boost than water
energy -= 5;
updateEnergyDisplay();
}
}
// If selected slot is now empty, deselect it
if (selectedInventorySlot && selectedInventorySlot.quantity <= 0) {
selectedInventorySlot.deselect();
selectedInventorySlot = null;
}
}
return;
}
}
};
// Start playing music
LK.playMusic('farmMusic');
// Game update loop
game.update = function () {
// Update day timer
dayTimer++;
// Update energy decay based on weather
var weatherEffect = weatherEffects[weatherCondition];
energy -= 0.01 * weatherEffect.energyDrain;
energy = Math.max(0, energy);
updateEnergyDisplay();
// Check for end of day
if (dayTimer >= dayLength && !endOfDayTriggered) {
endOfDayTriggered = true;
endDay();
}
// Update all plots
for (var i = 0; i < plots.length; i++) {
// Apply weather growth multiplier directly without creating a new function
var plot = plots[i];
if (plot.planted) {
plot.growthTime += weatherEffect.growthMultiplier - 1;
}
plot.update();
}
// Game over if energy reaches zero
if (energy <= 0) {
statsText.setText("Game Over! You ran out of energy on day ".concat(gameDay, ". Total harvests: ").concat(storage.totalHarvests));
LK.showGameOver();
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highestDay: 1,
totalHarvests: 0
});
/****
* Classes
****/
var InventorySlot = Container.expand(function () {
var self = Container.call(this);
self.item = null;
self.quantity = 0;
self.selected = false;
var background = self.attachAsset('inventorySlot', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.9,
scaleY: 0.9
});
self.icon = null;
self.itemLabel = new Text2('', {
size: 42,
fill: 0xFF0000
});
self.itemLabel.anchor.set(0.5, 0);
self.itemLabel.y = 90;
self.addChild(self.itemLabel);
self.quantityLabel = new Text2('0', {
size: 40,
fill: 0xFFFFFF
});
self.quantityLabel.anchor.set(1, 1);
self.quantityLabel.x = 60;
self.quantityLabel.y = 60;
self.addChild(self.quantityLabel);
self.setItem = function (itemType, amount) {
self.item = itemType;
self.quantity = amount;
// Remove previous icon if exists
if (self.icon) {
self.removeChild(self.icon);
self.icon = null;
}
// Create new icon
if (itemType) {
var iconAssetId = '';
if (itemType === 'wheatSeed' || itemType === 'cornSeed' || itemType === 'potatoSeed') {
iconAssetId = 'seedIcon';
} else if (itemType === 'water') {
iconAssetId = 'waterIcon';
} else if (itemType === 'fertilizer') {
iconAssetId = 'fertilizerIcon';
} else if (itemType === 'wheat' || itemType === 'corn' || itemType === 'potato') {
iconAssetId = 'foodIcon';
}
if (iconAssetId) {
self.icon = LK.getAsset(iconAssetId, {
anchorX: 0.5,
anchorY: 0.5
});
// Set icon tint based on item type
if (itemType === 'wheatSeed') {
self.icon.tint = 0xFFD700; // Gold
} else if (itemType === 'cornSeed') {
self.icon.tint = 0xFFFF00; // Yellow
} else if (itemType === 'potatoSeed') {
self.icon.tint = 0xCD853F; // Peru
} else if (itemType === 'wheat') {
self.icon.tint = 0xDAA520; // Golden rod
} else if (itemType === 'corn') {
self.icon.tint = 0xF0E68C; // Khaki
} else if (itemType === 'potato') {
self.icon.tint = 0xD2B48C; // Tan
}
self.addChild(self.icon);
}
}
// Update labels
self.itemLabel.setText(itemType ? itemType : '');
self.quantityLabel.setText(amount > 0 ? amount.toString() : '');
};
self.addItems = function (amount) {
if (self.item) {
self.quantity += amount;
self.quantityLabel.setText(self.quantity.toString());
return true;
}
return false;
};
self.removeItems = function (amount) {
if (self.item && self.quantity >= amount) {
self.quantity -= amount;
self.quantityLabel.setText(self.quantity > 0 ? self.quantity.toString() : '');
if (self.quantity <= 0) {
self.setItem(null, 0);
}
return true;
}
return false;
};
self.select = function () {
self.selected = true;
background.tint = 0xAAAAAA;
};
self.deselect = function () {
self.selected = false;
background.tint = 0xFFFFFF;
};
self.down = function (x, y, obj) {
if (game && !self.selected && self.item && self.quantity > 0) {
// Deselect any previously selected slot
if (selectedInventorySlot) {
selectedInventorySlot.deselect();
}
// Select this slot
selectedInventorySlot = self;
self.select();
}
};
return self;
});
var Plot = Container.expand(function () {
var self = Container.call(this);
self.planted = false;
self.growthStage = 0;
self.growthTime = 0;
self.maxGrowthTime = 100; // 5 seconds at 60 FPS
self.cropType = null;
var dirtGraphics = self.attachAsset('dirt', {
anchorX: 0.5,
anchorY: 0.5
});
self.seedling = self.attachAsset('seedling', {
anchorX: 0.5,
anchorY: 0.5,
visible: false
});
self.crop = self.attachAsset('crop', {
anchorX: 0.5,
anchorY: 0.5,
visible: false
});
self.plant = function (cropType) {
if (!self.planted) {
self.planted = true;
self.growthStage = 0;
self.growthTime = 0;
self.cropType = cropType;
self.seedling.visible = true;
self.crop.visible = false;
// Change seedling color based on crop type
if (cropType === 'wheat') {
self.seedling.tint = 0x00FF00; // Green
self.crop.tint = 0xFFD700; // Gold
self.maxGrowthTime = 100; // 5 seconds
} else if (cropType === 'corn') {
self.seedling.tint = 0x32CD32; // Lime Green
self.crop.tint = 0xFFFF00; // Yellow
self.maxGrowthTime = 200; // 7 seconds
} else if (cropType === 'potato') {
self.seedling.tint = 0x556B2F; // Dark Olive Green
self.crop.tint = 0xCD853F; // Peru
self.maxGrowthTime = 240; // 4 seconds
}
LK.getSound('plantSound').play();
return true;
}
return false;
};
self.harvest = function () {
if (self.planted && self.growthStage === 2) {
LK.getSound('harvestSound').play();
var harvestYield = {
type: self.cropType,
quantity: 1
};
// Reset plot state
self.planted = false;
self.growthStage = 0;
self.growthTime = 0;
self.cropType = null;
self.seedling.visible = false;
self.crop.visible = false;
return harvestYield;
}
return null;
};
self.update = function () {
if (self.planted) {
self.growthTime++;
// Handle growth stages
if (self.growthTime >= self.maxGrowthTime && self.growthStage < 2) {
self.growthStage = 2; // Fully grown
self.seedling.visible = false;
self.crop.visible = true;
} else if (self.growthTime >= self.maxGrowthTime / 2 && self.growthStage < 1) {
self.growthStage = 1; // Middle stage
self.seedling.scale.set(1.3, 1.3);
}
}
};
self.down = function (x, y, obj) {
// Will be handled by game's plot interaction logic
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x5DA130 // Green background for farm theme
});
/****
* Game Code
****/
// Game state
var gameDay = 1;
var energy = 200;
var maxEnergy = 200;
var weatherCondition = 'sunny'; // sunny, cloudy, rainy, stormy
var weatherEffects = {
'sunny': {
growthMultiplier: 1.2,
energyDrain: 1.0
},
'cloudy': {
growthMultiplier: 1.0,
energyDrain: 0.8
},
'rainy': {
growthMultiplier: 1.5,
energyDrain: 1.2
},
'stormy': {
growthMultiplier: 0.7,
energyDrain: 1.5
}
};
var selectedInventorySlot = null;
var dayTimer = 0;
var dayLength = 36000; // 60 seconds at 60 FPS
var endOfDayTriggered = false;
// Create farm grid
var gridSize = 6;
var plotSize = 300;
var gridStartX = (2400 - gridSize * plotSize) / 2;
var gridStartY = 400;
var plots = [];
// Create plots
for (var row = 0; row < gridSize; row++) {
for (var col = 0; col < gridSize; col++) {
var plot = new Plot();
plot.x = gridStartX + col * plotSize;
plot.y = gridStartY + row * plotSize;
plot.gridX = col;
plot.gridY = row;
plots.push(plot);
game.addChild(plot);
}
}
// Create inventory system
var inventorySize = 8;
var inventorySlots = [];
var inventoryStartX = (2550 - inventorySize * 240) / 2; // Adjust starting position to account for larger slots
var inventoryStartY = 2400;
// Create inventory slots
for (var i = 0; i < inventorySize; i++) {
var slot = new InventorySlot();
slot.x = inventoryStartX + i * 300; // Increase spacing between slots
slot.y = inventoryStartY;
slot.slotIndex = i;
inventorySlots.push(slot);
game.addChild(slot);
}
// Initialize inventory with starting items
inventorySlots[0].setItem('wheatSeed', 10);
inventorySlots[1].setItem('cornSeed', 10);
inventorySlots[2].setItem('potatoSeed', 10);
inventorySlots[3].setItem('water', 10);
inventorySlots[4].setItem('fertilizer', 10);
// Create UI elements
// Day counter
var dayCounter = new Text2('Day: 1', {
size: 60,
fill: 0xFFFFFF
});
dayCounter.anchor.set(0.5, 0.5);
var dayCounterBg = LK.getAsset('dayCounterBg', {
anchorX: 0.5,
anchorY: 0.5
});
dayCounterBg.x = 120;
dayCounterBg.y = 60;
dayCounter.x = 120;
dayCounter.y = 60;
game.addChild(dayCounterBg);
game.addChild(dayCounter);
// Weather indicator
var weatherIcon = LK.getAsset('weatherIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
weatherIcon.x = 230;
weatherIcon.y = 60;
game.addChild(weatherIcon);
// Energy bar
var energyBarBg = LK.getAsset('energyBarBg', {
anchorX: 0,
anchorY: 0.5
});
energyBarBg.x = 300;
energyBarBg.y = 60;
game.addChild(energyBarBg);
var energyBarFill = LK.getAsset('energyBar', {
anchorX: 0,
anchorY: 0.5
});
energyBarFill.x = 300;
energyBarFill.y = 60;
game.addChild(energyBarFill);
var energyLabel = new Text2('Energy: 100/100', {
size: 40,
fill: 0xFFFFFF
});
energyLabel.anchor.set(0.5, 0.5);
energyLabel.x = 550;
energyLabel.y = 60;
game.addChild(energyLabel);
// Instructions
var instructionsText = new Text2('Select seeds from inventory and tap plots to plant. Harvest when crops are fully grown.', {
size: 50,
fill: 0xFFFFFF
});
instructionsText.anchor.set(0.5, 0);
instructionsText.x = 2048 / 2;
instructionsText.y = 2150;
game.addChild(instructionsText);
// Stats display
var statsText = new Text2('Harvests: 0', {
size: 50,
fill: 0xFFFFFF
});
statsText.anchor.set(0.5, 0);
statsText.x = 2048 / 2;
statsText.y = 200;
game.addChild(statsText);
// Helper functions
function updateEnergyDisplay() {
energyBarFill.scale.x = energy / maxEnergy;
energyLabel.setText("Energy: ".concat(Math.floor(energy), "/").concat(maxEnergy));
}
function updateWeatherDisplay() {
if (weatherCondition === 'sunny') {
weatherIcon.tint = 0xFFFF00; // Yellow
} else if (weatherCondition === 'cloudy') {
weatherIcon.tint = 0xCCCCCC; // Gray
} else if (weatherCondition === 'rainy') {
weatherIcon.tint = 0x4682B4; // Steel Blue
} else if (weatherCondition === 'stormy') {
weatherIcon.tint = 0x191970; // Midnight Blue
}
}
function changeWeather() {
var weatherOptions = ['sunny', 'cloudy', 'rainy', 'stormy'];
var weights = [4, 3, 2, 1];
// Adjust weights based on day number (increase difficulty)
if (gameDay > 5) {
weights = [3, 3, 3, 2];
}
if (gameDay > 10) {
weights = [2, 3, 3, 3];
}
// Create weighted selection array
var selectionArray = [];
for (var i = 0; i < weatherOptions.length; i++) {
for (var j = 0; j < weights[i]; j++) {
selectionArray.push(weatherOptions[i]);
}
}
// Select random weather
var randomIndex = Math.floor(Math.random() * selectionArray.length);
weatherCondition = selectionArray[randomIndex];
LK.getSound('weatherChangeSound').play();
updateWeatherDisplay();
}
function startNewDay() {
gameDay++;
dayCounter.setText("Day: ".concat(gameDay));
energy = maxEnergy;
updateEnergyDisplay();
changeWeather();
dayTimer = 0;
endOfDayTriggered = false;
// Record highest day if applicable
if (gameDay > storage.highestDay) {
storage.highestDay = gameDay;
}
// Add some random seeds based on day progression
var seedChance = 0.5 + gameDay * 0.05;
if (Math.random() < seedChance) {
// Find empty slot
var emptySlot = inventorySlots.find(function (slot) {
return !slot.item;
});
if (emptySlot) {
var seedTypes = ['wheatSeed', 'cornSeed', 'potatoSeed'];
var randomSeed = seedTypes[Math.floor(Math.random() * seedTypes.length)];
var amount = 1 + Math.floor(Math.random() * Math.min(gameDay, 5));
emptySlot.setItem(randomSeed, amount);
}
}
// Add new challenge effects as days progress
if (gameDay > 3) {
// Reduce energy max slightly each day
maxEnergy = Math.max(50, 100 - (gameDay - 3) * 5);
energy = maxEnergy;
updateEnergyDisplay();
}
}
function endDay() {
// Check game over condition
var hasSeeds = inventorySlots.some(function (slot) {
return slot.item && (slot.item === 'wheatSeed' || slot.item === 'cornSeed' || slot.item === 'potatoSeed') && slot.quantity > 0;
});
var hasFood = inventorySlots.some(function (slot) {
return slot.item && (slot.item === 'wheat' || slot.item === 'corn' || slot.item === 'potato') && slot.quantity > 0;
});
var hasGrowingCrops = plots.some(function (plot) {
return plot.planted;
});
if (!hasSeeds && !hasFood && !hasGrowingCrops) {
// Game over - no way to continue
statsText.setText("Game Over! You survived ".concat(gameDay, " days. Total harvests: ").concat(storage.totalHarvests));
LK.showGameOver();
return;
}
// Consume food at end of day
var foodConsumed = false;
for (var _i = 0, _inventorySlots = inventorySlots; _i < _inventorySlots.length; _i++) {
var slot = _inventorySlots[_i];
if (slot.item === 'wheat' || slot.item === 'corn' || slot.item === 'potato') {
slot.removeItems(1);
foodConsumed = true;
break;
}
}
if (!foodConsumed) {
// Game over - no food to eat
statsText.setText("Game Over! You survived ".concat(gameDay, " days but starved. Total harvests: ").concat(storage.totalHarvests));
LK.showGameOver();
return;
}
// Start new day
startNewDay();
}
// Event handlers
game.down = function (x, y, obj) {
// Check if we tapped an inventory slot
for (var i = 0; i < inventorySlots.length; i++) {
var slot = inventorySlots[i];
if (slot.getBounds().contains(x, y)) {
// Deselect previous slot
if (selectedInventorySlot) {
selectedInventorySlot.deselect();
}
// Select new slot if it has items
if (slot.item && slot.quantity > 0) {
selectedInventorySlot = slot;
slot.select();
} else {
selectedInventorySlot = null;
}
return;
}
}
// Check if we tapped a plot
for (var j = 0; j < plots.length; j++) {
var plot = plots[j];
if (plot.getBounds().contains(x, y)) {
// If we have a selected inventory item
if (selectedInventorySlot && selectedInventorySlot.item) {
var item = selectedInventorySlot.item;
// Handle planting
if ((item === 'wheatSeed' || item === 'cornSeed' || item === 'potatoSeed') && !plot.planted) {
if (energy >= 10) {
var cropType = item.replace('Seed', '');
if (plot.plant(cropType)) {
selectedInventorySlot.removeItems(1);
energy -= 10;
updateEnergyDisplay();
}
}
}
// Handle harvesting
else if (plot.planted && plot.growthStage === 2) {
var harvestResult = plot.harvest();
if (harvestResult) {
// Find empty slot or matching crop slot
var targetSlot = inventorySlots.find(function (slot) {
return !slot.item || slot.item === harvestResult.type;
});
if (targetSlot) {
if (!targetSlot.item) {
targetSlot.setItem(harvestResult.type, harvestResult.quantity);
} else {
targetSlot.addItems(harvestResult.quantity);
}
energy -= 5;
updateEnergyDisplay();
// Update harvests counter
storage.totalHarvests++;
statsText.setText("Harvests: ".concat(storage.totalHarvests));
}
}
}
// Handle water
else if (item === 'water' && plot.planted) {
if (energy >= 5) {
selectedInventorySlot.removeItems(1);
plot.growthTime += 60; // Boost growth
energy -= 5;
updateEnergyDisplay();
}
}
// Handle fertilizer
else if (item === 'fertilizer' && plot.planted) {
if (energy >= 5) {
selectedInventorySlot.removeItems(1);
plot.growthTime += 120; // Bigger boost than water
energy -= 5;
updateEnergyDisplay();
}
}
// If selected slot is now empty, deselect it
if (selectedInventorySlot && selectedInventorySlot.quantity <= 0) {
selectedInventorySlot.deselect();
selectedInventorySlot = null;
}
}
return;
}
}
};
// Start playing music
LK.playMusic('farmMusic');
// Game update loop
game.update = function () {
// Update day timer
dayTimer++;
// Update energy decay based on weather
var weatherEffect = weatherEffects[weatherCondition];
energy -= 0.01 * weatherEffect.energyDrain;
energy = Math.max(0, energy);
updateEnergyDisplay();
// Check for end of day
if (dayTimer >= dayLength && !endOfDayTriggered) {
endOfDayTriggered = true;
endDay();
}
// Update all plots
for (var i = 0; i < plots.length; i++) {
// Apply weather growth multiplier directly without creating a new function
var plot = plots[i];
if (plot.planted) {
plot.growthTime += weatherEffect.growthMultiplier - 1;
}
plot.update();
}
// Game over if energy reaches zero
if (energy <= 0) {
statsText.setText("Game Over! You ran out of energy on day ".concat(gameDay, ". Total harvests: ").concat(storage.totalHarvests));
LK.showGameOver();
}
};