Code edit (5 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'type')' in or related to this line: 'log('Flowing through pipe at:', key, 'Tile type:', tile.type);' Line Number: 510
User prompt
add log in flowThroughPipe
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught ReferenceError: _slicedToArray is not defined' in or related to this line: 'var _pos$split$map = pos.split(',').map(Number),' Line Number: 283
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: setInterval is not a function' in or related to this line: 'var checkFlowInterval = setInterval(function () {' Line Number: 628
User prompt
in animateWaterFlow, set flow to true in start tile and wait until flow returns to false; then do same for the next tile until the end pipe tile.
User prompt
When puzzle is solved we will run an animation to simulate water flowing from start pipe to end pipe. Add a new function for flow animation; call it when puzzle is solved;
User prompt
prevent moves when !isPlaying
User prompt
add an isPlaying global defaulting to true; set it to false when level is solved
User prompt
Please fix the bug: 'Uncaught TypeError: Set is not a constructor' in or related to this line: 'var visited = new Set();' Line Number: 438
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'currentLevel')' in or related to this line: 'levelText = new Text2('Level: ' + puzzleManager.currentLevel, {' Line Number: 726
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'setText')' in or related to this line: 'levelText.setText('Level: ' + self.currentLevel + ' Solved!');' Line Number: 592
User prompt
Please fix the bug: 'Uncaught ReferenceError: levelText is not defined' in or related to this line: 'levelText.setText('Level: ' + self.currentLevel + ' Solved!');' Line Number: 592
User prompt
Please fix the bug: 'Uncaught TypeError: Set is not a constructor' in or related to this line: 'var visited = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Set();' Line Number: 608
User prompt
Please fix the bug: 'Uncaught TypeError: Set is not a constructor' in or related to this line: 'var visited = new Set();' Line Number: 424
User prompt
implement checkWinCondition
User prompt
ok, now after each player move, check if puzzle is solved. if so add "Solved !" in levelText
User prompt
add a text under the board to show current level number
Code edit (14 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'undefined')' in or related to this line: 'var baseTileAsset = type == 'start' || type == 'end' || self.levelConfigs[self.currentLevel] && self.levelConfigs[self.currentLevel].fixedTiles && self.levelConfigs[self.currentLevel].fixedTiles.includes(self.position.x + ',' + self.position.y) ? 'baseTile' : 'baseMobileTile';' Line Number: 40
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'undefined')' in or related to this line: 'var baseTileAsset = type == 'start' || type == 'end' || self.levelConfigs[self.currentLevel] && self.levelConfigs[self.currentLevel].fixedTiles.includes(self.position.x + ',' + self.position.y) ? 'baseTile' : 'baseMobileTile';' Line Number: 40
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'undefined')' in or related to this line: 'var baseTileAsset = type == 'start' || type == 'end' || self.levelConfigs[self.currentLevel].fixedTiles.includes(self.position.x + ',' + self.position.y) ? 'baseTile' : 'baseMobileTile';' Line Number: 40
/****
* Classes
****/
var Tile = Container.expand(function () {
var self = Container.call(this);
// Properties
self.type = 'empty';
self.baseTint = 0x8FBE00;
self.baseTintLight = 0x5ED000;
self.maxWaterSize = 250;
self.flowSpeed = 4;
self.flow = false;
self.connections = [];
self.position = {
x: 0,
y: 0
};
self.rotation = 0;
// Methods
self.setType = function (type, x, y) {
self.type = type;
var baseTileAsset = type == 'start' || type == 'end' || puzzleManager && puzzleManager.levelConfigs[puzzleManager.currentLevel].fixedTiles.includes(x + ',' + y) ? 'baseTile' : 'baseMobileTile';
if (type == 'empty') {
return;
}
// Attach baseTile asset
self.baseTile = self.attachAsset(baseTileAsset, {
anchorX: 0.5,
anchorY: 0.5,
width: tileSize,
height: tileSize,
tint: 0xFFFFFF
});
self.width = tileSize;
self.height = tileSize;
switch (type) {
case 'start':
self.attachAsset('startPipeAsset', {
anchorX: 0.5,
anchorY: 0.5,
x: 20,
y: 55,
tint: self.baseTint
});
self.attachAsset('vane', {
anchorX: 0.5,
anchorY: 0.5,
x: 20,
y: 15,
tint: self.baseTint
});
self.water = self.attachAsset('waterV', {
anchorX: 0.5,
anchorY: 0,
x: -0,
y: -120,
height: 0,
visible: false,
dir: ''
});
break;
case 'end':
self.attachAsset('endPipeAsset', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
y: 55,
tint: self.baseTint
});
self.water = self.attachAsset('waterV', {
anchorX: 0.5,
anchorY: 0,
x: -0,
y: -120,
height: 0,
visible: false,
dir: ''
});
break;
case 'crossPipe':
self.attachAsset('straightPipeHAsset', {
anchorX: 0.5,
anchorY: 0.5,
y: -40,
tint: self.baseTint
});
self.attachAsset('straightPipeHAsset', {
anchorX: 0.5,
anchorY: 0.5,
scaleY: -1,
y: 30,
tint: self.baseTint
});
self.attachAsset('straightPipeVAsset', {
anchorX: 0.5,
anchorY: 0.5,
x: -30,
tint: self.baseTint
});
self.attachAsset('straightPipeVAsset', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: -1,
x: 30,
tint: self.baseTint
});
self.water = self.attachAsset('waterV', {
anchorX: 0.5,
anchorY: 0,
x: -0,
y: -120,
height: 0,
visible: false,
dir: ''
});
break;
case 'straightPipeH':
self.attachAsset('straightPipeHAsset', {
anchorX: 0.5,
anchorY: 0.5,
y: -40,
tint: self.baseTint
});
self.attachAsset('straightPipeHAsset', {
anchorX: 0.5,
anchorY: 0.5,
scaleY: -1,
y: 30,
tint: self.baseTint
});
self.water = self.attachAsset('waterH', {
anchorX: 0,
anchorY: 0.5,
x: -130,
y: -5,
width: 0,
visible: false,
dir: 'lr'
});
break;
case 'straightPipeV':
self.attachAsset('straightPipeVAsset', {
anchorX: 0.5,
anchorY: 0.5,
x: -30,
tint: self.baseTint
});
self.attachAsset('straightPipeVAsset', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: -1,
x: 30,
tint: self.baseTint
});
self.water = self.attachAsset('waterV', {
anchorX: 0.5,
anchorY: 0,
x: -0,
y: -120,
height: 0,
visible: false,
dir: 'tb'
});
break;
case 'cornerPipe':
self.attachAsset('cornerPipeAsset', {
anchorX: 0.5,
anchorY: 0.5,
x: -60,
y: -60,
tint: self.baseTintLight
});
self.water = self.attachAsset('waterV', {
anchorX: 0.5,
anchorY: 0,
x: -0,
y: -120,
height: 0,
visible: false,
dir: ''
});
break;
}
};
self.updatePosition = function (x, y) {
self.position.x = x;
self.position.y = y;
if (self.baseTile) {
self.baseTile.width = tileSize;
self.baseTile.height = tileSize;
}
self.x = x * tileSize + gridBoard.x - gridBoard.width / 2 + tileSize / 2 + boardOffsetX;
self.y = y * tileSize + gridBoard.y - gridBoard.height / 2 + tileSize / 2 + boardOffsetY;
log('Tile index:', x, y, 'Tile position:', self.x, self.y, 'Tile dimensions:', self.width, self.height, ' self:', self);
};
self.setRotation = function (direction) {
switch (direction) {
case 'left':
self.rotation = Math.PI * 0.5;
break;
case 'right':
self.rotation = -Math.PI * 0.5;
break;
case 'up':
self.rotation = 0;
break;
case 'down':
self.rotation = Math.PI;
break;
}
log('Tile position:', self.x, self.y, 'Tile rotation:', self.rotation);
};
self.update = function () {
if (self.water && self.flow) {
if (!self.water.height || !self.water.width) {
self.water.visible = true;
}
var heightValue = LK.ticks * self.flowSpeed;
if (self.water.dir != '' && heightValue < self.maxWaterSize) {
if (self.water.dir == 'tb') {
self.water.height = heightValue;
}
if (self.water.dir == 'lr') {
self.water.width = heightValue;
}
} else {
self.flow = false;
}
}
};
self.setType(self.type);
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
var PuzzleManager = function PuzzleManager() {
var self = this;
// Properties
self.currentLevel = 1;
self.maxLevels = 30;
self.grid = [];
self.gridSize = 4;
self.selectedTile = null;
self.waterFlowing = false;
// Level configurations
self.levelConfigs = {
1: {
tiles: [['start', 'straightPipeV', 'straightPipeV', 'cornerPipe'], [null, null, null, 'straightPipeH'], [null, null, 'straightPipeH', null], [null, null, null, 'end']],
rotations: {
'0,3': 'left',
'3,3': 'left'
},
fixedTiles: ['0,0', '0,1', '0,2', '0,3', '1,3', '3,3'] // All tiles fixed except 2,2
},
2: {
tiles: [['start', 'straightPipeH', null, null], [null, 'cornerPipe', null, null], [null, 'straightPipeV', 'cornerPipe', null], [null, null, null, 'end']],
rotations: {
'0,1': 'right',
'1,1': 'down',
'2,2': 'left'
}
}
// Add more levels here
};
// Initialize the puzzle for current level
self.initPuzzle = function () {
log("Initializing puzzle for level", self.currentLevel);
// Clear existing grid
self.grid = [];
// Get level config
var config = self.levelConfigs[self.currentLevel];
if (!config) {
log("No configuration for level", self.currentLevel);
return;
}
// Initialize grid
for (var i = 0; i < self.gridSize; i++) {
self.grid[i] = [];
for (var j = 0; j < self.gridSize; j++) {
var tile = new Tile();
if (config.tiles[i] && config.tiles[i][j]) {
tile.setType(config.tiles[i][j], i, j);
}
tile.updatePosition(i, j);
self.grid[i][j] = tile;
game.addChild(tile);
}
}
// Apply rotations
if (config.rotations) {
for (var pos in config.rotations) {
var _pos$split$map = pos.split(',').map(Number),
x = _pos$split$map[0],
y = _pos$split$map[1];
self.grid[x][y].setRotation(config.rotations[pos]);
}
}
};
// Check if the current puzzle is solved
self.checkWinCondition = function () {
// Find start position
var startX = -1,
startY = -1;
for (var i = 0; i < self.gridSize; i++) {
for (var j = 0; j < self.gridSize; j++) {
if (self.grid[i][j].type === 'start') {
startX = i;
startY = j;
break;
}
}
if (startX !== -1) {
break;
}
}
// Use a Set to track visited positions
var visited = [];
// Check if we can reach the end tile
return this.canReachEnd(startX, startY + 1, visited); // Start flowing down from start
};
self.canReachEnd = function (x, y, visited) {
// Check bounds
if (x < 0 || x >= self.gridSize || y < 0 || y >= self.gridSize) {
return false;
}
// Create position key
var key = x + ',' + y;
if (visited.includes(key)) {
return false;
}
visited.push(key);
var tile = self.grid[x][y];
if (!tile || tile.type === 'empty') {
return false;
}
// If we reached the end, success!
if (tile.type === 'end') {
return true;
}
// Get next possible positions based on current tile type and rotation
var nextPositions = [];
switch (tile.type) {
case 'straightPipeV':
nextPositions.push([x, y - 1], [x, y + 1]); // Up and down
break;
case 'straightPipeH':
nextPositions.push([x - 1, y], [x + 1, y]); // Left and right
break;
case 'cornerPipe':
// Check rotation to determine flow direction
if (tile.rotation === Math.PI * 0.5) {
// Left rotation
nextPositions.push([x, y - 1], [x + 1, y]); // Up and right
}
// Add other rotations as needed
break;
case 'crossPipe':
nextPositions.push([x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]); // All directions
break;
}
// Try each possible next position
for (var i = 0; i < nextPositions.length; i++) {
var nextX = nextPositions[i][0];
var nextY = nextPositions[i][1];
if (this.canReachEnd(nextX, nextY, visited)) {
return true;
}
}
return false;
};
// Handle tile selection and movement
self.selectTile = function (x, y) {
// Convert screen coordinates to grid coordinates
if (!isPlaying) {
return;
}
var gridX = Math.floor((x - (gridBoard.x - gridBoard.width / 2 + tileSize / 2)) / tileSize);
var gridY = Math.floor((y - (gridBoard.y - gridBoard.height / 2 + tileSize / 2)) / tileSize);
// Check if coordinates are within grid
if (gridX >= 0 && gridX < self.gridSize && gridY >= 0 && gridY < self.gridSize) {
var tile = self.grid[gridX][gridY];
var key = gridX + ',' + gridY;
// Check if tile exists, is not empty, not start/end, and not fixed
if (tile && tile.type !== 'empty' && tile.type !== 'start' && tile.type !== 'end' && !self.levelConfigs[self.currentLevel].fixedTiles.includes(key)) {
self.selectedTile = {
x: gridX,
y: gridY,
tile: tile
};
// Check possible moves
self.checkPossibleMoves();
}
}
};
self.checkPossibleMoves = function () {
if (!self.selectedTile) {
return;
}
var x = self.selectedTile.x;
var y = self.selectedTile.y;
var possibleMoves = [];
// Check each direction
// Right
if (x < self.gridSize - 1 && self.grid[x + 1][y].type === 'empty') {
possibleMoves.push('right');
}
// Left
if (x > 0 && self.grid[x - 1][y].type === 'empty') {
possibleMoves.push('left');
}
// Down
if (y < self.gridSize - 1 && self.grid[x][y + 1].type === 'empty') {
possibleMoves.push('down');
}
// Up
if (y > 0 && self.grid[x][y - 1].type === 'empty') {
possibleMoves.push('up');
}
self.selectedTile.possibleMoves = possibleMoves;
};
self.moveTile = function (direction) {
if (!isPlaying || !self.selectedTile || !self.selectedTile.possibleMoves.includes(direction)) {
return;
}
var oldX = self.selectedTile.x;
var oldY = self.selectedTile.y;
var newX = oldX;
var newY = oldY;
// Calculate new position
switch (direction) {
case 'right':
newX++;
break;
case 'left':
newX--;
break;
case 'down':
newY++;
break;
case 'up':
newY--;
break;
}
// Swap tiles
var movingTile = self.grid[oldX][oldY];
var emptyTile = self.grid[newX][newY];
// Update grid
self.grid[newX][newY] = movingTile;
self.grid[oldX][oldY] = emptyTile;
// Update positions
movingTile.updatePosition(newX, newY);
emptyTile.updatePosition(oldX, oldY);
// Clear selection
self.selectedTile = null;
// Check if puzzle is solved
if (self.checkWinCondition()) {
self.startWaterFlow();
levelText.setText('Level: ' + self.currentLevel + ' Solved!');
isPlaying = false;
}
};
self.startWaterFlow = function () {
// Find start position
var startX = -1,
startY = -1;
for (var i = 0; i < self.gridSize; i++) {
for (var j = 0; j < self.gridSize; j++) {
if (self.grid[i][j].type === 'start') {
startX = i;
startY = j;
break;
}
}
if (startX !== -1) {
break;
}
}
// Start the flow animation from the start tile
self.flowThroughPipe(startX, startY, []);
};
self.flowThroughPipe = function (x, y, visited) {
var key = x + ',' + y;
if (visited.includes(key)) {
log('Already visited tile at', x, y);
return;
}
visited.push(key);
var tile = self.grid[x][y];
if (!tile || tile.type === 'empty') {
log('Invalid or empty tile at', x, y);
return;
}
log('Flowing through tile:', tile.type, 'at position:', x, y);
// Enable flow for current tile and set water direction
tile.flow = true;
if (tile.water) {
tile.water.visible = true;
// Set water direction based on tile type
switch (tile.type) {
case 'start':
case 'straightPipeV':
tile.water.dir = 'tb';
break;
case 'straightPipeH':
tile.water.dir = 'lr';
break;
case 'cornerPipe':
// Set direction based on rotation
if (tile.rotation === Math.PI * 0.5) {
tile.water.dir = 'tr'; // top to right
}
// Add other rotation cases here
break;
case 'crossPipe':
// Set direction based on where water came from
if (self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
tile.water.dir = 'tb';
} else if (self.grid[x - 1][y] && self.grid[x - 1][y].flow) {
tile.water.dir = 'lr';
}
break;
}
log('Set water direction for tile at', x, y, 'to:', tile.water.dir);
} else {
log('Warning: No water asset found for tile at', x, y);
}
// Wait for flow animation to complete before moving to next tile
LK.setTimeout(function () {
// Get next tile position based on current tile type
var nextPos = self.getNextPosition(x, y, tile);
if (nextPos) {
log('Moving to next tile at:', nextPos[0], nextPos[1]);
self.flowThroughPipe(nextPos[0], nextPos[1], visited);
} else {
log('No next position found for tile at', x, y);
}
}, 500); // Wait 500ms before flowing to next tile
};
self.getNextPosition = function (x, y, tile) {
switch (tile.type) {
case 'start':
return [x, y + 1];
// Flow down from start
case 'straightPipeV':
// Check if water came from top or bottom
var nextY = self.grid[x][y - 1] && self.grid[x][y - 1].flow ? y + 1 : y - 1;
return [x, nextY];
case 'straightPipeH':
// Check if water came from left or right
var nextX = self.grid[x - 1][y] && self.grid[x - 1][y].flow ? x + 1 : x - 1;
return [x, nextX];
case 'cornerPipe':
if (tile.rotation === Math.PI * 0.5) {
// Left rotation
// If water came from top, go right; if from right, go up
if (self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
return [x + 1, y];
} else if (self.grid[x + 1][y] && self.grid[x + 1][y].flow) {
return [x, y - 1];
}
}
// Add other rotations as needed
break;
case 'crossPipe':
// Determine direction based on where water came from
if (self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
return [x, y + 1];
}
if (self.grid[x][y + 1] && self.grid[x][y + 1].flow) {
return [x, y - 1];
}
if (self.grid[x - 1][y] && self.grid[x - 1][y].flow) {
return [x + 1, y];
}
if (self.grid[x + 1][y] && self.grid[x + 1][y].flow) {
return [x - 1, y];
}
break;
}
return null;
};
return self;
};
var debug = true;
function log() {
if (debug) {
console.log.apply(console, arguments);
}
}
// Game constants
var tileSize = 400;
var boardOffsetX = 90;
var boardOffsetY = 90;
// Game state
var isPlaying = true;
var gridBoard;
var puzzleManager;
var isMouseDown = false;
var startX = 0;
var startY = 0;
var selectedTile = null;
var dragThreshold = 20;
// Initialize game board
gridBoard = LK.getAsset('gridBoard', {
anchorX: 0.5,
anchorY: 0.5
});
gridBoard.x = 2048 / 2;
gridBoard.y = 2732 / 2;
game.addChild(gridBoard);
// Event handlers
game.down = function (x, y, obj) {
startX = x;
startY = y;
isMouseDown = true;
if (puzzleManager) {
puzzleManager.selectTile(x, y);
}
};
game.move = function (x, y, obj) {
if (!isPlaying || !isMouseDown || !puzzleManager || !puzzleManager.selectedTile) {
return;
}
var deltaX = x - startX;
var deltaY = y - startY;
// 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)) {
direction = deltaX > 0 ? 'right' : 'left';
} else {
direction = deltaY > 0 ? 'down' : 'up';
}
puzzleManager.moveTile(direction);
isMouseDown = false; // Reset after move
}
};
game.up = function (x, y, obj) {
isMouseDown = false;
if (puzzleManager) {
puzzleManager.selectedTile = null;
}
};
var levelText;
// Initialize game
function initializeGame() {
puzzleManager = new PuzzleManager();
puzzleManager.initPuzzle();
// Create a text element to display the current level number
levelText = new Text2('Level: ' + puzzleManager.currentLevel, {
size: 100,
fill: "#ffffff"
});
// Set the position of the level text under the board
levelText.anchor.set(0.5, 0);
levelText.x = gridBoard.x;
levelText.y = gridBoard.y + gridBoard.height / 2 + 50;
// Add the level text to the game
game.addChild(levelText);
}
// Start the game
initializeGame(); ===================================================================
--- original.js
+++ change.js
@@ -488,26 +488,61 @@
self.flowThroughPipe(startX, startY, []);
};
self.flowThroughPipe = function (x, y, visited) {
var key = x + ',' + y;
- var tile = self.grid[x][y];
- log('Flowing through pipe at:', key, 'Tile type:', tile.type);
if (visited.includes(key)) {
+ log('Already visited tile at', x, y);
return;
}
visited.push(key);
var tile = self.grid[x][y];
if (!tile || tile.type === 'empty') {
+ log('Invalid or empty tile at', x, y);
return;
}
- // Enable flow for current tile
+ log('Flowing through tile:', tile.type, 'at position:', x, y);
+ // Enable flow for current tile and set water direction
tile.flow = true;
+ if (tile.water) {
+ tile.water.visible = true;
+ // Set water direction based on tile type
+ switch (tile.type) {
+ case 'start':
+ case 'straightPipeV':
+ tile.water.dir = 'tb';
+ break;
+ case 'straightPipeH':
+ tile.water.dir = 'lr';
+ break;
+ case 'cornerPipe':
+ // Set direction based on rotation
+ if (tile.rotation === Math.PI * 0.5) {
+ tile.water.dir = 'tr'; // top to right
+ }
+ // Add other rotation cases here
+ break;
+ case 'crossPipe':
+ // Set direction based on where water came from
+ if (self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
+ tile.water.dir = 'tb';
+ } else if (self.grid[x - 1][y] && self.grid[x - 1][y].flow) {
+ tile.water.dir = 'lr';
+ }
+ break;
+ }
+ log('Set water direction for tile at', x, y, 'to:', tile.water.dir);
+ } else {
+ log('Warning: No water asset found for tile at', x, y);
+ }
// Wait for flow animation to complete before moving to next tile
LK.setTimeout(function () {
// Get next tile position based on current tile type
var nextPos = self.getNextPosition(x, y, tile);
if (nextPos) {
+ log('Moving to next tile at:', nextPos[0], nextPos[1]);
self.flowThroughPipe(nextPos[0], nextPos[1], visited);
+ } else {
+ log('No next position found for tile at', x, y);
}
}, 500); // Wait 500ms before flowing to next tile
};
self.getNextPosition = function (x, y, tile) {
straigth zenith view square light wooden pallet. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
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.
simple yellow rating star. Modern video game style
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