User prompt
Please fix the bug: 'TypeError: butterflyGraphics.setFrame is not a function' in or related to this line: 'butterflyGraphics.setFrame(butterflyFrames[currentFrame]);' Line Number: 87
User prompt
Please fix the bug: 'TypeError: butterflyGraphics.setTexture is not a function' in or related to this line: 'butterflyGraphics.setTexture(LK.getAsset(butterflyFrames[currentFrame], {}).texture);' Line Number: 87
User prompt
create a new function playButterflyAnim similar to playPlumesAnim but for butterflies
User prompt
create a new class Butterfly that animates a butterfly using the 4 butterfly assets
Code edit (1 edits merged)
Please save this source code
User prompt
add a new function playPlumesAnim, that animates a plume asset randomly from right of the screen to the left. run it randomly like playAmbientSounds
User prompt
now include also birds3 in playAmbiantSounds
Code edit (13 edits merged)
Please save this source code
User prompt
add a new function playAmbiantSounds that randomly plays 'birds' or 'birds2'
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (11 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Timeout.tick error: storage is not defined' in or related to this line: 'self.currentLevel = storage.currentLevel || (debug && levelConfigs[0] ? 0 : 1);' Line Number: 1498 βͺπ‘ Consider importing and using the following plugins: @upit/storage.v1
User prompt
Please fix the bug: 'Unable to load plugin: @upit/storage.v1' in or related to this line: 'var storage = LK.import("@upit/storage.v1");' Line Number: 55
Code edit (1 edits merged)
Please save this source code
Code edit (8 edits merged)
Please save this source code
User prompt
Please fix the bug: 'tween is not defined' in or related to this line: 'tween(logoShadow, {' Line Number: 2264 βͺπ‘ Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'Unable to load plugin: @upit/tween.v1' in or related to this line: 'var tween = LK.import("@upit/tween.v1");' Line Number: 55
Code edit (1 edits merged)
Please save this source code
User prompt
``` LK.setTimeout(function () { game.removeChild(confetti); }, 1000); ``` solve the scope bug using a closure
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Reset the storage.currentLevel just before GameOver
Code edit (10 edits merged)
Please save this source code
User prompt
Use the storage plugin to store puzzleManager.currentLevel. βͺπ‘ Consider importing and using the following plugins: @upit/storage.v1
/**** 
* Plugins
****/ 
var storage = LK.import("@upit/storage.v1");
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
// Removed the import statement for the tween plugin as it is causing an error
var Flower = Container.expand(function (maxIndex, x, y) {
	var self = Container.call(this);
	var positionRange = 200;
	var sizeRatioRange = 50;
	var popSpeed = 0.05;
	self.popPlayed = false;
	var assetId = 'flower' + Math.min(10, Math.floor(Math.random() * maxIndex + 1));
	var rand1 = Math.random();
	var rand2 = Math.random();
	// Define final scale values for the grow effect
	var finalScaleX = 1 + rand2 * (sizeRatioRange / 100);
	var finalScaleY = 1 + rand1 * (sizeRatioRange / 100);
	var randRot = -Math.PI * 0.25 + 0.5 * (rand1 + rand2) * Math.PI * 0.5;
	var randScaleX = rand1 > 0.5 ? 1 : -1;
	var flowerShadow = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 5 + 5 * rand1 + x + (rand1 * positionRange - positionRange / 2),
		y: 5 + 5 * rand2 + y + (rand2 * positionRange - positionRange / 2),
		scaleX: finalScaleX * randScaleX,
		// Start with scale 0 for grow effect
		scaleY: finalScaleY,
		// Start with scale 0 for grow effect
		alpha: 0.5,
		// Start with alpha 0 for fade-in effect
		rotation: randRot,
		// Random rotation
		tint: 0x222222
	});
	var flowerGraphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5,
		x: x + (rand1 * positionRange - positionRange / 2),
		y: y + (rand2 * positionRange - positionRange / 2),
		scaleX: 0,
		// Start with scale 0 for grow effect
		scaleY: 0,
		// Start with scale 0 for grow effect
		alpha: 0,
		// Start with alpha 0 for fade-in effect
		rotation: randRot
	});
	// Implement fade-in effect by incrementing alpha and grow animation
	self.update = function () {
		if (flowerGraphics.alpha < 1) {
			flowerGraphics.alpha += popSpeed; // Adjust the increment for desired speed
		} else {
			flowerGraphics.alpha = 1;
		}
		if (Math.abs(flowerGraphics.scaleX) < finalScaleX) {
			flowerGraphics.scaleX += popSpeed * randScaleX; // Adjust the increment for desired speed
		}
		if (flowerGraphics.scaleY < finalScaleY) {
			flowerGraphics.scaleY += popSpeed; // Adjust the increment for desired speed
		} else {
			if (!self.popPlayed) {
				self.popPlayed = true;
				LK.setTimeout(function () {
					LK.getSound('flowerPop').play();
				}, 0.5 * (rand1 + rand2) * 500);
			}
		}
	};
	return self;
});
var RatingStars = Container.expand(function (stars) {
	var self = Container.call(this);
	self.starsContainer = new Container();
	self.addChild(self.starsContainer);
	var offset = 450;
	// Ensure stars is between 1 and 3
	stars = Math.max(1, Math.min(stars, 3));
	for (var j = 0; j < 3; j++) {
		var baseTile = self.starsContainer.attachAsset('baseTile', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: j * offset - offset,
			// Position 1st at x = -400, 2nd at x = 0, 3rd at x = 400
			y: 0
		});
	}
	for (var i = 0; i < stars; i++) {
		(function (i) {
			LK.setTimeout(function () {
				var starAsset = self.starsContainer.attachAsset('star', {
					anchorX: 0.5,
					anchorY: 0.5,
					x: i * offset - offset,
					// Adjust spacing as needed
					scaleX: 0,
					// Start with scale 0 for grow effect
					scaleY: 0,
					// Start with scale 0 for grow effect
					visible: i < stars // Only show the number of stars specified
				});
				LK.setTimeout(function () {
					LK.getSound("roundResult").play();
				}, 300);
				// Animate the starAsset to grow from 0 to its final size with easeIn
				tween(starAsset, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 400,
					easing: tween.easeIn
				});
			}, i * 300);
		})(i);
	}
	return self;
});
var ResetButton = Container.expand(function () {
	var self = Container.call(this);
	var buttonGraphics = self.attachAsset('resetButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.interactive = true;
	self.buttonMode = true;
	self.down = function (x, y, obj) {
		log("Reset button pressed");
		LK.getSound("resetSound").play();
		// Reset the game or perform any reset action here
		puzzleManager.initPuzzle();
	};
	self.disable = function () {
		self.alpha = 0.5;
		self.interactive = false;
	};
	self.enable = function () {
		self.alpha = 1;
		self.interactive = true;
	};
	return self;
});
var WaterDrop = Container.expand(function () {
	var self = Container.call(this);
	var waterDropGraphics = self.attachAsset('waterDrop', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Set initial tint to a random  color
	var rainbowColors = [0xC7ECFE, 0xDDFDFF, 0xF4FFFF, 0xC8F8FF]; // Rainbow [0xFF0000, 0xFF7F00, 0xFFFF00, 0x00FF00, 0x0000FF, 0x4B0082, 0x8B00FF];
	waterDropGraphics.tint = rainbowColors[Math.floor(Math.random() * rainbowColors.length)];
	self.vx = 0;
	self.vy = 0;
	self.life = 0;
	self.size = 10;
	self.alpha = 0.75 + Math.random() * 0.25;
	self.update = function () {
		self.x += self.vx;
		self.y += self.vy;
		self.rotation = Math.atan2(self.vy, self.vx) + Math.PI * 0.55;
		// Animate size 
		var sizeProgress = (150 - self.life) / 150; // Assuming life starts at 120
		waterDropGraphics.width = self.size + sizeProgress * self.size * 2;
		waterDropGraphics.height = self.size * 2 + sizeProgress * self.size * 4;
		self.life--;
		if (self.life <= 0) {
			self.visible = false;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
/**** 
* Global level configurations
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
function playAmbiantSounds() {
	// Randomly choose between 'birds', 'birds2', and 'birds3'
	var soundId = ['birds', 'birds2', 'birds3'][Math.floor(Math.random() * 3)];
	// Play the chosen sound
	LK.getSound(soundId).play();
}
// Set an interval to play ambient sounds every 5 to 10 seconds
LK.setInterval(playAmbiantSounds, 5000 + Math.random() * 10000);
// Removed the import statement for the tween plugin as it is causing an error
function finalAnimation() {
	var confettiCount = 200;
	var confettis = [];
	var spawnedCount = 0;
	LK.getSound("gameWon").play();
	finalAnimationInterval = LK.setInterval(function () {
		if (spawnedCount >= confettiCount) {
			LK.clearInterval(finalAnimationInterval);
			isFinalAnimationFinished = true;
			return;
		}
		var rand1 = Math.random();
		var rand2 = Math.random();
		var confetti = LK.getAsset('star', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: rand1 * 2048,
			y: rand2 * 2000 - 3000,
			scaleX: 0.25 + Math.random() * 0.75,
			scaleY: 0.25 + Math.random() * 0.75,
			rotation: 0.5 * (rand1 + rand2) * Math.PI * 2
		});
		confettis.push(confetti);
		game.addChild(confetti);
		var targetY = 2600;
		var duration = 2000 + rand1 * 2000;
		tween(confetti, {
			y: targetY,
			rotation: confetti.rotation + Math.PI * 2
		}, {
			duration: duration,
			easing: tween.easeInOut
			/* 
			onFinish: function (confetti) {
			return function () {
			LK.setTimeout(function () {
			game.removeChild(confetti);
			}, 1000);
			};
			}(confetti)
			*/ 
		});
		spawnedCount++;
		var x = rand1 * 2048;
		var y = 350 + rand2 * (2732 - 750);
		var flower = new Flower(puzzleManager.currentLevel, x, y);
		flowers.push(flower);
		game.addChild(flower);
	}, 20);
	LK.setTimeout(function () {
		game.removeChild(congratsText);
		var finalResultText = new Text2("All Solved!\nCongratulations!", {
			size: 250,
			anchorX: 0.5,
			fill: 0xFFFFFF,
			weight: 1000,
			dropShadow: true,
			align: 'center'
		});
		finalResultText.x = 50;
		finalResultText.y = -1300;
		finalResultText.alpha = 0.9;
		finalResultText.visible = true;
		game.addChild(finalResultText);
		tween(finalResultText, {
			y: 400
		}, {
			duration: 1000,
			easing: tween.easeInOut
		});
	}, 2000);
	LK.setTimeout(function () {
		var finalResultText2 = new Text2("All Solved!\nCongratulations!", {
			size: 250,
			anchorX: 0.5,
			fill: 0xFFFFFF,
			alpha: 0,
			weight: 1000,
			dropShadow: true,
			align: 'center'
		});
		finalResultText2.x = 50;
		finalResultText2.y = 400;
		finalResultText2.visible = true;
		game.addChild(finalResultText2);
		tween(finalResultText2, {
			alpha: 1
		}, {
			duration: 3000,
			easing: tween.easeInOut
		});
	}, 7000);
}
function calculateStarsScore() {
	log("Calculating stars score..."); // Log the start of the calculation
	log("Current round moves:", currentRoundMoves); // Log current round moves
	log("Level minimal moves for current level:", levelMinimalMoves[puzzleManager.currentLevel]); // Log minimal moves for current level
	if (currentRoundMoves <= levelMinimalMoves[puzzleManager.currentLevel]) {
		log("Awarding 3 stars"); // Log decision for 3 stars
		return 3;
	} else if (currentRoundMoves < levelMinimalMoves[puzzleManager.currentLevel] * 1.66) {
		log("Awarding 2 stars"); // Log decision for 2 stars
		return 2;
	} else {
		log("Awarding 1 star"); // Log decision for 1 star
		return 1;
	}
}
function assetExists(assetId) {
	try {
		LK.getAsset(assetId, {});
		return true;
	} catch (e) {
		return false;
	}
}
function _slicedToArray5(r, e) {
	return _arrayWithHoles5(r) || _iterableToArrayLimit5(r, e) || _unsupportedIterableToArray5(r, e) || _nonIterableRest5();
}
function _arrayWithHoles5(r) {
	if (Array.isArray(r)) {
		return r;
	}
}
function _iterableToArrayLimit5(r, l) {
	var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
	if (null != t) {
		var e,
			n,
			i,
			u,
			a = [],
			f = !0,
			o = !1;
		try {
			if (i = (t = t.call(r)).next, 0 === l) {
				if (Object(t) !== t) {
					return;
				}
				f = !1;
			} else {
				for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) {
					;
				}
			}
		} catch (r) {
			o = !0, n = r;
		} finally {
			try {
				if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) {
					return;
				}
			} finally {
				if (o) {
					throw n;
				}
			}
		}
		return a;
	}
}
function _unsupportedIterableToArray5(r, a) {
	if (r) {
		if ("string" == typeof r) {
			return _arrayLikeToArray5(r, a);
		}
		var t = {}.toString.call(r).slice(8, -1);
		return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray5(r, a) : void 0;
	}
}
function _arrayLikeToArray5(r, a) {
	(null == a || a > r.length) && (a = r.length);
	for (var e = 0, n = Array(a); e < a; e++) {
		n[e] = r[e];
	}
	return n;
}
function _nonIterableRest5() {
	throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _createForOfIteratorHelper(o, allowArrayLike) {
	var it;
	if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
		if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
			if (it) {
				o = it;
			}
			var i = 0;
			var F = function F() {};
			return {
				s: F,
				n: function n() {
					if (i >= o.length) {
						return {
							done: true
						};
					}
					return {
						done: false,
						value: o[i++]
					};
				},
				e: function e(_e) {
					throw _e;
				},
				f: F
			};
		}
		throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
	}
	var normalCompletion = true,
		didErr = false,
		err;
	return {
		s: function s() {
			it = o[Symbol.iterator]();
		},
		n: function n() {
			var step = it.next();
			normalCompletion = step.done;
			return step;
		},
		e: function e(_e2) {
			didErr = true;
			err = _e2;
		},
		f: function f() {
			try {
				if (!normalCompletion && it["return"] != null) {
					it["return"]();
				}
			} finally {
				if (didErr) {
					throw err;
				}
			}
		}
	};
}
/**** 
* Global level configurations
****/ 
function _slicedToArray2(r, e) {
	return _arrayWithHoles2(r) || _iterableToArrayLimit2(r, e) || _unsupportedIterableToArray2(r, e) || _nonIterableRest2();
}
function _nonIterableRest2() {
	throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray2(r, a) {
	if (r) {
		if ("string" == typeof r) {
			return _arrayLikeToArray2(r, a);
		}
		var t = {}.toString.call(r).slice(8, -1);
		return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray2(r, a) : void 0;
	}
}
function _arrayLikeToArray2(r, a) {
	(null == a || a > r.length) && (a = r.length);
	for (var e = 0, n = Array(a); e < a; e++) {
		n[e] = r[e];
	}
	return n;
}
function _iterableToArrayLimit2(r, l) {
	var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
	if (null != t) {
		var e,
			n,
			i,
			u,
			a = [],
			f = !0,
			o = !1;
		try {
			if (i = (t = t.call(r)).next, 0 === l) {
				if (Object(t) !== t) {
					return;
				}
				f = !1;
			} else {
				for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) {
					;
				}
			}
		} catch (r) {
			o = !0, n = r;
		} finally {
			try {
				if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) {
					return;
				}
			} finally {
				if (o) {
					throw n;
				}
			}
		}
		return a;
	}
}
function _arrayWithHoles2(r) {
	if (Array.isArray(r)) {
		return r;
	}
}
function _slicedToArray4(r, e) {
	return _arrayWithHoles4(r) || _iterableToArrayLimit4(r, e) || _unsupportedIterableToArray4(r, e) || _nonIterableRest4();
}
function _nonIterableRest4() {
	throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray4(r, a) {
	if (r) {
		if ("string" == typeof r) {
			return _arrayLikeToArray4(r, a);
		}
		var t = {}.toString.call(r).slice(8, -1);
		return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray4(r, a) : void 0;
	}
}
function _arrayLikeToArray4(r, a) {
	(null == a || a > r.length) && (a = r.length);
	for (var e = 0, n = Array(a); e < a; e++) {
		n[e] = r[e];
	}
	return n;
}
function _iterableToArrayLimit4(r, l) {
	var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
	if (null != t) {
		var e,
			n,
			i,
			u,
			a = [],
			f = !0,
			o = !1;
		try {
			if (i = (t = t.call(r)).next, 0 === l) {
				if (Object(t) !== t) {
					return;
				}
				f = !1;
			} else {
				for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) {
					;
				}
			}
		} catch (r) {
			o = !0, n = r;
		} finally {
			try {
				if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) {
					return;
				}
			} finally {
				if (o) {
					throw n;
				}
			}
		}
		return a;
	}
}
function _arrayWithHoles4(r) {
	if (Array.isArray(r)) {
		return r;
	}
}
function Tile() {
	var self = this;
	Container.call(self);
	self.type = null;
	self.fixed = false;
	self.gridRow = -1;
	self.gridCol = -1;
	self.flow = false;
	self.startFlowTicks = 0;
	self.baseTint = 0xaaaaaa;
	self.baseTintLight = 0xaaaaaa;
	self.maxWaterSize = 270;
	self.maxWaterCornerSize = 180;
	self.flowSpeed = 12;
	self.pipeContainer = new Container();
	//self.addChild(self.pipeContainer);
	self.createStartPipe = function () {
		self.pipeContainer.attachAsset('startPipeAsset', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 20,
			y: 40,
			tint: self.baseTint
		});
		self.valve = self.pipeContainer.attachAsset('vane', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 20,
			y: -8,
			tint: self.baseTint
		});
		self.water = self.pipeContainer.attachAsset('waterV', {
			anchorX: 0.5,
			anchorY: 0,
			x: -0,
			y: -120,
			height: 0,
			visible: false,
			dir: ''
		});
	};
	self.createEndPipe = function () {
		self.pipeContainer.attachAsset('endPipeAsset', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: 55,
			tint: self.baseTint
		});
		self.water = self.pipeContainer.attachAsset('waterV', {
			anchorX: 0.5,
			anchorY: 0,
			x: -0,
			y: -120,
			height: 0,
			visible: false,
			dir: ''
		});
		self.fountain = self.pipeContainer.attachAsset('fontain', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: 0,
			width: 0,
			height: 0,
			visible: false
		});
	};
	self.createStraightPipe = function (isVertical) {
		if (isVertical) {
			self.pipeContainer.attachAsset('straightPipeVAsset', {
				anchorX: 0.5,
				anchorY: 0.5,
				x: -30,
				tint: self.baseTint
			});
			self.pipeContainer.attachAsset('straightPipeVAsset', {
				anchorX: 0.5,
				anchorY: 0.5,
				scaleX: -1,
				x: 30,
				tint: self.baseTint
			});
			self.water = self.pipeContainer.attachAsset('waterV', {
				anchorX: 0.5,
				anchorY: 0,
				x: -0,
				y: -120,
				height: 0,
				visible: false,
				dir: 'tb'
			});
		} else {
			self.pipeContainer.attachAsset('straightPipeHAsset', {
				anchorX: 0.5,
				anchorY: 0.5,
				y: -40,
				tint: self.baseTint
			});
			self.pipeContainer.attachAsset('straightPipeHAsset', {
				anchorX: 0.5,
				anchorY: 0.5,
				scaleY: -1,
				y: 30,
				tint: self.baseTint
			});
			self.water = self.pipeContainer.attachAsset('waterH', {
				anchorX: 0,
				anchorY: 0.5,
				x: -130,
				y: -5,
				width: 0,
				visible: false,
				dir: 'lr'
			});
		}
	};
	self.createCornerPipe = function () {
		self.pipeContainer.attachAsset('cornerPipeAsset', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: -60,
			y: -60,
			tint: self.baseTintLight
		});
		// TODO : Remove
		self.waterCorner = self.pipeContainer.attachAsset('waterCorner', {
			anchorX: 1,
			anchorY: 1,
			width: 108,
			height: 108,
			x: -65,
			y: -65,
			rotation: -Math.PI / 2,
			visible: false,
			dir: ''
		});
		self.waterV = self.pipeContainer.attachAsset('waterV', {
			anchorX: 0.5,
			anchorY: 0,
			width: 110,
			x: -120,
			y: -5,
			rotation: -Math.PI / 2,
			height: 0,
			visible: false,
			dir: ''
		});
		self.waterH = self.pipeContainer.attachAsset('waterH', {
			anchorX: 0,
			anchorY: 0.5,
			x: -5,
			y: -50,
			width: 0,
			height: 112,
			rotation: -Math.PI / 2,
			visible: false,
			dir: ''
		});
		// TODO : Remove?
		self.waterCornerCover = self.pipeContainer.attachAsset('waterCorner', {
			anchorX: 1,
			anchorY: 1,
			width: 108,
			height: 108,
			x: -65,
			y: -65,
			rotation: -Math.PI,
			visible: false,
			alpha: 0,
			dir: ''
		});
		self.pipeContainer.attachAsset('cornerPipeSection', {
			anchorX: 0.5,
			anchorY: 0.5,
			width: 148,
			height: 148,
			x: -0,
			y: -0,
			tint: self.baseTintLight
		});
		self.pipeContainer.attachAsset('pipeRing', {
			anchorX: 0.5,
			anchorY: 0.5,
			width: 144,
			height: 30,
			x: 0,
			y: -80,
			rotation: Math.PI * 0,
			tint: self.baseTintLight
		});
		self.pipeContainer.attachAsset('pipeRing', {
			anchorX: 0.5,
			anchorY: 0.5,
			width: 144,
			height: 30,
			x: -80,
			y: -5,
			rotation: Math.PI * 0.5,
			tint: self.baseTintLight
		});
	};
	self.createCrossPipe = function () {
		self.pipeContainer.attachAsset('straightPipeHAsset', {
			anchorX: 0.5,
			anchorY: 0.5,
			y: -40,
			tint: self.baseTint
		});
		self.pipeContainer.attachAsset('straightPipeHAsset', {
			anchorX: 0.5,
			anchorY: 0.5,
			scaleY: -1,
			y: 30,
			tint: self.baseTint
		});
		self.pipeContainer.attachAsset('straightPipeVAsset', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: -30,
			tint: self.baseTint
		});
		self.pipeContainer.attachAsset('straightPipeVAsset', {
			anchorX: 0.5,
			anchorY: 0.5,
			scaleX: -1,
			x: 30,
			tint: self.baseTint
		});
		self.water = self.pipeContainer.attachAsset('waterV', {
			anchorX: 0.5,
			anchorY: 0,
			x: -0,
			y: -120,
			height: 0,
			visible: false,
			dir: ''
		});
	};
	self.setType = function (type, row, col, fixed) {
		self.type = type;
		self.gridRow = row;
		self.gridCol = col;
		// Add base tile last so it appears under the pipes
		var baseTileAsset = fixed ? 'baseTile' : 'baseMobileTile';
		log("setType : setting baseTile...");
		self.baseTile = self.attachAsset(baseTileAsset, {
			anchorX: 0.5,
			anchorY: 0.5,
			width: tileSize,
			height: tileSize,
			tint: 0xFFFFFF
		});
		self.addChild(self.pipeContainer);
		// Clear existing pipe graphics
		while (self.pipeContainer.children.length > 0) {
			self.pipeContainer.removeChildAt(0);
		}
		// Create pipe graphics based on type
		switch (type) {
			case TileFormat.TYPES.START:
				self.createStartPipe();
				break;
			case TileFormat.TYPES.END:
				self.createEndPipe();
				break;
			case TileFormat.TYPES.VERTICAL:
				self.createStraightPipe(true);
				break;
			case TileFormat.TYPES.HORIZONTAL:
				self.createStraightPipe(false);
				break;
			case TileFormat.TYPES.CORNER:
				self.createCornerPipe();
				break;
			case TileFormat.TYPES.CROSS:
				self.createCrossPipe();
				break;
		}
	};
	self.getNextPositions = function (col, row, rotation, type, incomingDirection) {
		var positions = [];
		log("Getting next position for tile at", row, col, "type:", type, "rotation:", rotation, "incoming direction:", incomingDirection); //, "normalizedIncoming::", normalizedIncoming);
		switch (type) {
			case TileFormat.TYPES.START:
				// Start pipe flows in direction of rotation
				var dx = -Math.sin(rotation);
				var dy = Math.cos(rotation);
				positions.push([col + Math.round(dx), row + Math.round(dy)]);
				log("START: flowing in direction dx:", dx, "dy:", dy);
				break;
			case TileFormat.TYPES.VERTICAL:
				// For vertical pipes, we flow in the opposite direction of incoming
				// If water comes from top (Ο/2), we flow down
				// If water comes from bottom (3Ο/2), we flow up
				if (Math.abs(incomingDirection - 3 * Math.PI / 2) < 0.1) {
					positions.push([col, row + 1]); // Flow down
					log("VERTICAL: incoming from top, flowing down");
				} else if (Math.abs(incomingDirection - Math.PI / 2) < 0.1) {
					positions.push([col, row - 1]); // Flow up
					log("VERTICAL: incoming from bottom, flowing up");
				} else if (incomingDirection === 0) {
					// Initial flow from start - flow down
					positions.push([col, row + 1]);
					log("VERTICAL: initial flow, going down");
				}
				break;
			case TileFormat.TYPES.HORIZONTAL:
				// For horizontal pipes, we flow in the opposite direction of incoming
				// If water comes from left (0), we flow right
				// If water comes from right (Ο), we flow left
				if (Math.abs(incomingDirection) < 0.1) {
					positions.push([col + 1, row]); // Flow right
					log("HORIZONTAL: incoming from left, flowing right");
				} else if (Math.abs(incomingDirection - Math.PI) < 0.1 || incomingDirection === 0) {
					positions.push([col - 1, row]); // Flow left
					log("HORIZONTAL: incoming from right, flowing left");
				} else {
					log("ERROR HORIZONTAL: incoming not matching!");
				}
				break;
			case TileFormat.TYPES.CORNER:
				var outgoingDirection;
				var normalizedRotation = (rotation + Math.PI / 2) % (2 * Math.PI);
				log("CORNER PIPE with rotation:", rotation, " normalized rotation:", normalizedRotation, " with incoming:", incomingDirection);
				log("sin(rotation)=", Math.sin(rotation));
				var isSinRot = !(Math.abs(Math.sin(rotation)) < 0.1);
				var acceptAngle1 = isSinRot ? rotation + Math.PI / 2 : rotation;
				var acceptAngle2 = isSinRot ? rotation + Math.PI : rotation - Math.PI / 2;
				acceptAngle1 = (2 * Math.PI + acceptAngle1) % (2 * Math.PI);
				acceptAngle2 = (2 * Math.PI + acceptAngle2) % (2 * Math.PI);
				log("Checking if incoming match pipe entry:", acceptAngle1, " or ", acceptAngle2, " vs ", incomingDirection);
				if (Math.abs(incomingDirection - acceptAngle1) < 0.1) {
					outgoingDirection = acceptAngle2 + Math.PI;
				}
				if (Math.abs(incomingDirection - acceptAngle2) < 0.1) {
					outgoingDirection = acceptAngle1 + Math.PI;
				}
				log("CORNER: turning from", incomingDirection, "to", outgoingDirection);
				if (outgoingDirection !== undefined) {
					// Convert angle to grid movement
					var dx = Math.round(Math.cos(outgoingDirection));
					var dy = -Math.round(Math.sin(outgoingDirection));
					positions.push([col + dx, row + dy]);
					log("CORNER: moving dx:", dx, "dy:", dy);
				} else {
					log("ERROR CORNER: incoming not matching!");
				}
				break;
			case TileFormat.TYPES.END:
				// End tile doesn't flow anywhere
				log("END: no next position");
				break;
		}
		log("Final next positions:", positions);
		return positions;
	};
	self.normalizeRotation = function (rotation) {
		while (rotation < 0) {
			rotation += 2 * Math.PI;
		}
		return rotation % (2 * Math.PI);
	};
	self.setRotation = function (rot, rotation) {
		if (typeof rotation === 'number') {
			self.pipeContainer.rot = rot;
			self.pipeContainer.rotation = self.normalizeRotation(rotation); //* Math.PI / 2 DBO
		} else {
			// Handle string rotations (legacy support)
			switch (rotation) {
				case 'up':
					self.pipeContainer.rotation = 0;
					break;
				case 'right':
					self.pipeContainer.rotation = Math.PI / 2;
					break;
				case 'down':
					self.pipeContainer.rotation = Math.PI;
					break;
				case 'left':
					self.pipeContainer.rotation = 3 * Math.PI / 2;
					break;
			}
		}
	};
	self.updatePosition = function (row, col) {
		self.gridRow = row;
		self.gridCol = col;
		self.x = col * tileSize + gridBoard.x - gridBoard.width / 2 + tileSize / 2 + boardOffsetX;
		self.y = row * tileSize + gridBoard.y - gridBoard.height / 2 + tileSize / 2 + boardOffsetY;
		log('Tile row,col:', row, col, 'Tile position:', self.x, self.y, 'Tile dimensions:', self.width, self.height, ' self:', self);
		//log('pipeContainer:', self.pipeContainer.x, self.pipeContainer.y, ' child:', self.pipeContainer.children[0].x, self.pipeContainer.children[0].y);
		//log('gridBoard :', gridBoard.x, gridBoard.y, ' size:', gridBoard.width, gridBoard.height);
	};
	// Water flow update methods
	self.updateStartTile = function () {
		if (self.valve) {
			var rotationValue = (LK.ticks - self.startFlowTicks) * 0.1;
			self.valve.rotation = rotationValue;
			if (rotationValue >= Math.PI * 3) {
				self.flow = false;
			}
		}
	};
	self.updateEndTile = function () {
		if (self.fountain) {
			var sizeValue = (LK.ticks - self.startFlowTicks) * self.flowSpeed;
			if (sizeValue < self.maxWaterSize) {
				self.fountain.width = sizeValue;
				self.fountain.height = sizeValue;
				if (!self.fountain.visible) {
					self.fountain.visible = true;
					LK.getSound('fountain').play();
				}
			} else {
				self.flow = false;
				if (!waterDropInterval) {
					createWaterDrops(self.x, self.y + 100, game);
					waterDropInterval = LK.setInterval(function () {
						createWaterDrops(self.x, self.y + 100, game);
					}, 250 + Math.random() * 250);
				}
			}
		}
	};
	self.updateRegularPipe = function () {
		if (self.water) {
			var heightValue = (LK.ticks - self.startFlowTicks) * self.flowSpeed;
			if (heightValue < self.maxWaterSize) {
				//log("updateRegularPipe ", self.water.dir, self.rotation, self.pipeContainer.rotation, self);
				self.water.visible = true;
				if (self.water.dir === 'lr') {
					self.water.width = heightValue;
					self.water.x = -130;
					self.water.scaleX = 1;
				} else if (self.water.dir === 'rl') {
					self.water.width = heightValue;
					self.water.x = 130;
					self.water.scaleX = -1;
				} else if (self.water.dir === 'tb') {
					self.water.height = heightValue;
					self.water.y = -130;
					self.water.scaleY = 1;
				} else if (self.water.dir === 'bt') {
					log("=> bt", self.rotation, self.pipeContainer.rotation, self);
					self.water.height = heightValue;
					self.water.y = 130;
					self.water.scaleY = -1;
				} else {
					self.water.height = heightValue;
				}
			} else {
				self.flow = false;
			}
		}
	};
	self.getFlowOrder = function (rot, dir) {
		// Specific function take rotation (x4) + dir(x4) => order (rot + dir + => final order)
		log("getFlowOrder rot=", rot, " / dir=", dir);
		var normalOrder = true;
		switch (rot) {
			case 0:
				normalOrder = dir == 'tb' ? false : true;
				break;
			case 1:
				normalOrder = dir == 'rl' ? false : true;
				break;
			case 2:
				normalOrder = dir == 'rl' ? true : false;
				break;
			case 3:
				normalOrder = dir == 'bt' ? true : false;
				break;
			default:
				log("ERROR in getFlowOrder : Unknown rotation ", rot);
		}
		return normalOrder;
	};
	self.updateCornerPipe = function () {
		var progress = (LK.ticks - self.startFlowTicks) * self.flowSpeed;
		var thirdSize = self.maxWaterCornerSize / 3;
		var normalOrder = self.getFlowOrder(self.pipeContainer.rot, self.pipeContainer.dir);
		log("updateCornerPipe: Choosing order for tile:", self.pipeContainer.rotation, self.pipeContainer.dir);
		if (progress < thirdSize) {
			// 0 or PI/2
			if (normalOrder) {
				self.updateCornerPipeVerticalPart(progress, thirdSize, false);
			} else {
				self.updateCornerPipeHorizontalPart(progress, thirdSize, true);
			}
		} else if (progress > thirdSize * 2 && progress < thirdSize * 3) {
			// 0 or PI/2
			if (normalOrder) {
				self.updateCornerPipeHorizontalPart(progress, thirdSize, false);
			} else {
				self.updateCornerPipeVerticalPart(progress, thirdSize, true);
			}
		}
		if (progress >= self.maxWaterCornerSize) {
			self.flow = false;
		}
	};
	self.getCornerVerticalAnimWay = function (rot, dir) {
		// Specific function take rotation (x4) + dir(x4) => way (true = exterior => interior / false = interior exterior)
		log("get Corner Vert Anim  rot=", rot, " / dir=", dir);
		var normalWay = true;
		switch (rot) {
			case 0:
				normalWay = dir == 'tb' ? false : true;
				break;
			case 1:
				normalWay = dir == 'rl' ? false : true;
				break;
			case 2:
				normalWay = dir == 'bt' ? false : true;
				break;
			case 3:
				normalWay = dir == 'lr' ? false : true;
				break;
			default:
				log("ERROR in getCornerVerticalAnimWay : Unknown rotation ", rot);
		}
		return normalWay;
	};
	self.updateCornerPipeVerticalPart = function (progress, thirdSize, reverted) {
		// Specific function take rotation (x4) + dir(x4) => revertedV (Log x,y + rot + dir + => final)
		if (!self.waterV) {
			return;
		}
		var currentProgress = reverted ? progress - thirdSize * 2 : progress;
		var normalWay = self.getCornerVerticalAnimWay(self.pipeContainer.rot, self.pipeContainer.dir);
		log("updateCornerPipeVerticalPart: normal way:", normalWay);
		log("updateCornerPipeVerticalPart: Updated waterV height to:", currentProgress);
		if (normalWay) {
			//self.waterV.tint = 0x0000FF;
		} else {
			//self.waterV.tint = 0x00FF00;
			self.waterV.y = -7;
			self.waterV.x = -80;
			self.waterV.rotation = Math.PI / 2;
		}
		self.waterV.visible = true;
		self.waterV.height = currentProgress;
	};
	self.getCornerHorizontalAnimWay = function (rot, dir) {
		// Specific function take rotation (x4) + dir(x4) => way (true = exterior => interior / false = interior exterior)
		log("get Corner Horiz Anim rot=", rot, " / dir=", dir);
		var normalWay = true;
		switch (rot) {
			case 0:
				normalWay = dir == 'lr' ? false : true; // dir == 'tb' ||
				break;
			case 1:
				normalWay = dir == 'tb' ? false : true; //dir == 'rl' || 
				break;
			case 2:
				normalWay = dir == 'bt' ? true : false;
				break;
			case 3:
				normalWay = dir == 'lr' ? true : false;
				break;
			default:
				log("ERROR in getCornerHorizontalAnimWay : Unknown rotation ", rot);
		}
		return normalWay;
	};
	self.updateCornerPipeHorizontalPart = function (progress, thirdSize, reverted) {
		if (!self.waterH) {
			return;
		}
		var currentProgress = reverted ? progress : progress - thirdSize * 2;
		var normalWay = self.getCornerHorizontalAnimWay(self.pipeContainer.rot, self.pipeContainer.dir);
		log("Upd Corner H: normal way:", normalWay);
		log("Upd Corner H: Updated waterH width to:", currentProgress, " reverted=", reverted);
		if (normalWay) {
			//self.waterH.tint = 0xF050A0;
			if (self.pipeContainer.rot == 0) {
				self.waterH.y = -140;
				self.waterH.x = -5;
				self.waterH.rotation = -Math.PI / 2;
			}
			if (self.pipeContainer.rot == 1) {
				self.waterH.y = -140;
				self.waterH.x = -5;
				self.waterH.rotation = -Math.PI / 2;
			}
			if (self.pipeContainer.rot == 2) {
				self.waterH.y = -140;
				self.waterH.x = -5;
				self.waterH.rotation = -Math.PI / 2;
			}
			if (self.pipeContainer.rot == 3) {
				self.waterH.y = -140;
				self.waterH.x = -5;
				self.waterH.rotation = Math.PI / 2;
			}
		} else {
			//self.waterH.tint = 0xFF0000;
			if (self.pipeContainer.rot == 1) {
				self.waterH.y = -90;
				self.waterH.x = -3;
				self.waterH.rotation = Math.PI / 2;
			}
			if (self.pipeContainer.rot == 0) {
				self.waterH.y = -90;
				self.waterH.x = -3;
				self.waterH.rotation = -Math.PI / 2;
			}
			if (self.pipeContainer.rot == 2) {
				self.waterH.y = -90;
				self.waterH.x = -3;
				self.waterH.rotation = Math.PI / 2;
			}
			if (self.pipeContainer.rot == 3) {
				self.waterH.y = -90;
				self.waterH.x = -3;
				self.waterH.rotation = Math.PI / 2;
			}
		}
		log("Upd Corner H: waterH position x:", self.waterH.x, "y:", self.waterH.y, "dir:", self.waterH.dir);
		self.waterH.visible = true;
		self.waterH.width = currentProgress;
	};
	self.update = function () {
		if (self.flow) {
			if (!self.startFlowTicks) {
				self.startFlowTicks = LK.ticks;
				if (self.water) {
					self.water.visible = true;
				}
				if (self.waterV) {
					self.waterV.visible = true;
				}
				if (self.waterH) {
					self.waterH.visible = true;
				}
			}
			switch (self.type) {
				case TileFormat.TYPES.START:
					self.updateStartTile();
					break;
				case TileFormat.TYPES.END:
					self.updateEndTile();
					break;
				case TileFormat.TYPES.CORNER:
					self.updateCornerPipe();
					break;
				default:
					self.updateRegularPipe();
					break;
			}
		}
		// Animate end tile fountain
		if (self.type === TileFormat.TYPES.END && self.fountain && self.fountain.visible) {
			self.fountain.rotation += 0.1;
			var sizeVariation = 30 * Math.sin(LK.ticks * 0.1);
			self.fountain.width = 250 + sizeVariation;
			self.fountain.height = 250 + sizeVariation;
		}
	};
}
Tile.prototype = Object.create(Container.prototype);
function _slicedToArray3(r, e) {
	return _arrayWithHoles3(r) || _iterableToArrayLimit3(r, e) || _unsupportedIterableToArray3(r, e) || _nonIterableRest3();
}
function _nonIterableRest3() {
	throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray3(r, a) {
	if (r) {
		if ("string" == typeof r) {
			return _arrayLikeToArray3(r, a);
		}
		var t = {}.toString.call(r).slice(8, -1);
		return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray3(r, a) : void 0;
	}
}
function _arrayLikeToArray3(r, a) {
	(null == a || a > r.length) && (a = r.length);
	for (var e = 0, n = Array(a); e < a; e++) {
		n[e] = r[e];
	}
	return n;
}
function _iterableToArrayLimit3(r, l) {
	var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
	if (null != t) {
		var e,
			n,
			i,
			u,
			a = [],
			f = !0,
			o = !1;
		try {
			if (i = (t = t.call(r)).next, 0 === l) {
				if (Object(t) !== t) {
					return;
				}
				f = !1;
			} else {
				for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) {
					;
				}
			}
		} catch (r) {
			o = !0, n = r;
		} finally {
			try {
				if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) {
					return;
				}
			} finally {
				if (o) {
					throw n;
				}
			}
		}
		return a;
	}
}
function _arrayWithHoles3(r) {
	if (Array.isArray(r)) {
		return r;
	}
}
function _typeof(o) {
	"@babel/helpers - typeof";
	return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
		return typeof o;
	} : function (o) {
		return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
	}, _typeof(o);
}
function _defineProperty(e, r, t) {
	return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
		value: t,
		enumerable: !0,
		configurable: !0,
		writable: !0
	}) : e[r] = t, e;
}
function _toPropertyKey(t) {
	var i = _toPrimitive(t, "string");
	return "symbol" == _typeof(i) ? i : i + "";
}
function _toPrimitive(t, r) {
	if ("object" != _typeof(t) || !t) {
		return t;
	}
	var e = t[Symbol.toPrimitive];
	if (void 0 !== e) {
		var i = e.call(t, r || "default");
		if ("object" != _typeof(i)) {
			return i;
		}
		throw new TypeError("@@toPrimitive must return a primitive value.");
	}
	return ("string" === r ? String : Number)(t);
}
function _slicedToArray(r, e) {
	return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
function _nonIterableRest() {
	throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray(r, a) {
	if (r) {
		if ("string" == typeof r) {
			return _arrayLikeToArray(r, a);
		}
		var t = {}.toString.call(r).slice(8, -1);
		return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
	}
}
function _arrayLikeToArray(r, a) {
	(null == a || a > r.length) && (a = r.length);
	for (var e = 0, n = Array(a); e < a; e++) {
		n[e] = r[e];
	}
	return n;
}
function _iterableToArrayLimit(r, l) {
	var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
	if (null != t) {
		var e,
			n,
			i,
			u,
			a = [],
			f = !0,
			o = !1;
		try {
			if (i = (t = t.call(r)).next, 0 === l) {
				if (Object(t) !== t) {
					return;
				}
				f = !1;
			} else {
				for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) {
					;
				}
			}
		} catch (r) {
			o = !0, n = r;
		} finally {
			try {
				if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) {
					return;
				}
			} finally {
				if (o) {
					throw n;
				}
			}
		}
		return a;
	}
}
function _arrayWithHoles(r) {
	if (Array.isArray(r)) {
		return r;
	}
}
var SimpleSet = function SimpleSet() {
	this.items = {};
	this.has = function (item) {
		return this.items.hasOwnProperty(item);
	};
	this.add = function (item) {
		if (!this.has(item)) {
			this.items[item] = true;
		}
	};
};
var PuzzleManager = function PuzzleManager(game) {
	var self = this;
	self.game = game;
	self.grid = [];
	self.gridSize = 4;
	self.selectedTile = null;
	self.isComplete = false;
	self.waterFlowing = false;
	self.solutionPath = null; // Store the solution path for water flow
	self.reset = function () {
		// Clear grid
		for (var row = 0; row < self.gridSize; row++) {
			if (!self.grid[row]) {
				self.grid[row] = [];
			}
			for (var col = 0; col < self.gridSize; col++) {
				if (self.grid[row][col]) {
					self.grid[row][col].destroy();
				}
				self.grid[row][col] = null;
			}
		}
		// Reset state
		self.selectedTile = null;
		// Remove all flowers
		for (var i = flowers.length - 1; i >= 0; i--) {
			if (flowers[i].parent) {
				flowers[i].parent.removeChild(flowers[i]);
			}
		}
		flowers = [];
		self.waterFlowing = false;
		// Clear water drops
		if (waterDropInterval) {
			LK.clearInterval(waterDropInterval);
			waterDropInterval = null;
		}
		// Reset water particles
		if (game.waterParticles) {
			for (var i = game.waterParticles.length - 1; i >= 0; i--) {
				var particle = game.waterParticles[i];
				if (particle && particle.parent) {
					particle.parent.removeChild(particle);
				}
			}
			game.waterParticles = [];
		}
		currentRoundMoves = 0;
		if (self.isComplete) {
			self.currentLevel++;
			storage.currentLevel = self.currentLevel; // Store current level in storage
		} else {
			self.currentLevel = storage.currentLevel || (debug && levelConfigs[0] ? 0 : 1);
			//self.currentLevel = debug ? 0 : self.currentLevel;
		}
		self.isComplete = false;
	};
	self.initPuzzle = function () {
		self.reset();
		var level = levelConfigs[self.currentLevel];
		if (!level) {
			log("Next Level not found:", self.currentLevel);
			changeGameState(GAME_STATE.SCORE);
			return;
		}
		resetButton.visible = true;
		resetButton.disable();
		// Initialize grid with tiles from level configuration
		for (var row = 0; row < self.gridSize; row++) {
			self.grid[row] = [];
			for (var col = 0; col < self.gridSize; col++) {
				var tileStr = level[row][col];
				var tile = createTile(tileStr, row, col);
				if (tile) {
					//gridBoard.addChild(tile);
					game.addChild(tile);
					self.grid[row][col] = tile;
				}
			}
		}
	};
	self.checkWinCondition = function () {
		log("=== Starting Win Condition Check ===");
		log("Grid state:");
		for (var row = 0; row < self.gridSize; row++) {
			var rowStr = [];
			for (var col = 0; col < self.gridSize; col++) {
				var tile = self.grid[row][col];
				if (tile) {
					var rotIndex = TileFormat.getRotationIndex(tile.normalizeRotation(tile.pipeContainer.rotation));
					rowStr.push("".concat(tile.fixed ? 'F' : 'M', "-").concat(tile.type, "-").concat(rotIndex));
				} else {
					rowStr.push('empty');
				}
			}
			log("Row ".concat(row, ":"), rowStr.join(' | '));
		}
		// Find start tile
		var startTile = null;
		var startRow = -1,
			startCol = -1;
		for (var row = 0; row < self.gridSize; row++) {
			for (var col = 0; col < self.gridSize; col++) {
				if (self.grid[row][col] && self.grid[row][col].type === TileFormat.TYPES.START) {
					startTile = self.grid[row][col];
					startRow = row;
					startCol = col;
					break;
				}
			}
			if (startTile) {
				break;
			}
		}
		if (!startTile) {
			log("No start tile found!");
			return false;
		}
		log("Found start tile at row:", startRow, "col:", startCol);
		var visited = new SimpleSet();
		var startDir = getFlowDirection(startTile);
		var toCheck = [[startRow, startCol, startDir]];
		var pathMap = {}; // Store the path for each visited position
		// Store start position
		pathMap[startRow + "," + startCol] = {
			row: startRow,
			col: startCol,
			flowDirection: null,
			// Start doesn't have incoming //startDir,
			prevKey: null
		};
		while (toCheck.length > 0) {
			var _toCheck$pop = toCheck.pop(),
				_toCheck$pop2 = _slicedToArray2(_toCheck$pop, 3),
				row = _toCheck$pop2[0],
				col = _toCheck$pop2[1],
				flowDirection = _toCheck$pop2[2];
			var key = "".concat(row, ",").concat(col);
			if (visited.has(key)) {
				continue;
			}
			visited.add(key);
			log("======================= Checking position - row:", row, "col:", col, "=========================");
			// Store current position in path map
			//pathMap.set(key, { row, col, flowDirection });
			// Check if coordinates are within grid
			if (row < 0 || row >= self.gridSize || col < 0 || col >= self.gridSize) {
				log("Position out of bounds");
				continue;
			}
			var currentTile = self.grid[row][col];
			if (!currentTile) {
				log("No tile at position");
				continue;
			}
			var normalizedRotation = currentTile.normalizeRotation(currentTile.pipeContainer.rotation);
			log("Checking tile:", currentTile.type, "rotation:", TileFormat.getRotationIndex(normalizedRotation));
			if (currentTile.type == TileFormat.TYPES.START) {
				flowDirection = null; // Start doesn't have incoming flow
			}
			var nextPositions = currentTile.getNextPositions(col, row, normalizedRotation, currentTile.type, flowDirection);
			log("Next positions to check:", nextPositions);
			var _iterator = _createForOfIteratorHelper(nextPositions),
				_step;
			try {
				for (_iterator.s(); !(_step = _iterator.n()).done;) {
					var nextPos = _step.value;
					var _nextPos = nextPos,
						_nextPos2 = _slicedToArray2(_nextPos, 2),
						nextCol = _nextPos2[0],
						nextRow = _nextPos2[1];
					log("Trying next position - row:", nextRow, "col:", nextCol);
					if (nextRow < 0 || nextRow >= self.gridSize || nextCol < 0 || nextCol >= self.gridSize) {
						log("Next position out of bounds");
						continue;
					}
					var nextTile = self.grid[nextRow][nextCol];
					if (!nextTile) {
						log("No tile at next position");
						continue;
					}
					var incomingDirection = Math.atan2(nextRow - row, col - nextCol);
					if (!canAcceptFlowFromDirection(nextTile, incomingDirection)) {
						log("Tile at row:", nextRow, "col:", nextCol, "cannot accept flow from direction:", incomingDirection);
						continue;
					}
					log("== OK. Prepare next tile with previous tile at ", incomingDirection, " (", nextRow, row, nextCol, col, ")");
					// Update nextTile.water.dir depending on incomingDirection
					if (nextTile.water) {
						log("Updating water direction for tile at row:", nextRow, "col:", nextCol);
						if (nextTile.type === TileFormat.TYPES.VERTICAL) {
							if (Math.abs(incomingDirection - Math.PI / 2) < 0.1) {
								nextTile.water.dir = 'tb'; // Top to Bottom
								log("Set water direction to 'tb' (Top to Bottom)");
							} else {
								//if (Math.abs(incomingDirection - 3 * Math.PI / 2) < 0.1)
								nextTile.water.dir = 'bt'; // Bottom to Top
								log("Set water direction to 'bt' (Bottom to Top)");
							}
						} else if (nextTile.type === TileFormat.TYPES.HORIZONTAL) {
							if (Math.abs(incomingDirection - Math.PI) < 0.1) {
								nextTile.water.dir = 'lr'; // Left to Right
								log("Set water direction to 'lr' (Left to Right)");
							} else {
								// if (Math.abs(incomingDirection) < 0.1)
								nextTile.water.dir = 'rl'; // Right to Left
								log("Set water direction to 'rl' (Right to Left)");
							}
						}
					}
					if (nextTile.type === TileFormat.TYPES.CORNER) {
						// For corner store water dir in pipeContainer
						if (Math.abs(incomingDirection - Math.PI / 2) < 0.1) {
							nextTile.pipeContainer.dir = 'tb'; // Top to Bottom
							log("Set Corner water direction to 'tb' (Top to Bottom)");
						} else if (Math.abs(incomingDirection + Math.PI / 2) % (2 * Math.PI) < 0.1) {
							nextTile.pipeContainer.dir = 'bt'; // Bottom to Top
							log("Set Corner water direction to 'bt' (Bottom to Top)");
						} else if (Math.abs(incomingDirection - Math.PI) < 0.1) {
							nextTile.pipeContainer.dir = 'lr'; // Left to Right
							log("Set Corner water direction to 'lr' (Left to Right)");
						} else {
							nextTile.pipeContainer.dir = 'rl'; // Right to Left
							log("Set Corner water direction to 'rl' (Right to Left)");
						}
					}
					if (nextTile.type === TileFormat.TYPES.END) {
						log("Found path to end!");
						// Start reconstruction from the end position
						self.solutionPath = [];
						// Add all tiles from pathMap to solution path
						Object.keys(pathMap).forEach(function (key) {
							var _key$split$map = key.split(",").map(Number),
								_key$split$map2 = _slicedToArray5(_key$split$map, 2),
								row = _key$split$map2[0],
								col = _key$split$map2[1];
							var tile = self.grid[row][col];
							if (tile) {
								self.solutionPath.push({
									row: row,
									col: col,
									flowDirection: tile.normalizeRotation(tile.pipeContainer.rotation),
									tile: tile
								});
								log("Added tile from pathMap:", row, col);
							}
						});
						// Add the end tile
						self.solutionPath.push({
							row: nextRow,
							col: nextCol,
							flowDirection: nextTile.normalizeRotation(nextTile.pipeContainer.rotation),
							tile: nextTile
						});
						log("Added end tile:", nextRow, nextCol);
						log("Solution path reconstructed with", self.solutionPath.length, "tiles");
						return true;
					}
					//var nextFlowDirection = getFlowDirection(nextTile, incomingDirection);
					var nextFlowDirection = getFlowDirection(currentTile, flowDirection); // incomingDirection);
					var nextKey = nextRow + "," + nextCol;
					log("Set next tile ", nextKey, " flow dir:", nextFlowDirection);
					toCheck.push([nextRow, nextCol, nextFlowDirection]);
					pathMap[nextKey] = {
						row: nextRow,
						col: nextCol,
						flowDirection: nextFlowDirection,
						prevKey: key
					};
				}
			} catch (err) {
				_iterator.e(err);
			} finally {
				_iterator.f();
			}
		}
		log("No valid path found");
		return false;
	};
	self.selectTile = function (x, y) {
		if (!isPlaying) {
			return;
		}
		// Convert screen coordinates to grid coordinates
		var boardX = gridBoard.x - gridBoard.width / 2 + boardOffsetX;
		var boardY = gridBoard.y - gridBoard.height / 2 + boardOffsetY;
		var row = Math.floor((y - boardY) / tileSize);
		var col = Math.floor((x - boardX) / tileSize);
		log("Click at ", x, ",", y, " => Grid coordinates - row:", row, "col:", col);
		// Check if coordinates are within grid
		if (row >= 0 && row < this.gridSize && col >= 0 && col < this.gridSize) {
			var tile = this.grid[row][col];
			log("Clicked tile ", tile);
			// Check if tile exists, is mobile, and not start/end
			if (tile && !tile.fixed && tile.type !== TileFormat.TYPES.START && tile.type !== TileFormat.TYPES.END) {
				this.selectedTile = {
					row: row,
					col: col,
					tile: tile
				};
				log("Selected tile row:", this.selectedTile.row, "col:", this.selectedTile.col, this.selectedTile.tile);
				// Check possible moves
				this.checkPossibleMoves();
			}
		}
		// If no tile is found, search nearby tiles using tapOffset
		if (!this.selectedTile) {
			var nearbyOffsets = [{
				dx: tapOffset,
				dy: 0
			}, {
				dx: -tapOffset,
				dy: 0
			}, {
				dx: 0,
				dy: tapOffset
			}, {
				dx: 0,
				dy: -tapOffset
			}];
			for (var _i = 0, _nearbyOffsets = nearbyOffsets; _i < _nearbyOffsets.length; _i++) {
				var offset = _nearbyOffsets[_i];
				var nearbyRow = Math.floor((y - boardY + offset.dy) / tileSize);
				var nearbyCol = Math.floor((x - boardX + offset.dx) / tileSize);
				if (nearbyRow >= 0 && nearbyRow < this.gridSize && nearbyCol >= 0 && nearbyCol < this.gridSize) {
					var nearbyTile = this.grid[nearbyRow][nearbyCol];
					if (nearbyTile && !nearbyTile.fixed && nearbyTile.type !== TileFormat.TYPES.START && nearbyTile.type !== TileFormat.TYPES.END) {
						this.selectedTile = {
							row: nearbyRow,
							col: nearbyCol,
							tile: nearbyTile
						};
						log("Selected nearby tile row:", this.selectedTile.row, "col:", this.selectedTile.col, this.selectedTile.tile);
						this.checkPossibleMoves();
						break;
					}
				}
			}
		}
	};
	self.checkPossibleMoves = function () {
		if (!this.selectedTile) {
			return;
		}
		var row = this.selectedTile.row;
		var col = this.selectedTile.col;
		var possibleMoves = [];
		// Check each direction
		// Right
		if (col < this.gridSize - 1 && !this.grid[row][col + 1]) {
			possibleMoves.push('right');
		}
		// Left
		if (col > 0 && !this.grid[row][col - 1]) {
			possibleMoves.push('left');
		}
		// Down
		if (row < this.gridSize - 1 && !this.grid[row + 1][col]) {
			possibleMoves.push('down');
		}
		// Up
		if (row > 0 && !this.grid[row - 1][col]) {
			possibleMoves.push('up');
		}
		this.selectedTile.possibleMoves = possibleMoves;
	};
	self.moveTile = function (direction) {
		if (!isPlaying || !this.selectedTile || !this.selectedTile.possibleMoves.includes(direction)) {
			LK.getSound('tileBlocked').play();
			log("Invalid move attempt in direction:", direction);
			return;
		}
		currentRoundMoves++;
		resetButton.enable();
		var oldRow = this.selectedTile.row;
		var oldCol = this.selectedTile.col;
		var newRow = oldRow;
		var newCol = oldCol;
		// Calculate new position
		switch (direction) {
			case 'right':
				newCol++;
				break;
			case 'left':
				newCol--;
				break;
			case 'down':
				newRow++;
				break;
			case 'up':
				newRow--;
				break;
		}
		// Play tile slide sound
		LK.getSound('tileSlide').play();
		// Move tile with tween animation
		var movingTile = this.grid[oldRow][oldCol];
		this.grid[oldRow][oldCol] = null;
		this.grid[newRow][newCol] = movingTile;
		// Use tween to animate the tile movement
		tween(movingTile, {
			x: newCol * tileSize + gridBoard.x - gridBoard.width / 2 + tileSize / 2 + boardOffsetX,
			y: newRow * tileSize + gridBoard.y - gridBoard.height / 2 + tileSize / 2 + boardOffsetY
		}, {
			duration: 300,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				// Update tile position after animation completes
				movingTile.updatePosition(newRow, newCol);
			}
		});
		// Clear selection
		this.selectedTile = null;
		// Check if puzzle is solved
		if (this.checkWinCondition()) {
			this.isComplete = true;
			resetButton.disable();
			isPlaying = false;
			isGameFinished = puzzleManager.currentLevel == nbLevels; // || debug && puzzleManager.currentLevel == 1;
			LK.setTimeout(function () {
				puzzleManager.startWaterFlow();
				LK.getSound('levelWon').play();
			}, 300);
		}
	};
	self.startWaterFlow = function () {
		if (!self.solutionPath || self.solutionPath.length === 0) {
			log("No solution path available!");
			return false;
		}
		// Reset any existing flow states
		for (var row = 0; row < self.gridSize; row++) {
			for (var col = 0; col < self.gridSize; col++) {
				if (self.grid[row][col]) {
					self.grid[row][col].flow = false;
					if (self.grid[row][col].water) {
						self.grid[row][col].water.visible = false;
					}
					if (self.grid[row][col].waterV) {
						self.grid[row][col].waterV.visible = false;
					}
					if (self.grid[row][col].waterH) {
						self.grid[row][col].waterH.visible = false;
					}
				}
			}
		}
		self.waterFlowing = true;
		log("Starting water flow through solution path:", self.solutionPath.length, "tiles");
		var currentIndex = 0;
		waterFlowReachedEnd = false;
		function animateNextTile() {
			if (currentIndex >= self.solutionPath.length) {
				// End of path reached - start end game sequence
				LK.setTimeout(function () {
					animateSoil();
				}, 300);
				return;
			}
			var currentTile = self.solutionPath[currentIndex].tile;
			currentTile.flow = true;
			// Show appropriate water animation based on tile type
			if (currentTile.water) {
				currentTile.water.visible = true;
			}
			if (currentTile.waterV) {
				currentTile.waterV.visible = true;
			}
			if (currentTile.waterH) {
				currentTile.waterH.visible = true;
				// Set water flow direction based on path
				if (currentIndex > 0) {
					var prevTile = self.solutionPath[currentIndex - 1].tile;
					currentTile.waterH.scaleX = prevTile.gridCol < currentTile.gridCol ? 1 : -1;
				}
			}
			// Special handling for start tile
			if (currentTile.type === TileFormat.TYPES.START) {
				LK.getSound('waterInPipe').play();
				var waterFlowSoundInterval = LK.setInterval(function () {
					if (!waterFlowReachedEnd) {
						LK.getSound('waterInPipe').play();
					} else {
						LK.clearInterval(waterFlowSoundInterval);
					}
				}, 2000);
			}
			// Special handling for end tile
			if (currentTile.type === TileFormat.TYPES.END) {
				waterFlowReachedEnd = true;
				createWaterDrops(currentTile.x, currentTile.y + 100, game);
			}
			currentIndex++;
			LK.setTimeout(animateNextTile, 400);
		}
		// Start the animation
		animateNextTile();
		return true;
	};
	return this;
};
/**** 
* Game Variables
****/ 
var debug = false;
function log() {
	if (debug) {
		console.log.apply(console, arguments);
	}
}
// Game constants
var tileSize = 400;
var boardOffsetX = 100;
var boardOffsetY = 100;
// Game state variables
var GAME_STATE = {
	INIT: 'INIT',
	MENU: 'MENU',
	NEW_ROUND: 'NEW_ROUND',
	PLAYING: 'PLAYING',
	SCORE: 'SCORE'
};
var currentState = GAME_STATE.INIT;
var isPlaying = true;
var backgroundLayer;
var backgroundImage1;
var backgroundImage2;
var middleLayer;
var frontLayer;
var gridBoard;
var gridBoardSoil;
var gridBoardSoilOffset = 20;
var growGrass;
var growGrassOffsetX = 20;
var isMouseDown = false;
var startX = 0;
var startY = 0;
var selectedTile = null;
var dragThreshold = 100; // Lower threshold for easier movement detection
var tapOffset = 50; // Offset for searching nearby tiles when no tile is found
var levelText;
var levelTextXOffset = -50;
var resetButton;
var congratsText;
var congratsTextOffset = 300;
var nbMovesText;
var nbMovesTextOffset = 800;
var waterDrops = [];
var flowers = [];
var waterDropInterval;
var waterFlowReachedEnd = false;
var logo;
var logoShadow;
var logoMaze;
var logoMazeShadow;
var puzzleManager;
var isLogoAnimInFinished = false;
var isLogoAnimOutFinished = false;
var isRoundExitAnimFinished = false;
var isGameFinished = false;
var isFinalAnimationFinished = false;
var finalAnimationInterval;
var currentRoundMoves = 0;
var congratsMessages = {
	1: "Well Done!",
	2: "Great Job!",
	3: "Marvelous!"
};
// Initialize the game
initializeGame();
/**** 
* Helper Functions
****/ 
function getFlowDirection(tile, incomingDirection) {
	//var rotation = tile.normalizeRotation(tile.pipeContainer.rotation);
	var flowDir; //(incomingDirection + Math.PI) % (2 * Math.PI);
	log("Get Output flow dir:", tile.type, "Rotation:", tile.pipeContainer.rotation, "incomingDirection:", incomingDirection); //, "Normalized incoming:", rotation);
	switch (tile.type) {
		case TileFormat.TYPES.START:
			log("Start detail: ", Math.PI * 2, " + ", tile.pipeContainer.rotation, " + ", Math.PI / 2, " * (", Math.abs(Math.sin(tile.pipeContainer.rotation)), " - ", Math.abs(Math.cos(tile.pipeContainer.rotation)), ")");
			flowDir = Math.PI * 2 + tile.pipeContainer.rotation + Math.PI / 2 * (Math.abs(Math.sin(tile.pipeContainer.rotation)) - Math.abs(Math.cos(tile.pipeContainer.rotation)));
			break;
		case TileFormat.TYPES.CORNER:
			if (Math.abs(tile.pipeContainer.rotation - incomingDirection) < 0.1 || Math.abs((tile.pipeContainer.rotation + Math.PI) % (2 * Math.PI) - incomingDirection) < 0.1) {
				flowDir = incomingDirection + Math.PI / 2;
			} else {
				flowDir = incomingDirection - Math.PI / 2;
			}
			break;
		default:
			flowDir = incomingDirection; // + Math.PI; // From side to oposite side
			break;
	}
	flowDir = (2 * Math.PI + flowDir) % (2 * Math.PI);
	log("Returned output flow direction:", flowDir);
	return flowDir;
}
function canAcceptFlowFromDirection(tile, incomingDirection) {
	var normalizedIncoming = (incomingDirection + Math.PI) % (2 * Math.PI);
	var tileRotation = tile.normalizeRotation(tile.pipeContainer.rotation);
	log("Checking if tile can accept flow. Tile type:", tile.type, "Tile rotation:", tileRotation, " previous tile at:", incomingDirection, " => Incoming flow dir:", normalizedIncoming);
	var angleDiff = null;
	switch (tile.type) {
		case TileFormat.TYPES.START:
			log("No => Tile type is START. Cannot accept flow.");
			return false;
		case TileFormat.TYPES.END:
			angleDiff = Math.abs(normalizedIncoming - tileRotation + (Math.abs(Math.sin(tileRotation)) - Math.abs(Math.cos(tileRotation))) * Math.PI / 2) % (2 * Math.PI);
			log("Yes => Tile type is END.", angleDiff, angleDiff < 0.1);
			return angleDiff < 0.1;
		case TileFormat.TYPES.VERTICAL:
			angleDiff = Math.abs(normalizedIncoming - Math.PI / 2) % (2 * Math.PI);
			log("Yes => Tile type is VERTICAL");
			return angleDiff < 0.1 || Math.abs(angleDiff - Math.PI) < 0.1;
		case TileFormat.TYPES.HORIZONTAL:
			angleDiff = normalizedIncoming % (2 * Math.PI);
			log("Yes => Tile type is HORIZONTAL");
			return angleDiff < 0.1 || Math.abs(angleDiff - Math.PI) < 0.1;
		case TileFormat.TYPES.CORNER:
			angleDiff = (normalizedIncoming - tileRotation - Math.PI) % (2 * Math.PI);
			log("Yes => Tile type is CORNER.");
			return angleDiff < 0.1 || Math.abs(angleDiff - Math.PI / 2) < 0.1;
		default:
			log("No => Tile type is unknown. Cannot accept flow.");
			return false;
	}
}
function createWaterDrops(x, y, game) {
	for (var i = 0; i < 30; i++) {
		var waterDrop = waterDrops.find(function (drop) {
			return !drop.visible;
		});
		if (!waterDrop) {
			waterDrop = new WaterDrop();
			waterDrops.push(waterDrop);
			frontLayer.addChild(waterDrop);
		}
		waterDrop.x = x;
		waterDrop.y = y;
		var angle = Math.random() * Math.PI * 2;
		var speed = Math.random() * 5 + 5;
		var easeFactor = Math.random() * 0.05 + 0.95; // Random easing factor between 0.95 and 1.0
		waterDrop.vx = Math.cos(angle) * speed * easeFactor;
		waterDrop.vy = Math.sin(angle) * speed * easeFactor;
		waterDrop.life = 150;
		waterDrop.visible = true;
	}
}
function animateSoil() {
	log("Animate soil...");
	var alphaIncrement = 0.05; // Adjust the increment for desired speed
	// Animate all baseTile tiles' alpha from 1 to 0
	puzzleManager.grid.forEach(function (row) {
		row.forEach(function (tile) {
			log("animate tile:", tile);
			if (tile && tile.baseTile) {
				tile.baseTile.alpha = 1;
				LK.setInterval(function () {
					if (tile.baseTile.alpha > 0) {
						tile.baseTile.alpha -= alphaIncrement;
					}
				}, 30);
			}
		});
	});
	growGrass.visible = true;
	var stars = calculateStarsScore();
	// Spawn a Flower in each grid cell
	for (var i = 0; i < puzzleManager.gridSize; i++) {
		for (var j = 0; j < puzzleManager.gridSize; j++) {
			for (var k = 0; k < stars; k++) {
				var x = i * tileSize + gridBoard.x - gridBoard.width / 2 + tileSize / 2 + boardOffsetX;
				var y = j * tileSize + gridBoard.y - gridBoard.height / 2 + tileSize / 2 + boardOffsetY;
				var flower = new Flower(puzzleManager.currentLevel, x, y);
				flowers.push(flower);
				game.addChild(flower);
			}
		}
	}
	// Animate all pipeContainer tiles' alpha from 1 to 0
	puzzleManager.grid.forEach(function (row) {
		row.forEach(function (tile) {
			log("animate pipeContainer tile:", tile);
			if (tile && tile.pipeContainer) {
				tile.pipeContainer.alpha = 1;
				LK.setInterval(function () {
					if (tile.pipeContainer.alpha > 0) {
						tile.pipeContainer.alpha -= alphaIncrement * 0.5;
					}
				}, 30);
			}
		});
	});
	// Animate growGrass alpha from 0 to 1
	var grassAnimation = LK.setInterval(function () {
		if (growGrass.alpha < 1) {
			growGrass.alpha += alphaIncrement; // Adjust the increment for desired speed
		} else {
			LK.clearInterval(grassAnimation);
			LK.setTimeout(function () {
				if (isGameFinished) {
					changeGameState(GAME_STATE.SCORE);
				} else {
					changeGameState(GAME_STATE.NEW_ROUND);
				}
			}, 2000);
		}
	}, 80); // Adjust the interval for desired speed
}
/**** 
* Game State Management
****/ 
function initializeGame() {
	// Init and Add backgroundLayer
	backgroundLayer = new Container();
	middleLayer = new Container();
	frontLayer = new Container();
	game.addChild(backgroundLayer);
	// Add backgroundPlaying1 to the menu state
	backgroundImage1 = LK.getAsset('backgroundPlaying1', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	backgroundImage1.x = 2048 / 2;
	backgroundImage1.y = 2732 / 2;
	backgroundLayer.addChild(backgroundImage1);
	backgroundImage2 = LK.getAsset('backgroundPlaying1', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: -1
	});
	backgroundImage2.x = 2048 + 2048 / 2;
	backgroundImage2.y = 2732 / 2;
	backgroundLayer.addChild(backgroundImage2);
	// Initialize grid board shadow
	gridBoardSoil = LK.getAsset('gridBoardSoil', {
		anchorX: 0.5,
		anchorY: 0.5,
		visible: false,
		alpha: 0.75,
		tint: 0x000000
	});
	gridBoardSoil.x = 2048 / 2 + 2048 + gridBoardSoilOffset;
	gridBoardSoil.y = 2732 / 2 + gridBoardSoilOffset;
	game.addChild(gridBoardSoil);
	// Initialize grid board
	gridBoard = LK.getAsset('gridBoard', {
		anchorX: 0.5,
		anchorY: 0.5,
		visible: false
	});
	gridBoard.x = 2048 / 2 + 2048;
	gridBoard.y = 2732 / 2;
	game.addChild(gridBoard);
	game.addChild(middleLayer);
	game.addChild(frontLayer);
	// Create growGrass asset
	growGrass = LK.getAsset('growGrass', {
		anchorX: 0.5,
		anchorY: 0.5,
		visible: false,
		alpha: 0
	});
	growGrass.x = 2048 / 2 + growGrassOffsetX;
	growGrass.y = 2732 / 2;
	middleLayer.addChild(growGrass);
	// Initialize game assets and variables
	puzzleManager = new PuzzleManager(game);
	// Initialize level text
	levelText = new Text2("1", {
		size: 200,
		fill: 0x222222,
		weight: 1000,
		dropShadow: false
	});
	levelText.x = 2048 / 2 + 2048 + levelTextXOffset;
	levelText.y = 250;
	levelText.anchorX = 0.5;
	levelText.blendMode = 3;
	levelText.visible = false;
	// Add the level text to the game
	game.addChild(levelText);
	congratsText = new Text2(congratsMessages[3], {
		size: 300,
		fill: 0xFFFFFF,
		// Green Color 0x00cd20,
		weight: 1000,
		dropShadow: true
	});
	congratsText.x = congratsTextOffset + 2048;
	congratsText.y = 750;
	congratsText.anchorX = 0.5;
	congratsText.blendMode = 0;
	congratsText.visible = true;
	// Add the level text to the game
	game.addChild(congratsText);
	// Nb Moves Text
	nbMovesText = new Text2("0/0 moves", {
		size: 150,
		fill: 0xFFFFFF,
		// Green Color 0x00cd20,
		weight: 1000,
		align: 'center',
		dropShadow: true
	});
	nbMovesText.x = nbMovesTextOffset + 2048;
	nbMovesText.y = 1750;
	nbMovesText.anchorX = 0.5;
	nbMovesText.alpha = 0.55;
	nbMovesText.blendMode = 0;
	nbMovesText.visible = true;
	game.addChild(nbMovesText);
	// reset button
	resetButton = new ResetButton();
	resetButton.x = 2048 - 180;
	resetButton.y = 130;
	resetButton.visible = false;
	// Add the level text to the game
	game.addChild(resetButton);
	// Transition to menu state
	changeGameState(GAME_STATE.MENU);
}
function initMenuState() {
	log("Entering Menu State...");
	isPlaying = false;
	// Add logo to the menu state
	logoShadow = LK.getAsset('logo', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 1480,
		height: 1480,
		tint: 0x000000,
		alpha: 0.5
	});
	logoShadow.x = 2048 / 2 + 20;
	logoShadow.y = -logoShadow.height + 20; // Initialize logo out of screen 
	middleLayer.addChild(logoShadow);
	// Add logo to the menu state
	logo = LK.getAsset('logo', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 1480,
		height: 1480
	});
	logo.x = 2048 / 2;
	logo.y = -logo.height; // Initialize logo out of screen 
	middleLayer.addChild(logo);
	// logoMaze Shadow
	logoMazeShadow = LK.getAsset('logoMaze', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 1400,
		height: 400,
		tint: 0x000000,
		alpha: 0.5
	});
	logoMazeShadow.x = 2048 / 2 + 20;
	logoMazeShadow.y = -3000 + 20; // Initialize logo out of screen 
	middleLayer.addChild(logoMazeShadow);
	// logoMaze
	logoMaze = LK.getAsset('logoMaze', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 1400,
		height: 400
	});
	logoMaze.x = 2048 / 2;
	logoMaze.y = -3000; // Initialize logo out of screen 
	middleLayer.addChild(logoMaze);
	// Update any menu animations here
	// Define a simple tween function to animate the logo
	// Use the tween function to animate the logo
	tween(logoShadow, {
		x: 2048 / 2 + 20,
		// Center horizontally
		y: 2732 / 2 - 30 // Center vertically with offset
	}, {
		duration: 1000,
		easing: tween.bounceOut
	});
	tween(logo, {
		x: 2048 / 2,
		// Center horizontally
		y: 2732 / 2 - 50 // Center vertically with offset
	}, {
		duration: 1000,
		easing: tween.bounceOut,
		// Simple ease-out function
		onFinish: function onFinish() {
			isLogoAnimInFinished = true;
		}
	});
	tween(logoMaze, {
		x: 2048 / 2,
		// Center horizontally
		y: 2732 - 450
	}, {
		duration: 2000,
		easing: tween.bounceOut
	});
	tween(logoMazeShadow, {
		x: 2048 / 2 + 20,
		// Center horizontally
		y: 2732 - 450 + 20
	}, {
		duration: 2000,
		easing: tween.bounceOut
	});
	LK.getSound('logoBounce').play();
}
function handleMenuLoop() {}
function cleanMenuState() {
	if (logo) {
		tween(logo, {
			x: -logo.width
		}, {
			// Animate logo out to the left
			duration: 1000,
			// Duration of 1 second
			easing: tween.easeInOut,
			// Easing function for smooth animation
			onFinish: function onFinish() {
				logo.visible = false; // Hide logo after animation
				game.removeChild(logo); // Remove logo from game
				isLogoAnimOutFinished = true;
			}
		});
	}
	if (logoShadow) {
		tween(logoShadow, {
			x: -logoShadow.width
		}, {
			// Animate logo out to the left
			duration: 1000,
			// Duration of 1 second
			easing: tween.easeInOut,
			// Easing function for smooth animation
			onFinish: function onFinish() {
				logoShadow.visible = false; // Hide logo after animation
				game.removeChild(logoShadow); // Remove logo from game
			}
		});
	}
	if (logoMaze) {
		tween(logoMaze, {
			x: -logoMaze.width
		}, {
			// Animate logo out to the left
			duration: 1000,
			// Duration of 1 second
			easing: tween.easeInOut,
			// Easing function for smooth animation
			onFinish: function onFinish() {
				logoMaze.visible = false; // Hide logo after animation
				game.removeChild(logoMaze); // Remove logo from game
			}
		});
	}
	if (logoMazeShadow) {
		tween(logoMazeShadow, {
			x: -logoMazeShadow.width
		}, {
			// Animate logo out to the left
			duration: 1000,
			// Duration of 1 second
			easing: tween.easeInOut,
			// Easing function for smooth animation
			onFinish: function onFinish() {
				logoMazeShadow.visible = false; // Hide logo after animation
				game.removeChild(logoMazeShadow); // Remove logo from game
			}
		});
	}
}
function animateRoundEntrance() {
	// Animate background images to slide to the left
	tween(backgroundImage1, {
		x: -2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	tween(backgroundImage2, {
		x: 2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	tween(gridBoard, {
		x: 2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	tween(gridBoardSoil, {
		x: 2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	// Initialize grid with tiles from level configuration
	for (var row = 0; row < puzzleManager.gridSize; row++) {
		for (var col = 0; col < puzzleManager.gridSize; col++) {
			var tile = puzzleManager.grid[row][col];
			if (tile) {
				tween(tile, {
					x: tile.x - 2048 // Animate entrance
				}, {
					duration: 1000,
					easing: tween.easeInOut
				});
			}
		}
	}
	tween(levelText, {
		x: 2048 / 2 + ("" + puzzleManager.currentLevel).length * levelTextXOffset
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
}
function initNewRoundState() {
	log("Entering New Round State...");
	LK.getSound('levelStart').play();
	// Reset puzzle manager for new round
	if (puzzleManager) {
		puzzleManager.initPuzzle();
	}
	if (isGameFinished) {
		return;
	}
	// Show gridBoard 
	gridBoardSoil.visible = true;
	gridBoard.visible = true;
	levelText.visible = true;
	levelText.setText(puzzleManager.currentLevel);
	log("Levels is now ", puzzleManager.currentLevel);
	animateRoundEntrance();
	// After a short delay, transition to PLAYING state
	LK.setTimeout(function () {
		changeGameState(GAME_STATE.PLAYING);
	}, 1000);
}
function handleNewRoundLoop() {
	// Any pre-game animations can go here
}
function cleanNewRoundState() {
	// Clean up any new round state
}
function initPlayingState() {
	// Start the gameplay
	log("Entering Playing State...");
	isPlaying = true;
}
function handlePlayingLoop() {
	// Update game logic
	if (puzzleManager) {
		puzzleManager.grid.forEach(function (row) {
			row.forEach(function (tile) {
				if (tile) {
					tile.update();
				}
			});
		});
		// Check if level is complete
		if (puzzleManager.isComplete) {
			changeGameState(GAME_STATE.NEW_ROUND);
		}
	}
}
function animateRoundExit(callBack) {
	log("animateRoundExit...with callback=", !!callBack);
	// Animate background images to slide to the left
	backgroundImage1.x = 2048 + 2048 / 2;
	tween(backgroundImage1, {
		x: 2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	tween(backgroundImage2, {
		x: -2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	tween(gridBoard, {
		x: -2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	tween(gridBoardSoil, {
		x: -2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	// Initialize grid with tiles from level configuration
	for (var row = 0; row < puzzleManager.gridSize; row++) {
		for (var col = 0; col < puzzleManager.gridSize; col++) {
			var tile = puzzleManager.grid[row][col];
			if (tile) {
				tween(tile, {
					x: tile.x - 2048 // Animate 
				}, {
					duration: 1000,
					easing: tween.easeInOut
				});
			}
		}
	}
	if (growGrass) {
		tween(growGrass, {
			x: -2048 / 2
		}, {
			duration: 1000,
			easing: tween.easeInOut
		});
	}
	if (flowers && flowers.length) {
		for (var i = flowers.length - 1; i >= 0; i--) {
			if (flowers[i]) {
				var destX = flowers[i].x - 2048;
				tween(flowers[i], {
					x: destX
				}, {
					duration: 1000,
					easing: tween.easeInOut
				});
			}
		}
	}
	tween(levelText, {
		x: -2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut,
		onFinish: function onFinish() {
			callBack();
			//LK.setTimeout(function () {			}, 2300);
		}
	});
	// Calculate the number of stars based on moves
	var stars = calculateStarsScore();
	// Create a new RatingStars instance
	var ratingStars = new RatingStars(stars);
	// Set initial position off-screen
	ratingStars.x = 2048 + 2048 / 2;
	ratingStars.y = 2732 / 2;
	game.addChild(ratingStars);
	// Animate the RatingStars to the center of the screen
	tween(ratingStars, {
		x: 2048 / 2
	}, {
		duration: 1000,
		easing: tween.easeInOut,
		onFinish: function onFinish() {
			LK.setTimeout(function () {
				tween(ratingStars, {
					x: -2048 / 2
				}, {
					duration: 1000,
					easing: tween.easeInOut
				});
			}, 1700);
		}
	});
	congratsText.setText(congratsMessages[stars]);
	congratsText.visible = true;
	congratsText.x = congratsTextOffset + 2048;
	tween(congratsText, {
		x: congratsTextOffset
	}, {
		duration: 1000,
		easing: tween.easeInOut,
		onFinish: function onFinish() {
			LK.setTimeout(function () {
				tween(congratsText, {
					x: congratsTextOffset - 2048
				}, {
					duration: 1000,
					easing: tween.easeInOut,
					onFinish: function onFinish() {
						congratsText.visible = false;
					}
				});
			}, 1700);
		}
	});
	// Nb Moves text
	nbMovesText.setText("level " + puzzleManager.currentLevel + "\nmoves\n" + currentRoundMoves + "/" + levelMinimalMoves[puzzleManager.currentLevel]);
	nbMovesText.visible = true;
	nbMovesText.x = nbMovesTextOffset + 2048;
	tween(nbMovesText, {
		x: nbMovesTextOffset
	}, {
		duration: 1000,
		easing: tween.easeInOut,
		onFinish: function onFinish() {
			LK.setTimeout(function () {
				tween(nbMovesText, {
					x: nbMovesTextOffset - 2048
				}, {
					duration: 1000,
					easing: tween.easeInOut,
					onFinish: function onFinish() {
						nbMovesText.visible = false;
					}
				});
			}, 1700);
		}
	});
}
function cleanPlayingState() {
	log("cleanPlayingState...");
	isPlaying = false;
	isRoundExitAnimFinished = false;
	animateRoundExit(function () {
		log("animateRoundExit Callback...");
		// Hide growGrass
		if (growGrass) {
			growGrass.visible = false;
			growGrass.alpha = 0;
			growGrass.x = 2048 / 2 + growGrassOffsetX;
		}
		// Stop waterdrops animations if any
		if (waterDropInterval) {
			LK.clearInterval(waterDropInterval);
			waterDropInterval = null;
		}
		// Remove remaining waterdrops if any
		for (var i = waterDrops.length - 1; i >= 0; i--) {
			if (waterDrops[i].visible) {
				waterDrops[i].destroy();
				waterDrops.splice(i, 1);
			}
		}
		backgroundImage1.x = 2048 / 2;
		backgroundImage2.x = 2048 + 2048 / 2;
		gridBoard.x = 2048 + 2048 / 2;
		gridBoardSoil.x = 2048 + 2048 / 2 + gridBoardSoilOffset;
		levelText.x = 2048 + 2048 / 2;
		isRoundExitAnimFinished = true;
	});
}
function initScoreState() {
	// Show score screen
	log("Entering Score State");
	LK.setTimeout(finalAnimation, 2000);
}
function handleScoreLoop() {
	// Update any score animations
}
function cleanScoreState() {
	levelText.visible = false;
	if (finalAnimationInterval) {
		LK.clearInterval(finalAnimationInterval);
	}
}
function changeGameState(newState) {
	// Clean up current state
	log("Changing state from", currentState, "to", newState);
	switch (currentState) {
		case GAME_STATE.MENU:
			cleanMenuState();
			break;
		case GAME_STATE.NEW_ROUND:
			cleanNewRoundState();
			break;
		case GAME_STATE.PLAYING:
			cleanPlayingState();
			break;
		case GAME_STATE.SCORE:
			cleanScoreState();
			break;
	}
	// Initialize new state
	currentState = newState;
	switch (newState) {
		case GAME_STATE.MENU:
			initMenuState();
			break;
		case GAME_STATE.NEW_ROUND:
			if (!isLogoAnimOutFinished) {
				LK.setTimeout(initNewRoundState, 1000);
				break;
			}
			if (!isRoundExitAnimFinished) {
				log("Wait exit anim before new round...");
				LK.setTimeout(initNewRoundState, 2700);
				break;
			}
			initNewRoundState();
			break;
		case GAME_STATE.PLAYING:
			initPlayingState();
			break;
		case GAME_STATE.SCORE:
			initScoreState();
			break;
	}
}
/**** 
* Event Handlers
****/ 
game.down = function (x, y, obj) {
	if (!isLogoAnimInFinished) {
		return;
	}
	switch (currentState) {
		case GAME_STATE.MENU:
			changeGameState(GAME_STATE.NEW_ROUND);
			break;
		case GAME_STATE.PLAYING:
			startX = x;
			startY = y;
			isMouseDown = true;
			if (puzzleManager) {
				log("Down at ", x, y);
				puzzleManager.selectTile(x, y);
			}
			break;
		case GAME_STATE.SCORE:
			//changeGameState(GAME_STATE.MENU);
			if (isFinalAnimationFinished) {
				storage.currentLevel = 1; // Reset current level in storage
				LK.showGameOver();
			}
			break;
	}
};
game.move = function (x, y, obj) {
	if (currentState !== GAME_STATE.PLAYING || !isMouseDown || !puzzleManager || !puzzleManager.selectedTile) {
		return;
	}
	var deltaX = x - startX;
	var deltaY = y - startY;
	log("Move at ", x, y, " => dx,dy: ", deltaX, deltaY);
	// Only move if drag distance exceeds threshold
	if (Math.abs(deltaX) > dragThreshold || Math.abs(deltaY) > dragThreshold) {
		var direction = null;
		if (Math.abs(deltaX) > Math.abs(deltaY)) {
			// Allow some vertical drag for horizontal moves
			direction = deltaX > 0 ? 'right' : 'left';
		} else if (Math.abs(deltaY) > Math.abs(deltaX)) {
			// Allow some horizontal drag for vertical moves
			direction = deltaY > 0 ? 'down' : 'up';
		}
		log("=> Move Tile to direction:", direction);
		puzzleManager.moveTile(direction);
		isMouseDown = false; // Reset after move
	}
};
game.up = function (x, y, obj) {
	isMouseDown = false;
	if (currentState === GAME_STATE.PLAYING && puzzleManager) {
		log("Up at ", x, y);
		puzzleManager.selectedTile = null;
	}
};
/**** 
* Main Update Loop
****/ 
function update() {
	// Handle state-specific updates
	switch (currentState) {
		case GAME_STATE.MENU:
			handleMenuLoop();
			break;
		case GAME_STATE.NEW_ROUND:
			handleNewRoundLoop();
			break;
		case GAME_STATE.PLAYING:
			handlePlayingLoop();
			break;
		case GAME_STATE.SCORE:
			handleScoreLoop();
			break;
	}
	// Update water drops
	for (var i = waterDrops.length - 1; i >= 0; i--) {
		if (waterDrops[i].visible) {
			waterDrops[i].update();
		}
	}
}
// Tile Format Utilities
var TileFormat = {
	// Constants
	FIXED: 'F',
	MOBILE: 'M',
	TYPES: {
		START: 'S',
		END: 'E',
		VERTICAL: 'V',
		HORIZONTAL: 'H',
		CORNER: 'C',
		CROSS: 'X',
		BLANK: 'B'
	},
	// Parse a tile string into its components
	parse: function parse(tileStr) {
		if (!tileStr || tileStr === "") {
			return null;
		}
		var parts = tileStr.split('-');
		if (parts.length !== 3) {
			log("ERROR !!! Invalid tile format:", tileStr);
			return null;
		}
		return {
			fixed: parts[0] === this.FIXED,
			type: parts[1],
			rot: parseInt(parts[2]),
			rotation: parseInt(parts[2]) * (Math.PI / 2) // Convert to radians
		};
	},
	// Create a tile string from components
	create: function create(fixed, type, rotationIndex) {
		return "".concat(fixed ? this.FIXED : this.MOBILE, "-").concat(type, "-").concat(rotationIndex);
	},
	// Convert old tile type to new format
	convertOldType: function convertOldType(oldType) {
		switch (oldType) {
			case 'start':
				return this.TYPES.START;
			case 'end':
				return this.TYPES.END;
			case 'straightPipeV':
				return this.TYPES.VERTICAL;
			case 'straightPipeH':
				return this.TYPES.HORIZONTAL;
			case 'cornerPipe':
				return this.TYPES.CORNER;
			default:
				return this.TYPES.BLANK;
		}
	},
	// Convert rotation in radians to index (0-3)
	getRotationIndex: function getRotationIndex(radians) {
		if (typeof radians !== 'number') {
			return 0;
		}
		return Math.round(radians % (2 * Math.PI) / (Math.PI / 2)) % 4;
	},
	// Convert old level format to new format
	convertOldLevel: function convertOldLevel(oldLevel) {
		var newGrid = [];
		for (var row = 0; row < 4; row++) {
			newGrid[row] = [];
			for (var col = 0; col < 4; col++) {
				var oldTile = oldLevel.tiles[row][col];
				if (!oldTile) {
					newGrid[row][col] = "";
					continue;
				}
				var isFixed = oldLevel.fixedTiles && oldLevel.fixedTiles.includes("".concat(row, ",").concat(col));
				var type = this.convertOldType(oldTile);
				var rotation = oldLevel.rotations["".concat(row, ",").concat(col)] || 'up';
				var rotationIndex = this.getRotationIndex(rotation);
				newGrid[row][col] = this.create(isFixed, type, rotationIndex);
			}
		}
		return newGrid;
	}
};
// Update tile creation to use new format
function createTile(tileStr, row, col) {
	if (!tileStr || tileStr === "") {
		return null;
	}
	var tileData = TileFormat.parse(tileStr);
	if (!tileData) {
		return null;
	}
	var tile = new Tile();
	tile.type = tileData.type;
	tile.fixed = tileData.fixed;
	tile.gridRow = row;
	tile.gridCol = col;
	// Set type and create pipe graphics
	tile.setType(tileData.type, row, col, tileData.fixed);
	// Set initial rotation
	log("Set initial rotation:", tileData.rot);
	tile.setRotation(tileData.rot, tileData.rotation);
	// Set position
	tile.updatePosition(row, col);
	return tile;
}
/* All Corners : 
	0: [["F-E-0", "F-C-2", "F-C-3", ""], ["F-C-1", "F-C-0", "F-C-1", "F-C-3"], ["F-C-2", "F-C-3", "F-C-2", "F-C-0"], ["F-S-2", "F-C-1", "", "M-C-0"]],
	0: [["F-S-0", "F-C-2", "F-C-3", ""], ["F-C-1", "F-C-0", "F-C-1", "F-C-3"], ["F-C-2", "F-C-3", "F-C-2", "F-C-0"], ["F-E-2", "F-C-1", "", "M-C-0"]],
*/ 
var nbLevels = 20;
var levelMinimalMoves = {
	0: 33,
	1: 1,
	2: 6,
	3: 8,
	4: 10,
	5: 12,
	6: 15,
	7: 16,
	8: 16,
	9: 16,
	10: 16,
	11: 19,
	12: 24,
	13: 25,
	14: 26,
	15: 24,
	16: 27,
	17: 30,
	18: 33,
	19: 35,
	20: 38
};
var levelConfigs = {
	0: [["M-C-1", "M-C-2", "M-C-0", "F-S-0"], ["M-C-2", "M-V-0", "F-V-0", "M-C-0"], ["F-V-0", "", "", ""], ["F-E-2", "", "", "M-C-3"]],
	// Level 1 - 1
	1: [["F-S-0", "", "", ""], ["F-V-0", "", "", ""], ["F-V-0", "", "M-H-0", ""], ["F-C-1", "F-H-0", "", "F-E-1"]],
	// Level 2 - 6
	2: [["", "M-V-0", "F-E-0", ""], ["", "M-C-0", "F-V-0", ""], ["", "", "", ""], ["F-S-3", "F-H-0", "", ""]],
	// Level 3 - 8
	3: [["", "", "", ""], ["F-S-3", "F-C-3", "F-C-2", "F-E-1"], ["", "M-V-0", "F-V-0", ""], ["", "M-C-0", "M-C-1", ""]],
	// Level 4 - 10
	4: [["M-V-0", "F-C-2", "F-C-3", ""], ["M-C-1", "M-V-0", "F-V-0", "F-E-0"], ["F-S-0", "", "", "F-C-0"], ["F-C-1", "", "", "M-C-0"]],
	// Level 5 - 12
	5: [["M-C-2", "M-H-0", "F-H-0", "F-E-1"], ["M-C-0", "", "", ""], ["M-H-0", "M-C-1", "M-C-3", ""], ["F-S-3", "F-H-0", "M-V-0", ""]],
	// Level 6 - 15
	6: [["M-H-0", "", "M-V-0", "F-E-1"], ["", "", "", "M-H-0"], ["", "", "", ""], ["F-S-2", "", "M-V-0", "M-C-2"]],
	// Level 7 - 16
	7: [["M-C-3", "F-C-2", "", ""], ["", "F-E-2", "", ""], ["", "", "", "F-V-0"], ["M-V-0", "M-H-0", "", "F-S-2"]],
	// Level 8 - 16
	8: [["F-S-0", "F-E-0", "M-C-0", "F-C-3"], ["F-V-0", "F-C-1", "", "M-C-2"], ["M-H-0", "M-V-0", "", "M-V-0"], ["F-C-1", "M-V-0", "F-H-0", "F-C-0"]],
	// Level 9 - 16
	9: [["F-S-3", "M-V-0", "F-C-3", "M-H-0"], ["", "", "", ""], ["", "", "", ""], ["F-E-3", "M-V-0", "F-C-0", "M-H-0"]],
	// Level 10 - 16
	10: [["M-C-2", "M-V-0", "M-V-0", "F-C-3"], ["M-V-0", "M-H-0", "M-H-0", "M-V-0"], ["M-C-1", "F-S-0", "F-E-0", "M-C-0"], ["", "F-C-0", "F-C-1", ""]],
	// Level 11 - 19
	11: [["F-E-0", "M-V-0", "", "M-V-0"], ["M-H-0", "M-C-0", "", "F-S-0"], ["M-H-0", "", "", ""], ["F-C-1", "", "M-V-0", ""]],
	// Level 12 - 24
	12: [["F-S-0", "M-V-0", "M-C-3", "M-C-0"], ["F-C-1", "F-C-0", "M-C-2", "M-C-2"], ["F-E-0", "", "F-C-0", "M-C-1"], ["", "", "", ""]],
	// Level 13 - 25
	13: [["", "M-V-0", "M-H-0", "F-C-3"], ["", "F-S-0", "M-C-2", "F-E-2"], ["", "F-C-0", "", ""], ["M-H-0", "", "", "M-C-1"]],
	// Level 14 - 26
	14: [["M-V-0", "F-C-2", "F-H-0", "F-C-3"], ["", "F-E-2", "", ""], ["", "M-V-0", "F-S-0", ""], ["", "M-C-1", "M-C-0", ""]],
	// Level 15 - 27
	15: [["M-C-2", "F-S-0", "M-H-0", "M-C-0"], ["M-C-1", "", "", "F-V-0"], ["M-C-0", "", "", "F-V-0"], ["M-C-3", "F-E-3", "", ""]],
	// Level 16 - 29
	16: [["M-H-0", "F-C-2", "", "F-C-3"], ["M-V-0", "F-E-2", "", ""], ["", "", "F-S-3", "M-C-0"], ["", "", "", ""]],
	// Level 17 - 32
	17: [["F-E-3", "M-H-0", "M-C-2", "F-C-3"], ["", "", "F-C-2", "M-H-0"], ["M-H-0", "M-H-0", "M-C-0", ""], ["F-C-1", "M-H-0", "M-C-0", "F-S-1"]],
	// Level 18 - 36
	18: [["M-C-1", "M-C-2", "M-C-0", "F-S-0"], ["M-C-2", "M-V-0", "F-V-0", "M-C-0"], ["F-V-0", "", "", ""], ["F-E-2", "", "", "M-C-3"]],
	// Level 19 - 37
	19: [["M-C-2", "M-H-0", "M-H-0", "M-C-3"], ["M-V-0", "", "", ""], ["F-E-0", "F-C-1", "F-S-1", ""], ["F-C-1", "M-C-0", "M-H-0", ""]],
	// Level 20 - 38
	20: [["M-H-0", "", "", "F-S-1"], ["M-H-0", "F-V-0", "", ""], ["M-H-0", "F-C-0", "M-C-2", "M-C-2"], ["", "", "M-C-1", "F-E-1"]]
}; ===================================================================
--- original.js
+++ change.js
@@ -186,10 +186,10 @@
 /**** 
 * Game Code
 ****/ 
 function playAmbiantSounds() {
-	// Randomly choose between 'birds' and 'birds2'
-	var soundId = Math.random() < 0.5 ? 'birds' : 'birds2';
+	// Randomly choose between 'birds', 'birds2', and 'birds3'
+	var soundId = ['birds', 'birds2', 'birds3'][Math.floor(Math.random() * 3)];
 	// Play the chosen sound
 	LK.getSound(soundId).play();
 }
 // Set an interval to play ambient sounds every 5 to 10 seconds
:quality(85)/https://cdn.frvr.ai/675ee824eb1e8cb9a4112d39.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/675efc2dc09b2ac5a2d5e69c.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/675f0079c09b2ac5a2d5e6b6.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67608dd1dd0ea71dc49cb892.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/676091b3dd0ea71dc49cb8d6.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67609314dd0ea71dc49cb8dd.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67609417dd0ea71dc49cb8e6.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67609c5bdd0ea71dc49cb922.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6763c93400d0b55d46b5c989.png%3F3) 
 straigth zenith view square light wooden pallet. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/6763cda800d0b55d46b5c99a.png%3F3) 
 straigth zenith view square wooden pallet with big screws in each corner Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/6771626fb7d7d61ce947a750.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67716e32b7d7d61ce947a785.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6771b052f22179f74e2f8688.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6771b2bbf22179f74e2f86bd.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6775704d09f127fede9ecc8c.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/677572d509f127fede9eccc2.png%3F3) 
 simple yellow rating star. Modern video game style
:quality(85)/https://cdn.frvr.ai/6775b1194092767f3c012b3a.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/677667743ed42f57c4b74968.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/677668033ed42f57c4b74970.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/677668423ed42f57c4b74975.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/677668c13ed42f57c4b7497c.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/677803e49a90e8c7f345394a.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6784033d5446b29dfd6758af.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6784080d5446b29dfd67594b.png%3F3) 
 tileSlide
Sound effect
levelWon
Sound effect
tileBlocked
Sound effect
fountain
Sound effect
waterInPipe
Sound effect
bgMusic
Music
logoBounce
Sound effect
levelStart
Sound effect
bgMusic2
Music
flowerPop
Sound effect
roundResult
Sound effect
gameWon
Sound effect
resetSound
Sound effect
birds
Sound effect
birds2
Sound effect
birds3
Sound effect