/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Plant = Container.expand(function () {
var self = Container.call(this);
var dirtGraphics = self.attachAsset('Soil', {
anchorX: 0.5,
anchorY: 0.5
});
self.maxGrowthTime = 60000; // 60 seconds in milliseconds
self.growthTimeRemaining = 0;
self.isReady = false;
self.gridX = 0;
self.gridY = 0;
self.seedPlanted = false;
self.harvestCount = 0;
self.growthTimer = null;
self.timerDisplay = null;
self.initialize = function () {
self.growthTimeRemaining = self.maxGrowthTime;
self.isReady = false;
self.seedPlanted = true;
// Don't reset plantedSeedType here - it's already set before initialize is called
self.harvestCount = Math.floor(Math.random() * 3) + 1; // Random 1-3 coins
};
self.update = function () {
if (self.seedPlanted && !self.isReady) {
self.growthTimeRemaining -= 1000 / 60; // Subtract time per frame (60 FPS)
if (self.timerDisplay) {
var secondsRemaining = Math.ceil(self.growthTimeRemaining / 1000);
self.timerDisplay.setText(secondsRemaining + 's');
}
if (self.growthTimeRemaining <= 0) {
self.growthTimeRemaining = 0;
self.isReady = true;
if (self.timerDisplay) {
self.timerDisplay.alpha = 0;
}
if (self.seedPlanted) {
self.spawnCoins(); //{spawnCoins_call}
}
}
}
};
self.harvest = function () {
if (self.isReady) {
self.harvestCount++;
self.isReady = false;
self.seedPlanted = false;
self.growthTimeRemaining = self.maxGrowthTime;
if (self.timerDisplay) {
self.timerDisplay.alpha = 0;
}
return true;
}
return false;
};
self.spawnCoins = function () {
// Only spawn if a seed type was actually planted
if (!self.plantedSeedType) {
return;
}
var coinCount = Math.floor(Math.random() * 3) + 2; // Random 2-4 coins
var seedCount = Math.floor(Math.random() * 3) + 1; // Random 1-3 seeds
for (var c = 0; c < coinCount; c++) {
var angle = Math.PI * 2 * c / coinCount; // Distribute coins in circle
var offsetX = Math.cos(angle) * 150;
var offsetY = Math.sin(angle) * 150;
var coinAsset = LK.getAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
coinAsset.x = self.x;
coinAsset.y = self.y;
coinAsset.alpha = 1;
coinAsset.isCollectable = true;
coinAsset.coinValue = 1;
gameInstance.addChild(coinAsset);
spawnedCoins.push(coinAsset);
// First bounce up
tween(coinAsset, {
y: self.y - 100
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFirstBounce() {
// Fall back down
tween(coinAsset, {
y: self.y
}, {
duration: 200,
easing: tween.easeIn,
onFinish: function onFirstLand() {
// Second bounce up
tween(coinAsset, {
y: self.y - 60
}, {
duration: 250,
easing: tween.easeOut,
onFinish: function onSecondBounce() {
// Fall back down and stay still
tween(coinAsset, {
y: self.y
}, {
duration: 150,
easing: tween.easeIn
});
}
});
}
});
}
});
}
var actualSeedCount = Math.floor(Math.random() * 3) + 1; // Random 1-3 seeds
for (var s = 0; s < actualSeedCount; s++) {
var seedType = self.plantedSeedType; // Use the planted seed type
if (!seedType) {
seedType = 'carrotseed'; // Default fallback only if no seed type exists
}
var seedAsset = LK.getAsset(seedType, {
anchorX: 0.5,
//{seedAsset_a}
anchorY: 0.5 //{seedAsset_b}
}); //{seedAsset_c}
seedAsset.plantedSeedType = seedType; // Track the seed type on spawned asset
seedAsset.x = self.x;
seedAsset.y = self.y;
seedAsset.alpha = 1;
seedAsset.isCollectable = true;
seedAsset.seedValue = 1;
gameInstance.addChild(seedAsset);
spawnedSeeds.push(seedAsset);
// First bounce up
tween(seedAsset, {
//{seedAsset_d}
y: self.y - 100 //{seedAsset_e}
}, {
//{seedAsset_f}
duration: 300,
//{seedAsset_g}
easing: tween.easeOut,
//{seedAsset_h}
onFinish: function onFirstSeedBounce() {
// Fall back down//{seedAsset_i}
tween(seedAsset, {
//{seedAsset_j}
y: self.y //{seedAsset_k}
}, {
//{seedAsset_l}
duration: 200,
//{seedAsset_m}
easing: tween.easeIn,
//{seedAsset_n}
onFinish: function onFirstSeedLand() {
// Second bounce up//{seedAsset_o}
tween(seedAsset, {
//{seedAsset_p}
y: self.y - 60 //{seedAsset_q}
}, {
//{seedAsset_r}
duration: 250,
//{seedAsset_s}
easing: tween.easeOut,
//{seedAsset_t}
onFinish: function onSecondSeedBounce() {
// Fall back down and stay still//{seedAsset_u}
tween(seedAsset, {
//{seedAsset_v}
y: self.y //{seedAsset_w}
}, {
//{seedAsset_x}
duration: 150,
//{seedAsset_y}
easing: tween.easeIn //{seedAsset_z}
}); //{seedAsset_A}
} //{seedAsset_B}
}); //{seedAsset_C}
} //{seedAsset_D}
}); //{seedAsset_E}
} //{seedAsset_F}
}); //{seedAsset_G}
} //{seedAsset_H}
};
self.down = function (x, y, obj) {
// Handled by game-level event handler
};
self.initialize();
return self;
});
var Seed = Container.expand(function () {
var self = Container.call(this);
var slotGraphics = self.attachAsset('woodbox', {
anchorX: 0.5,
anchorY: 0.5
});
var seedGraphics = null;
self.seedType = null;
self.slotIndex = 0;
self.initialX = 0;
self.initialY = 0;
self.isDragging = false;
self.seedGraphicsLocalX = 0;
self.seedGraphicsLocalY = 0;
self.seedCount = 5;
self.seedCountText = null;
self.setSeed = function (seedType) {
if (seedGraphics) {
seedGraphics.destroy();
seedGraphics = null;
}
self.seedType = seedType;
if (seedType) {
seedGraphics = self.attachAsset(seedType, {
anchorX: 0.5,
anchorY: 0.5
});
self.seedGraphicsRef = seedGraphics;
if (!self.seedCountText) {
self.seedCountText = new Text2(self.seedCount + '/100', {
size: 48,
fill: '#00ff00',
font: "'Times New Roman'"
});
self.seedCountText.anchor.set(0.5, 0);
self.seedCountText.x = 0;
self.seedCountText.y = 130;
self.addChild(self.seedCountText);
} else {
self.seedCountText.setText(self.seedCount + '/100');
}
if (seedGraphics) {
seedGraphics.alpha = 1;
}
}
};
self.decrementSeedCount = function () {
if (self.seedCount > 0) {
self.seedCount--;
if (self.seedCountText) {
self.seedCountText.setText(self.seedCount + '/100');
}
if (self.seedCount <= 0 && seedGraphics) {
seedGraphics.alpha = 1;
}
}
};
self.getSeed = function () {
return self.seedType;
};
self.getSeedGraphics = function () {
return seedGraphics;
};
self.removeSeed = function () {
if (seedGraphics) {
seedGraphics.destroy();
seedGraphics = null;
}
self.seedType = null;
};
self.hideSeedGraphics = function () {
if (seedGraphics) {
seedGraphics.alpha = 0;
}
};
self.showSeedGraphics = function () {
if (seedGraphics) {
seedGraphics.alpha = 1;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1b4332
});
/****
* Game Code
****/
// Add background
var background = game.addChild(LK.getAsset('Background1', {
anchorX: 0.5,
anchorY: 0.5
}));
background.x = 2048 / 2;
background.y = 2732 / 2;
background.scaleX = 2048 / background.width;
background.scaleY = 2732 / background.height;
var plants = [];
var totalRewards = storage.totalRewards || 0;
var gameInstance = game;
var dirtSlotSize = 350;
var dirtSlotSpacing = 40;
var dirtGridStartX = 2048 / 2 - (5 * dirtSlotSize + 4 * dirtSlotSpacing) / 2;
var dirtGridStartY = 2732 - 900;
// Create 5 dirt slots in horizontal line at bottom (2 on sides + 3 in center)
for (var dirtIndex = 0; dirtIndex < 5; dirtIndex++) {
var plant = game.addChild(new Plant());
plant.gridX = dirtIndex;
plant.gridY = 0;
plant.x = dirtGridStartX + dirtIndex * (dirtSlotSize + dirtSlotSpacing) + dirtSlotSize / 2;
plant.y = dirtGridStartY;
plants.push(plant);
// Add many separation assets tightly surrounding each soil slot
var separationSize = 50;
var soilSize = 200;
var halfSoilSize = soilSize / 2;
var offset = 0;
// Top row separations
for (var topSepX = -halfSoilSize - offset; topSepX <= halfSoilSize + offset; topSepX += separationSize) {
var topSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
topSeparation.x = plant.x + topSepX;
topSeparation.y = plant.y - halfSoilSize - offset;
}
// Bottom row separations
for (var botSepX = -halfSoilSize - offset; botSepX <= halfSoilSize + offset; botSepX += separationSize) {
var botSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
botSeparation.x = plant.x + botSepX;
botSeparation.y = plant.y + halfSoilSize + offset;
}
// Left column separations (excluding corners already covered)
for (var leftSepY = -halfSoilSize; leftSepY <= halfSoilSize; leftSepY += separationSize) {
var leftSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
leftSeparation.x = plant.x - halfSoilSize - offset;
leftSeparation.y = plant.y + leftSepY;
}
// Right column separations (excluding corners already covered)
for (var rightSepY = -halfSoilSize; rightSepY <= halfSoilSize; rightSepY += separationSize) {
var rightSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
rightSeparation.x = plant.x + halfSoilSize + offset;
rightSeparation.y = plant.y + rightSepY;
}
}
// Create 5 dirt slots in horizontal line above the first row
var dirtGridStartYTop = dirtGridStartY - (dirtSlotSize + dirtSlotSpacing);
for (var dirtIndex = 0; dirtIndex < 5; dirtIndex++) {
var plant = game.addChild(new Plant());
plant.gridX = dirtIndex;
plant.gridY = 1;
plant.x = dirtGridStartX + dirtIndex * (dirtSlotSize + dirtSlotSpacing) + dirtSlotSize / 2;
plant.y = dirtGridStartYTop;
plants.push(plant);
// Add many separation assets tightly surrounding each soil slot
var separationSize = 50;
var soilSize = 200;
var halfSoilSize = soilSize / 2;
var offset = 0;
// Top row separations
for (var topSepX = -halfSoilSize - offset; topSepX <= halfSoilSize + offset; topSepX += separationSize) {
var topSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
topSeparation.x = plant.x + topSepX;
topSeparation.y = plant.y - halfSoilSize - offset;
}
// Bottom row separations
for (var botSepX = -halfSoilSize - offset; botSepX <= halfSoilSize + offset; botSepX += separationSize) {
var botSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
botSeparation.x = plant.x + botSepX;
botSeparation.y = plant.y + halfSoilSize + offset;
}
// Left column separations (excluding corners already covered)
for (var leftSepY = -halfSoilSize; leftSepY <= halfSoilSize; leftSepY += separationSize) {
var leftSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
leftSeparation.x = plant.x - halfSoilSize - offset;
leftSeparation.y = plant.y + leftSepY;
}
// Right column separations (excluding corners already covered)
for (var rightSepY = -halfSoilSize; rightSepY <= halfSoilSize; rightSepY += separationSize) {
var rightSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
rightSeparation.x = plant.x + halfSoilSize + offset;
rightSeparation.y = plant.y + rightSepY;
}
}
// Create 5 dirt slots in horizontal line above the second row
var dirtGridStartYTop2 = dirtGridStartYTop - (dirtSlotSize + dirtSlotSpacing);
for (var dirtIndex = 0; dirtIndex < 5; dirtIndex++) {
var plant = game.addChild(new Plant());
plant.gridX = dirtIndex;
plant.gridY = 2;
plant.x = dirtGridStartX + dirtIndex * (dirtSlotSize + dirtSlotSpacing) + dirtSlotSize / 2;
plant.y = dirtGridStartYTop2;
plants.push(plant);
// Add many separation assets tightly surrounding each soil slot
var separationSize = 50;
var soilSize = 200;
var halfSoilSize = soilSize / 2;
var offset = 0;
// Top row separations
for (var topSepX = -halfSoilSize - offset; topSepX <= halfSoilSize + offset; topSepX += separationSize) {
var topSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
topSeparation.x = plant.x + topSepX;
topSeparation.y = plant.y - halfSoilSize - offset;
}
// Bottom row separations
for (var botSepX = -halfSoilSize - offset; botSepX <= halfSoilSize + offset; botSepX += separationSize) {
var botSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
botSeparation.x = plant.x + botSepX;
botSeparation.y = plant.y + halfSoilSize + offset;
}
// Left column separations (excluding corners already covered)
for (var leftSepY = -halfSoilSize; leftSepY <= halfSoilSize; leftSepY += separationSize) {
var leftSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
leftSeparation.x = plant.x - halfSoilSize - offset;
leftSeparation.y = plant.y + leftSepY;
}
// Right column separations (excluding corners already covered)
for (var rightSepY = -halfSoilSize; rightSepY <= halfSoilSize; rightSepY += separationSize) {
var rightSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
rightSeparation.x = plant.x + halfSoilSize + offset;
rightSeparation.y = plant.y + rightSepY;
}
}
// Create 5 dirt slots in horizontal line above the third row
var dirtGridStartYTop3 = dirtGridStartYTop2 - (dirtSlotSize + dirtSlotSpacing);
for (var dirtIndex = 0; dirtIndex < 5; dirtIndex++) {
var plant = game.addChild(new Plant());
plant.gridX = dirtIndex;
plant.gridY = 3;
plant.x = dirtGridStartX + dirtIndex * (dirtSlotSize + dirtSlotSpacing) + dirtSlotSize / 2;
plant.y = dirtGridStartYTop3;
plants.push(plant);
// Add many separation assets tightly surrounding each soil slot
var separationSize = 50;
var soilSize = 200;
var halfSoilSize = soilSize / 2;
var offset = 0;
// Top row separations
for (var topSepX = -halfSoilSize - offset; topSepX <= halfSoilSize + offset; topSepX += separationSize) {
var topSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
topSeparation.x = plant.x + topSepX;
topSeparation.y = plant.y - halfSoilSize - offset;
}
// Bottom row separations
for (var botSepX = -halfSoilSize - offset; botSepX <= halfSoilSize + offset; botSepX += separationSize) {
var botSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
botSeparation.x = plant.x + botSepX;
botSeparation.y = plant.y + halfSoilSize + offset;
}
// Left column separations (excluding corners already covered)
for (var leftSepY = -halfSoilSize; leftSepY <= halfSoilSize; leftSepY += separationSize) {
var leftSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
leftSeparation.x = plant.x - halfSoilSize - offset;
leftSeparation.y = plant.y + leftSepY;
}
// Right column separations (excluding corners already covered)
for (var rightSepY = -halfSoilSize; rightSepY <= halfSoilSize; rightSepY += separationSize) {
var rightSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
rightSeparation.x = plant.x + halfSoilSize + offset;
rightSeparation.y = plant.y + rightSepY;
}
}
// Shop icon
var shopIcon = game.addChild(LK.getAsset('Shop', {
anchorX: 0.5,
anchorY: 0.5
}));
shopIcon.x = 2048 - 200;
shopIcon.y = 200;
// Score display
var scoreText = new Text2('coins: 0', {
size: 64,
fill: '#ffffff',
font: "'Times New Roman'"
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
function updateScoreDisplay() {
scoreText.setText('coins: ' + totalRewards);
storage.totalRewards = totalRewards;
}
updateScoreDisplay();
// Create individual timer displays for each plant - initially hidden
for (var i = 0; i < plants.length; i++) {
var plantTimer = new Text2('60s', {
size: 80,
fill: '#ffffff',
font: "'Times New Roman'"
});
plantTimer.anchor.set(0.5, 0.5);
plantTimer.x = plants[i].x;
plantTimer.y = plants[i].y;
plantTimer.alpha = 0;
game.addChild(plantTimer);
plants[i].timerDisplay = plantTimer;
plants[i].lastHarvested = false;
}
// Create seed slots at bottom of screen - 2 horizontal lines spanning full width
var seeds = [];
var seedSlotSize = 120;
var seedSlotSpacing = 120;
var seedsPerRow = 6;
var totalSeedSlotWidth = seedsPerRow * seedSlotSize + (seedsPerRow - 1) * seedSlotSpacing;
var seedGridStartX = 120;
var seedGridStartY = 2732 - 550;
var seedRowSpacing = 310;
var seedTypes = ['tomatoseed', 'potatoseed', 'carrotseed', 'ananasseed', 'appleseed', 'brocoliseed', 'eggplantseed', 'garlicseed', 'lemonseed', 'Orangeseed', 'pumpkinseed', 'watermelonseed'];
for (var seedIndex = 0; seedIndex < seedTypes.length; seedIndex++) {
var rowIndex = Math.floor(seedIndex / seedsPerRow);
var colIndex = seedIndex % seedsPerRow;
var seedSlot = game.addChild(new Seed());
seedSlot.slotIndex = seedIndex;
seedSlot.x = seedGridStartX + colIndex * (seedSlotSize + seedSlotSpacing) + seedSlotSize / 2;
seedSlot.y = seedGridStartY + rowIndex * seedRowSpacing;
seeds.push(seedSlot);
seedSlot.setSeed(seedTypes[seedIndex]);
}
var draggedSeed = null;
var draggedSeedStartSlot = null;
var draggedSeedDuplicate = null;
var spawnedCoins = [];
var spawnedSeeds = [];
game.down = function (x, y, obj) {
// Check if clicking on spawned coins
for (var c = spawnedCoins.length - 1; c >= 0; c--) {
var coin = spawnedCoins[c];
var coinDistance = Math.sqrt(Math.pow(x - coin.x, 2) + Math.pow(y - coin.y, 2));
if (coinDistance < 80 && coin.isCollectable) {
totalRewards += coin.coinValue;
updateScoreDisplay();
coin.isCollectable = false;
LK.getSound('collectingsound1').play();
tween(coin, {
alpha: 0,
y: coin.y - 150
}, {
duration: 400,
easing: tween.easeOut,
onFinish: function onCoinCollected() {
coin.destroy();
spawnedCoins.splice(spawnedCoins.indexOf(coin), 1);
}
});
return;
}
}
// Check if clicking on spawned seeds
for (var s = spawnedSeeds.length - 1; s >= 0; s--) {
var seed = spawnedSeeds[s];
var seedDistance = Math.sqrt(Math.pow(x - seed.x, 2) + Math.pow(y - seed.y, 2));
if (seedDistance < 80 && seed.isCollectable) {
// Add seeds back to inventory - find matching seed slot by type
var seedTypeToRestore = seed.plantedSeedType || 'carrotseed';
for (var seedSlotIdx = 0; seedSlotIdx < seeds.length; seedSlotIdx++) {
if (seeds[seedSlotIdx].seedType === seedTypeToRestore) {
seeds[seedSlotIdx].seedCount = Math.min(seeds[seedSlotIdx].seedCount + seed.seedValue, 100);
if (seeds[seedSlotIdx].seedCountText) {
seeds[seedSlotIdx].seedCountText.setText(seeds[seedSlotIdx].seedCount + '/100');
}
break;
}
}
seed.isCollectable = false;
LK.getSound('collectingsound1').play();
tween(seed, {
alpha: 0,
y: seed.y - 150
}, {
duration: 400,
easing: tween.easeOut,
onFinish: function onSeedCollected() {
seed.destroy();
spawnedSeeds.splice(spawnedSeeds.indexOf(seed), 1);
}
});
return;
}
}
// Check if clicking on a seed (not the slot itself)
for (var seedIndex = 0; seedIndex < seeds.length; seedIndex++) {
var seed = seeds[seedIndex];
// Only allow dragging if seed has a seedType and seed count > 0
if (seed.seedType && seed.seedCount > 0) {
var distance = Math.sqrt(Math.pow(x - seed.x, 2) + Math.pow(y - seed.y, 2));
// Check if click is within seed bounds (radius approximately 50 pixels for 100x100 seed)
if (distance < 60) {
draggedSeed = seed;
draggedSeedStartSlot = seedIndex;
seed.isDragging = true;
seed.initialX = seed.x;
seed.initialY = seed.y;
LK.getSound('pickupsound1').play();
var seedGraphics = seed.getSeedGraphics();
if (seedGraphics) {
tween.stop(seedGraphics);
// Create a duplicate seed graphic for dragging
draggedSeedDuplicate = LK.getAsset(seed.seedType, {
anchorX: 0.5,
anchorY: 0.5
});
draggedSeedDuplicate.x = x;
draggedSeedDuplicate.y = y;
game.addChild(draggedSeedDuplicate);
tween(seedGraphics, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 100
});
}
return;
}
}
}
for (var i = 0; i < plants.length; i++) {
var plant = plants[i];
var distance = Math.sqrt(Math.pow(x - plant.x, 2) + Math.pow(y - plant.y, 2));
if (distance < 80 && plant.isReady) {
plant.harvest();
var coinCount = plant.harvestCount;
totalRewards += coinCount;
updateScoreDisplay();
// Create coin effect with count text
var effect = game.addChild(LK.getAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
}));
effect.x = plant.x;
effect.y = plant.y;
effect.alpha = 1;
// Create text showing coin count
var coinText = new Text2('+' + coinCount, {
size: 48,
fill: '#FFD700',
font: "'Times New Roman'"
});
coinText.anchor.set(0.5, 0.5);
coinText.x = plant.x;
coinText.y = plant.y - 80;
game.addChild(coinText);
// Animate coin jumping up and fading
tween(effect, {
alpha: 0,
y: plant.y - 100
}, {
duration: 800,
easing: tween.easeOut,
onFinish: function onFinish() {
effect.destroy();
}
});
// Animate coin text jumping and fading
tween(coinText, {
alpha: 0,
y: plant.y - 150
}, {
duration: 800,
easing: tween.easeOut,
onFinish: function onFinishText() {
coinText.destroy();
}
});
break;
}
}
};
game.move = function (x, y, obj) {
if (draggedSeed) {
if (draggedSeedDuplicate) {
draggedSeedDuplicate.x = x;
draggedSeedDuplicate.y = y;
}
}
};
game.up = function (x, y, obj) {
if (draggedSeed) {
var placed = false;
for (var plantIndex = 0; plantIndex < plants.length; plantIndex++) {
var plant = plants[plantIndex];
var plantDistance = Math.sqrt(Math.pow(x - plant.x, 2) + Math.pow(y - plant.y, 2));
if (plantDistance < 175) {
if (draggedSeed.seedType) {
plant.seedPlanted = true;
plant.plantedSeedType = draggedSeed.seedType; // Store which seed type was planted
draggedSeed.decrementSeedCount();
plant.initialize();
LK.getSound('placingsound1').play();
if (plant.timerDisplay) {
plant.timerDisplay.alpha = 1;
}
var dirtGraphics = plant.children[0];
if (dirtGraphics) {
var originalX = dirtGraphics.x;
tween(dirtGraphics, {
x: originalX + 8
}, {
duration: 50,
easing: tween.easeInOut
});
tween(dirtGraphics, {
x: originalX - 8
}, {
duration: 50,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(dirtGraphics, {
x: originalX
}, {
duration: 50,
easing: tween.easeInOut
});
}
});
}
draggedSeed.showSeedGraphics();
placed = true;
}
break;
}
}
if (draggedSeedDuplicate) {
draggedSeedDuplicate.destroy();
draggedSeedDuplicate = null;
}
var seedGraphics = draggedSeed.getSeedGraphics();
if (seedGraphics) {
tween.stop(seedGraphics);
if (!placed) {
tween(seedGraphics, {
x: 0,
y: 0,
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeOut
});
seedGraphics.alpha = draggedSeed.seedCount <= 0 ? 0.3 : 1;
} else {
tween(seedGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
}
}
draggedSeed.isDragging = false;
draggedSeed = null;
draggedSeedStartSlot = null;
}
};
// Create walls around the edges
var wallSize = 50;
var wallAssetWidth = 50;
var wallAssetHeight = 50;
// Top walls - spanning full width
for (var topWallIndex = 0; topWallIndex < 2048; topWallIndex += wallSize) {
var topWall = game.addChild(LK.getAsset('walls', {
anchorX: 0,
anchorY: 0
}));
topWall.x = topWallIndex;
topWall.y = 0;
} //{wall_top}
// Bottom walls - spanning full width
for (var bottomWallIndex = 0; bottomWallIndex < 2048; bottomWallIndex += wallSize) {
var bottomWall = game.addChild(LK.getAsset('walls', {
anchorX: 0,
anchorY: 0
}));
bottomWall.x = bottomWallIndex;
bottomWall.y = 2732 - wallSize;
} //{wall_bottom}
// Left walls - spanning full height
for (var leftWallIndex = 0; leftWallIndex < 2732; leftWallIndex += wallSize) {
var leftWall = game.addChild(LK.getAsset('walls', {
anchorX: 0,
anchorY: 0
}));
leftWall.x = 0;
leftWall.y = leftWallIndex;
} //{wall_left}
// Right walls - spanning full height
for (var rightWallIndex = 0; rightWallIndex < 2732; rightWallIndex += wallSize) {
var rightWall = game.addChild(LK.getAsset('walls', {
anchorX: 0,
anchorY: 0
}));
rightWall.x = 2048 - wallSize;
rightWall.y = rightWallIndex;
} //{wall_right}
LK.playMusic('gamemusic1');
game.update = function () {
for (var i = 0; i < plants.length; i++) {
if (plants[i].seedPlanted) {
plants[i].update();
}
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Plant = Container.expand(function () {
var self = Container.call(this);
var dirtGraphics = self.attachAsset('Soil', {
anchorX: 0.5,
anchorY: 0.5
});
self.maxGrowthTime = 60000; // 60 seconds in milliseconds
self.growthTimeRemaining = 0;
self.isReady = false;
self.gridX = 0;
self.gridY = 0;
self.seedPlanted = false;
self.harvestCount = 0;
self.growthTimer = null;
self.timerDisplay = null;
self.initialize = function () {
self.growthTimeRemaining = self.maxGrowthTime;
self.isReady = false;
self.seedPlanted = true;
// Don't reset plantedSeedType here - it's already set before initialize is called
self.harvestCount = Math.floor(Math.random() * 3) + 1; // Random 1-3 coins
};
self.update = function () {
if (self.seedPlanted && !self.isReady) {
self.growthTimeRemaining -= 1000 / 60; // Subtract time per frame (60 FPS)
if (self.timerDisplay) {
var secondsRemaining = Math.ceil(self.growthTimeRemaining / 1000);
self.timerDisplay.setText(secondsRemaining + 's');
}
if (self.growthTimeRemaining <= 0) {
self.growthTimeRemaining = 0;
self.isReady = true;
if (self.timerDisplay) {
self.timerDisplay.alpha = 0;
}
if (self.seedPlanted) {
self.spawnCoins(); //{spawnCoins_call}
}
}
}
};
self.harvest = function () {
if (self.isReady) {
self.harvestCount++;
self.isReady = false;
self.seedPlanted = false;
self.growthTimeRemaining = self.maxGrowthTime;
if (self.timerDisplay) {
self.timerDisplay.alpha = 0;
}
return true;
}
return false;
};
self.spawnCoins = function () {
// Only spawn if a seed type was actually planted
if (!self.plantedSeedType) {
return;
}
var coinCount = Math.floor(Math.random() * 3) + 2; // Random 2-4 coins
var seedCount = Math.floor(Math.random() * 3) + 1; // Random 1-3 seeds
for (var c = 0; c < coinCount; c++) {
var angle = Math.PI * 2 * c / coinCount; // Distribute coins in circle
var offsetX = Math.cos(angle) * 150;
var offsetY = Math.sin(angle) * 150;
var coinAsset = LK.getAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
coinAsset.x = self.x;
coinAsset.y = self.y;
coinAsset.alpha = 1;
coinAsset.isCollectable = true;
coinAsset.coinValue = 1;
gameInstance.addChild(coinAsset);
spawnedCoins.push(coinAsset);
// First bounce up
tween(coinAsset, {
y: self.y - 100
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFirstBounce() {
// Fall back down
tween(coinAsset, {
y: self.y
}, {
duration: 200,
easing: tween.easeIn,
onFinish: function onFirstLand() {
// Second bounce up
tween(coinAsset, {
y: self.y - 60
}, {
duration: 250,
easing: tween.easeOut,
onFinish: function onSecondBounce() {
// Fall back down and stay still
tween(coinAsset, {
y: self.y
}, {
duration: 150,
easing: tween.easeIn
});
}
});
}
});
}
});
}
var actualSeedCount = Math.floor(Math.random() * 3) + 1; // Random 1-3 seeds
for (var s = 0; s < actualSeedCount; s++) {
var seedType = self.plantedSeedType; // Use the planted seed type
if (!seedType) {
seedType = 'carrotseed'; // Default fallback only if no seed type exists
}
var seedAsset = LK.getAsset(seedType, {
anchorX: 0.5,
//{seedAsset_a}
anchorY: 0.5 //{seedAsset_b}
}); //{seedAsset_c}
seedAsset.plantedSeedType = seedType; // Track the seed type on spawned asset
seedAsset.x = self.x;
seedAsset.y = self.y;
seedAsset.alpha = 1;
seedAsset.isCollectable = true;
seedAsset.seedValue = 1;
gameInstance.addChild(seedAsset);
spawnedSeeds.push(seedAsset);
// First bounce up
tween(seedAsset, {
//{seedAsset_d}
y: self.y - 100 //{seedAsset_e}
}, {
//{seedAsset_f}
duration: 300,
//{seedAsset_g}
easing: tween.easeOut,
//{seedAsset_h}
onFinish: function onFirstSeedBounce() {
// Fall back down//{seedAsset_i}
tween(seedAsset, {
//{seedAsset_j}
y: self.y //{seedAsset_k}
}, {
//{seedAsset_l}
duration: 200,
//{seedAsset_m}
easing: tween.easeIn,
//{seedAsset_n}
onFinish: function onFirstSeedLand() {
// Second bounce up//{seedAsset_o}
tween(seedAsset, {
//{seedAsset_p}
y: self.y - 60 //{seedAsset_q}
}, {
//{seedAsset_r}
duration: 250,
//{seedAsset_s}
easing: tween.easeOut,
//{seedAsset_t}
onFinish: function onSecondSeedBounce() {
// Fall back down and stay still//{seedAsset_u}
tween(seedAsset, {
//{seedAsset_v}
y: self.y //{seedAsset_w}
}, {
//{seedAsset_x}
duration: 150,
//{seedAsset_y}
easing: tween.easeIn //{seedAsset_z}
}); //{seedAsset_A}
} //{seedAsset_B}
}); //{seedAsset_C}
} //{seedAsset_D}
}); //{seedAsset_E}
} //{seedAsset_F}
}); //{seedAsset_G}
} //{seedAsset_H}
};
self.down = function (x, y, obj) {
// Handled by game-level event handler
};
self.initialize();
return self;
});
var Seed = Container.expand(function () {
var self = Container.call(this);
var slotGraphics = self.attachAsset('woodbox', {
anchorX: 0.5,
anchorY: 0.5
});
var seedGraphics = null;
self.seedType = null;
self.slotIndex = 0;
self.initialX = 0;
self.initialY = 0;
self.isDragging = false;
self.seedGraphicsLocalX = 0;
self.seedGraphicsLocalY = 0;
self.seedCount = 5;
self.seedCountText = null;
self.setSeed = function (seedType) {
if (seedGraphics) {
seedGraphics.destroy();
seedGraphics = null;
}
self.seedType = seedType;
if (seedType) {
seedGraphics = self.attachAsset(seedType, {
anchorX: 0.5,
anchorY: 0.5
});
self.seedGraphicsRef = seedGraphics;
if (!self.seedCountText) {
self.seedCountText = new Text2(self.seedCount + '/100', {
size: 48,
fill: '#00ff00',
font: "'Times New Roman'"
});
self.seedCountText.anchor.set(0.5, 0);
self.seedCountText.x = 0;
self.seedCountText.y = 130;
self.addChild(self.seedCountText);
} else {
self.seedCountText.setText(self.seedCount + '/100');
}
if (seedGraphics) {
seedGraphics.alpha = 1;
}
}
};
self.decrementSeedCount = function () {
if (self.seedCount > 0) {
self.seedCount--;
if (self.seedCountText) {
self.seedCountText.setText(self.seedCount + '/100');
}
if (self.seedCount <= 0 && seedGraphics) {
seedGraphics.alpha = 1;
}
}
};
self.getSeed = function () {
return self.seedType;
};
self.getSeedGraphics = function () {
return seedGraphics;
};
self.removeSeed = function () {
if (seedGraphics) {
seedGraphics.destroy();
seedGraphics = null;
}
self.seedType = null;
};
self.hideSeedGraphics = function () {
if (seedGraphics) {
seedGraphics.alpha = 0;
}
};
self.showSeedGraphics = function () {
if (seedGraphics) {
seedGraphics.alpha = 1;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1b4332
});
/****
* Game Code
****/
// Add background
var background = game.addChild(LK.getAsset('Background1', {
anchorX: 0.5,
anchorY: 0.5
}));
background.x = 2048 / 2;
background.y = 2732 / 2;
background.scaleX = 2048 / background.width;
background.scaleY = 2732 / background.height;
var plants = [];
var totalRewards = storage.totalRewards || 0;
var gameInstance = game;
var dirtSlotSize = 350;
var dirtSlotSpacing = 40;
var dirtGridStartX = 2048 / 2 - (5 * dirtSlotSize + 4 * dirtSlotSpacing) / 2;
var dirtGridStartY = 2732 - 900;
// Create 5 dirt slots in horizontal line at bottom (2 on sides + 3 in center)
for (var dirtIndex = 0; dirtIndex < 5; dirtIndex++) {
var plant = game.addChild(new Plant());
plant.gridX = dirtIndex;
plant.gridY = 0;
plant.x = dirtGridStartX + dirtIndex * (dirtSlotSize + dirtSlotSpacing) + dirtSlotSize / 2;
plant.y = dirtGridStartY;
plants.push(plant);
// Add many separation assets tightly surrounding each soil slot
var separationSize = 50;
var soilSize = 200;
var halfSoilSize = soilSize / 2;
var offset = 0;
// Top row separations
for (var topSepX = -halfSoilSize - offset; topSepX <= halfSoilSize + offset; topSepX += separationSize) {
var topSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
topSeparation.x = plant.x + topSepX;
topSeparation.y = plant.y - halfSoilSize - offset;
}
// Bottom row separations
for (var botSepX = -halfSoilSize - offset; botSepX <= halfSoilSize + offset; botSepX += separationSize) {
var botSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
botSeparation.x = plant.x + botSepX;
botSeparation.y = plant.y + halfSoilSize + offset;
}
// Left column separations (excluding corners already covered)
for (var leftSepY = -halfSoilSize; leftSepY <= halfSoilSize; leftSepY += separationSize) {
var leftSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
leftSeparation.x = plant.x - halfSoilSize - offset;
leftSeparation.y = plant.y + leftSepY;
}
// Right column separations (excluding corners already covered)
for (var rightSepY = -halfSoilSize; rightSepY <= halfSoilSize; rightSepY += separationSize) {
var rightSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
rightSeparation.x = plant.x + halfSoilSize + offset;
rightSeparation.y = plant.y + rightSepY;
}
}
// Create 5 dirt slots in horizontal line above the first row
var dirtGridStartYTop = dirtGridStartY - (dirtSlotSize + dirtSlotSpacing);
for (var dirtIndex = 0; dirtIndex < 5; dirtIndex++) {
var plant = game.addChild(new Plant());
plant.gridX = dirtIndex;
plant.gridY = 1;
plant.x = dirtGridStartX + dirtIndex * (dirtSlotSize + dirtSlotSpacing) + dirtSlotSize / 2;
plant.y = dirtGridStartYTop;
plants.push(plant);
// Add many separation assets tightly surrounding each soil slot
var separationSize = 50;
var soilSize = 200;
var halfSoilSize = soilSize / 2;
var offset = 0;
// Top row separations
for (var topSepX = -halfSoilSize - offset; topSepX <= halfSoilSize + offset; topSepX += separationSize) {
var topSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
topSeparation.x = plant.x + topSepX;
topSeparation.y = plant.y - halfSoilSize - offset;
}
// Bottom row separations
for (var botSepX = -halfSoilSize - offset; botSepX <= halfSoilSize + offset; botSepX += separationSize) {
var botSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
botSeparation.x = plant.x + botSepX;
botSeparation.y = plant.y + halfSoilSize + offset;
}
// Left column separations (excluding corners already covered)
for (var leftSepY = -halfSoilSize; leftSepY <= halfSoilSize; leftSepY += separationSize) {
var leftSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
leftSeparation.x = plant.x - halfSoilSize - offset;
leftSeparation.y = plant.y + leftSepY;
}
// Right column separations (excluding corners already covered)
for (var rightSepY = -halfSoilSize; rightSepY <= halfSoilSize; rightSepY += separationSize) {
var rightSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
rightSeparation.x = plant.x + halfSoilSize + offset;
rightSeparation.y = plant.y + rightSepY;
}
}
// Create 5 dirt slots in horizontal line above the second row
var dirtGridStartYTop2 = dirtGridStartYTop - (dirtSlotSize + dirtSlotSpacing);
for (var dirtIndex = 0; dirtIndex < 5; dirtIndex++) {
var plant = game.addChild(new Plant());
plant.gridX = dirtIndex;
plant.gridY = 2;
plant.x = dirtGridStartX + dirtIndex * (dirtSlotSize + dirtSlotSpacing) + dirtSlotSize / 2;
plant.y = dirtGridStartYTop2;
plants.push(plant);
// Add many separation assets tightly surrounding each soil slot
var separationSize = 50;
var soilSize = 200;
var halfSoilSize = soilSize / 2;
var offset = 0;
// Top row separations
for (var topSepX = -halfSoilSize - offset; topSepX <= halfSoilSize + offset; topSepX += separationSize) {
var topSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
topSeparation.x = plant.x + topSepX;
topSeparation.y = plant.y - halfSoilSize - offset;
}
// Bottom row separations
for (var botSepX = -halfSoilSize - offset; botSepX <= halfSoilSize + offset; botSepX += separationSize) {
var botSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
botSeparation.x = plant.x + botSepX;
botSeparation.y = plant.y + halfSoilSize + offset;
}
// Left column separations (excluding corners already covered)
for (var leftSepY = -halfSoilSize; leftSepY <= halfSoilSize; leftSepY += separationSize) {
var leftSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
leftSeparation.x = plant.x - halfSoilSize - offset;
leftSeparation.y = plant.y + leftSepY;
}
// Right column separations (excluding corners already covered)
for (var rightSepY = -halfSoilSize; rightSepY <= halfSoilSize; rightSepY += separationSize) {
var rightSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
rightSeparation.x = plant.x + halfSoilSize + offset;
rightSeparation.y = plant.y + rightSepY;
}
}
// Create 5 dirt slots in horizontal line above the third row
var dirtGridStartYTop3 = dirtGridStartYTop2 - (dirtSlotSize + dirtSlotSpacing);
for (var dirtIndex = 0; dirtIndex < 5; dirtIndex++) {
var plant = game.addChild(new Plant());
plant.gridX = dirtIndex;
plant.gridY = 3;
plant.x = dirtGridStartX + dirtIndex * (dirtSlotSize + dirtSlotSpacing) + dirtSlotSize / 2;
plant.y = dirtGridStartYTop3;
plants.push(plant);
// Add many separation assets tightly surrounding each soil slot
var separationSize = 50;
var soilSize = 200;
var halfSoilSize = soilSize / 2;
var offset = 0;
// Top row separations
for (var topSepX = -halfSoilSize - offset; topSepX <= halfSoilSize + offset; topSepX += separationSize) {
var topSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
topSeparation.x = plant.x + topSepX;
topSeparation.y = plant.y - halfSoilSize - offset;
}
// Bottom row separations
for (var botSepX = -halfSoilSize - offset; botSepX <= halfSoilSize + offset; botSepX += separationSize) {
var botSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
botSeparation.x = plant.x + botSepX;
botSeparation.y = plant.y + halfSoilSize + offset;
}
// Left column separations (excluding corners already covered)
for (var leftSepY = -halfSoilSize; leftSepY <= halfSoilSize; leftSepY += separationSize) {
var leftSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
leftSeparation.x = plant.x - halfSoilSize - offset;
leftSeparation.y = plant.y + leftSepY;
}
// Right column separations (excluding corners already covered)
for (var rightSepY = -halfSoilSize; rightSepY <= halfSoilSize; rightSepY += separationSize) {
var rightSeparation = game.addChild(LK.getAsset('separation', {
anchorX: 0.5,
anchorY: 0.5
}));
rightSeparation.x = plant.x + halfSoilSize + offset;
rightSeparation.y = plant.y + rightSepY;
}
}
// Shop icon
var shopIcon = game.addChild(LK.getAsset('Shop', {
anchorX: 0.5,
anchorY: 0.5
}));
shopIcon.x = 2048 - 200;
shopIcon.y = 200;
// Score display
var scoreText = new Text2('coins: 0', {
size: 64,
fill: '#ffffff',
font: "'Times New Roman'"
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
function updateScoreDisplay() {
scoreText.setText('coins: ' + totalRewards);
storage.totalRewards = totalRewards;
}
updateScoreDisplay();
// Create individual timer displays for each plant - initially hidden
for (var i = 0; i < plants.length; i++) {
var plantTimer = new Text2('60s', {
size: 80,
fill: '#ffffff',
font: "'Times New Roman'"
});
plantTimer.anchor.set(0.5, 0.5);
plantTimer.x = plants[i].x;
plantTimer.y = plants[i].y;
plantTimer.alpha = 0;
game.addChild(plantTimer);
plants[i].timerDisplay = plantTimer;
plants[i].lastHarvested = false;
}
// Create seed slots at bottom of screen - 2 horizontal lines spanning full width
var seeds = [];
var seedSlotSize = 120;
var seedSlotSpacing = 120;
var seedsPerRow = 6;
var totalSeedSlotWidth = seedsPerRow * seedSlotSize + (seedsPerRow - 1) * seedSlotSpacing;
var seedGridStartX = 120;
var seedGridStartY = 2732 - 550;
var seedRowSpacing = 310;
var seedTypes = ['tomatoseed', 'potatoseed', 'carrotseed', 'ananasseed', 'appleseed', 'brocoliseed', 'eggplantseed', 'garlicseed', 'lemonseed', 'Orangeseed', 'pumpkinseed', 'watermelonseed'];
for (var seedIndex = 0; seedIndex < seedTypes.length; seedIndex++) {
var rowIndex = Math.floor(seedIndex / seedsPerRow);
var colIndex = seedIndex % seedsPerRow;
var seedSlot = game.addChild(new Seed());
seedSlot.slotIndex = seedIndex;
seedSlot.x = seedGridStartX + colIndex * (seedSlotSize + seedSlotSpacing) + seedSlotSize / 2;
seedSlot.y = seedGridStartY + rowIndex * seedRowSpacing;
seeds.push(seedSlot);
seedSlot.setSeed(seedTypes[seedIndex]);
}
var draggedSeed = null;
var draggedSeedStartSlot = null;
var draggedSeedDuplicate = null;
var spawnedCoins = [];
var spawnedSeeds = [];
game.down = function (x, y, obj) {
// Check if clicking on spawned coins
for (var c = spawnedCoins.length - 1; c >= 0; c--) {
var coin = spawnedCoins[c];
var coinDistance = Math.sqrt(Math.pow(x - coin.x, 2) + Math.pow(y - coin.y, 2));
if (coinDistance < 80 && coin.isCollectable) {
totalRewards += coin.coinValue;
updateScoreDisplay();
coin.isCollectable = false;
LK.getSound('collectingsound1').play();
tween(coin, {
alpha: 0,
y: coin.y - 150
}, {
duration: 400,
easing: tween.easeOut,
onFinish: function onCoinCollected() {
coin.destroy();
spawnedCoins.splice(spawnedCoins.indexOf(coin), 1);
}
});
return;
}
}
// Check if clicking on spawned seeds
for (var s = spawnedSeeds.length - 1; s >= 0; s--) {
var seed = spawnedSeeds[s];
var seedDistance = Math.sqrt(Math.pow(x - seed.x, 2) + Math.pow(y - seed.y, 2));
if (seedDistance < 80 && seed.isCollectable) {
// Add seeds back to inventory - find matching seed slot by type
var seedTypeToRestore = seed.plantedSeedType || 'carrotseed';
for (var seedSlotIdx = 0; seedSlotIdx < seeds.length; seedSlotIdx++) {
if (seeds[seedSlotIdx].seedType === seedTypeToRestore) {
seeds[seedSlotIdx].seedCount = Math.min(seeds[seedSlotIdx].seedCount + seed.seedValue, 100);
if (seeds[seedSlotIdx].seedCountText) {
seeds[seedSlotIdx].seedCountText.setText(seeds[seedSlotIdx].seedCount + '/100');
}
break;
}
}
seed.isCollectable = false;
LK.getSound('collectingsound1').play();
tween(seed, {
alpha: 0,
y: seed.y - 150
}, {
duration: 400,
easing: tween.easeOut,
onFinish: function onSeedCollected() {
seed.destroy();
spawnedSeeds.splice(spawnedSeeds.indexOf(seed), 1);
}
});
return;
}
}
// Check if clicking on a seed (not the slot itself)
for (var seedIndex = 0; seedIndex < seeds.length; seedIndex++) {
var seed = seeds[seedIndex];
// Only allow dragging if seed has a seedType and seed count > 0
if (seed.seedType && seed.seedCount > 0) {
var distance = Math.sqrt(Math.pow(x - seed.x, 2) + Math.pow(y - seed.y, 2));
// Check if click is within seed bounds (radius approximately 50 pixels for 100x100 seed)
if (distance < 60) {
draggedSeed = seed;
draggedSeedStartSlot = seedIndex;
seed.isDragging = true;
seed.initialX = seed.x;
seed.initialY = seed.y;
LK.getSound('pickupsound1').play();
var seedGraphics = seed.getSeedGraphics();
if (seedGraphics) {
tween.stop(seedGraphics);
// Create a duplicate seed graphic for dragging
draggedSeedDuplicate = LK.getAsset(seed.seedType, {
anchorX: 0.5,
anchorY: 0.5
});
draggedSeedDuplicate.x = x;
draggedSeedDuplicate.y = y;
game.addChild(draggedSeedDuplicate);
tween(seedGraphics, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 100
});
}
return;
}
}
}
for (var i = 0; i < plants.length; i++) {
var plant = plants[i];
var distance = Math.sqrt(Math.pow(x - plant.x, 2) + Math.pow(y - plant.y, 2));
if (distance < 80 && plant.isReady) {
plant.harvest();
var coinCount = plant.harvestCount;
totalRewards += coinCount;
updateScoreDisplay();
// Create coin effect with count text
var effect = game.addChild(LK.getAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
}));
effect.x = plant.x;
effect.y = plant.y;
effect.alpha = 1;
// Create text showing coin count
var coinText = new Text2('+' + coinCount, {
size: 48,
fill: '#FFD700',
font: "'Times New Roman'"
});
coinText.anchor.set(0.5, 0.5);
coinText.x = plant.x;
coinText.y = plant.y - 80;
game.addChild(coinText);
// Animate coin jumping up and fading
tween(effect, {
alpha: 0,
y: plant.y - 100
}, {
duration: 800,
easing: tween.easeOut,
onFinish: function onFinish() {
effect.destroy();
}
});
// Animate coin text jumping and fading
tween(coinText, {
alpha: 0,
y: plant.y - 150
}, {
duration: 800,
easing: tween.easeOut,
onFinish: function onFinishText() {
coinText.destroy();
}
});
break;
}
}
};
game.move = function (x, y, obj) {
if (draggedSeed) {
if (draggedSeedDuplicate) {
draggedSeedDuplicate.x = x;
draggedSeedDuplicate.y = y;
}
}
};
game.up = function (x, y, obj) {
if (draggedSeed) {
var placed = false;
for (var plantIndex = 0; plantIndex < plants.length; plantIndex++) {
var plant = plants[plantIndex];
var plantDistance = Math.sqrt(Math.pow(x - plant.x, 2) + Math.pow(y - plant.y, 2));
if (plantDistance < 175) {
if (draggedSeed.seedType) {
plant.seedPlanted = true;
plant.plantedSeedType = draggedSeed.seedType; // Store which seed type was planted
draggedSeed.decrementSeedCount();
plant.initialize();
LK.getSound('placingsound1').play();
if (plant.timerDisplay) {
plant.timerDisplay.alpha = 1;
}
var dirtGraphics = plant.children[0];
if (dirtGraphics) {
var originalX = dirtGraphics.x;
tween(dirtGraphics, {
x: originalX + 8
}, {
duration: 50,
easing: tween.easeInOut
});
tween(dirtGraphics, {
x: originalX - 8
}, {
duration: 50,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(dirtGraphics, {
x: originalX
}, {
duration: 50,
easing: tween.easeInOut
});
}
});
}
draggedSeed.showSeedGraphics();
placed = true;
}
break;
}
}
if (draggedSeedDuplicate) {
draggedSeedDuplicate.destroy();
draggedSeedDuplicate = null;
}
var seedGraphics = draggedSeed.getSeedGraphics();
if (seedGraphics) {
tween.stop(seedGraphics);
if (!placed) {
tween(seedGraphics, {
x: 0,
y: 0,
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeOut
});
seedGraphics.alpha = draggedSeed.seedCount <= 0 ? 0.3 : 1;
} else {
tween(seedGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
}
}
draggedSeed.isDragging = false;
draggedSeed = null;
draggedSeedStartSlot = null;
}
};
// Create walls around the edges
var wallSize = 50;
var wallAssetWidth = 50;
var wallAssetHeight = 50;
// Top walls - spanning full width
for (var topWallIndex = 0; topWallIndex < 2048; topWallIndex += wallSize) {
var topWall = game.addChild(LK.getAsset('walls', {
anchorX: 0,
anchorY: 0
}));
topWall.x = topWallIndex;
topWall.y = 0;
} //{wall_top}
// Bottom walls - spanning full width
for (var bottomWallIndex = 0; bottomWallIndex < 2048; bottomWallIndex += wallSize) {
var bottomWall = game.addChild(LK.getAsset('walls', {
anchorX: 0,
anchorY: 0
}));
bottomWall.x = bottomWallIndex;
bottomWall.y = 2732 - wallSize;
} //{wall_bottom}
// Left walls - spanning full height
for (var leftWallIndex = 0; leftWallIndex < 2732; leftWallIndex += wallSize) {
var leftWall = game.addChild(LK.getAsset('walls', {
anchorX: 0,
anchorY: 0
}));
leftWall.x = 0;
leftWall.y = leftWallIndex;
} //{wall_left}
// Right walls - spanning full height
for (var rightWallIndex = 0; rightWallIndex < 2732; rightWallIndex += wallSize) {
var rightWall = game.addChild(LK.getAsset('walls', {
anchorX: 0,
anchorY: 0
}));
rightWall.x = 2048 - wallSize;
rightWall.y = rightWallIndex;
} //{wall_right}
LK.playMusic('gamemusic1');
game.update = function () {
for (var i = 0; i < plants.length; i++) {
if (plants[i].seedPlanted) {
plants[i].update();
}
}
};