User prompt
Please fix the bug: 'TypeError: colorList is undefined' in or related to this line: 'liquidGraphics.tint = colorList[color];' Line Number: 36
User prompt
create liquidStream only once, the just use init & stop each time
User prompt
create a global liquidStream variable to avoid create multiple liquidStreams
User prompt
at the end of liquidStream pouring, call stop
User prompt
Please fix the bug: 'Timeout.tick error: selectedTube is null' in or related to this line: 'if (liquidStream.height < selectedTube.height) {' Line Number: 179
User prompt
in pouring anime, after LiquidStream init, make its height grow to simulate pouring
User prompt
animate LiquidStream in init to simulate liquid pouring
User prompt
set LiquidStream width to 10
User prompt
add a stop function LiquidStream
User prompt
LiquidStream should have alpha = 0 initially, and alpha = 1 in init()
User prompt
during pour animation, init a liquidStream at the top of the selected tube (take rotation into account)
User prompt
in LiquidStream class add an init function that takes x, y and color
Code edit (2 edits merged)
Please save this source code
User prompt
create a new class liquidStream that uses the liquidBase asset
Code edit (2 edits merged)
Please save this source code
User prompt
start rotating the tube while moving
Code edit (1 edits merged)
Please save this source code
User prompt
when destination tube x is < to selected tube x don't go at x-100 but at x+ 100
Code edit (2 edits merged)
Please save this source code
User prompt
rotation direction should depend on the side of the destination tube
User prompt
`vial.y += selectionYOffset; // Restore each vial to its initial position` is wrong : use it only for the selected tube (move selectedTube = null; after the block)
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
in resetButton down call initRound with the distributionConfiguration parameter
User prompt
add an argument to initRound allowing to pass a distributionConfiguration to be reladed if provided
/**** * Classes ****/ var LiquidBase = Container.expand(function (color) { var self = Container.call(this); self.color = color; var liquidGraphics = self.attachAsset('liquidBase', { anchorX: 0.5, anchorY: 0.0 }); liquidGraphics.tint = colorList[color]; return self; }); var LiquidStream = Container.expand(function (color) { var self = Container.call(this); self.color = color; var liquidGraphics = self.attachAsset('liquidBase', { width: 10, anchorX: 0.5, anchorY: 0.0, alpha: 0 }); liquidGraphics.tint = colorList[color]; self.height = 0; self.init = function (x, y, color) { self.x = x; self.y = y; liquidGraphics.tint = colorList[color]; liquidGraphics.alpha = 1; }; self.stop = function () { liquidGraphics.alpha = 0; }; return self; }); var Vial = Container.expand(function () { var self = Container.call(this); // Attach graphical representation var tubeGraphics = self.attachAsset('tube', { anchorX: 0.5, anchorY: 0.0, alpha: 0.5 }); self.fillHeight = tubeGraphics.height - 25 - 100; self.baseLiquidY = tubeGraphics.height - 25; self.addChild(tubeGraphics); self.liquids = []; // Initialize functional representation self.addLiquid = function (liquid, ratio) { if (self.liquids.length > 0 && self.liquids[self.liquids.length - 1].color === liquid.color) { self.liquids[self.liquids.length - 1].ratio += ratio; } else { if (ratio > 0) { var newLiquid = { color: liquid.color, index: self.liquids.length, ratio: ratio }; self.liquids.push(newLiquid); } } }; self.removeLiquid = function (ratio) { if (self.liquids.length > 0) { var topLiquid = self.liquids[self.liquids.length - 1]; if (ratio >= topLiquid.ratio) { var removedLiquid = self.liquids.pop(); if (self.liquids.length > 0 && self.liquids[self.liquids.length - 1].color === removedLiquid.color) { self.liquids[self.liquids.length - 1].ratio += removedLiquid.ratio; } return removedLiquid; } else { topLiquid.ratio -= ratio; return { color: topLiquid.color, ratio: ratio }; } } return null; }; self.containsPoint = function (point) { var bounds = self.getBounds(); return point.x >= bounds.x && point.x <= bounds.x + bounds.width && point.y >= bounds.y + bounds.height; }; self.canPour = function (destinationTube) { if (self.liquids.length === 0) { return 0; // No liquid to pour } if (destinationTube.liquids.length === 0) { return 1; // Destination tube is empty, can pour all } var topLiquid = self.liquids[self.liquids.length - 1]; var destinationTopLiquid = destinationTube.liquids[destinationTube.liquids.length - 1]; if (topLiquid.color !== destinationTopLiquid.color) { return 0; // Different colors, cannot pour } var sameColorCount = 0; for (var i = self.liquids.length - 1; i >= 0; i--) { if (self.liquids[i].color === topLiquid.color) { sameColorCount++; } else { break; } } var totalRatio = destinationTube.liquids.reduce(function (sum, liquid) { return sum + liquid.ratio; }, 0); var availableSpace = 1 - totalRatio; return Math.min(sameColorCount, availableSpace) / sameColorCount; }; self.renderLiquids = function () { // Remove all existing liquid graphics for (var i = self.children.length - 1; i >= 0; i--) { if (self.children[i] !== tubeGraphics) { self.removeChild(self.children[i]); } } // Add new liquid graphics based on the current state of self.liquids var cumulativeHeight = 0; for (var j = 0; j < self.liquids.length; j++) { var liquid = self.liquids[j]; var liquidGraphics = self.attachAsset('liquidBase', { anchorX: 0.5, anchorY: 1.0 }); liquidGraphics.tint = colorList[liquid.color]; liquidGraphics.y = self.baseLiquidY - cumulativeHeight; // Adjust the offset to prevent liquids from appearing out of the vial bottom liquidGraphics.height = self.fillHeight * liquid.ratio; cumulativeHeight += liquidGraphics.height; self.addChildAt(liquidGraphics, 0); // Add debug text for liquidGraphics.y for index 0 and tubeGraphics.height if (j === 0) { updateDebugText('.y for index 0: ' + liquidGraphics.y + ' | .height: ' + tubeGraphics.height); } } }; self.down = function (x, y, obj) { var liquidInfo = self.liquids.map(function (liquid) { return liquid.color + ": " + liquid.ratio; }).join(", "); updateDebugText("Vial : " + liquidInfo); if (selectedTube === self) { self.scale.set(1, 1); // Reset the scale of the selected tube self.y += selectionYOffset; // Restore the tube to its initial position LK.getSound('drop').play(); selectedTube = null; // Unselect the tube } else if (selectedTube) { var pourRatio = selectedTube.canPour(self); if (pourRatio > 0) { // Phase 1: Move the selected vial near the destination vial, then rotate it var originalX = selectedTube.x; var originalY = selectedTube.y; var targetX = selectedTube.x < self.x ? self.x - 50 : self.x + 50; // Adjust the target position based on the side of the destination tube var targetY = self.y - 200; // Adjust the target position as needed var rotationDirection = selectedTube.x < self.x ? Math.PI / 4 : -Math.PI / 4; // Determine rotation direction var moveAndRotate = function moveAndRotate() { selectedTube.x = targetX; selectedTube.y = targetY; selectedTube.rotation = rotationDirection; // Rotate the selected tube // Initialize a liquidStream at the top of the selected tube if (selectedTube) { if (!liquidStream) { liquidStream = new LiquidStream(selectedTube.liquids[selectedTube.liquids.length - 1].color); game.addChild(liquidStream); } liquidStream.init(selectedTube.x, selectedTube.y - selectedTube.height / 2, selectedTube.liquids[selectedTube.liquids.length - 1].color); } // Make the LiquidStream height grow to simulate pouring var pourInterval = LK.setInterval(function () { if (selectedTube && liquidStream.height < selectedTube.height) { liquidStream.height += 10; // Adjust the growth rate as needed } else { LK.clearInterval(pourInterval); liquidStream.stop(); // Call stop function at the end of pouring liquidStream = null; // Reset liquidStream } }, 16); // 60 FPS // Proceed to the next phase after a short delay LK.setTimeout(function () { // Phase 2 and 3 will be implemented later if (selectedTube) { var liquid = selectedTube.removeLiquid(pourRatio); self.addLiquid(liquid, liquid.ratio); selectedTube.renderLiquids(); self.renderLiquids(); selectedTube.x = originalX; selectedTube.y = originalY; selectedTube.rotation = 0; // Reset rotation selectedTube.y += selectionYOffset; // Restore the selected tube to its initial position selectedTube.scale.set(1, 1); // Reset the scale of the previously selected tube selectedTube = null; } if (puzzleManager.checkWinCondition()) { // Play 'goodJob' sound LK.getSound('goodJob').play(); // Level up and initialize a new round currentLevel++; levelTxt.setText('Level: ' + currentLevel); initRound(); } else { if (isPlaying && self.liquids[self.liquids.length - 1].ratio === 1) { LK.getSound('yes').play(); } else { LK.getSound('drop').play(); } } }, 500); // Adjust the delay as needed }; // Move the selected tube to the target position var moveInterval = LK.setInterval(function () { if (Math.abs(selectedTube.x - targetX) < 5 && Math.abs(selectedTube.y - targetY) < 5) { LK.clearInterval(moveInterval); moveAndRotate(); } else { selectedTube.x += (targetX - selectedTube.x) * 0.1; selectedTube.y += (targetY - selectedTube.y) * 0.1; selectedTube.rotation += (rotationDirection - selectedTube.rotation) * 0.1; // Rotate the tube while moving } }, 16); // 60 FPS } else { if (selectedTube) { selectedTube.scale.set(1, 1); // Reset the scale if selection is wrong selectedTube.y += selectionYOffset; // Restore the previously selected tube to its initial position LK.getSound('wrong').play(); // Play wrong sound } selectedTube = null; } } else { LK.getSound('tap').play(); selectedTube = self; self.y -= selectionYOffset; // Move the selected tube up by selectionYOffset } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var liquidStream; function deepCopyVials(vials) { return (vials || []).map(function (vial) { var newVial = new Vial(); vial.liquids.forEach(function (liquid) { newVial.addLiquid(new LiquidBase(liquid.color), liquid.ratio); }); return newVial; }); } // Function to update debug text function updateDebugText(info) { if (debugTxt) { debugTxt.setText(info); } } // Create an instance of the PuzzleManager var PuzzleManager = function PuzzleManager() { var self = this; self.liquids = []; // Public property to store liquids self.initVials = function () { var nbVials = Math.min(currentLevel + 1, 10); self.vials = []; for (var i = 0; i < nbVials; i++) { self.vials.push(new Vial()); } var positions = []; var maxPerLine = 5; var spacingX = 2048 / (Math.min(nbVials, maxPerLine) + 1); var spacingY = 2732 / Math.ceil(nbVials / maxPerLine); for (var i = 0; i < nbVials; i++) { positions.push({ x: spacingX * (i % maxPerLine + 1), y: spacingY * Math.floor(i / maxPerLine) + 600 }); } for (var i = 0; i < self.vials.length; i++) { self.vials[i].x = positions[i].x; self.vials[i].y = positions[i].y; if (self.vials.length > 5) { if (i < maxPerLine) { self.vials[i].y -= 300; // Move the first row up by 300 } else { self.vials[i].y -= 600; // Move the second row up by 500 } } game.addChild(self.vials[i]); } }; // Initialize liquids self.initLiquids = function (level) { // Create and initialize liquid objects based on level if (level === 1) { self.liquids = [new LiquidBase('green')]; } else { if (level === 2) { self.liquids = [new LiquidBase('blue'), new LiquidBase('green')]; } else { self.liquids = []; for (var i = 0; i < level; i++) { var colorName = Object.keys(colorList)[i % Object.keys(colorList).length]; self.liquids.push(new LiquidBase(colorName)); } } } }; self.distributeLiquids = function () { // Helper function to shuffle an array function shuffle(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } } // Distribute initialized liquids into the provided vials if (self.vials.length === 0) { return; } if (currentLevel === 1) { self.vials[0].addLiquid(self.liquids[0], 0.25); self.vials[1].addLiquid(self.liquids[0], 0.75); } else if (currentLevel === 2) { self.vials[0].addLiquid(self.liquids[0], 0.5); self.vials[1].addLiquid(self.liquids[1], 0.5); self.vials[2].addLiquid(self.liquids[0], 0.5); self.vials[2].addLiquid(self.liquids[1], 0.5); } else { var totalPortions = (self.vials.length - 1) * (1 / BASE_LIQUID_RATIO); var portionsPerColor = Math.floor(totalPortions / self.liquids.length); var portions = self.liquids.flatMap(function (liquid) { return Array(portionsPerColor).fill(liquid.color); }); shuffle(portions); var attempts = 0; var maxAttempts = 1000; while (attempts < maxAttempts) { var portionIndex = 0; for (var i = 0; i < self.vials.length - 1; i++) { self.vials[i].liquids = []; var vialPortions = totalPortions / (self.vials.length - 1); for (var j = 0; j < vialPortions; j++) { var color = portions[portionIndex++]; var liquid = self.liquids.find(function (l) { return l.color === color; }); if (liquid) { self.vials[i].addLiquid(liquid, BASE_LIQUID_RATIO); } } } if (self.isSolvable()) { break; } attempts++; shuffle(portions); } lastDistributionConfig = deepCopyVials(self.vials); } }; self.isSolvable = function () { var visitedStates = []; var stateQueue = []; function serializeState(vials) { return vials.map(function (vial) { return vial.liquids.map(function (liquid) { return liquid.color + ":" + liquid.ratio; }).join(","); }).join("|"); } function isSolved(vials) { return vials.every(function (vial) { return vial.liquids.length === 0 || vial.liquids.every(function (liquid) { return liquid.color === vial.liquids[0].color; }); }); } function getNextStates(vials) { var nextStates = []; for (var i = 0; i < vials.length; i++) { for (var j = 0; j < vials.length; j++) { if (i !== j && vials[i].canPour(vials[j]) > 0) { var newVials = deepCopyVials(vials); var liquid = newVials[i].removeLiquid(newVials[i].canPour(newVials[j])); newVials[j].addLiquid(liquid, liquid.ratio); nextStates.push(newVials); } } } return nextStates; } stateQueue.push(deepCopyVials(self.vials)); visitedStates.push(serializeState(self.vials)); var maxDepth = 1000; var heuristicLimit = 100; var heuristicChecks = 0; var depth = 0; while (stateQueue.length > 0 && depth < maxDepth) { var currentState = stateQueue.shift(); if (isSolved(currentState)) { return true; } var nextStates = getNextStates(currentState); heuristicChecks++; if (heuristicChecks > heuristicLimit) { return false; } for (var k = 0; k < nextStates.length; k++) { var nextState = nextStates[k]; var serializedState = serializeState(nextState); if (visitedStates.indexOf(serializedState) === -1) { visitedStates.push(serializedState); stateQueue.push(nextState); } } depth++; } return false; }; self.reloadDistribution = function () { var savedVials = lastDistributionConfig; if (self.vials && Array.isArray(self.vials) && self.vials.length > 0) { self.vials.forEach(function (vial, i) { if (savedVials[i]) { vial.liquids = savedVials[i].liquids.map(function (liquid) { return { color: liquid.color, ratio: liquid.ratio }; }); vial.renderLiquids(); } }); } }; // Check for win conditions self.checkWinCondition = function () { // Implement logic to check if the puzzle is solved var colorArray = []; if (!self.vials || self.vials.length === 0) { return false; // No vials to check } for (var i = 0; i < self.vials.length; i++) { if (self.vials[i].liquids.length > 0) { var firstColor = self.vials[i].liquids[0].color; if (colorArray.indexOf(firstColor) !== -1) { return false; // Color already found in another vial } colorArray.push(firstColor); for (var j = 1; j < self.vials[i].liquids.length; j++) { if (self.vials[i].liquids[j].color !== firstColor) { return false; // Different colors in the same vial } } } } return true; }; // Initialize the puzzle manager self.init = function (level) { self.initVials(); self.initLiquids(level); self.distributeLiquids(); for (var i = 0; i < self.vials.length; i++) { self.vials[i].renderLiquids(); } updateDebugText('Puzzle Solvable: ' + self.isSolvable()); }; return self; }; var BASE_LIQUID_RATIO = 0.25; var currentLevel = 4; // TEMP DEBUG 1; var score = 0; var isPlaying = false; var roundDuration = 60; // Global variable for round duration var distributionConfigurations = []; var lastDistributionConfig = null; var scoreTxt; var timerTxt; var debugTxt; var restartButton; var colorList = { blue: 0x0000FF, // DodgerBlue green: 0x00FF00, // LimeGreen red: 0xFF0000, // OrangeRed white: 0xFFFFFF, // White yellow: 0xFFFF00, // Yellow purple: 0x800080, // Purple cyan: 0x00FFFF, // Cyan magenta: 0xFF00FF, // Magenta orange: 0xFFA500, // Orange pink: 0xFFC0CB // Pink }; var selectionYOffset = 300; // Function to update score function updateScore(newScore) { score = newScore; if (scoreTxt) { if (newScore) { scoreTxt.setText(newScore.toString()); } else { scoreTxt.setText('0'); } } } // Function to update timer function updateTimer(newTime) { timeLeft = newTime; timerTxt.setText(timeLeft.toString()); } // Game update function game.update = function () { // Update timer if (LK.ticks % 60 == 0) { // Decrease time every second updateTimer(timeLeft - 1); if (timeLeft <= 0) { if (selectedTube) { selectedTube.scale.set(1, 1); // Reset the scale of the selected tube // Update debug text with current time and score updateDebugText('Time: ' + timeLeft + ' | Score: ' + score); } initRound(true); // Reset the time counter updateTimer(roundDuration); } } }; function initUI() { scoreTxt = new Text2('0', { size: 140, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); timerTxt = new Text2('60', { size: 140, fill: "#ffffff" }); timerTxt.anchor.set(1, 0); LK.gui.topRight.addChild(timerTxt); // Create debug text debugTxt = new Text2('Debug Info', { size: 50, fill: "#FFFFFF" }); debugTxt.anchor.set(0, 1); LK.gui.bottomLeft.addChild(debugTxt); levelTxt = new Text2('Level: ' + currentLevel, { size: 100, fill: "#ffffff" }); levelTxt.anchor.set(1, 1); LK.gui.bottomRight.addChild(levelTxt); restartButton = LK.getAsset('restartRound', { anchorX: 0.5, anchorY: 0.5, x: 612, y: 130 }); game.addChild(restartButton); restartButton.down = function () { LK.getSound('reset').play(); initRound(true); }; } function initRound(reset) { // Remove previous vials if (reset && lastDistributionConfig) { console.log("Reloading :", lastDistributionConfig); puzzleManager.reloadDistribution(); } else { if (puzzleManager.vials) { for (var i = 0; i < puzzleManager.vials.length; i++) { game.removeChild(puzzleManager.vials[i]); } } puzzleManager.init(currentLevel); lastDistributionConfig = deepCopyVials(puzzleManager.vials); console.log("Level saved :", lastDistributionConfig); } selectedTube = null; if (puzzleManager.vials && puzzleManager.vials.length > 0) { puzzleManager.vials.forEach(function (vial) { vial.scale.set(1, 1); // Reset the scale of each vial }); } selectedTube = null; timeLeft = roundDuration; // Initialize the timer with the global round duration } // Function to initialize the game function initializeGame() { score = 0; // Initialize the score with 0 updateScore(score); initUI(); puzzleManager = new PuzzleManager(); initRound(); updateTimer(timeLeft); isPlaying = true; } initializeGame();
===================================================================
--- original.js
+++ change.js
@@ -158,19 +158,22 @@
selectedTube.y = targetY;
selectedTube.rotation = rotationDirection; // Rotate the selected tube
// Initialize a liquidStream at the top of the selected tube
if (selectedTube) {
- var liquidStream = new LiquidStream(selectedTube.liquids[selectedTube.liquids.length - 1].color);
+ if (!liquidStream) {
+ liquidStream = new LiquidStream(selectedTube.liquids[selectedTube.liquids.length - 1].color);
+ game.addChild(liquidStream);
+ }
liquidStream.init(selectedTube.x, selectedTube.y - selectedTube.height / 2, selectedTube.liquids[selectedTube.liquids.length - 1].color);
- game.addChild(liquidStream);
}
// Make the LiquidStream height grow to simulate pouring
var pourInterval = LK.setInterval(function () {
if (selectedTube && liquidStream.height < selectedTube.height) {
liquidStream.height += 10; // Adjust the growth rate as needed
} else {
LK.clearInterval(pourInterval);
liquidStream.stop(); // Call stop function at the end of pouring
+ liquidStream = null; // Reset liquidStream
}
}, 16); // 60 FPS
// Proceed to the next phase after a short delay
LK.setTimeout(function () {
@@ -240,8 +243,9 @@
/****
* Game Code
****/
+var liquidStream;
function deepCopyVials(vials) {
return (vials || []).map(function (vial) {
var newVial = new Vial();
vial.liquids.forEach(function (liquid) {
Basic white Restart icon (rounded arrow). UI
Une classe d’une école de sorcière sans les élèves.
un sablier de sorcière.
a bubble.
exploded broken glass
Yound generously beautifull teacher witch smiling, with glasses, a black witch hat, holding a little brown book in her hands and looking at the camera. wearing light black clothes. Torso head and hat should appear.
tap
Sound effect
drop
Sound effect
reset
Sound effect
wrong
Sound effect
yes
Sound effect
goodJob
Sound effect
pouring
Sound effect
welcome
Sound effect
rememberTheRules
Sound effect
letsgo
Sound effect
hurryUp
Sound effect
boom
Sound effect
tryAgain
Sound effect
rainbowBoom
Sound effect
youDidIt
Sound effect
letmetry
Sound effect
rainbowMix
Sound effect
thankYou1
Sound effect
thankYou2
Sound effect
thankYou3
Sound effect
thankYou4
Sound effect
bonusTime
Sound effect