/**** * Plugins ****/ var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var GalleryItem = Container.expand(function (plantData, index) { var self = Container.call(this); self.plantData = plantData; self.index = index; self.isDragging = false; var background = self.attachAsset('gallerySlot', { anchorX: 0.5, anchorY: 0.5 }); if (plantData.top) { var topComponent = self.attachAsset(plantData.top, { anchorX: 0.5, anchorY: 0.5, scaleX: 0.6, scaleY: 0.6, y: -40 }); } if (plantData.bottom) { var bottomComponent = self.attachAsset(plantData.bottom, { anchorX: 0.5, anchorY: 0.5, scaleX: 0.6, scaleY: 0.6, y: 20 }); } self.down = function (x, y, obj) { self.isDragging = true; draggedGalleryItem = self; }; return self; }); var PlantComponent = Container.expand(function (assetId, componentType) { var self = Container.call(this); self.assetId = assetId; self.componentType = componentType; // 'top' or 'bottom' self.isInWorkspace = false; var graphic = self.attachAsset(assetId, { anchorX: 0.5, anchorY: 0.5 }); self.down = function (x, y, obj) { if (!self.isInWorkspace) { // Create a copy for the workspace var newComponent = new PlantComponent(self.assetId, self.componentType); newComponent.x = x; newComponent.y = y; newComponent.isInWorkspace = true; game.addChild(newComponent); selectedComponent = newComponent; LK.getSound('place').play(); } else { selectedComponent = self; } }; return self; }); var PlantCreation = Container.expand(function () { var self = Container.call(this); self.topComponent = null; self.bottomComponent = null; self.setTop = function (component) { if (self.topComponent) { self.removeChild(self.topComponent); } self.topComponent = component; if (component) { self.addChild(component); component.x = 0; component.y = -100; } }; self.setBottom = function (component) { if (self.bottomComponent) { self.removeChild(self.bottomComponent); } self.bottomComponent = component; if (component) { self.addChild(component); component.x = 0; component.y = 50; } }; self.clear = function () { self.setTop(null); self.setBottom(null); }; self.getData = function () { return { top: self.topComponent ? self.topComponent.assetId : null, bottom: self.bottomComponent ? self.bottomComponent.assetId : null }; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xf5f5dc }); /**** * Game Code ****/ // Game state // Plant tops - flowers, leaves, combinations // Plant bottoms - stems, bark, potted options // UI elements var selectedComponent = null; var currentPlant = new PlantCreation(); var savedPlants = []; try { var storedPlants = storage.savedPlants; if (storedPlants && Array.isArray(storedPlants)) { savedPlants = storedPlants; } else { savedPlants = []; } } catch (e) { console.log('Storage retrieval error:', e); savedPlants = []; } // Ensure savedPlants is always an array if (!savedPlants || !Array.isArray(savedPlants)) { savedPlants = []; } var showingGallery = false; var draggedGalleryItem = null; // UI containers var topComponents = []; var bottomComponents = []; var galleryItems = []; // Create workspace var workspace = LK.getAsset('workspace', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 1000 }); game.addChild(workspace); // Add current plant to workspace currentPlant.x = 1024; currentPlant.y = 1000; game.addChild(currentPlant); // Create component selection areas var topComponentIds = ['flowerTop1', 'flowerTop2', 'flowerTop3', 'leavesTop1', 'leavesTop2', 'singleLeaf']; var bottomComponentIds = ['stem1', 'stem2', 'bark1', 'bark2', 'pot1', 'pot2', 'bottle1', 'bottleBottom', 'carBottom', 'realisticCar', 'Orb', 'Orb2']; // Top components section var topSectionY = 300; for (var i = 0; i < topComponentIds.length; i++) { var button = LK.getAsset('topButton', { anchorX: 0.5, anchorY: 0.5, x: 200 + i % 3 * 180, y: topSectionY + Math.floor(i / 3) * 120 }); game.addChild(button); var component = new PlantComponent(topComponentIds[i], 'top'); component.x = button.x; component.y = button.y; game.addChild(component); topComponents.push(component); } // Bottom components section var bottomSectionY = 600; for (var i = 0; i < bottomComponentIds.length; i++) { var button = LK.getAsset('bottomButton', { anchorX: 0.5, anchorY: 0.5, x: 200 + i % 3 * 180, y: bottomSectionY + Math.floor(i / 3) * 120 }); game.addChild(button); var component = new PlantComponent(bottomComponentIds[i], 'bottom'); component.x = button.x; component.y = button.y; game.addChild(component); bottomComponents.push(component); } // UI buttons var saveButton = LK.getAsset('saveButton', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 1500 }); game.addChild(saveButton); var clearButton = LK.getAsset('clearButton', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 1600 }); game.addChild(clearButton); var clearGalleryButton = LK.getAsset('clearButton', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 2400 }); game.addChild(clearGalleryButton); // Labels var titleText = new Text2('Plant Creator Studio', { size: 60, fill: 0x2D5A27 }); titleText.anchor.set(0.5, 0); titleText.x = 1024; titleText.y = 50; game.addChild(titleText); var topSectionLabel = new Text2('Plant Tops', { size: 40, fill: 0x4A7C59 }); topSectionLabel.anchor.set(0.5, 0.5); topSectionLabel.x = 1024; topSectionLabel.y = 250; game.addChild(topSectionLabel); var bottomSectionLabel = new Text2('Plant Bottoms', { size: 40, fill: 0x4A7C59 }); bottomSectionLabel.anchor.set(0.5, 0.5); bottomSectionLabel.x = 1024; bottomSectionLabel.y = 550; game.addChild(bottomSectionLabel); var saveText = new Text2('Save Plant', { size: 36, fill: 0xFFFFFF }); saveText.anchor.set(0.5, 0.5); saveText.x = saveButton.x; saveText.y = saveButton.y; game.addChild(saveText); var clearText = new Text2('Clear', { size: 36, fill: 0xFFFFFF }); clearText.anchor.set(0.5, 0.5); clearText.x = clearButton.x; clearText.y = clearButton.y; game.addChild(clearText); var galleryText = new Text2('Gallery', { size: 40, fill: 0x4A7C59 }); galleryText.anchor.set(0.5, 0.5); galleryText.x = 1024; galleryText.y = 1800; game.addChild(galleryText); var clearGalleryText = new Text2('Clear Gallery', { size: 36, fill: 0xFFFFFF }); clearGalleryText.anchor.set(0.5, 0.5); clearGalleryText.x = clearGalleryButton.x; clearGalleryText.y = clearGalleryButton.y; game.addChild(clearGalleryText); // Gallery display function updateGallery() { // Clear existing gallery items for (var i = 0; i < galleryItems.length; i++) { galleryItems[i].destroy(); } galleryItems = []; // Create new gallery items var maxItems = Math.min(savedPlants.length, 9); // Show max 9 items for (var i = 0; i < maxItems; i++) { var item = new GalleryItem(savedPlants[i], i); item.x = 400 + i % 3 * 200; item.y = 1900 + Math.floor(i / 3) * 240; game.addChild(item); galleryItems.push(item); } } // Initialize gallery updateGallery(); // Event handlers game.move = function (x, y, obj) { if (selectedComponent && selectedComponent.isInWorkspace) { selectedComponent.x = x; selectedComponent.y = y; } if (draggedGalleryItem && draggedGalleryItem.isDragging) { draggedGalleryItem.x = x; draggedGalleryItem.y = y; } }; game.down = function (x, y, obj) { // Check if clicking save button if (x >= saveButton.x - 100 && x <= saveButton.x + 100 && y >= saveButton.y - 40 && y <= saveButton.y + 40) { var plantData = currentPlant.getData(); if (plantData.top || plantData.bottom) { // Ensure savedPlants is always a valid array before any operations try { if (!savedPlants || !Array.isArray(savedPlants) || typeof savedPlants.push !== 'function') { savedPlants = []; } // Additional safety check right before push if (Array.isArray(savedPlants) && plantData) { savedPlants.push(plantData); } } catch (pushError) { console.log('Push error in save handler, reinitializing:', pushError); savedPlants = []; if (plantData) { savedPlants.push(plantData); } } try { storage.savedPlants = savedPlants; } catch (e) { console.log('Storage error:', e); } updateGallery(); LK.getSound('save').play(); } return; } // Check if clicking clear button if (x >= clearButton.x - 100 && x <= clearButton.x + 100 && y >= clearButton.y - 40 && y <= clearButton.y + 40) { currentPlant.clear(); return; } // Check if clicking clear gallery button if (x >= clearGalleryButton.x - 100 && x <= clearGalleryButton.x + 100 && y >= clearGalleryButton.y - 40 && y <= clearGalleryButton.y + 40) { savedPlants = []; try { storage.savedPlants = savedPlants; } catch (e) { console.log('Storage error:', e); } updateGallery(); return; } // Check if clicking music toggle button if (x >= musicButton.x - 75 && x <= musicButton.x + 75 && y >= musicButton.y - 50 && y <= musicButton.y + 50) { musicState = (musicState + 1) % 3; updateMusic(); return; } }; game.up = function (x, y, obj) { if (selectedComponent && selectedComponent.isInWorkspace) { // Check if dropping in workspace if (x >= workspace.x - 300 && x <= workspace.x + 300 && y >= workspace.y - 400 && y <= workspace.y + 400) { if (selectedComponent.componentType === 'top') { currentPlant.setTop(selectedComponent); } else if (selectedComponent.componentType === 'bottom') { currentPlant.setBottom(selectedComponent); } } else { // Remove from game if not in workspace selectedComponent.destroy(); } } if (draggedGalleryItem && draggedGalleryItem.isDragging) { // Check if dropping in workspace to import plant if (x >= workspace.x - 300 && x <= workspace.x + 300 && y >= workspace.y - 400 && y <= workspace.y + 400) { // Import plant to creator var plantData = draggedGalleryItem.plantData; currentPlant.clear(); if (plantData.top) { var topComponent = new PlantComponent(plantData.top, 'top'); topComponent.isInWorkspace = true; currentPlant.setTop(topComponent); } if (plantData.bottom) { var bottomComponent = new PlantComponent(plantData.bottom, 'bottom'); bottomComponent.isInWorkspace = true; currentPlant.setBottom(bottomComponent); } LK.getSound('place').play(); // Return gallery item to original position draggedGalleryItem.x = 400 + draggedGalleryItem.index % 3 * 200; draggedGalleryItem.y = 1900 + Math.floor(draggedGalleryItem.index / 3) * 240; } else { // Check if dropping on another gallery item var targetItem = null; for (var i = 0; i < galleryItems.length; i++) { var item = galleryItems[i]; if (item !== draggedGalleryItem) { var distance = Math.sqrt(Math.pow(x - item.x, 2) + Math.pow(y - item.y, 2)); if (distance < 100) { targetItem = item; break; } } } if (targetItem) { // Merge plants: random top from either plant, bottom from target var plant1 = draggedGalleryItem.plantData; var plant2 = targetItem.plantData; var availableTops = []; if (plant1.top) availableTops.push(plant1.top); if (plant2.top) availableTops.push(plant2.top); var randomTop = availableTops.length > 0 ? availableTops[Math.floor(Math.random() * availableTops.length)] : null; var possibleMergedPlant = { top: randomTop, bottom: plant2.bottom }; // Check if this would create a parent plant and regenerate if needed var isParent1 = possibleMergedPlant.top === plant1.top && possibleMergedPlant.bottom === plant1.bottom; var isParent2 = possibleMergedPlant.top === plant2.top && possibleMergedPlant.bottom === plant2.bottom; // If it matches a parent, try the other combination if (isParent1 || isParent2) { var otherTop = null; if (plant1.top && plant2.top && plant1.top !== plant2.top) { // If both plants have different tops, use the other one otherTop = randomTop === plant1.top ? plant2.top : plant1.top; } else if (plant1.top && !plant2.top) { // If only plant1 has a top, we can't avoid using it, so use null otherTop = null; } else if (!plant1.top && plant2.top) { // If only plant2 has a top, we can't avoid using it, so use null otherTop = null; } possibleMergedPlant.top = otherTop; } var mergedPlant = possibleMergedPlant; // Add merged plant to saved plants if (!savedPlants || !Array.isArray(savedPlants)) { savedPlants = []; } // Double check before pushing with additional safety try { // Ensure savedPlants is properly initialized before push if (!savedPlants || !Array.isArray(savedPlants) || typeof savedPlants.push !== 'function') { savedPlants = []; } if (Array.isArray(savedPlants) && mergedPlant && typeof savedPlants.push === 'function') { savedPlants.push(mergedPlant); } } catch (pushError) { console.log('Push error, reinitializing savedPlants:', pushError); savedPlants = []; if (mergedPlant) { try { savedPlants.push(mergedPlant); } catch (secondPushError) { console.log('Second push error, skipping merge save:', secondPushError); } } } try { storage.savedPlants = savedPlants; } catch (e) { console.log('Storage error:', e); } updateGallery(); LK.getSound('save').play(); } else { // Return to original position draggedGalleryItem.x = 400 + draggedGalleryItem.index % 3 * 200; draggedGalleryItem.y = 1900 + Math.floor(draggedGalleryItem.index / 3) * 240; } } draggedGalleryItem.isDragging = false; draggedGalleryItem = null; } selectedComponent = null; }; // Music state management var musicState = storage.musicState || 0; // 0: Bgmusic, 1: Music2, 2: no music var musicStates = ['Bgmusic', 'Music2', 'off']; var musicLabels = ['Music 1', 'Music 2', 'No Music']; // Create music toggle button var musicButton = LK.getAsset('topButton', { anchorX: 0.5, anchorY: 0.5, x: 1700, y: 200 }); game.addChild(musicButton); var musicText = new Text2(musicLabels[musicState], { size: 30, fill: 0x4A7C59 }); musicText.anchor.set(0.5, 0.5); musicText.x = musicButton.x; musicText.y = musicButton.y; game.addChild(musicText); // Function to update music based on state function updateMusic() { LK.stopMusic(); if (musicState < 2) { // 0 or 1 LK.playMusic(musicStates[musicState]); } musicText.setText(musicLabels[musicState]); storage.musicState = musicState; } // Start music based on saved state updateMusic(); game.update = function () { // Update any animations or state changes here if needed };
/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var GalleryItem = Container.expand(function (plantData, index) {
var self = Container.call(this);
self.plantData = plantData;
self.index = index;
self.isDragging = false;
var background = self.attachAsset('gallerySlot', {
anchorX: 0.5,
anchorY: 0.5
});
if (plantData.top) {
var topComponent = self.attachAsset(plantData.top, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.6,
scaleY: 0.6,
y: -40
});
}
if (plantData.bottom) {
var bottomComponent = self.attachAsset(plantData.bottom, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.6,
scaleY: 0.6,
y: 20
});
}
self.down = function (x, y, obj) {
self.isDragging = true;
draggedGalleryItem = self;
};
return self;
});
var PlantComponent = Container.expand(function (assetId, componentType) {
var self = Container.call(this);
self.assetId = assetId;
self.componentType = componentType; // 'top' or 'bottom'
self.isInWorkspace = false;
var graphic = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
if (!self.isInWorkspace) {
// Create a copy for the workspace
var newComponent = new PlantComponent(self.assetId, self.componentType);
newComponent.x = x;
newComponent.y = y;
newComponent.isInWorkspace = true;
game.addChild(newComponent);
selectedComponent = newComponent;
LK.getSound('place').play();
} else {
selectedComponent = self;
}
};
return self;
});
var PlantCreation = Container.expand(function () {
var self = Container.call(this);
self.topComponent = null;
self.bottomComponent = null;
self.setTop = function (component) {
if (self.topComponent) {
self.removeChild(self.topComponent);
}
self.topComponent = component;
if (component) {
self.addChild(component);
component.x = 0;
component.y = -100;
}
};
self.setBottom = function (component) {
if (self.bottomComponent) {
self.removeChild(self.bottomComponent);
}
self.bottomComponent = component;
if (component) {
self.addChild(component);
component.x = 0;
component.y = 50;
}
};
self.clear = function () {
self.setTop(null);
self.setBottom(null);
};
self.getData = function () {
return {
top: self.topComponent ? self.topComponent.assetId : null,
bottom: self.bottomComponent ? self.bottomComponent.assetId : null
};
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf5f5dc
});
/****
* Game Code
****/
// Game state
// Plant tops - flowers, leaves, combinations
// Plant bottoms - stems, bark, potted options
// UI elements
var selectedComponent = null;
var currentPlant = new PlantCreation();
var savedPlants = [];
try {
var storedPlants = storage.savedPlants;
if (storedPlants && Array.isArray(storedPlants)) {
savedPlants = storedPlants;
} else {
savedPlants = [];
}
} catch (e) {
console.log('Storage retrieval error:', e);
savedPlants = [];
}
// Ensure savedPlants is always an array
if (!savedPlants || !Array.isArray(savedPlants)) {
savedPlants = [];
}
var showingGallery = false;
var draggedGalleryItem = null;
// UI containers
var topComponents = [];
var bottomComponents = [];
var galleryItems = [];
// Create workspace
var workspace = LK.getAsset('workspace', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1000
});
game.addChild(workspace);
// Add current plant to workspace
currentPlant.x = 1024;
currentPlant.y = 1000;
game.addChild(currentPlant);
// Create component selection areas
var topComponentIds = ['flowerTop1', 'flowerTop2', 'flowerTop3', 'leavesTop1', 'leavesTop2', 'singleLeaf'];
var bottomComponentIds = ['stem1', 'stem2', 'bark1', 'bark2', 'pot1', 'pot2', 'bottle1', 'bottleBottom', 'carBottom', 'realisticCar', 'Orb', 'Orb2'];
// Top components section
var topSectionY = 300;
for (var i = 0; i < topComponentIds.length; i++) {
var button = LK.getAsset('topButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 200 + i % 3 * 180,
y: topSectionY + Math.floor(i / 3) * 120
});
game.addChild(button);
var component = new PlantComponent(topComponentIds[i], 'top');
component.x = button.x;
component.y = button.y;
game.addChild(component);
topComponents.push(component);
}
// Bottom components section
var bottomSectionY = 600;
for (var i = 0; i < bottomComponentIds.length; i++) {
var button = LK.getAsset('bottomButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 200 + i % 3 * 180,
y: bottomSectionY + Math.floor(i / 3) * 120
});
game.addChild(button);
var component = new PlantComponent(bottomComponentIds[i], 'bottom');
component.x = button.x;
component.y = button.y;
game.addChild(component);
bottomComponents.push(component);
}
// UI buttons
var saveButton = LK.getAsset('saveButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1500
});
game.addChild(saveButton);
var clearButton = LK.getAsset('clearButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1600
});
game.addChild(clearButton);
var clearGalleryButton = LK.getAsset('clearButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2400
});
game.addChild(clearGalleryButton);
// Labels
var titleText = new Text2('Plant Creator Studio', {
size: 60,
fill: 0x2D5A27
});
titleText.anchor.set(0.5, 0);
titleText.x = 1024;
titleText.y = 50;
game.addChild(titleText);
var topSectionLabel = new Text2('Plant Tops', {
size: 40,
fill: 0x4A7C59
});
topSectionLabel.anchor.set(0.5, 0.5);
topSectionLabel.x = 1024;
topSectionLabel.y = 250;
game.addChild(topSectionLabel);
var bottomSectionLabel = new Text2('Plant Bottoms', {
size: 40,
fill: 0x4A7C59
});
bottomSectionLabel.anchor.set(0.5, 0.5);
bottomSectionLabel.x = 1024;
bottomSectionLabel.y = 550;
game.addChild(bottomSectionLabel);
var saveText = new Text2('Save Plant', {
size: 36,
fill: 0xFFFFFF
});
saveText.anchor.set(0.5, 0.5);
saveText.x = saveButton.x;
saveText.y = saveButton.y;
game.addChild(saveText);
var clearText = new Text2('Clear', {
size: 36,
fill: 0xFFFFFF
});
clearText.anchor.set(0.5, 0.5);
clearText.x = clearButton.x;
clearText.y = clearButton.y;
game.addChild(clearText);
var galleryText = new Text2('Gallery', {
size: 40,
fill: 0x4A7C59
});
galleryText.anchor.set(0.5, 0.5);
galleryText.x = 1024;
galleryText.y = 1800;
game.addChild(galleryText);
var clearGalleryText = new Text2('Clear Gallery', {
size: 36,
fill: 0xFFFFFF
});
clearGalleryText.anchor.set(0.5, 0.5);
clearGalleryText.x = clearGalleryButton.x;
clearGalleryText.y = clearGalleryButton.y;
game.addChild(clearGalleryText);
// Gallery display
function updateGallery() {
// Clear existing gallery items
for (var i = 0; i < galleryItems.length; i++) {
galleryItems[i].destroy();
}
galleryItems = [];
// Create new gallery items
var maxItems = Math.min(savedPlants.length, 9); // Show max 9 items
for (var i = 0; i < maxItems; i++) {
var item = new GalleryItem(savedPlants[i], i);
item.x = 400 + i % 3 * 200;
item.y = 1900 + Math.floor(i / 3) * 240;
game.addChild(item);
galleryItems.push(item);
}
}
// Initialize gallery
updateGallery();
// Event handlers
game.move = function (x, y, obj) {
if (selectedComponent && selectedComponent.isInWorkspace) {
selectedComponent.x = x;
selectedComponent.y = y;
}
if (draggedGalleryItem && draggedGalleryItem.isDragging) {
draggedGalleryItem.x = x;
draggedGalleryItem.y = y;
}
};
game.down = function (x, y, obj) {
// Check if clicking save button
if (x >= saveButton.x - 100 && x <= saveButton.x + 100 && y >= saveButton.y - 40 && y <= saveButton.y + 40) {
var plantData = currentPlant.getData();
if (plantData.top || plantData.bottom) {
// Ensure savedPlants is always a valid array before any operations
try {
if (!savedPlants || !Array.isArray(savedPlants) || typeof savedPlants.push !== 'function') {
savedPlants = [];
}
// Additional safety check right before push
if (Array.isArray(savedPlants) && plantData) {
savedPlants.push(plantData);
}
} catch (pushError) {
console.log('Push error in save handler, reinitializing:', pushError);
savedPlants = [];
if (plantData) {
savedPlants.push(plantData);
}
}
try {
storage.savedPlants = savedPlants;
} catch (e) {
console.log('Storage error:', e);
}
updateGallery();
LK.getSound('save').play();
}
return;
}
// Check if clicking clear button
if (x >= clearButton.x - 100 && x <= clearButton.x + 100 && y >= clearButton.y - 40 && y <= clearButton.y + 40) {
currentPlant.clear();
return;
}
// Check if clicking clear gallery button
if (x >= clearGalleryButton.x - 100 && x <= clearGalleryButton.x + 100 && y >= clearGalleryButton.y - 40 && y <= clearGalleryButton.y + 40) {
savedPlants = [];
try {
storage.savedPlants = savedPlants;
} catch (e) {
console.log('Storage error:', e);
}
updateGallery();
return;
}
// Check if clicking music toggle button
if (x >= musicButton.x - 75 && x <= musicButton.x + 75 && y >= musicButton.y - 50 && y <= musicButton.y + 50) {
musicState = (musicState + 1) % 3;
updateMusic();
return;
}
};
game.up = function (x, y, obj) {
if (selectedComponent && selectedComponent.isInWorkspace) {
// Check if dropping in workspace
if (x >= workspace.x - 300 && x <= workspace.x + 300 && y >= workspace.y - 400 && y <= workspace.y + 400) {
if (selectedComponent.componentType === 'top') {
currentPlant.setTop(selectedComponent);
} else if (selectedComponent.componentType === 'bottom') {
currentPlant.setBottom(selectedComponent);
}
} else {
// Remove from game if not in workspace
selectedComponent.destroy();
}
}
if (draggedGalleryItem && draggedGalleryItem.isDragging) {
// Check if dropping in workspace to import plant
if (x >= workspace.x - 300 && x <= workspace.x + 300 && y >= workspace.y - 400 && y <= workspace.y + 400) {
// Import plant to creator
var plantData = draggedGalleryItem.plantData;
currentPlant.clear();
if (plantData.top) {
var topComponent = new PlantComponent(plantData.top, 'top');
topComponent.isInWorkspace = true;
currentPlant.setTop(topComponent);
}
if (plantData.bottom) {
var bottomComponent = new PlantComponent(plantData.bottom, 'bottom');
bottomComponent.isInWorkspace = true;
currentPlant.setBottom(bottomComponent);
}
LK.getSound('place').play();
// Return gallery item to original position
draggedGalleryItem.x = 400 + draggedGalleryItem.index % 3 * 200;
draggedGalleryItem.y = 1900 + Math.floor(draggedGalleryItem.index / 3) * 240;
} else {
// Check if dropping on another gallery item
var targetItem = null;
for (var i = 0; i < galleryItems.length; i++) {
var item = galleryItems[i];
if (item !== draggedGalleryItem) {
var distance = Math.sqrt(Math.pow(x - item.x, 2) + Math.pow(y - item.y, 2));
if (distance < 100) {
targetItem = item;
break;
}
}
}
if (targetItem) {
// Merge plants: random top from either plant, bottom from target
var plant1 = draggedGalleryItem.plantData;
var plant2 = targetItem.plantData;
var availableTops = [];
if (plant1.top) availableTops.push(plant1.top);
if (plant2.top) availableTops.push(plant2.top);
var randomTop = availableTops.length > 0 ? availableTops[Math.floor(Math.random() * availableTops.length)] : null;
var possibleMergedPlant = {
top: randomTop,
bottom: plant2.bottom
};
// Check if this would create a parent plant and regenerate if needed
var isParent1 = possibleMergedPlant.top === plant1.top && possibleMergedPlant.bottom === plant1.bottom;
var isParent2 = possibleMergedPlant.top === plant2.top && possibleMergedPlant.bottom === plant2.bottom;
// If it matches a parent, try the other combination
if (isParent1 || isParent2) {
var otherTop = null;
if (plant1.top && plant2.top && plant1.top !== plant2.top) {
// If both plants have different tops, use the other one
otherTop = randomTop === plant1.top ? plant2.top : plant1.top;
} else if (plant1.top && !plant2.top) {
// If only plant1 has a top, we can't avoid using it, so use null
otherTop = null;
} else if (!plant1.top && plant2.top) {
// If only plant2 has a top, we can't avoid using it, so use null
otherTop = null;
}
possibleMergedPlant.top = otherTop;
}
var mergedPlant = possibleMergedPlant;
// Add merged plant to saved plants
if (!savedPlants || !Array.isArray(savedPlants)) {
savedPlants = [];
}
// Double check before pushing with additional safety
try {
// Ensure savedPlants is properly initialized before push
if (!savedPlants || !Array.isArray(savedPlants) || typeof savedPlants.push !== 'function') {
savedPlants = [];
}
if (Array.isArray(savedPlants) && mergedPlant && typeof savedPlants.push === 'function') {
savedPlants.push(mergedPlant);
}
} catch (pushError) {
console.log('Push error, reinitializing savedPlants:', pushError);
savedPlants = [];
if (mergedPlant) {
try {
savedPlants.push(mergedPlant);
} catch (secondPushError) {
console.log('Second push error, skipping merge save:', secondPushError);
}
}
}
try {
storage.savedPlants = savedPlants;
} catch (e) {
console.log('Storage error:', e);
}
updateGallery();
LK.getSound('save').play();
} else {
// Return to original position
draggedGalleryItem.x = 400 + draggedGalleryItem.index % 3 * 200;
draggedGalleryItem.y = 1900 + Math.floor(draggedGalleryItem.index / 3) * 240;
}
}
draggedGalleryItem.isDragging = false;
draggedGalleryItem = null;
}
selectedComponent = null;
};
// Music state management
var musicState = storage.musicState || 0; // 0: Bgmusic, 1: Music2, 2: no music
var musicStates = ['Bgmusic', 'Music2', 'off'];
var musicLabels = ['Music 1', 'Music 2', 'No Music'];
// Create music toggle button
var musicButton = LK.getAsset('topButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 1700,
y: 200
});
game.addChild(musicButton);
var musicText = new Text2(musicLabels[musicState], {
size: 30,
fill: 0x4A7C59
});
musicText.anchor.set(0.5, 0.5);
musicText.x = musicButton.x;
musicText.y = musicButton.y;
game.addChild(musicText);
// Function to update music based on state
function updateMusic() {
LK.stopMusic();
if (musicState < 2) {
// 0 or 1
LK.playMusic(musicStates[musicState]);
}
musicText.setText(musicLabels[musicState]);
storage.musicState = musicState;
}
// Start music based on saved state
updateMusic();
game.update = function () {
// Update any animations or state changes here if needed
};
Leaf. In-Game asset. 2d. High contrast. No shadows
Wiggly stem. In-Game asset. 2d. High contrast. No shadows
Leaves. In-Game asset. 2d. High contrast. No shadows
Tree bark texture. In-Game asset. 2d. High contrast. No shadows
Pot. In-Game asset. 2d. High contrast. No shadows
Flower. In-Game asset. 2d. High contrast. No shadows
Blue flower with stem. In-Game asset. 2d. High contrast. No shadows
Remove the cap
Car facing front. In-Game asset. 2d. High contrast. No shadows
Make it realistic, bg is green
Invert colors