Code edit (1 edits merged)
Please save this source code
User prompt
debug the lerping logic ensure it works correctly
User prompt
lerp each placed cheese to the scoreboard before disable it
User prompt
Ensure cheese is placed only on floor tiles
User prompt
Ensure picking up logic and collection logic and score in logic works correctly with a variable number of cheese on the scene
User prompt
Fix bug, player is not collecting cheese
User prompt
Fix bug, player is not picking up the cheese
User prompt
Fix bug, if player pressed the A button and the character is on the hole tile and it has a cheese the character must be able to collect it and score a point
User prompt
fix bug, player must be able to pick any cheese if they dont currently are holding any
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'visible')' in or related to this line: 'if (cheesePlaced && !cheeseIcon.visible) {' Line Number: 290
User prompt
fix bug, player must only pick up one cheese at a time
User prompt
Fix bug, more than one cheese collected st s time
User prompt
Fix bug, cheese lerp not working, when cheese is collected lerp it to the score board position before disabling it
User prompt
Fix bug, more points scored than total available cheese
User prompt
Encapsule lerp logic on cheese class for easier handling
User prompt
Please fix the bug: 'Timeout.tick error: cheese is not defined' in or related to this line: 'cheese.visible = false; // Turn cheese visibility off' Line Number: 316
User prompt
Please fix the bug: 'Timeout.tick error: Cannot set properties of undefined (setting 'x')' in or related to this line: 'pickedCheese.x = startPos.x + (endPos.x - startPos.x) * lerpFactor;' Line Number: 319
User prompt
Fix bug, when cheese is collected all instances lerp together, expected behavior just the collected cheese must lerp
User prompt
Please fix the bug: 'ReferenceError: cheese is not defined' in or related to this line: 'currentCheesePosition.x = cheese.x; // Save current cheese x position' Line Number: 515
User prompt
Fix bug, cheese not present on the floor
User prompt
Please fix the bug: 'ReferenceError: cheese is not defined' in or related to this line: 'if (cheese) {' Line Number: 513
User prompt
Please fix the bug: 'ReferenceError: cheese is not defined' in or related to this line: 'if (cheese) {' Line Number: 510
User prompt
Fix bug, lerp must support multiple cheese instances
User prompt
Please fix the bug: 'Timeout.tick error: startPos is not defined' in or related to this line: 'cheese.x = startPos.x + (endPos.x - startPos.x) * lerpFactor;' Line Number: 316
User prompt
Please fix the bug: 'Timeout.tick error: startPos is not defined' in or related to this line: 'cheese.x = startPos.x + (endPos.x - startPos.x) * lerpFactor;' Line Number: 319
/**** * Classes ****/ var Cheese = Container.expand(function () { var self = Container.call(this); // Attach cheese asset with anchor point set to center var cheeseShadow = self.attachAsset('CircularShadow', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5, alpha: 0.5, y: 20 // zIndex removed }); var cheeseGraphics = self.attachAsset('cheese', { anchorX: 0.5, anchorY: 0.5 // zIndex removed }); }); // Modular Enemy class to support different enemy types var Enemy = Container.expand(function (type) { var self = Container.call(this); var shadowAssetId, enemyAssetId, shadowScale, enemySpeed; // Enemy type enumerator with stats var EnemyTypes = { Roomba: { shadowAssetId: 'CircularShadow', enemyAssetId: 'Roomba', shadowScale: 3.2, enemySpeed: 2 } // Define additional enemy types here }; // Retrieve enemy type stats var enemyStats = EnemyTypes[type]; if (!enemyStats) { console.error('Unknown enemy type:', type); return; } shadowAssetId = enemyStats.shadowAssetId; enemyAssetId = enemyStats.enemyAssetId; shadowScale = enemyStats.shadowScale; enemySpeed = enemyStats.enemySpeed; // Attach a shadow asset to the enemy and make it semitransparent var shadowGraphics = self.attachAsset(shadowAssetId, { anchorX: 0.5, anchorY: 0.5, alpha: 0.5, scaleX: shadowScale, scaleY: shadowScale // zIndex removed }); var enemyGraphics = self.attachAsset(enemyAssetId, { anchorX: 0.5, anchorY: 0.5 // zIndex removed }); // Adjust shadow position relative to the enemy shadowGraphics.x = enemyGraphics.x; shadowGraphics.y = enemyGraphics.y + 40; // Slightly below the enemy for a realistic shadow effect // Set enemy speed self.speed = enemySpeed; // Enemy movement logic self.move = function () { // Movement logic remains the same if (self.targetTile) { var distanceToTarget = Math.sqrt(Math.pow(self.x - self.targetTile.x, 2) + Math.pow(self.y - self.targetTile.y, 2)); } if (distanceToTarget < 10 || !self.targetTile) { self.targetTile = floorTiles[Math.floor(Math.random() * floorTiles.length)]; } var angle = Math.atan2(self.targetTile.y - self.y, self.targetTile.x - self.x); var currentAngle = enemyGraphics.rotation; var targetAngle = angle; var angleDifference = targetAngle - currentAngle; angleDifference = (angleDifference + Math.PI) % (2 * Math.PI) - Math.PI; enemyGraphics.rotation += angleDifference * 0.1; var distance = Math.sqrt(Math.pow(self.targetTile.x - self.x, 2) + Math.pow(self.targetTile.y - self.y, 2)); var moveStep = self.speed / distance; self.x += (self.targetTile.x - self.x) * moveStep; self.y += (self.targetTile.y - self.y) * moveStep; }; }); // OnScreenController class encapsulating A, B buttons and D-pad var OnScreenController = Container.expand(function () { var self = Container.call(this); // Add a background for the on screen controllers var yPosAdjustment = -180; var buttonYPosAdjustment = -273.2; var dpadButtonSize = 290; // Removed redundant tint application on A and B buttons LK.screen = { width: 2048, height: 2732 }; // Initialize screen dimensions var aButtonPosition = { x: LK.screen.width * 0.75, y: LK.screen.height * 0.85 + buttonYPosAdjustment + LK.screen.height * 0.055 }; var buttonSize = { width: 400, height: 400 }; var dPadSize = { width: 750, height: 750 }; this.aButton = self.attachAsset('aButton', { anchorX: 0.5, anchorY: 0.5, x: aButtonPosition.x, y: aButtonPosition.y, width: buttonSize.width, height: buttonSize.height }); // D-pad var dpadBase = self.attachAsset('dpadBase', { anchorX: 0.5, anchorY: 0.5, x: LK.screen.width * 0.24, y: LK.screen.height * 0.89 + yPosAdjustment - 10, width: dPadSize.width, height: dPadSize.height }); self.setChildIndex(dpadBase, self.children.length - 1); var dpadLeft = self.attachAsset('dpadButtonLeft', { anchorX: 0.5, anchorY: 0.5, x: dpadBase.x - 300, y: dpadBase.y, width: dpadButtonSize, height: dpadButtonSize, alpha: 0, // Tint arrow yellow orientation: 3 }); var moveInterval; function startMovingCharacter(xDir, yDir) { if (moveInterval) { LK.clearInterval(moveInterval); } isCharacterMoving = true; moveCharacter(xDir, yDir); moveInterval = LK.setInterval(function () { moveCharacter(xDir, yDir); }, 150); } dpadLeft.on('down', function () { startMovingCharacter(-1, 0); }); var dpadUp = self.attachAsset('dpadButtonUp', { anchorX: 0.5, anchorY: 0.5, x: dpadBase.x, y: dpadBase.y - 300, width: dpadButtonSize, height: dpadButtonSize, alpha: 0, // Tint arrow yellow orientation: 0 }); dpadUp.on('down', function () { startMovingCharacter(0, -1); }); var dpadRight = self.attachAsset('dpadButtonRight', { anchorX: 0.5, anchorY: 0.5, x: dpadBase.x + 300, y: dpadBase.y, width: dpadButtonSize, height: dpadButtonSize, alpha: 0, // Tint arrow yellow orientation: 1 }); dpadRight.on('down', function () { startMovingCharacter(1, 0); }); var dpadDown = self.attachAsset('dpadButtonDown', { anchorX: 0.5, anchorY: 0.5, x: dpadBase.x, y: dpadBase.y + 300, width: dpadButtonSize, height: dpadButtonSize, alpha: 0, // Tint arrow yellow orientation: 2 }); dpadDown.on('down', function () { startMovingCharacter(0, 1); }); function stopMovingCharacter() { isCharacterMoving = false; LK.clearInterval(moveInterval); } dpadLeft.on('up', stopMovingCharacter); dpadUp.on('up', stopMovingCharacter); dpadRight.on('up', stopMovingCharacter); dpadDown.on('up', stopMovingCharacter); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xf9e076 // Fun yellow background }); /**** * Game Code ****/ // Initialize scoreboard container and elements once and update score dynamically if (!window.scoreBoardInitialized) { window.scoreBoardContainer = new Container(); var scoreBoardX = 200, scoreBoardY = 125; var cheeseIconForScore = window.scoreBoardContainer.attachAsset('cheese', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.5, scaleY: 0.5, x: -50, y: 0 }); window.cheeseScoreIcon = new Text2('0', { size: 50, fill: "#ffffff", font: "Arial Black, bold", x: 20, y: -25 }); window.scoreBoardContainer.addChild(window.cheeseScoreIcon); window.scoreBoardContainer.x = scoreBoardX; window.scoreBoardContainer.y = scoreBoardY; LK.gui.addChild(window.scoreBoardContainer); window.scoreBoardInitialized = true; } // Update score dynamically without recreating the scoreboard elements window.cheeseScoreIcon.setText(LK.getScore().toString()); // Move the onScreenController initialization to the top of Game Code section to ensure it's available before adding event listeners var onScreenController = game.addChild(new OnScreenController()); // Event listener for A button press to pick up the cheese using a refined proximity check and method encapsulation onScreenController.aButton.on('down', function () { // Check if cheese is picked up and character is on the hole tile if (cheesePlaced && cheeseIcon && cheeseIcon.visible && isWithinProximity(character, holeTile, 75)) { console.log('Cheese placed on hole tile!'); // Update to handle multiple cheeses cheeses.forEach(function (cheese) { if (isWithinProximity(cheese, character, 75)) { cheese.x = holeTile.x; // Place cheese on hole tile cheese.y = holeTile.y; cheese.visible = true; // Make cheese visible on the hole tile } }); // Disable cheese icon on character head if (cheeseIcon) { cheeseIcon.visible = false; } // Lerp cheese to scoreboard and turn off visibility upon reaching var lerpDuration = 500; // Duration of the lerp in milliseconds var startTime = Date.now(); // Start time of the lerp // Corrected to iterate over cheeses array and apply logic to each cheese cheeses.forEach(function (cheese) { // This block intentionally left blank as the correction involves moving the startPos definition inside the lerpInterval function where it's used }); var endPos = { x: window.scoreBoardContainer.x + window.cheeseScoreIcon.x, y: window.scoreBoardContainer.y + window.cheeseScoreIcon.y }; // Corrected end position to account for scoreboard container position var lerpInterval = LK.setInterval(function () { var currentTime = Date.now(); var timeElapsed = currentTime - startTime; var lerpFactor = timeElapsed / lerpDuration; if (lerpFactor >= 1) { LK.clearInterval(lerpInterval); // Stop the lerp when it's complete if (cheesePlaced) { // Increment score for each cheese collected cheeses.forEach(function (cheese) { if (!cheese.visible) { // Check if cheese has been collected (not visible) LK.setScore(LK.getScore() + 1); // Add one to the score for each cheese collected } }); } window.cheeseScoreIcon.setText(LK.getScore()); // Update cheese score display cheesePlaced = false; // Reset cheese placement flag cheese.visible = false; // Turn cheese visibility off } else { // Update cheese position based on lerp factor cheeses.forEach(function (cheese) { cheese.x = startPos.x + (endPos.x - startPos.x) * lerpFactor; cheese.y = startPos.y + (endPos.y - startPos.y) * lerpFactor; }); } }, 16); // Update approximately every 16ms (~60fps) } else if (cheeses.some(function (cheese) { return isWithinProximity(character, cheese, 75); })) { var pickedCheese = cheeses.find(function (cheese) { return isWithinProximity(character, cheese, 75); }); pickedCheese.visible = false; // Hide the picked cheese from the game // Check if cheese is initialized and within a refined proximity to the character for pickup console.log('Cheese picked up!'); pickedCheese.visible = false; // Hide the picked cheese from the game // Show UI icon indicating cheese has been picked up only if not already visible if (!cheeseIcon || !cheeseIcon.visible) { cheeseIcon = character.attachAsset('cheese', { anchorX: 0.5, anchorY: 0.5, x: 0, // Centered on character's head y: -character.height * 0.75, // Positioned above the character's head scaleX: 0.5, scaleY: 0.5 }); } cheesePlaced = true; // Set cheese placement flag to true } }); // Helper function to check if two objects are within a certain distance function isWithinProximity(obj1, obj2, threshold) { return Math.abs(obj1.x - obj2.x) < threshold && Math.abs(obj1.y - obj2.y) < threshold; } // This block intentionally left blank to signify removal of the above code var isCharacterMoving = false; // Function to move character based on direction var dpadButtonSize = { width: 450, height: 450 }; function moveCharacter(xDir, yDir) { var nextX = character.x + xDir * tileWidth; var nextY = character.y + yDir * tileHeight; // Check if the next position is within the grid boundaries if (nextX >= gridOffsetX && nextX <= gridOffsetX + gridSize * tileWidth && nextY >= gridOffsetY && nextY <= gridOffsetY + gridSize * tileHeight) { // Check if the next tile is not a wall var nextTileIndex = Math.floor((nextY - gridOffsetY) / tileHeight) * gridSize + Math.floor((nextX - gridOffsetX) / tileWidth); if (floorTiles.includes(grid[nextTileIndex]) || holeTile && grid[nextTileIndex].x === holeTile.x && grid[nextTileIndex].y === holeTile.y) { character.x = nextX; character.y = nextY; character.currentTile = grid[nextTileIndex]; } } } LK.screen = { width: 2048, height: 2732 }; // Initialize screen dimensions var gridSize = 10; var character; var cheeses = []; // Initialize cheeses array globally var cheeseIcon; // Declare cheeseIcon as a global variable var wallTiles = []; var floorTiles = []; var grid = new Array(gridSize * gridSize); var tileWidth = LK.screen.width * .6 / (gridSize - 1); // Adjust tile width to ensure tiles are touching var tileHeight = tileWidth; // Ensure square tiles for a uniform grid var gridOffsetX = (LK.screen.width - tileWidth * gridSize) / 2; // Corrected grid offset X calculation var gridOffsetY = (LK.screen.height - tileHeight * gridSize) / 2 - LK.screen.height * 0.15; // Corrected grid offset Y calculation // Move the onScreenController initialization to the top of Game Code section to ensure it's available before adding event listeners var onScreenController = game.addChild(new OnScreenController()); // Removed the A button press event listener for picking up cheese based on tile comparison function createTiles() { // Create a black square background for the grid var gridBackground = LK.getAsset('gridBackground', { anchorX: 0.5, anchorY: 0.8, x: LK.screen.width / 2, y: LK.screen.height / 2, width: LK.screen.width * .9, height: LK.screen.height * .6, tint: 0xFFA500 // Apply orange tint }); game.addChild(gridBackground); var gridBackgroundShrunk = LK.getAsset('gridBackground', { anchorX: 0.5, anchorY: 0.8, x: LK.screen.width / 2, y: LK.screen.height / 2, width: LK.screen.width * .9 - 50, height: LK.screen.height * .6 - 50, tint: 0x000000 // Apply black tint }); game.addChild(gridBackgroundShrunk); game.setChildIndex(gridBackgroundShrunk, game.getChildIndex(gridBackground) + 1); for (var i = 0; i < gridSize * gridSize; i++) { var x = i % gridSize; var y = Math.floor(i / gridSize); var tile; var tileWidth = LK.screen.width * .6 / (gridSize - 1); // Adjust tile width to ensure tiles are touching var tileHeight = tileWidth; // Ensure square tiles for a uniform grid var gridOffsetX = (LK.screen.width - tileWidth * (gridSize - 1)) / 2; // Adjust grid offset to account for new tile width var gridOffsetY = (LK.screen.height - tileHeight * (gridSize - 1)) / 2 - LK.screen.height * 0.15; // Adjust grid offset to account for new tile height if (!(x === 0 || y === 0 || x === gridSize - 1 || y === gridSize - 1)) { tile = LK.getAsset('floorTile', { anchorX: 0.5, anchorY: 0.5, x: x * tileWidth + gridOffsetX, y: y * tileHeight + gridOffsetY }); floorTiles.push(tile); game.addChild(tile); grid[i] = tile; } } // Add walls after floor tiles to ensure they render on top for (var i = 0; i < gridSize * gridSize; i++) { var x = i % gridSize; var y = Math.floor(i / gridSize); if (x === 0 || y === 0 || x === gridSize - 1 || y === gridSize - 1) { var tile = LK.getAsset('wallTile', { anchorX: 0.5, anchorY: 0.5, x: x * tileWidth + gridOffsetX, y: y * tileHeight + gridOffsetY }); wallTiles.push(tile); game.addChild(tile); grid[i] = tile; } } } createTiles(); // Function to check if a tile is in a corner function isTileInCorner(tile) { var corners = [{ x: LK.screen.width / 2 - tileWidth * gridSize / 2, y: LK.screen.height / 2 - tileHeight * gridSize / 2 }, { x: LK.screen.width / 2 - tileWidth * gridSize / 2, y: LK.screen.height / 2 + tileHeight * (gridSize - 1) / 2 }, { x: LK.screen.width / 2 + tileWidth * (gridSize - 1) / 2, y: LK.screen.height / 2 - tileHeight * gridSize / 2 }, { x: LK.screen.width / 2 + tileWidth * (gridSize - 1) / 2, y: LK.screen.height / 2 + tileHeight * (gridSize - 1) / 2 }]; return corners.some(function (corner) { return Math.abs(tile.x - corner.x) < tileWidth / 2 && Math.abs(tile.y - corner.y) < tileHeight / 2; }); } // Select a random wall tile and replace it with a hole tile, ensuring it's not in a corner var validWallTiles = wallTiles.filter(function (tile) { // Exclude tiles that are in the corners by checking their position against the grid's corners return !isTileInCorner(tile); }); var randomWallTile = validWallTiles[Math.floor(Math.random() * validWallTiles.length)]; var holeTile = LK.getAsset('holeTile', { anchorX: 0.5, anchorY: 0.5, x: randomWallTile.x, y: randomWallTile.y }); game.addChild(holeTile); game.removeChild(randomWallTile); var randomWallTileIndex = wallTiles.indexOf(randomWallTile); wallTiles[randomWallTileIndex] = holeTile; var isGameStarted = false; var cheesePlaced = false; LK.on('tick', function () { if (!isGameStarted) { isGameStarted = true; // Ensure game start logic is only triggered once character = LK.getAsset('character', { anchorX: 0.5, anchorY: 0.5, x: holeTile.x, y: holeTile.y - 50, scaleX: 1.5, scaleY: 1.5 // zIndex removed }); var currentCheesePosition = { x: 0, y: 0 }; // Initialize variable to store cheese position if (!cheesePlaced) { // Ensure cheese is initialized before accessing its properties if (cheese) { currentCheesePosition.x = cheese.x; // Save current cheese x position } if (cheese) { currentCheesePosition.y = cheese.y; // Save current cheese y position } // Filter out tiles that are too close to corners var nonCornerFloorTiles = floorTiles.filter(function (tile) { return !isTileInCorner(tile); }); // Further filter to find tiles farthest from the character's starting position var farthestTiles = nonCornerFloorTiles.filter(function (tile) { var distanceToCharacter = Math.sqrt(Math.pow(tile.x - character.x, 2) + Math.pow(tile.y - character.y, 2)); return distanceToCharacter > LK.screen.width / 2; // Arbitrary distance threshold }); // If no tile is found far enough (unlikely but possible), default to non-corner tiles var targetTiles = farthestTiles.length > 0 ? farthestTiles : nonCornerFloorTiles; // Select a random tile from the final list var randomFloorTile = targetTiles[Math.floor(Math.random() * targetTiles.length)]; // Place multiple cheeses on different tiles for (var i = 0; i < 3; i++) { // Example for placing 3 cheeses var cheese = game.addChild(new Cheese()); var randomFloorTile = targetTiles[Math.floor(Math.random() * targetTiles.length)]; cheese.x = randomFloorTile.x; cheese.y = randomFloorTile.y; cheeses.push(cheese); } cheesePlaced = true; // Indicate that cheeses have been placed } var characterShadow = LK.getAsset('characterShadow', { anchorX: 0.5, anchorY: 0.5, x: -15, y: 50, scaleX: 0.6, scaleY: 0.6, alpha: 0.5 // zIndex removed }); character.addChild(characterShadow); character.currentTile = getTileAtPosition(holeTile.x, holeTile.y); // Correctly initialize character's current tile game.addChild(character); } // Iterate over enemies array to move each enemy enemies.forEach(function (enemy) { enemy.move(); }); }); // Create instances of enemies and store them in an array var enemies = []; enemies.push(game.addChild(new Enemy('Roomba'))); enemies.push(game.addChild(new Enemy('Roomba'))); // Position enemies enemies[0].x = LK.screen.width / 2; enemies[0].y = LK.screen.height / 2; enemies[1].x = LK.screen.width / 4; enemies[1].y = LK.screen.height / 4; // Define a function to get the tile at a given position function getTileAtPosition(x, y) { var index = Math.floor(y / 100) * gridSize + Math.floor(x / 100); return grid[index]; }
/****
* Classes
****/
var Cheese = Container.expand(function () {
var self = Container.call(this);
// Attach cheese asset with anchor point set to center
var cheeseShadow = self.attachAsset('CircularShadow', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5,
alpha: 0.5,
y: 20
// zIndex removed
});
var cheeseGraphics = self.attachAsset('cheese', {
anchorX: 0.5,
anchorY: 0.5
// zIndex removed
});
});
// Modular Enemy class to support different enemy types
var Enemy = Container.expand(function (type) {
var self = Container.call(this);
var shadowAssetId, enemyAssetId, shadowScale, enemySpeed;
// Enemy type enumerator with stats
var EnemyTypes = {
Roomba: {
shadowAssetId: 'CircularShadow',
enemyAssetId: 'Roomba',
shadowScale: 3.2,
enemySpeed: 2
}
// Define additional enemy types here
};
// Retrieve enemy type stats
var enemyStats = EnemyTypes[type];
if (!enemyStats) {
console.error('Unknown enemy type:', type);
return;
}
shadowAssetId = enemyStats.shadowAssetId;
enemyAssetId = enemyStats.enemyAssetId;
shadowScale = enemyStats.shadowScale;
enemySpeed = enemyStats.enemySpeed;
// Attach a shadow asset to the enemy and make it semitransparent
var shadowGraphics = self.attachAsset(shadowAssetId, {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.5,
scaleX: shadowScale,
scaleY: shadowScale
// zIndex removed
});
var enemyGraphics = self.attachAsset(enemyAssetId, {
anchorX: 0.5,
anchorY: 0.5
// zIndex removed
});
// Adjust shadow position relative to the enemy
shadowGraphics.x = enemyGraphics.x;
shadowGraphics.y = enemyGraphics.y + 40; // Slightly below the enemy for a realistic shadow effect
// Set enemy speed
self.speed = enemySpeed;
// Enemy movement logic
self.move = function () {
// Movement logic remains the same
if (self.targetTile) {
var distanceToTarget = Math.sqrt(Math.pow(self.x - self.targetTile.x, 2) + Math.pow(self.y - self.targetTile.y, 2));
}
if (distanceToTarget < 10 || !self.targetTile) {
self.targetTile = floorTiles[Math.floor(Math.random() * floorTiles.length)];
}
var angle = Math.atan2(self.targetTile.y - self.y, self.targetTile.x - self.x);
var currentAngle = enemyGraphics.rotation;
var targetAngle = angle;
var angleDifference = targetAngle - currentAngle;
angleDifference = (angleDifference + Math.PI) % (2 * Math.PI) - Math.PI;
enemyGraphics.rotation += angleDifference * 0.1;
var distance = Math.sqrt(Math.pow(self.targetTile.x - self.x, 2) + Math.pow(self.targetTile.y - self.y, 2));
var moveStep = self.speed / distance;
self.x += (self.targetTile.x - self.x) * moveStep;
self.y += (self.targetTile.y - self.y) * moveStep;
};
});
// OnScreenController class encapsulating A, B buttons and D-pad
var OnScreenController = Container.expand(function () {
var self = Container.call(this);
// Add a background for the on screen controllers
var yPosAdjustment = -180;
var buttonYPosAdjustment = -273.2;
var dpadButtonSize = 290;
// Removed redundant tint application on A and B buttons
LK.screen = {
width: 2048,
height: 2732
}; // Initialize screen dimensions
var aButtonPosition = {
x: LK.screen.width * 0.75,
y: LK.screen.height * 0.85 + buttonYPosAdjustment + LK.screen.height * 0.055
};
var buttonSize = {
width: 400,
height: 400
};
var dPadSize = {
width: 750,
height: 750
};
this.aButton = self.attachAsset('aButton', {
anchorX: 0.5,
anchorY: 0.5,
x: aButtonPosition.x,
y: aButtonPosition.y,
width: buttonSize.width,
height: buttonSize.height
});
// D-pad
var dpadBase = self.attachAsset('dpadBase', {
anchorX: 0.5,
anchorY: 0.5,
x: LK.screen.width * 0.24,
y: LK.screen.height * 0.89 + yPosAdjustment - 10,
width: dPadSize.width,
height: dPadSize.height
});
self.setChildIndex(dpadBase, self.children.length - 1);
var dpadLeft = self.attachAsset('dpadButtonLeft', {
anchorX: 0.5,
anchorY: 0.5,
x: dpadBase.x - 300,
y: dpadBase.y,
width: dpadButtonSize,
height: dpadButtonSize,
alpha: 0,
// Tint arrow yellow
orientation: 3
});
var moveInterval;
function startMovingCharacter(xDir, yDir) {
if (moveInterval) {
LK.clearInterval(moveInterval);
}
isCharacterMoving = true;
moveCharacter(xDir, yDir);
moveInterval = LK.setInterval(function () {
moveCharacter(xDir, yDir);
}, 150);
}
dpadLeft.on('down', function () {
startMovingCharacter(-1, 0);
});
var dpadUp = self.attachAsset('dpadButtonUp', {
anchorX: 0.5,
anchorY: 0.5,
x: dpadBase.x,
y: dpadBase.y - 300,
width: dpadButtonSize,
height: dpadButtonSize,
alpha: 0,
// Tint arrow yellow
orientation: 0
});
dpadUp.on('down', function () {
startMovingCharacter(0, -1);
});
var dpadRight = self.attachAsset('dpadButtonRight', {
anchorX: 0.5,
anchorY: 0.5,
x: dpadBase.x + 300,
y: dpadBase.y,
width: dpadButtonSize,
height: dpadButtonSize,
alpha: 0,
// Tint arrow yellow
orientation: 1
});
dpadRight.on('down', function () {
startMovingCharacter(1, 0);
});
var dpadDown = self.attachAsset('dpadButtonDown', {
anchorX: 0.5,
anchorY: 0.5,
x: dpadBase.x,
y: dpadBase.y + 300,
width: dpadButtonSize,
height: dpadButtonSize,
alpha: 0,
// Tint arrow yellow
orientation: 2
});
dpadDown.on('down', function () {
startMovingCharacter(0, 1);
});
function stopMovingCharacter() {
isCharacterMoving = false;
LK.clearInterval(moveInterval);
}
dpadLeft.on('up', stopMovingCharacter);
dpadUp.on('up', stopMovingCharacter);
dpadRight.on('up', stopMovingCharacter);
dpadDown.on('up', stopMovingCharacter);
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf9e076 // Fun yellow background
});
/****
* Game Code
****/
// Initialize scoreboard container and elements once and update score dynamically
if (!window.scoreBoardInitialized) {
window.scoreBoardContainer = new Container();
var scoreBoardX = 200,
scoreBoardY = 125;
var cheeseIconForScore = window.scoreBoardContainer.attachAsset('cheese', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.5,
scaleY: 0.5,
x: -50,
y: 0
});
window.cheeseScoreIcon = new Text2('0', {
size: 50,
fill: "#ffffff",
font: "Arial Black, bold",
x: 20,
y: -25
});
window.scoreBoardContainer.addChild(window.cheeseScoreIcon);
window.scoreBoardContainer.x = scoreBoardX;
window.scoreBoardContainer.y = scoreBoardY;
LK.gui.addChild(window.scoreBoardContainer);
window.scoreBoardInitialized = true;
}
// Update score dynamically without recreating the scoreboard elements
window.cheeseScoreIcon.setText(LK.getScore().toString());
// Move the onScreenController initialization to the top of Game Code section to ensure it's available before adding event listeners
var onScreenController = game.addChild(new OnScreenController());
// Event listener for A button press to pick up the cheese using a refined proximity check and method encapsulation
onScreenController.aButton.on('down', function () {
// Check if cheese is picked up and character is on the hole tile
if (cheesePlaced && cheeseIcon && cheeseIcon.visible && isWithinProximity(character, holeTile, 75)) {
console.log('Cheese placed on hole tile!');
// Update to handle multiple cheeses
cheeses.forEach(function (cheese) {
if (isWithinProximity(cheese, character, 75)) {
cheese.x = holeTile.x; // Place cheese on hole tile
cheese.y = holeTile.y;
cheese.visible = true; // Make cheese visible on the hole tile
}
});
// Disable cheese icon on character head
if (cheeseIcon) {
cheeseIcon.visible = false;
}
// Lerp cheese to scoreboard and turn off visibility upon reaching
var lerpDuration = 500; // Duration of the lerp in milliseconds
var startTime = Date.now(); // Start time of the lerp
// Corrected to iterate over cheeses array and apply logic to each cheese
cheeses.forEach(function (cheese) {
// This block intentionally left blank as the correction involves moving the startPos definition inside the lerpInterval function where it's used
});
var endPos = {
x: window.scoreBoardContainer.x + window.cheeseScoreIcon.x,
y: window.scoreBoardContainer.y + window.cheeseScoreIcon.y
}; // Corrected end position to account for scoreboard container position
var lerpInterval = LK.setInterval(function () {
var currentTime = Date.now();
var timeElapsed = currentTime - startTime;
var lerpFactor = timeElapsed / lerpDuration;
if (lerpFactor >= 1) {
LK.clearInterval(lerpInterval); // Stop the lerp when it's complete
if (cheesePlaced) {
// Increment score for each cheese collected
cheeses.forEach(function (cheese) {
if (!cheese.visible) {
// Check if cheese has been collected (not visible)
LK.setScore(LK.getScore() + 1); // Add one to the score for each cheese collected
}
});
}
window.cheeseScoreIcon.setText(LK.getScore()); // Update cheese score display
cheesePlaced = false; // Reset cheese placement flag
cheese.visible = false; // Turn cheese visibility off
} else {
// Update cheese position based on lerp factor
cheeses.forEach(function (cheese) {
cheese.x = startPos.x + (endPos.x - startPos.x) * lerpFactor;
cheese.y = startPos.y + (endPos.y - startPos.y) * lerpFactor;
});
}
}, 16); // Update approximately every 16ms (~60fps)
} else if (cheeses.some(function (cheese) {
return isWithinProximity(character, cheese, 75);
})) {
var pickedCheese = cheeses.find(function (cheese) {
return isWithinProximity(character, cheese, 75);
});
pickedCheese.visible = false; // Hide the picked cheese from the game
// Check if cheese is initialized and within a refined proximity to the character for pickup
console.log('Cheese picked up!');
pickedCheese.visible = false; // Hide the picked cheese from the game
// Show UI icon indicating cheese has been picked up only if not already visible
if (!cheeseIcon || !cheeseIcon.visible) {
cheeseIcon = character.attachAsset('cheese', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
// Centered on character's head
y: -character.height * 0.75,
// Positioned above the character's head
scaleX: 0.5,
scaleY: 0.5
});
}
cheesePlaced = true; // Set cheese placement flag to true
}
});
// Helper function to check if two objects are within a certain distance
function isWithinProximity(obj1, obj2, threshold) {
return Math.abs(obj1.x - obj2.x) < threshold && Math.abs(obj1.y - obj2.y) < threshold;
}
// This block intentionally left blank to signify removal of the above code
var isCharacterMoving = false;
// Function to move character based on direction
var dpadButtonSize = {
width: 450,
height: 450
};
function moveCharacter(xDir, yDir) {
var nextX = character.x + xDir * tileWidth;
var nextY = character.y + yDir * tileHeight;
// Check if the next position is within the grid boundaries
if (nextX >= gridOffsetX && nextX <= gridOffsetX + gridSize * tileWidth && nextY >= gridOffsetY && nextY <= gridOffsetY + gridSize * tileHeight) {
// Check if the next tile is not a wall
var nextTileIndex = Math.floor((nextY - gridOffsetY) / tileHeight) * gridSize + Math.floor((nextX - gridOffsetX) / tileWidth);
if (floorTiles.includes(grid[nextTileIndex]) || holeTile && grid[nextTileIndex].x === holeTile.x && grid[nextTileIndex].y === holeTile.y) {
character.x = nextX;
character.y = nextY;
character.currentTile = grid[nextTileIndex];
}
}
}
LK.screen = {
width: 2048,
height: 2732
}; // Initialize screen dimensions
var gridSize = 10;
var character;
var cheeses = []; // Initialize cheeses array globally
var cheeseIcon; // Declare cheeseIcon as a global variable
var wallTiles = [];
var floorTiles = [];
var grid = new Array(gridSize * gridSize);
var tileWidth = LK.screen.width * .6 / (gridSize - 1); // Adjust tile width to ensure tiles are touching
var tileHeight = tileWidth; // Ensure square tiles for a uniform grid
var gridOffsetX = (LK.screen.width - tileWidth * gridSize) / 2; // Corrected grid offset X calculation
var gridOffsetY = (LK.screen.height - tileHeight * gridSize) / 2 - LK.screen.height * 0.15; // Corrected grid offset Y calculation
// Move the onScreenController initialization to the top of Game Code section to ensure it's available before adding event listeners
var onScreenController = game.addChild(new OnScreenController());
// Removed the A button press event listener for picking up cheese based on tile comparison
function createTiles() {
// Create a black square background for the grid
var gridBackground = LK.getAsset('gridBackground', {
anchorX: 0.5,
anchorY: 0.8,
x: LK.screen.width / 2,
y: LK.screen.height / 2,
width: LK.screen.width * .9,
height: LK.screen.height * .6,
tint: 0xFFA500 // Apply orange tint
});
game.addChild(gridBackground);
var gridBackgroundShrunk = LK.getAsset('gridBackground', {
anchorX: 0.5,
anchorY: 0.8,
x: LK.screen.width / 2,
y: LK.screen.height / 2,
width: LK.screen.width * .9 - 50,
height: LK.screen.height * .6 - 50,
tint: 0x000000 // Apply black tint
});
game.addChild(gridBackgroundShrunk);
game.setChildIndex(gridBackgroundShrunk, game.getChildIndex(gridBackground) + 1);
for (var i = 0; i < gridSize * gridSize; i++) {
var x = i % gridSize;
var y = Math.floor(i / gridSize);
var tile;
var tileWidth = LK.screen.width * .6 / (gridSize - 1); // Adjust tile width to ensure tiles are touching
var tileHeight = tileWidth; // Ensure square tiles for a uniform grid
var gridOffsetX = (LK.screen.width - tileWidth * (gridSize - 1)) / 2; // Adjust grid offset to account for new tile width
var gridOffsetY = (LK.screen.height - tileHeight * (gridSize - 1)) / 2 - LK.screen.height * 0.15; // Adjust grid offset to account for new tile height
if (!(x === 0 || y === 0 || x === gridSize - 1 || y === gridSize - 1)) {
tile = LK.getAsset('floorTile', {
anchorX: 0.5,
anchorY: 0.5,
x: x * tileWidth + gridOffsetX,
y: y * tileHeight + gridOffsetY
});
floorTiles.push(tile);
game.addChild(tile);
grid[i] = tile;
}
}
// Add walls after floor tiles to ensure they render on top
for (var i = 0; i < gridSize * gridSize; i++) {
var x = i % gridSize;
var y = Math.floor(i / gridSize);
if (x === 0 || y === 0 || x === gridSize - 1 || y === gridSize - 1) {
var tile = LK.getAsset('wallTile', {
anchorX: 0.5,
anchorY: 0.5,
x: x * tileWidth + gridOffsetX,
y: y * tileHeight + gridOffsetY
});
wallTiles.push(tile);
game.addChild(tile);
grid[i] = tile;
}
}
}
createTiles();
// Function to check if a tile is in a corner
function isTileInCorner(tile) {
var corners = [{
x: LK.screen.width / 2 - tileWidth * gridSize / 2,
y: LK.screen.height / 2 - tileHeight * gridSize / 2
}, {
x: LK.screen.width / 2 - tileWidth * gridSize / 2,
y: LK.screen.height / 2 + tileHeight * (gridSize - 1) / 2
}, {
x: LK.screen.width / 2 + tileWidth * (gridSize - 1) / 2,
y: LK.screen.height / 2 - tileHeight * gridSize / 2
}, {
x: LK.screen.width / 2 + tileWidth * (gridSize - 1) / 2,
y: LK.screen.height / 2 + tileHeight * (gridSize - 1) / 2
}];
return corners.some(function (corner) {
return Math.abs(tile.x - corner.x) < tileWidth / 2 && Math.abs(tile.y - corner.y) < tileHeight / 2;
});
}
// Select a random wall tile and replace it with a hole tile, ensuring it's not in a corner
var validWallTiles = wallTiles.filter(function (tile) {
// Exclude tiles that are in the corners by checking their position against the grid's corners
return !isTileInCorner(tile);
});
var randomWallTile = validWallTiles[Math.floor(Math.random() * validWallTiles.length)];
var holeTile = LK.getAsset('holeTile', {
anchorX: 0.5,
anchorY: 0.5,
x: randomWallTile.x,
y: randomWallTile.y
});
game.addChild(holeTile);
game.removeChild(randomWallTile);
var randomWallTileIndex = wallTiles.indexOf(randomWallTile);
wallTiles[randomWallTileIndex] = holeTile;
var isGameStarted = false;
var cheesePlaced = false;
LK.on('tick', function () {
if (!isGameStarted) {
isGameStarted = true; // Ensure game start logic is only triggered once
character = LK.getAsset('character', {
anchorX: 0.5,
anchorY: 0.5,
x: holeTile.x,
y: holeTile.y - 50,
scaleX: 1.5,
scaleY: 1.5
// zIndex removed
});
var currentCheesePosition = {
x: 0,
y: 0
}; // Initialize variable to store cheese position
if (!cheesePlaced) {
// Ensure cheese is initialized before accessing its properties
if (cheese) {
currentCheesePosition.x = cheese.x; // Save current cheese x position
}
if (cheese) {
currentCheesePosition.y = cheese.y; // Save current cheese y position
}
// Filter out tiles that are too close to corners
var nonCornerFloorTiles = floorTiles.filter(function (tile) {
return !isTileInCorner(tile);
});
// Further filter to find tiles farthest from the character's starting position
var farthestTiles = nonCornerFloorTiles.filter(function (tile) {
var distanceToCharacter = Math.sqrt(Math.pow(tile.x - character.x, 2) + Math.pow(tile.y - character.y, 2));
return distanceToCharacter > LK.screen.width / 2; // Arbitrary distance threshold
});
// If no tile is found far enough (unlikely but possible), default to non-corner tiles
var targetTiles = farthestTiles.length > 0 ? farthestTiles : nonCornerFloorTiles;
// Select a random tile from the final list
var randomFloorTile = targetTiles[Math.floor(Math.random() * targetTiles.length)];
// Place multiple cheeses on different tiles
for (var i = 0; i < 3; i++) {
// Example for placing 3 cheeses
var cheese = game.addChild(new Cheese());
var randomFloorTile = targetTiles[Math.floor(Math.random() * targetTiles.length)];
cheese.x = randomFloorTile.x;
cheese.y = randomFloorTile.y;
cheeses.push(cheese);
}
cheesePlaced = true; // Indicate that cheeses have been placed
}
var characterShadow = LK.getAsset('characterShadow', {
anchorX: 0.5,
anchorY: 0.5,
x: -15,
y: 50,
scaleX: 0.6,
scaleY: 0.6,
alpha: 0.5
// zIndex removed
});
character.addChild(characterShadow);
character.currentTile = getTileAtPosition(holeTile.x, holeTile.y); // Correctly initialize character's current tile
game.addChild(character);
}
// Iterate over enemies array to move each enemy
enemies.forEach(function (enemy) {
enemy.move();
});
});
// Create instances of enemies and store them in an array
var enemies = [];
enemies.push(game.addChild(new Enemy('Roomba')));
enemies.push(game.addChild(new Enemy('Roomba')));
// Position enemies
enemies[0].x = LK.screen.width / 2;
enemies[0].y = LK.screen.height / 2;
enemies[1].x = LK.screen.width / 4;
enemies[1].y = LK.screen.height / 4;
// Define a function to get the tile at a given position
function getTileAtPosition(x, y) {
var index = Math.floor(y / 100) * gridSize + Math.floor(x / 100);
return grid[index];
}
grey square, black border. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
simple light yellow button front view game console, clean, rounded edges, high resolution, graphic. Single Game Texture. In-Game asset. 2d. Blank background. High contrast.
Worn out sticker for a video game, 90s style, cheese, simple, vintage. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
wall view from top-down, game asset videogame, black color, simple. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. worn out sticker. 90's style. vintage. simple. top-down view. poster. sticker
top-down view, videogame character enemy, roomba, 90s style sticker, flat, no perspective, silhouette, black and white, cartoon, fun, simple, from above. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
top-down view, videogame heart, 90s style sticker, flat, no perspective, silhouette, white, cartoon, fun, simple, from above. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Black square with white outline. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.