User prompt
Add waiting time of a random from one to two second each time the robot reaches a new node
User prompt
Force the robot to move only horizontally and vertically to reach each point
User prompt
When a robot spawns pick randomly for 4.up to 10 floor tiles, using those selected tiles plan a patrol route and make the robot follow that oath
User prompt
Remove patrol logic
User prompt
Draw the robot path on screen using red lines
User prompt
Make the robot path at least 5 nodes
User prompt
Hole tile must only spawn on wall tiles and replace it
User prompt
Make sure robot path cover more.ofmtje playable area
User prompt
Debug robot behavior ensure movement takes in consideration current grid offsets and screen sizes
User prompt
Ensure.tje character can freely move across all the grid except wall tiles
User prompt
Move the grid up 50 pixels
User prompt
Move the grid up 100 pixels
User prompt
Grid need to be inside the grid background
User prompt
Move.the whole grid up to avoid clipping with the screen controller
User prompt
Fix the hole tile must only spawn on a a wall tile and replace it
User prompt
Move the grid up 50 pixels
User prompt
Fix the grid is not squared
User prompt
Fix the grid Y positions are wrong
User prompt
Please fix the bug: 'Uncaught ReferenceError: randomWallTileIndex is not defined' in or related to this line: 'wallTiles[randomWallTileIndex] = holeTile;' Line Number: 320
User prompt
Improve readability of the code
User prompt
Please fix the bug: 'Uncaught ReferenceError: randomWallTile is not defined' in or related to this line: 'game.removeChild(randomWallTile);' Line Number: 333
User prompt
Ensure path nodes are only using floor tiles positions
User prompt
Ensure oath creation takes in consideration current grid offsets and screen size
User prompt
Nodes can only.be placed on floor tiles positionz
User prompt
Make sure the robot only moves on floor tiles and that the creation of the path includes current grid offsets and screen sizes
/****
* Classes
****/
// 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 = 270;
// 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: 403.01
};
var aButton = self.attachAsset('aButton', {
anchorX: 0.5,
anchorY: 0.5,
x: aButtonPosition.x,
y: aButtonPosition.y,
width: buttonSize.width,
height: buttonSize.height
});
// Removed tint application
// 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,
scaleX: LK.screen.width * 0.003,
scaleY: LK.screen.width * 0.003
});
// Move dpad base to the top of the display list so it appears on top of the arrows
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
});
// Refactored D-pad control to use moveCharacter function for cleaner code
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);
});
// Consolidate dpad 'up' event handlers into a single function
function stopMovingCharacter() {
isCharacterMoving = false;
LK.clearInterval(moveInterval);
}
dpadLeft.on('up', stopMovingCharacter);
dpadUp.on('up', stopMovingCharacter);
dpadRight.on('up', stopMovingCharacter);
dpadDown.on('up', stopMovingCharacter);
});
var Robot = Container.expand(function () {
var self = Container.call(this);
var robotGraphics = self.attachAsset('robot', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.9,
scaleY: 0.9
});
// Initialize patrol route for robot
self.patrolRoute = [];
self.currentPatrolIndex = 0;
self.patrolDirection = 1;
// Function to select patrol route
self.selectPatrolRoute = function () {
var numberOfTiles = Math.floor(Math.random() * (10 - 4 + 1)) + 4; // Random number between 4 and 10
var availableFloorTiles = floorTiles.slice(); // Copy of floorTiles to manipulate
while (self.patrolRoute.length < numberOfTiles && availableFloorTiles.length > 0) {
var randomIndex = Math.floor(Math.random() * availableFloorTiles.length);
var selectedTile = availableFloorTiles.splice(randomIndex, 1)[0];
self.patrolRoute.push({
x: selectedTile.x,
y: selectedTile.y
});
}
};
// Function to move robot along patrol route
self.patrol = function () {
if (self.patrolRoute.length === 0) {
return;
} // No patrol route defined
var target = self.patrolRoute[self.currentPatrolIndex];
self.x = target.x;
self.y = target.y;
// Update patrol index
self.currentPatrolIndex += self.patrolDirection;
if (self.currentPatrolIndex === self.patrolRoute.length || self.currentPatrolIndex < 0) {
self.patrolDirection *= -1; // Reverse direction
self.currentPatrolIndex += self.patrolDirection; // Adjust index to stay within bounds
}
};
// Select patrol route on robot creation
self.selectPatrolRoute();
self.update = function () {
self.patrol();
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf9e076 // Fun yellow background
});
/****
* Game 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]) || grid[nextTileIndex] === holeTile) {
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 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
var onScreenController = game.addChild(new OnScreenController());
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,
color: 0x000000
});
game.addChild(gridBackground);
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: gridOffsetX,
y: gridOffsetY
}, {
x: gridOffsetX,
y: gridOffsetY + tileHeight * (gridSize - 1)
}, {
x: gridOffsetX + tileWidth * (gridSize - 1),
y: gridOffsetY
}, {
x: gridOffsetX + tileWidth * (gridSize - 1),
y: gridOffsetY + tileHeight * (gridSize - 1)
}];
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
do {
randomWallTileIndex = Math.floor(Math.random() * wallTiles.length);
randomWallTile = wallTiles[randomWallTileIndex];
} while (isTileInCorner(randomWallTile));
var holeTile = LK.getAsset('holeTile', {
anchorX: 0.5,
anchorY: 0.5,
x: randomWallTile.x,
y: randomWallTile.y
});
game.addChild(holeTile);
game.removeChild(randomWallTile);
wallTiles[randomWallTileIndex] = holeTile;
var isGameStarted = 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
});
var characterShadow = LK.getAsset('characterShadow', {
anchorX: 0.5,
anchorY: 0.5,
x: -15,
y: 50,
scaleX: 0.6,
scaleY: 0.6,
alpha: 0.5
});
character.addChild(characterShadow);
character.currentTile = getTileAtPosition(holeTile.x, holeTile.y); // Correctly initialize character's current tile
game.addChild(character);
// Add a robot to a random floor tile and start its patrol
var randomFloorTileIndex = Math.floor(Math.random() * floorTiles.length);
var randomFloorTile = floorTiles[randomFloorTileIndex];
var robot = game.addChild(new Robot());
robot.x = randomFloorTile.x;
robot.y = randomFloorTile.y;
// Start robot patrol immediately
LK.on('tick', robot.update);
}
});
// 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];
} ===================================================================
--- original.js
+++ change.js
@@ -126,11 +126,44 @@
anchorY: 0.5,
scaleX: 0.9,
scaleY: 0.9
});
- // Patrol logic removed
+ // Initialize patrol route for robot
+ self.patrolRoute = [];
+ self.currentPatrolIndex = 0;
+ self.patrolDirection = 1;
+ // Function to select patrol route
+ self.selectPatrolRoute = function () {
+ var numberOfTiles = Math.floor(Math.random() * (10 - 4 + 1)) + 4; // Random number between 4 and 10
+ var availableFloorTiles = floorTiles.slice(); // Copy of floorTiles to manipulate
+ while (self.patrolRoute.length < numberOfTiles && availableFloorTiles.length > 0) {
+ var randomIndex = Math.floor(Math.random() * availableFloorTiles.length);
+ var selectedTile = availableFloorTiles.splice(randomIndex, 1)[0];
+ self.patrolRoute.push({
+ x: selectedTile.x,
+ y: selectedTile.y
+ });
+ }
+ };
+ // Function to move robot along patrol route
+ self.patrol = function () {
+ if (self.patrolRoute.length === 0) {
+ return;
+ } // No patrol route defined
+ var target = self.patrolRoute[self.currentPatrolIndex];
+ self.x = target.x;
+ self.y = target.y;
+ // Update patrol index
+ self.currentPatrolIndex += self.patrolDirection;
+ if (self.currentPatrolIndex === self.patrolRoute.length || self.currentPatrolIndex < 0) {
+ self.patrolDirection *= -1; // Reverse direction
+ self.currentPatrolIndex += self.patrolDirection; // Adjust index to stay within bounds
+ }
+ };
+ // Select patrol route on robot creation
+ self.selectPatrolRoute();
self.update = function () {
- // Robot update logic without patrol
+ self.patrol();
};
});
/****
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.