User prompt
Hey Upit, Please add clouds that move slowly across the top of the game screen. I want a cloud to appear every 10 seconds and move from one side to the other at a slow speed. Thanks! ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Hey Upit, Please remove the shop completely from the game. Do not open or show the shop anymore. The shop system should be totally disabled or deleted. Thanks!
User prompt
Hey Upit, Please make the shop close and disappear when I click the “Close” button or after buying an item. The shop should be completely removed from the screen. Thanks!
User prompt
Step 1 — Close Button Fix Add a big red "Close" button inside the shop. When I click or tap this Close button: The shop must disappear. The player should stop moving. After the shop is closed, the player can move again. 🔹 Step 2 — Disable movement while shop is open While the shop is open: Player should not move. Don’t allow walk keys or joystick. Only allow button clicks. Let’s do these two steps first. After that, I’ll ask for the rest. Thank you!
User prompt
Hey Upit, I think I know the problem now. When I click the shop Close button, my character moves forward, so the game thinks I’m controlling movement — not clicking the UI. That’s probably why the button doesn’t work! Please fix it like this: Make sure the shop UI is blocking player controls while it is open. Disable movement input (WASD, joystick, etc.) Or make the UI use “UI input priority” so buttons work correctly The Close button must work when clicked: Shop should disappear. Player should regain movement after shop is closed. Please update this and make sure the button can be clicked properly. Thanks!
User prompt
ey Upit, The "Close" button is visible, but it does not work when I click it. The shop stays open. Please make sure the "Close" button works correctly. Here’s what I need: When I click or tap the Close button, the shop should disappear (close and hide completely). This must work on both PC and mobile. If possible, use a big button or a colored box instead of a small "X", so it's easy to click.
User prompt
Hey Upit, The shop still doesn't close when I click the "X". Maybe the "X" is too small or not working on mobile. Let’s change it to something more clear: Please add a big red “Close” button (like a box that says “Close”) at the top or bottom of the shop. When a player clicks or taps this "Close" button, the entire shop should disappear. Also don’t forget: The shop should also close after buying something. Buttons must work with both mouse click and mobile touch. Thanks!
User prompt
Hey Upit, Can you please make the shop UI work properly on both PC and mobile? Here’s what I need: Buttons should be big enough to tap easily on mobile. The UI should scale correctly on different screen sizes. All buttons (Buy and Exit) must work with: Mouse click (for PC) Touch tap (for mobile) Also, remember: When a player buys a scythe, give them the correct one and close the shop. Add a visible Exit (X) button that also works on mobile and closes the shop. Thanks a lot! İstersen bunun için bir örnek UI tasarımı önerisi veya Roblox Studio'da kullanılabilecek bir ScreenGui ayarı da verebilirim. Kodla yardım ister misin? Şunu dedin: yine olmadı artı olarak bu ekledigi shoptaki şeylere tıklayan buy yapan oyuncu parası yetiyorsa owned olur yani alır onuda ekle ChatGPT: Tamam! Tüm isteklerini kapsayan şekilde Upit’e yazabileceğin net ve sade bir İngilizce mesajı aşağıda hazırladım. Bu mesaj, sorunları ve eklenmesini istediğin tüm özellikleri anlatıyor: Hey Upit, The shop still isn't working fully. Here's everything I need — please fix all of this: The shop must work on both PC and mobile: Buttons should be easy to click or tap. UI should scale properly for different screen sizes. The shop opens correctly, but: It should close when I click the "X" button. It should also close automatically when a player buys something. Each item in the shop (like scythes): Should be clickable. When a player clicks "Buy": If they have enough money, they get that exact scythe in their hand. Their money should decrease. The item should now show as "Owned". The shop should close after buying. If they don’t have enough money, nothing should happen.
User prompt
Hey Upit, Can you please make the shop UI work properly on both PC and mobile? Here’s what I need: Buttons should be big enough to tap easily on mobile. The UI should scale correctly on different screen sizes. All buttons (Buy and Exit) must work with: Mouse click (for PC) Touch tap (for mobile) Also, remember: When a player buys a scythe, give them the correct one and close the shop. Add a visible Exit (X) button that also works on mobile and closes the shop. Thanks a lot!
User prompt
Hey Upit, I want to improve the shop system: When a player buys something, the shop should automatically close. Players use money to buy scythes (oraks). After buying: The shop closes. The player gets only the scythe they bought. Please add an exit button (X) to close the shop without buying anything. Can you help with this? Thanks!
User prompt
The shop opens, but it doesn't close when I click the close button ("X"). Please make sure the shop UI closes (disappears) when the X is clicked. Also, I want to customize the items inside the shop. For example: When the player clicks an item (like a scythe/orak), they should get a different version in their hand. So I want to add multiple scythes (different models), and when a player buys one, they get that exact version. Please help me with: Shop closing properly with X button. Customizable items inside the shop. When a player clicks to buy, the correct version should appear in their hand. Thanks!
User prompt
In the shop UI, the buy button is not working. Can you please fix it? Also, please add an exit button (a close "X" button) to the shop. When I click the "X" button, the shop should close and disappear again. Thanks!
User prompt
Create a "Shop" button fixed on the right side of the screen at mid-height. When clicked, it opens a panel centered on the screen showing 5 different sickles for sale, each with a "Buy" button. The panel must have a close button ("X") inside it at the top-right corner. When the close button is clicked, the panel must close immediately by hiding or removing itself. Clicking the "Shop" button again toggles the panel’s visibility (opens if closed, closes if open). Make sure the close button’s click event properly hides the panel every time. Keep the interface simple, with no side menus or extra UI elements.
User prompt
local shopButton = script.Parent:WaitForChild("ShopButton") local shopFrame = script.Parent:WaitForChild("ShopFrame") local closeButton = shopFrame:WaitForChild("CloseButton") shopFrame.Visible = false shopButton.MouseButton1Click:Connect(function() shopFrame.Visible = not shopFrame.Visible end) closeButton.MouseButton1Click:Connect(function() shopFrame.Visible = false end) Make sure the buttons and panel are in the same ScreenGui and the script is a LocalScript inside the ScreenGui.
User prompt
Create a "Shop" button on the right side of the screen at mid-height. When the player clicks it, show a panel in the center of the screen that displays 5 different sickles with "Buy" buttons. Add an "X" button at the top-right of the panel. When the player clicks the "X" button, the panel must close by setting its Visible property to false. When the player clicks the Shop button again, the panel should become visible again (Visible = true). Make sure the "X" button is connected with a MouseButton1Click event to close the panel.
User prompt
Create a simple "Shop" button placed on the right side of the screen at mid-height. When the player clicks the button, open a UI panel in the center of the screen showing 5 different sickles for sale, each with a name and a "Buy" button. Add a visible "X" button on the top-right corner of the shop panel. When the player clicks the "X" button, the shop panel should close and disappear. Make sure the close button works properly. Do not use any side menus. Keep the interface clean and simple.
User prompt
Create a simple "Shop" button placed on the right side of the screen. When the player clicks the button, open a panel in the center of the screen. This panel should contain 5 different sickles for sale, each with a name and a "Buy" button. Include a close button (like an "X") on the panel so the player can close it. The Shop button should stay on the right side at mid-height, not at the top. Do not use any side menus or extra UI — keep everything simple and clean.
User prompt
Create a simple "Shop" button on the right side of the screen. Do not use any side menu or extra UI elements, just a single "Shop" button. When the player clicks the button, it should open a small panel that lists 5 different sickles for sale. Each sickle should have a name and a "Buy" button. The panel should only appear when the Shop button is clicked, and disappear when closed. Keep the design simple and clean.
User prompt
Please fix the bug: 'TypeError: Cannot read properties of null (reading 'update')' in or related to this line: 'farmer.update();' Line Number: 1032
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of null (reading 'moveTo')' in or related to this line: 'farmer.moveTo(x, y);' Line Number: 1003
User prompt
Please fix the bug: 'Cannot read properties of null (reading 'setText')' in or related to this line: 'moneyTxt.setText('$' + money);' Line Number: 854
User prompt
1. Create a new scene for the main menu. 2. Set the background image to a farm-themed picture (like a barn, field, or sky). 3. Add a big "Start" button in the center of the screen. 4. When the player clicks the Start button, it goes to the game scene. 5. Make sure all parts (background, text, button image) can be changed later.
User prompt
1. Create a new scene for the main menu. 2. Set the background image to a farm-themed picture (like a barn, field, or sky). 3. Add a big "Start" button in the center of the screen. 4. When the player clicks the Start button, it goes to the game scene. 5. Make sure all parts (background, text, button image) can be changed later.
User prompt
1. Select the background or main canvas. 2. Change its color to a grass green, for example: #4CAF50. 3. Make sure the green color fills the entire background.
User prompt
1. Create square road tiles with small gaps between them. 2. Arrange tiles vertically to form a road. 3. Use the same image on all tiles. 4. Position road tiles in the center. 5. Player and objects should appear on top.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Animal = Container.expand(function () {
var self = Container.call(this);
self.animalAsset = null;
self.setAnimalImage = function (imageId) {
if (self.animalAsset) {
self.animalAsset.destroy();
}
self.animalAsset = self.attachAsset(imageId, {
anchorX: 0.5,
anchorY: 0.5
});
};
return self;
});
// CarrotSlot class for area 2
var CarrotSlot = Container.expand(function () {
var self = Container.call(this);
self.slotAsset = self.attachAsset('carrot', {
anchorX: 0.5,
anchorY: 1
});
var sproutAsset = self.attachAsset('sprout', {
anchorX: 0.5,
anchorY: 1,
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
});
self.harvested = false;
self.regrowTime = 1800; // 30 seconds at 60fps
self.regrowCounter = 0;
self.fieldIndex = 1; // Area 2
self.regrowing = false;
// Show/hide carrot and handle regrow animation
self.setHarvested = function (harvested) {
self.harvested = harvested;
if (harvested) {
// Hide carrot with animation
tween(self.slotAsset, {
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut
});
// Show sprout with animation
sproutAsset.alpha = 1;
sproutAsset.scaleX = 0.1;
sproutAsset.scaleY = 0.1;
// Animate sprout appearing
tween(sproutAsset, {
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut
});
self.regrowCounter = self.regrowTime;
self.regrowing = true;
} else {
// Show carrot with animation
tween(self.slotAsset, {
alpha: 1,
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeIn
});
// Hide sprout with animation
tween(sproutAsset, {
alpha: 0
}, {
duration: 300,
easing: tween.easeIn
});
self.regrowing = false;
}
};
// Regrow logic
self.update = function () {
if (self.harvested && self.regrowing) {
if (self.regrowCounter > 0) {
// Animate sprout growing to carrot size
var t = 1 - self.regrowCounter / self.regrowTime;
sproutAsset.scaleX = 0.5 + 0.5 * t;
sproutAsset.scaleY = 0.5 + 0.5 * t;
self.regrowCounter--;
}
if (self.regrowCounter <= 0) {
// Sprout is now full size, switch back to carrot and allow harvest again
sproutAsset.scaleX = 1;
sproutAsset.scaleY = 1;
self.setHarvested(false);
}
}
};
// For hit detection
self.isHarvestable = function () {
return !self.harvested;
};
return self;
});
// EggplantSlot class for area 4
var EggplantSlot = Container.expand(function () {
var self = Container.call(this);
self.slotAsset = self.attachAsset('eggplant', {
anchorX: 0.5,
anchorY: 1
});
var sproutAsset = self.attachAsset('sprout', {
anchorX: 0.5,
anchorY: 1,
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
});
self.harvested = false;
self.regrowTime = 1800; // 30 seconds at 60fps
self.regrowCounter = 0;
self.fieldIndex = 3; // Area 4
self.regrowing = false;
// Show/hide eggplant and handle regrow animation
self.setHarvested = function (harvested) {
self.harvested = harvested;
if (harvested) {
// Hide eggplant with animation
tween(self.slotAsset, {
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut
});
// Show sprout with animation
sproutAsset.alpha = 1;
sproutAsset.scaleX = 0.1;
sproutAsset.scaleY = 0.1;
// Animate sprout appearing
tween(sproutAsset, {
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut
});
self.regrowCounter = self.regrowTime;
self.regrowing = true;
} else {
// Show eggplant with animation
tween(self.slotAsset, {
alpha: 1,
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeIn
});
// Hide sprout with animation
tween(sproutAsset, {
alpha: 0
}, {
duration: 300,
easing: tween.easeIn
});
self.regrowing = false;
}
};
// Regrow logic
self.update = function () {
if (self.harvested && self.regrowing) {
if (self.regrowCounter > 0) {
// Animate sprout growing to eggplant size
var t = 1 - self.regrowCounter / self.regrowTime;
sproutAsset.scaleX = 0.5 + 0.5 * t;
sproutAsset.scaleY = 0.5 + 0.5 * t;
self.regrowCounter--;
}
if (self.regrowCounter <= 0) {
// Sprout is now full size, switch back to eggplant and allow harvest again
sproutAsset.scaleX = 1;
sproutAsset.scaleY = 1;
self.setHarvested(false);
}
}
};
// For hit detection
self.isHarvestable = function () {
return !self.harvested;
};
return self;
});
// Farmer class
var Farmer = Container.expand(function () {
var self = Container.call(this);
// Create animation frames
var idleFrame = self.attachAsset('farmer', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 1
});
// Frame 2 (running)
var runningFrame = self.attachAsset('farmer_walk', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
// Frame 3 (right step)
var rightStepFrame = self.attachAsset('farmer_walk', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0,
rotation: 0.1
});
self.speed = 18; // px per move
self.targetX = self.x;
self.targetY = self.y;
self.moving = false;
self.lastMoving = false;
self.animationTimer = 0;
self.animationFrame = 0;
self.frameTime = 18; // 0.3 seconds at 60fps
// Animation frame switching
self.switchToFrame = function (frameNum) {
idleFrame.alpha = 0;
runningFrame.alpha = 0;
rightStepFrame.alpha = 0;
if (frameNum === 0) {
// Idle
idleFrame.alpha = 1;
} else if (frameNum === 1) {
// Running
runningFrame.alpha = 1;
} else if (frameNum === 2) {
// Right step
rightStepFrame.alpha = 1;
}
};
// Move farmer towards target
self.update = function () {
// Track movement direction and update image
if (self.moving) {
var dx = self.targetX - self.x;
// Check if moving right or left
if (dx > 0) {
// Moving right - use player_right asset
idleFrame.scaleX = 1;
runningFrame.scaleX = 1;
rightStepFrame.scaleX = 1;
// Switch to player_right image
if (self.currentAsset !== idleFrame || idleFrame.texture !== LK.getAsset('player_right', {}).texture) {
if (self.currentAsset) {
self.currentAsset.alpha = 0;
}
var rightAsset = LK.getAsset('player_right', {
anchorX: 0.5,
anchorY: 0.5
});
rightAsset.alpha = 1;
self.addChild(rightAsset);
self.currentAsset = rightAsset;
}
} else if (dx < 0) {
// Moving left - use player_left asset
idleFrame.scaleX = -1;
runningFrame.scaleX = -1;
rightStepFrame.scaleX = -1;
// Switch to player_left image
if (self.currentAsset !== idleFrame || idleFrame.texture !== LK.getAsset('player_left', {}).texture) {
if (self.currentAsset) {
self.currentAsset.alpha = 0;
}
var leftAsset = LK.getAsset('player_left', {
anchorX: 0.5,
anchorY: 0.5
});
leftAsset.alpha = 1;
self.addChild(leftAsset);
self.currentAsset = leftAsset;
}
}
// Animation handling
self.animationTimer++;
if (self.animationTimer >= self.frameTime) {
self.animationTimer = 0;
self.animationFrame = self.animationFrame === 1 ? 2 : 1; // Toggle between frames 1 and 2
self.switchToFrame(self.animationFrame);
}
} else if (!self.moving && self.lastMoving) {
// Just stopped moving - switch to idle
self.switchToFrame(0);
}
self.lastMoving = self.moving;
if (!self.moving) return;
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < self.speed) {
self.x = self.targetX;
self.y = self.targetY;
self.moving = false;
// Switch to idle frame when stopping
self.switchToFrame(0);
} else {
self.x += self.speed * dx / dist;
self.y += self.speed * dy / dist;
}
};
// Set movement target
self.moveTo = function (x, y) {
self.targetX = x;
self.targetY = y;
// Start movement
if (!self.moving) {
self.moving = true;
self.animationFrame = 1;
self.animationTimer = 0;
self.switchToFrame(self.animationFrame);
}
};
return self;
});
// Field class
var Field = Container.expand(function () {
var self = Container.call(this);
self.index = 0; // 0-3
self.locked = false;
self.lockCost = 0;
self.lockNode = null;
self.fenceNodes = [];
self.wheats = [];
self.unlockBtn = null;
// Draw fences - Empty implementation to eliminate fences
self.drawFences = function (x, y, w, h) {
self.fenceNodes = [];
// No fences or gates are created
};
// Place wheat
self.placeWheats = function (x, y, w, h, count) {
self.wheats = [];
// Define grid size
var rows = 5;
var cols = 10;
// Choose vegetable asset per area
var vegAssetId = 'wheat';
if (self.index === 1) vegAssetId = 'carrot';
if (self.index === 2) vegAssetId = 'tomato';
if (self.index === 3) vegAssetId = 'eggplant';
// Fallback if asset not found, use wheat
if (!LK.assets || !LK.assets[vegAssetId]) vegAssetId = 'wheat';
// Calculate asset size for spacing (use wheat size as reference for all)
var vegAsset = LK.getAsset('wheat', {
anchorX: 0.5,
anchorY: 1
});
var wheatW = vegAsset.width;
var wheatH = vegAsset.height;
// Padding from fences
var padX = 60;
var padY = 60;
// Compute available area for wheat grid
var gridW = w - 2 * padX;
var gridH = h - 2 * padY;
// Compute spacing between wheat plants
var spacingX = (gridW - wheatW) / (cols - 1);
var spacingY = (gridH - wheatH) / (rows - 1);
// Place appropriate vegetable in grid based on field index
for (var row = 0; row < rows; ++row) {
for (var col = 0; col < cols; ++col) {
var wx = x + padX + col * spacingX + wheatW / 2;
var wy = y + padY + row * spacingY + wheatH;
var veg;
// Create appropriate vegetable based on area/field index
if (self.index === 1) {
veg = new CarrotSlot();
} else if (self.index === 2) {
veg = new TomatoSlot();
} else if (self.index === 3) {
veg = new EggplantSlot();
} else {
veg = new Wheat();
veg.fieldIndex = self.index;
veg.vegAssetId = vegAssetId;
veg.setVegAsset && veg.setVegAsset(vegAssetId);
}
veg.x = wx;
veg.y = wy;
self.addChild(veg);
self.wheats.push(veg);
}
}
};
// Lock overlay
self.showLock = function (centerX, centerY) {
if (self.lockNode) return;
self.lockNode = LK.getAsset('lock', {
anchorX: 0.5,
anchorY: 0.5,
x: centerX,
y: centerY
});
self.addChild(self.lockNode);
// Unlock text
var unlockTxt = new Text2('Unlock\n$' + self.lockCost, {
size: 60,
fill: "#fff"
});
unlockTxt.anchor.set(0.5, 0.5);
unlockTxt.x = centerX;
unlockTxt.y = centerY + 70;
self.unlockBtn = unlockTxt;
self.addChild(unlockTxt);
};
self.hideLock = function () {
if (self.lockNode) {
self.lockNode.destroy();
self.lockNode = null;
}
if (self.unlockBtn) {
self.unlockBtn.destroy();
self.unlockBtn = null;
}
};
return self;
});
// MainMenu scene
var MainMenu = Container.expand(function () {
var self = Container.call(this);
// Create background
self.background = self.attachAsset('farm_background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
// Create start button
self.startButton = self.attachAsset('start_button', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
// Add start text
self.startText = new Text2('START', {
size: 100,
fill: 0xFFFFFF
});
self.startText.anchor.set(0.5, 0.5);
self.startText.x = 2048 / 2;
self.startText.y = 2732 / 2;
self.addChild(self.startText);
// Event listener for button press
self.down = function (x, y, obj) {
// Check if click is on button
var buttonBounds = {
left: self.startButton.x - self.startButton.width / 2,
right: self.startButton.x + self.startButton.width / 2,
top: self.startButton.y - self.startButton.height / 2,
bottom: self.startButton.y + self.startButton.height / 2
};
if (x >= buttonBounds.left && x <= buttonBounds.right && y >= buttonBounds.top && y <= buttonBounds.bottom) {
// Switch to game scene
if (sceneManager) {
sceneManager.switchToScene('game');
}
}
};
return self;
});
// Scene Manager to handle different scenes
var SceneManager = Container.expand(function () {
var self = Container.call(this);
// Store scenes and active scene
self.scenes = {};
self.activeScene = null;
// Create and register a new scene
self.createScene = function (name) {
var scene = new Container();
scene.name = name;
self.scenes[name] = scene;
return scene;
};
// Switch to a different scene
self.switchToScene = function (name) {
// If we have an active scene, remove it
if (self.activeScene) {
self.removeChild(self.activeScene);
}
// Get new scene and add it if it exists
var newScene = self.scenes[name];
if (newScene) {
self.activeScene = newScene;
self.addChild(newScene);
return true;
}
return false;
};
return self;
});
// TomatoSlot class for area 3
var TomatoSlot = Container.expand(function () {
var self = Container.call(this);
self.slotAsset = self.attachAsset('tomato', {
anchorX: 0.5,
anchorY: 1
});
var sproutAsset = self.attachAsset('sprout', {
anchorX: 0.5,
anchorY: 1,
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
});
self.harvested = false;
self.regrowTime = 1800; // 30 seconds at 60fps
self.regrowCounter = 0;
self.fieldIndex = 2; // Area 3
self.regrowing = false;
// Show/hide tomato and handle regrow animation
self.setHarvested = function (harvested) {
self.harvested = harvested;
if (harvested) {
// Hide tomato with animation
tween(self.slotAsset, {
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut
});
// Show sprout with animation
sproutAsset.alpha = 1;
sproutAsset.scaleX = 0.1;
sproutAsset.scaleY = 0.1;
// Animate sprout appearing
tween(sproutAsset, {
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut
});
self.regrowCounter = self.regrowTime;
self.regrowing = true;
} else {
// Show tomato with animation
tween(self.slotAsset, {
alpha: 1,
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeIn
});
// Hide sprout with animation
tween(sproutAsset, {
alpha: 0
}, {
duration: 300,
easing: tween.easeIn
});
self.regrowing = false;
}
};
// Regrow logic
self.update = function () {
if (self.harvested && self.regrowing) {
if (self.regrowCounter > 0) {
// Animate sprout growing to tomato size
var t = 1 - self.regrowCounter / self.regrowTime;
sproutAsset.scaleX = 0.5 + 0.5 * t;
sproutAsset.scaleY = 0.5 + 0.5 * t;
self.regrowCounter--;
}
if (self.regrowCounter <= 0) {
// Sprout is now full size, switch back to tomato and allow harvest again
sproutAsset.scaleX = 1;
sproutAsset.scaleY = 1;
self.setHarvested(false);
}
}
};
// For hit detection
self.isHarvestable = function () {
return !self.harvested;
};
return self;
});
// Wheat class
var Wheat = Container.expand(function () {
var self = Container.call(this);
self.vegAssetId = 'wheat';
self.wheatAsset = self.attachAsset('wheat', {
anchorX: 0.5,
anchorY: 1
});
var sproutAsset = self.attachAsset('sprout', {
anchorX: 0.5,
anchorY: 1,
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
});
self.harvested = false;
self.regrowTime = 1800; // 30 seconds at 60fps
self.regrowCounter = 0;
self.fieldIndex = 0; // which field this wheat belongs to
self.regrowing = false;
self.removeAfter = 0;
// Change vegetable asset if needed
self.setVegAsset = function (assetId) {
if (self.wheatAsset) {
self.wheatAsset.destroy();
}
self.vegAssetId = assetId || 'wheat';
// Only allow wheat, carrot, tomato, eggplant
var validAssets = {
wheat: 1,
carrot: 1,
tomato: 1,
eggplant: 1
};
if (!validAssets[self.vegAssetId]) self.vegAssetId = 'wheat';
self.wheatAsset = self.attachAsset(self.vegAssetId, {
anchorX: 0.5,
anchorY: 1
});
// Make sure sprout is on top
self.removeChild(sproutAsset);
self.addChild(sproutAsset);
};
// Show/hide wheat and handle regrow animation
self.setHarvested = function (harvested) {
self.harvested = harvested;
if (harvested) {
// Hide wheat, show sprout
if (self.wheatAsset) {
// Animate wheat harvesting with tween
tween(self.wheatAsset, {
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut
});
}
// Show and animate sprout growing from nothing
sproutAsset.alpha = 1;
sproutAsset.scaleX = 0.1;
sproutAsset.scaleY = 0.1;
// Animate sprout appearing
tween(sproutAsset, {
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut
});
self.regrowCounter = self.regrowTime;
self.regrowing = true;
} else {
// Show wheat/veg, hide sprout with animation
if (self.wheatAsset) {
tween(self.wheatAsset, {
alpha: 1,
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeIn
});
}
// Fade out sprout
tween(sproutAsset, {
alpha: 0
}, {
duration: 300,
easing: tween.easeIn
});
self.regrowing = false;
}
};
// Regrow logic and removal after 5 seconds
self.update = function () {
if (self.harvested && self.regrowing) {
if (self.regrowCounter > 0) {
// Animate sprout growing to wheat/veg size
var t = 1 - self.regrowCounter / self.regrowTime;
sproutAsset.scaleX = 0.5 + 0.5 * t;
sproutAsset.scaleY = 0.5 + 0.5 * t;
self.regrowCounter--;
}
if (self.regrowCounter <= 0) {
// Sprout is now full size, switch back to wheat/veg and allow harvest again
sproutAsset.scaleX = 1;
sproutAsset.scaleY = 1;
self.setHarvested(false);
}
}
};
// For hit detection
self.isHarvestable = function () {
return !self.harvested;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x4caf50 // Grass green
});
/****
* Game Code
****/
// Create scene manager for handling different game scenes
var sceneManager = new SceneManager();
game.addChild(sceneManager);
// Create main menu scene
var mainMenuScene = sceneManager.createScene('menu');
var mainMenu = new MainMenu();
mainMenuScene.addChild(mainMenu);
// Create game scene
var gameScene = sceneManager.createScene('game');
// Initially show the main menu
sceneManager.switchToScene('menu');
// --- Game constants ---
// Field fence (rectangle)
// Wheat (ellipse)
// Farmer (box)
// House (box)
// Sickle (ellipse, yellow)
// Lock icon (ellipse, gray)
// Area 2
// Area 3
// Area 4
var FIELD_W = 900;
var FIELD_H = 900;
var FIELD_GAP = 120; // Increased gap to move fences further from center
var FIELD_COUNT = 4;
var WHEAT_PER_FIELD = 10;
// Area unlock prices and reward multipliers
var FIELD_LOCK_COSTS = [0, 20000, 40000, 60000]; // Area 1 open, others locked
var FIELD_REWARD_MULT = [1, 2, 3, 4]; // Area 1=1x, 2=2x, 3=3x, 4=4x
// --- Area constants ---
// Define the distance from center for all areas (equal distance)
var AREA_DISTANCE = 600;
// Create an array to hold the areas
var areas = [];
var animals = [];
// --- Game state ---
var fields = [];
var farmer = null;
var money = 0;
var moneyTxt = null;
var sickle = null;
var dragging = false;
var dragOffsetX = 0;
var dragOffsetY = 0;
var unlockFieldIndex = -1;
// Initialize game elements when switching to game scene
function initGameElements() {
// --- Layout calculation ---
// Fields: 2x2 grid, with house in center
// [0][1]
// [2][3]
var fieldPositions = [{
x: 0,
y: 0
},
// top-left
{
x: FIELD_W + FIELD_GAP,
y: 0
},
// top-right
{
x: 0,
y: FIELD_H + FIELD_GAP
},
// bottom-left
{
x: FIELD_W + FIELD_GAP,
y: FIELD_H + FIELD_GAP
} // bottom-right
];
// Center everything in game area
var totalW = FIELD_W * 2 + FIELD_GAP;
var totalH = FIELD_H * 2 + FIELD_GAP;
var offsetX = Math.floor((2048 - totalW) / 2);
var offsetY = Math.floor((2732 - totalH) / 2);
// --- Create fields ---
for (var i = 0; i < FIELD_COUNT; ++i) {
var f = new Field();
f.index = i;
f.locked = FIELD_LOCK_COSTS[i] > 0;
f.lockCost = FIELD_LOCK_COSTS[i];
var pos = fieldPositions[i];
var fx = offsetX + pos.x;
var fy = offsetY + pos.y;
// Skip drawing fences
f.placeWheats(fx, fy, FIELD_W, FIELD_H, WHEAT_PER_FIELD);
if (f.locked) {
// Center of field
f.showLock(fx + FIELD_W / 2, fy + FIELD_H / 2);
}
gameScene.addChild(f);
fields.push(f);
}
// Center reference point for positions
var centerX = offsetX + FIELD_W;
var centerY = offsetY + FIELD_H - 250;
// --- Create farmer ---
farmer = new Farmer();
farmer.x = centerX;
farmer.y = centerY - 120; // Start at center area
gameScene.addChild(farmer);
// --- Create sickle (attached to farmer's hand) ---
sickle = LK.getAsset('sickle', {
anchorX: 0.2,
// hand position, left side of ellipse
anchorY: 0.7,
// slightly below center
x: 0,
y: 30
});
sickle.alpha = 1;
farmer.addChild(sickle);
// --- Money display ---
moneyTxt = new Text2('$0', {
size: 100,
fill: "#fff"
});
moneyTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(moneyTxt);
// --- Helper: update money display ---
function updateMoneyDisplay() {
moneyTxt.setText('$' + money);
}
// --- Helper: unlock field if enough money ---
function tryUnlockField(fieldIdx) {
var f = fields[fieldIdx];
if (!f.locked) return;
if (money >= f.lockCost) {
money -= f.lockCost;
updateMoneyDisplay();
f.locked = false;
f.hideLock();
// Flash field green
for (var i = 0; i < f.fenceNodes.length; ++i) {
LK.effects.flashObject(f.fenceNodes[i], 0x00ff00, 600);
}
} else {
// Not enough money, flash red
LK.effects.flashScreen(0xff0000, 400);
}
}
// --- Touch/mouse handling ---
var lastDownX = 0,
lastDownY = 0;
var harvestMode = false;
var harvestWheat = null;
// Helper: find harvestable vegetable under (x, y) in unlocked fields
function findHarvestableWheat(x, y) {
for (var i = 0; i < fields.length; ++i) {
var f = fields[i];
if (f.locked) continue;
for (var j = 0; j < f.wheats.length; ++j) {
var w = f.wheats[j];
if (w.isHarvestable()) {
// Use bounding box for hit
var wx = w.x,
wy = w.y;
var ww = 60,
wh = 80;
if (x >= wx - ww / 2 && x <= wx + ww / 2 && y >= wy - wh && y <= wy) {
return w;
}
}
}
}
return null;
}
// Helper: find locked field under (x, y)
function findLockedField(x, y) {
for (var i = 0; i < fields.length; ++i) {
var f = fields[i];
if (!f.locked) continue;
// Use field area
var pos = fieldPositions[i];
var fx = offsetX + pos.x;
var fy = offsetY + pos.y;
if (x >= fx && x <= fx + FIELD_W && y >= fy && y <= fy + FIELD_H) {
return i;
}
}
return -1;
}
// --- Game event handlers ---
// Move farmer or harvest wheat
game.down = function (x, y, obj) {
lastDownX = x;
lastDownY = y;
// Check if tapping on locked field unlock button
var lockedIdx = findLockedField(x, y);
if (lockedIdx >= 0) {
var f = fields[lockedIdx];
// If tap is near lock icon or unlock text
var centerX = offsetX + fieldPositions[lockedIdx].x + FIELD_W / 2;
var centerY = offsetY + fieldPositions[lockedIdx].y + FIELD_H / 2;
var dist = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
if (dist < 120) {
tryUnlockField(lockedIdx);
return;
}
}
// Check for wheat
var w = findHarvestableWheat(x, y);
if (w) {
harvestMode = true;
harvestWheat = w;
// Change player to face forward during harvest
// Switch to player_down for harvesting
if (farmer.currentAsset) {
farmer.currentAsset.alpha = 0;
}
var playerDown = LK.getAsset('player_down', {
anchorX: 0.5,
anchorY: 0.5
});
playerDown.alpha = 1;
farmer.addChild(playerDown);
farmer.currentAsset = playerDown;
// Animate sickle (in farmer's hand)
tween(sickle, {
rotation: Math.PI * 2
}, {
duration: 300,
easing: tween.easeIn,
onFinish: function onFinish() {
sickle.rotation = 0;
// Show harvest animation effect
var harvestEffect = LK.getAsset('sprout', {
anchorX: 0.5,
anchorY: 0.5,
x: w.x,
y: w.y - 50,
alpha: 0.8,
scaleX: 2,
scaleY: 2
});
game.addChild(harvestEffect);
// Animate the harvest effect
tween(harvestEffect, {
alpha: 0,
y: harvestEffect.y - 100
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
harvestEffect.destroy();
}
});
}
});
// Harvest
w.setHarvested(true);
var reward = 100 * (FIELD_REWARD_MULT[w.fieldIndex] || 1);
money += reward;
updateMoneyDisplay();
// Flash wheat
LK.effects.flashObject(w, 0xffff00, 300);
// No setHarvested(false) here; regrow/removal handled in Wheat.update
harvestMode = false;
harvestWheat = null;
return;
}
// Move farmer with animation
farmer.moveTo(x, y);
dragging = true;
dragOffsetX = x - farmer.x;
dragOffsetY = y - farmer.y;
};
// Drag farmer
game.move = function (x, y, obj) {
if (dragging) {
farmer.moveTo(x - dragOffsetX, y - dragOffsetY);
}
if (harvestMode && harvestWheat) {
sickle.x = x;
sickle.y = y;
}
};
// End drag/harvest
game.up = function (x, y, obj) {
dragging = false;
harvestMode = false;
harvestWheat = null;
};
// --- Main update loop ---
game.update = function () {
// Update farmer
farmer.update();
// Update wheat regrow
for (var i = 0; i < fields.length; ++i) {
var f = fields[i];
for (var j = 0; j < f.wheats.length; ++j) {
f.wheats[j].update();
}
}
};
// --- Create areas and place animals ---
// Calculate center of game screen
var gameWidth = 2048;
var gameHeight = 2732;
var gameCenterX = gameWidth / 2;
var gameCenterY = gameHeight / 2;
// Create the four areas at equal distances from center
// Area_Left
var areaLeft = new Container();
areaLeft.x = gameCenterX - AREA_DISTANCE;
areaLeft.y = gameCenterY;
areaLeft.name = "Area_Left";
gameScene.addChild(areaLeft);
areas.push(areaLeft);
// Area_Right
var areaRight = new Container();
areaRight.x = gameCenterX + AREA_DISTANCE;
areaRight.y = gameCenterY;
areaRight.name = "Area_Right";
gameScene.addChild(areaRight);
areas.push(areaRight);
// Area_Top
var areaTop = new Container();
areaTop.x = gameCenterX;
areaTop.y = gameCenterY - AREA_DISTANCE;
areaTop.name = "Area_Top";
gameScene.addChild(areaTop);
areas.push(areaTop);
// Area_Bottom
var areaBottom = new Container();
areaBottom.x = gameCenterX;
areaBottom.y = gameCenterY + AREA_DISTANCE;
areaBottom.name = "Area_Bottom";
gameScene.addChild(areaBottom);
areas.push(areaBottom);
// Place animals in the areas
// Animal_Right at right edge of screen, centered vertically
var animalRight = new Animal();
animalRight.setAnimalImage('animal_right');
animalRight.name = "Animal_Right";
animalRight.x = gameWidth; // Position at right edge
animalRight.y = gameCenterY; // Center vertically
gameScene.addChild(animalRight); // Add directly to gameScene instead of areaRight
animals.push(animalRight);
// Area_Bottom has no animal
// --- Initial money display ---
updateMoneyDisplay();
// --- Place trees at corners ---
// Tree size
var treeWidth = 400;
var treeHeight = 532;
// Top left corner tree
var topLeftTree = LK.getAsset('tree', {
anchorX: 0.5,
anchorY: 0.5,
x: offsetX / 2,
y: offsetY / 2
});
gameScene.addChild(topLeftTree);
// Top right corner tree
var topRightTree = LK.getAsset('tree', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 - offsetX / 2,
y: offsetY / 2
});
gameScene.addChild(topRightTree);
// Bottom left corner tree
var bottomLeftTree = LK.getAsset('tree', {
anchorX: 0.5,
anchorY: 0.5,
x: offsetX / 2,
y: 2732 - offsetY / 2
});
gameScene.addChild(bottomLeftTree);
// Bottom right corner tree
var bottomRightTree = LK.getAsset('tree', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 - offsetX / 2,
y: 2732 - offsetY / 2
});
gameScene.addChild(bottomRightTree);
// --- Place trees at middle sides aligned with corner trees ---
// Tree to the left of center
var leftCenterTree = LK.getAsset('tree', {
anchorX: 0.5,
anchorY: 0.5,
x: offsetX / 2,
y: topRightTree.y
});
gameScene.addChild(leftCenterTree);
// Tree to the right of center
var rightCenterTree = LK.getAsset('tree', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 - offsetX / 2,
y: topRightTree.y
});
gameScene.addChild(rightCenterTree);
} // Close initGameElements function
// Call initGameElements when game starts
initGameElements();
// Register game scene event handlers
gameScene.down = game.down;
gameScene.move = game.move;
gameScene.up = game.up;
// Update function for the game scene
gameScene.update = game.update;
// Override game update to delegate to active scene
game.update = function () {
if (sceneManager.activeScene && sceneManager.activeScene.update) {
sceneManager.activeScene.update();
}
};
// Initialize main menu event handlers to route to the menu
game.down = function (x, y, obj) {
if (sceneManager.activeScene && sceneManager.activeScene.down) {
sceneManager.activeScene.down(x, y, obj);
}
};
game.move = function (x, y, obj) {
if (sceneManager.activeScene && sceneManager.activeScene.move) {
sceneManager.activeScene.move(x, y, obj);
}
};
game.up = function (x, y, obj) {
if (sceneManager.activeScene && sceneManager.activeScene.up) {
sceneManager.activeScene.up(x, y, obj);
}
}; ===================================================================
--- original.js
+++ change.js
@@ -432,8 +432,82 @@
}
};
return self;
});
+// MainMenu scene
+var MainMenu = Container.expand(function () {
+ var self = Container.call(this);
+ // Create background
+ self.background = self.attachAsset('farm_background', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 / 2,
+ y: 2732 / 2
+ });
+ // Create start button
+ self.startButton = self.attachAsset('start_button', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 / 2,
+ y: 2732 / 2
+ });
+ // Add start text
+ self.startText = new Text2('START', {
+ size: 100,
+ fill: 0xFFFFFF
+ });
+ self.startText.anchor.set(0.5, 0.5);
+ self.startText.x = 2048 / 2;
+ self.startText.y = 2732 / 2;
+ self.addChild(self.startText);
+ // Event listener for button press
+ self.down = function (x, y, obj) {
+ // Check if click is on button
+ var buttonBounds = {
+ left: self.startButton.x - self.startButton.width / 2,
+ right: self.startButton.x + self.startButton.width / 2,
+ top: self.startButton.y - self.startButton.height / 2,
+ bottom: self.startButton.y + self.startButton.height / 2
+ };
+ if (x >= buttonBounds.left && x <= buttonBounds.right && y >= buttonBounds.top && y <= buttonBounds.bottom) {
+ // Switch to game scene
+ if (sceneManager) {
+ sceneManager.switchToScene('game');
+ }
+ }
+ };
+ return self;
+});
+// Scene Manager to handle different scenes
+var SceneManager = Container.expand(function () {
+ var self = Container.call(this);
+ // Store scenes and active scene
+ self.scenes = {};
+ self.activeScene = null;
+ // Create and register a new scene
+ self.createScene = function (name) {
+ var scene = new Container();
+ scene.name = name;
+ self.scenes[name] = scene;
+ return scene;
+ };
+ // Switch to a different scene
+ self.switchToScene = function (name) {
+ // If we have an active scene, remove it
+ if (self.activeScene) {
+ self.removeChild(self.activeScene);
+ }
+ // Get new scene and add it if it exists
+ var newScene = self.scenes[name];
+ if (newScene) {
+ self.activeScene = newScene;
+ self.addChild(newScene);
+ return true;
+ }
+ return false;
+ };
+ return self;
+});
// TomatoSlot class for area 3
var TomatoSlot = Container.expand(function () {
var self = Container.call(this);
self.slotAsset = self.attachAsset('tomato', {
@@ -652,8 +726,19 @@
/****
* Game Code
****/
+// Create scene manager for handling different game scenes
+var sceneManager = new SceneManager();
+game.addChild(sceneManager);
+// Create main menu scene
+var mainMenuScene = sceneManager.createScene('menu');
+var mainMenu = new MainMenu();
+mainMenuScene.addChild(mainMenu);
+// Create game scene
+var gameScene = sceneManager.createScene('game');
+// Initially show the main menu
+sceneManager.switchToScene('menu');
// --- Game constants ---
// Field fence (rectangle)
// Wheat (ellipse)
// Farmer (box)
@@ -686,353 +771,386 @@
var dragging = false;
var dragOffsetX = 0;
var dragOffsetY = 0;
var unlockFieldIndex = -1;
-// --- Layout calculation ---
-// Fields: 2x2 grid, with house in center
-// [0][1]
-// [2][3]
-var fieldPositions = [{
- x: 0,
- y: 0
-},
-// top-left
-{
- x: FIELD_W + FIELD_GAP,
- y: 0
-},
-// top-right
-{
- x: 0,
- y: FIELD_H + FIELD_GAP
-},
-// bottom-left
-{
- x: FIELD_W + FIELD_GAP,
- y: FIELD_H + FIELD_GAP
-} // bottom-right
-];
-// Center everything in game area
-var totalW = FIELD_W * 2 + FIELD_GAP;
-var totalH = FIELD_H * 2 + FIELD_GAP;
-var offsetX = Math.floor((2048 - totalW) / 2);
-var offsetY = Math.floor((2732 - totalH) / 2);
-// --- Create fields ---
-for (var i = 0; i < FIELD_COUNT; ++i) {
- var f = new Field();
- f.index = i;
- f.locked = FIELD_LOCK_COSTS[i] > 0;
- f.lockCost = FIELD_LOCK_COSTS[i];
- var pos = fieldPositions[i];
- var fx = offsetX + pos.x;
- var fy = offsetY + pos.y;
- // Skip drawing fences
- f.placeWheats(fx, fy, FIELD_W, FIELD_H, WHEAT_PER_FIELD);
- if (f.locked) {
- // Center of field
- f.showLock(fx + FIELD_W / 2, fy + FIELD_H / 2);
+// Initialize game elements when switching to game scene
+function initGameElements() {
+ // --- Layout calculation ---
+ // Fields: 2x2 grid, with house in center
+ // [0][1]
+ // [2][3]
+ var fieldPositions = [{
+ x: 0,
+ y: 0
+ },
+ // top-left
+ {
+ x: FIELD_W + FIELD_GAP,
+ y: 0
+ },
+ // top-right
+ {
+ x: 0,
+ y: FIELD_H + FIELD_GAP
+ },
+ // bottom-left
+ {
+ x: FIELD_W + FIELD_GAP,
+ y: FIELD_H + FIELD_GAP
+ } // bottom-right
+ ];
+ // Center everything in game area
+ var totalW = FIELD_W * 2 + FIELD_GAP;
+ var totalH = FIELD_H * 2 + FIELD_GAP;
+ var offsetX = Math.floor((2048 - totalW) / 2);
+ var offsetY = Math.floor((2732 - totalH) / 2);
+ // --- Create fields ---
+ for (var i = 0; i < FIELD_COUNT; ++i) {
+ var f = new Field();
+ f.index = i;
+ f.locked = FIELD_LOCK_COSTS[i] > 0;
+ f.lockCost = FIELD_LOCK_COSTS[i];
+ var pos = fieldPositions[i];
+ var fx = offsetX + pos.x;
+ var fy = offsetY + pos.y;
+ // Skip drawing fences
+ f.placeWheats(fx, fy, FIELD_W, FIELD_H, WHEAT_PER_FIELD);
+ if (f.locked) {
+ // Center of field
+ f.showLock(fx + FIELD_W / 2, fy + FIELD_H / 2);
+ }
+ gameScene.addChild(f);
+ fields.push(f);
}
- game.addChild(f);
- fields.push(f);
-}
-// Center reference point for positions
-var centerX = offsetX + FIELD_W;
-var centerY = offsetY + FIELD_H - 250;
-// --- Create farmer ---
-farmer = new Farmer();
-farmer.x = centerX;
-farmer.y = centerY - 120; // Start at center area
-game.addChild(farmer);
-// --- Create sickle (attached to farmer's hand) ---
-sickle = LK.getAsset('sickle', {
- anchorX: 0.2,
- // hand position, left side of ellipse
- anchorY: 0.7,
- // slightly below center
- x: 0,
- y: 30
-});
-sickle.alpha = 1;
-farmer.addChild(sickle);
-// --- Money display ---
-moneyTxt = new Text2('$0', {
- size: 100,
- fill: "#fff"
-});
-moneyTxt.anchor.set(0.5, 0);
-LK.gui.top.addChild(moneyTxt);
-// --- Helper: update money display ---
-function updateMoneyDisplay() {
- moneyTxt.setText('$' + money);
-}
-// --- Helper: unlock field if enough money ---
-function tryUnlockField(fieldIdx) {
- var f = fields[fieldIdx];
- if (!f.locked) return;
- if (money >= f.lockCost) {
- money -= f.lockCost;
- updateMoneyDisplay();
- f.locked = false;
- f.hideLock();
- // Flash field green
- for (var i = 0; i < f.fenceNodes.length; ++i) {
- LK.effects.flashObject(f.fenceNodes[i], 0x00ff00, 600);
+ // Center reference point for positions
+ var centerX = offsetX + FIELD_W;
+ var centerY = offsetY + FIELD_H - 250;
+ // --- Create farmer ---
+ farmer = new Farmer();
+ farmer.x = centerX;
+ farmer.y = centerY - 120; // Start at center area
+ gameScene.addChild(farmer);
+ // --- Create sickle (attached to farmer's hand) ---
+ sickle = LK.getAsset('sickle', {
+ anchorX: 0.2,
+ // hand position, left side of ellipse
+ anchorY: 0.7,
+ // slightly below center
+ x: 0,
+ y: 30
+ });
+ sickle.alpha = 1;
+ farmer.addChild(sickle);
+ // --- Money display ---
+ moneyTxt = new Text2('$0', {
+ size: 100,
+ fill: "#fff"
+ });
+ moneyTxt.anchor.set(0.5, 0);
+ LK.gui.top.addChild(moneyTxt);
+ // --- Helper: update money display ---
+ function updateMoneyDisplay() {
+ moneyTxt.setText('$' + money);
+ }
+ // --- Helper: unlock field if enough money ---
+ function tryUnlockField(fieldIdx) {
+ var f = fields[fieldIdx];
+ if (!f.locked) return;
+ if (money >= f.lockCost) {
+ money -= f.lockCost;
+ updateMoneyDisplay();
+ f.locked = false;
+ f.hideLock();
+ // Flash field green
+ for (var i = 0; i < f.fenceNodes.length; ++i) {
+ LK.effects.flashObject(f.fenceNodes[i], 0x00ff00, 600);
+ }
+ } else {
+ // Not enough money, flash red
+ LK.effects.flashScreen(0xff0000, 400);
}
- } else {
- // Not enough money, flash red
- LK.effects.flashScreen(0xff0000, 400);
}
-}
-// --- Touch/mouse handling ---
-var lastDownX = 0,
- lastDownY = 0;
-var harvestMode = false;
-var harvestWheat = null;
-// Helper: find harvestable vegetable under (x, y) in unlocked fields
-function findHarvestableWheat(x, y) {
- for (var i = 0; i < fields.length; ++i) {
- var f = fields[i];
- if (f.locked) continue;
- for (var j = 0; j < f.wheats.length; ++j) {
- var w = f.wheats[j];
- if (w.isHarvestable()) {
- // Use bounding box for hit
- var wx = w.x,
- wy = w.y;
- var ww = 60,
- wh = 80;
- if (x >= wx - ww / 2 && x <= wx + ww / 2 && y >= wy - wh && y <= wy) {
- return w;
+ // --- Touch/mouse handling ---
+ var lastDownX = 0,
+ lastDownY = 0;
+ var harvestMode = false;
+ var harvestWheat = null;
+ // Helper: find harvestable vegetable under (x, y) in unlocked fields
+ function findHarvestableWheat(x, y) {
+ for (var i = 0; i < fields.length; ++i) {
+ var f = fields[i];
+ if (f.locked) continue;
+ for (var j = 0; j < f.wheats.length; ++j) {
+ var w = f.wheats[j];
+ if (w.isHarvestable()) {
+ // Use bounding box for hit
+ var wx = w.x,
+ wy = w.y;
+ var ww = 60,
+ wh = 80;
+ if (x >= wx - ww / 2 && x <= wx + ww / 2 && y >= wy - wh && y <= wy) {
+ return w;
+ }
}
}
}
+ return null;
}
- return null;
-}
-// Helper: find locked field under (x, y)
-function findLockedField(x, y) {
- for (var i = 0; i < fields.length; ++i) {
- var f = fields[i];
- if (!f.locked) continue;
- // Use field area
- var pos = fieldPositions[i];
- var fx = offsetX + pos.x;
- var fy = offsetY + pos.y;
- if (x >= fx && x <= fx + FIELD_W && y >= fy && y <= fy + FIELD_H) {
- return i;
+ // Helper: find locked field under (x, y)
+ function findLockedField(x, y) {
+ for (var i = 0; i < fields.length; ++i) {
+ var f = fields[i];
+ if (!f.locked) continue;
+ // Use field area
+ var pos = fieldPositions[i];
+ var fx = offsetX + pos.x;
+ var fy = offsetY + pos.y;
+ if (x >= fx && x <= fx + FIELD_W && y >= fy && y <= fy + FIELD_H) {
+ return i;
+ }
}
+ return -1;
}
- return -1;
-}
-// --- Game event handlers ---
-// Move farmer or harvest wheat
-game.down = function (x, y, obj) {
- lastDownX = x;
- lastDownY = y;
- // Check if tapping on locked field unlock button
- var lockedIdx = findLockedField(x, y);
- if (lockedIdx >= 0) {
- var f = fields[lockedIdx];
- // If tap is near lock icon or unlock text
- var centerX = offsetX + fieldPositions[lockedIdx].x + FIELD_W / 2;
- var centerY = offsetY + fieldPositions[lockedIdx].y + FIELD_H / 2;
- var dist = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
- if (dist < 120) {
- tryUnlockField(lockedIdx);
+ // --- Game event handlers ---
+ // Move farmer or harvest wheat
+ game.down = function (x, y, obj) {
+ lastDownX = x;
+ lastDownY = y;
+ // Check if tapping on locked field unlock button
+ var lockedIdx = findLockedField(x, y);
+ if (lockedIdx >= 0) {
+ var f = fields[lockedIdx];
+ // If tap is near lock icon or unlock text
+ var centerX = offsetX + fieldPositions[lockedIdx].x + FIELD_W / 2;
+ var centerY = offsetY + fieldPositions[lockedIdx].y + FIELD_H / 2;
+ var dist = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
+ if (dist < 120) {
+ tryUnlockField(lockedIdx);
+ return;
+ }
+ }
+ // Check for wheat
+ var w = findHarvestableWheat(x, y);
+ if (w) {
+ harvestMode = true;
+ harvestWheat = w;
+ // Change player to face forward during harvest
+ // Switch to player_down for harvesting
+ if (farmer.currentAsset) {
+ farmer.currentAsset.alpha = 0;
+ }
+ var playerDown = LK.getAsset('player_down', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ playerDown.alpha = 1;
+ farmer.addChild(playerDown);
+ farmer.currentAsset = playerDown;
+ // Animate sickle (in farmer's hand)
+ tween(sickle, {
+ rotation: Math.PI * 2
+ }, {
+ duration: 300,
+ easing: tween.easeIn,
+ onFinish: function onFinish() {
+ sickle.rotation = 0;
+ // Show harvest animation effect
+ var harvestEffect = LK.getAsset('sprout', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: w.x,
+ y: w.y - 50,
+ alpha: 0.8,
+ scaleX: 2,
+ scaleY: 2
+ });
+ game.addChild(harvestEffect);
+ // Animate the harvest effect
+ tween(harvestEffect, {
+ alpha: 0,
+ y: harvestEffect.y - 100
+ }, {
+ duration: 500,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ harvestEffect.destroy();
+ }
+ });
+ }
+ });
+ // Harvest
+ w.setHarvested(true);
+ var reward = 100 * (FIELD_REWARD_MULT[w.fieldIndex] || 1);
+ money += reward;
+ updateMoneyDisplay();
+ // Flash wheat
+ LK.effects.flashObject(w, 0xffff00, 300);
+ // No setHarvested(false) here; regrow/removal handled in Wheat.update
+ harvestMode = false;
+ harvestWheat = null;
return;
}
- }
- // Check for wheat
- var w = findHarvestableWheat(x, y);
- if (w) {
- harvestMode = true;
- harvestWheat = w;
- // Change player to face forward during harvest
- // Switch to player_down for harvesting
- if (farmer.currentAsset) {
- farmer.currentAsset.alpha = 0;
+ // Move farmer with animation
+ farmer.moveTo(x, y);
+ dragging = true;
+ dragOffsetX = x - farmer.x;
+ dragOffsetY = y - farmer.y;
+ };
+ // Drag farmer
+ game.move = function (x, y, obj) {
+ if (dragging) {
+ farmer.moveTo(x - dragOffsetX, y - dragOffsetY);
}
- var playerDown = LK.getAsset('player_down', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- playerDown.alpha = 1;
- farmer.addChild(playerDown);
- farmer.currentAsset = playerDown;
- // Animate sickle (in farmer's hand)
- tween(sickle, {
- rotation: Math.PI * 2
- }, {
- duration: 300,
- easing: tween.easeIn,
- onFinish: function onFinish() {
- sickle.rotation = 0;
- // Show harvest animation effect
- var harvestEffect = LK.getAsset('sprout', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: w.x,
- y: w.y - 50,
- alpha: 0.8,
- scaleX: 2,
- scaleY: 2
- });
- game.addChild(harvestEffect);
- // Animate the harvest effect
- tween(harvestEffect, {
- alpha: 0,
- y: harvestEffect.y - 100
- }, {
- duration: 500,
- easing: tween.easeOut,
- onFinish: function onFinish() {
- harvestEffect.destroy();
- }
- });
- }
- });
- // Harvest
- w.setHarvested(true);
- var reward = 100 * (FIELD_REWARD_MULT[w.fieldIndex] || 1);
- money += reward;
- updateMoneyDisplay();
- // Flash wheat
- LK.effects.flashObject(w, 0xffff00, 300);
- // No setHarvested(false) here; regrow/removal handled in Wheat.update
+ if (harvestMode && harvestWheat) {
+ sickle.x = x;
+ sickle.y = y;
+ }
+ };
+ // End drag/harvest
+ game.up = function (x, y, obj) {
+ dragging = false;
harvestMode = false;
harvestWheat = null;
- return;
+ };
+ // --- Main update loop ---
+ game.update = function () {
+ // Update farmer
+ farmer.update();
+ // Update wheat regrow
+ for (var i = 0; i < fields.length; ++i) {
+ var f = fields[i];
+ for (var j = 0; j < f.wheats.length; ++j) {
+ f.wheats[j].update();
+ }
+ }
+ };
+ // --- Create areas and place animals ---
+ // Calculate center of game screen
+ var gameWidth = 2048;
+ var gameHeight = 2732;
+ var gameCenterX = gameWidth / 2;
+ var gameCenterY = gameHeight / 2;
+ // Create the four areas at equal distances from center
+ // Area_Left
+ var areaLeft = new Container();
+ areaLeft.x = gameCenterX - AREA_DISTANCE;
+ areaLeft.y = gameCenterY;
+ areaLeft.name = "Area_Left";
+ gameScene.addChild(areaLeft);
+ areas.push(areaLeft);
+ // Area_Right
+ var areaRight = new Container();
+ areaRight.x = gameCenterX + AREA_DISTANCE;
+ areaRight.y = gameCenterY;
+ areaRight.name = "Area_Right";
+ gameScene.addChild(areaRight);
+ areas.push(areaRight);
+ // Area_Top
+ var areaTop = new Container();
+ areaTop.x = gameCenterX;
+ areaTop.y = gameCenterY - AREA_DISTANCE;
+ areaTop.name = "Area_Top";
+ gameScene.addChild(areaTop);
+ areas.push(areaTop);
+ // Area_Bottom
+ var areaBottom = new Container();
+ areaBottom.x = gameCenterX;
+ areaBottom.y = gameCenterY + AREA_DISTANCE;
+ areaBottom.name = "Area_Bottom";
+ gameScene.addChild(areaBottom);
+ areas.push(areaBottom);
+ // Place animals in the areas
+ // Animal_Right at right edge of screen, centered vertically
+ var animalRight = new Animal();
+ animalRight.setAnimalImage('animal_right');
+ animalRight.name = "Animal_Right";
+ animalRight.x = gameWidth; // Position at right edge
+ animalRight.y = gameCenterY; // Center vertically
+ gameScene.addChild(animalRight); // Add directly to gameScene instead of areaRight
+ animals.push(animalRight);
+ // Area_Bottom has no animal
+ // --- Initial money display ---
+ updateMoneyDisplay();
+ // --- Place trees at corners ---
+ // Tree size
+ var treeWidth = 400;
+ var treeHeight = 532;
+ // Top left corner tree
+ var topLeftTree = LK.getAsset('tree', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: offsetX / 2,
+ y: offsetY / 2
+ });
+ gameScene.addChild(topLeftTree);
+ // Top right corner tree
+ var topRightTree = LK.getAsset('tree', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 - offsetX / 2,
+ y: offsetY / 2
+ });
+ gameScene.addChild(topRightTree);
+ // Bottom left corner tree
+ var bottomLeftTree = LK.getAsset('tree', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: offsetX / 2,
+ y: 2732 - offsetY / 2
+ });
+ gameScene.addChild(bottomLeftTree);
+ // Bottom right corner tree
+ var bottomRightTree = LK.getAsset('tree', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 - offsetX / 2,
+ y: 2732 - offsetY / 2
+ });
+ gameScene.addChild(bottomRightTree);
+ // --- Place trees at middle sides aligned with corner trees ---
+ // Tree to the left of center
+ var leftCenterTree = LK.getAsset('tree', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: offsetX / 2,
+ y: topRightTree.y
+ });
+ gameScene.addChild(leftCenterTree);
+ // Tree to the right of center
+ var rightCenterTree = LK.getAsset('tree', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 - offsetX / 2,
+ y: topRightTree.y
+ });
+ gameScene.addChild(rightCenterTree);
+} // Close initGameElements function
+// Call initGameElements when game starts
+initGameElements();
+// Register game scene event handlers
+gameScene.down = game.down;
+gameScene.move = game.move;
+gameScene.up = game.up;
+// Update function for the game scene
+gameScene.update = game.update;
+// Override game update to delegate to active scene
+game.update = function () {
+ if (sceneManager.activeScene && sceneManager.activeScene.update) {
+ sceneManager.activeScene.update();
}
- // Move farmer with animation
- farmer.moveTo(x, y);
- dragging = true;
- dragOffsetX = x - farmer.x;
- dragOffsetY = y - farmer.y;
};
-// Drag farmer
+// Initialize main menu event handlers to route to the menu
+game.down = function (x, y, obj) {
+ if (sceneManager.activeScene && sceneManager.activeScene.down) {
+ sceneManager.activeScene.down(x, y, obj);
+ }
+};
game.move = function (x, y, obj) {
- if (dragging) {
- farmer.moveTo(x - dragOffsetX, y - dragOffsetY);
+ if (sceneManager.activeScene && sceneManager.activeScene.move) {
+ sceneManager.activeScene.move(x, y, obj);
}
- if (harvestMode && harvestWheat) {
- sickle.x = x;
- sickle.y = y;
- }
};
-// End drag/harvest
game.up = function (x, y, obj) {
- dragging = false;
- harvestMode = false;
- harvestWheat = null;
-};
-// --- Main update loop ---
-game.update = function () {
- // Update farmer
- farmer.update();
- // Update wheat regrow
- for (var i = 0; i < fields.length; ++i) {
- var f = fields[i];
- for (var j = 0; j < f.wheats.length; ++j) {
- f.wheats[j].update();
- }
+ if (sceneManager.activeScene && sceneManager.activeScene.up) {
+ sceneManager.activeScene.up(x, y, obj);
}
-};
-// --- Create areas and place animals ---
-// Calculate center of game screen
-var gameWidth = 2048;
-var gameHeight = 2732;
-var gameCenterX = gameWidth / 2;
-var gameCenterY = gameHeight / 2;
-// Create the four areas at equal distances from center
-// Area_Left
-var areaLeft = new Container();
-areaLeft.x = gameCenterX - AREA_DISTANCE;
-areaLeft.y = gameCenterY;
-areaLeft.name = "Area_Left";
-game.addChild(areaLeft);
-areas.push(areaLeft);
-// Area_Right
-var areaRight = new Container();
-areaRight.x = gameCenterX + AREA_DISTANCE;
-areaRight.y = gameCenterY;
-areaRight.name = "Area_Right";
-game.addChild(areaRight);
-areas.push(areaRight);
-// Area_Top
-var areaTop = new Container();
-areaTop.x = gameCenterX;
-areaTop.y = gameCenterY - AREA_DISTANCE;
-areaTop.name = "Area_Top";
-game.addChild(areaTop);
-areas.push(areaTop);
-// Area_Bottom
-var areaBottom = new Container();
-areaBottom.x = gameCenterX;
-areaBottom.y = gameCenterY + AREA_DISTANCE;
-areaBottom.name = "Area_Bottom";
-game.addChild(areaBottom);
-areas.push(areaBottom);
-// Place animals in the areas
-// Animal_Right at right edge of screen, centered vertically
-var animalRight = new Animal();
-animalRight.setAnimalImage('animal_right');
-animalRight.name = "Animal_Right";
-animalRight.x = gameWidth; // Position at right edge
-animalRight.y = gameCenterY; // Center vertically
-game.addChild(animalRight); // Add directly to game instead of areaRight
-animals.push(animalRight);
-// Area_Bottom has no animal
-// --- Initial money display ---
-updateMoneyDisplay();
-// --- Place trees at corners ---
-// Tree size
-var treeWidth = 400;
-var treeHeight = 532;
-// Top left corner tree
-var topLeftTree = LK.getAsset('tree', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: offsetX / 2,
- y: offsetY / 2
-});
-game.addChild(topLeftTree);
-// Top right corner tree
-var topRightTree = LK.getAsset('tree', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: 2048 - offsetX / 2,
- y: offsetY / 2
-});
-game.addChild(topRightTree);
-// Bottom left corner tree
-var bottomLeftTree = LK.getAsset('tree', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: offsetX / 2,
- y: 2732 - offsetY / 2
-});
-game.addChild(bottomLeftTree);
-// Bottom right corner tree
-var bottomRightTree = LK.getAsset('tree', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: 2048 - offsetX / 2,
- y: 2732 - offsetY / 2
-});
-game.addChild(bottomRightTree);
-// --- Place trees at middle sides aligned with corner trees ---
-// Tree to the left of center
-var leftCenterTree = LK.getAsset('tree', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: offsetX / 2,
- y: topRightTree.y
-});
-game.addChild(leftCenterTree);
-// Tree to the right of center
-var rightCenterTree = LK.getAsset('tree', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: 2048 - offsetX / 2,
- y: topRightTree.y
-});
-game.addChild(rightCenterTree);
\ No newline at end of file
+};
\ No newline at end of file