Code viewer for World: New World
 


//---- normal P5 code -------------------------------------------------------

// Configuration
// Configuration
let cols, rows;
let w = 40; // Width of each cell
let grid = [];
let roadColor = '#D3D3D3'; // Light gray for roads
let wallColor = '#000000'; // Black for walls

function setup() {
  createCanvas(800, 800);
  cols = floor(width / w);
  rows = floor(height / w);
  
  // Create a grid
  for (let i = 0; i < cols; i++) {
    grid[i] = [];
    for (let j = 0; j < rows; j++) {
      grid[i][j] = {
        road: false,
        wallLeft: false,
        wallRight: false
      };
    }
  }
  
  generateStreets();
}

function draw() {
  background(255);
  drawStreets();
}

function generateStreets() {
  // Start from a random position
  let startX = floor(random(cols));
  let startY = floor(random(rows));
  let direction = floor(random(4)); // Random initial direction (0: up, 1: right, 2: down, 3: left)
  
  for (let i = 0; i < 200; i++) {
    if (startX < 0 || startX >= cols || startY < 0 || startY >= rows) {
      break; // Stop if out of bounds
    }
    
    // Mark the current cell as a road
    grid[startX][startY].road = true;
    
    // Mark walls on either side of the road
    if (direction === 0 && startY > 0) { // Up
      grid[startX][startY - 1].wallBottom = true; // Bottom wall of the cell above
    }
    if (direction === 2 && startY < rows - 1) { // Down
      grid[startX][startY + 1].wallTop = true; // Top wall of the cell below
    }
    if (direction === 1 && startX < cols - 1) { // Right
      grid[startX + 1][startY].wallLeft = true; // Left wall of the cell to the right
    }
    if (direction === 3 && startX > 0) { // Left
      grid[startX - 1][startY].wallRight = true; // Right wall of the cell to the left
    }

    // Randomly decide whether to turn left or right (random new direction)
    if (random() < 0.3) {
      direction = (direction + (random() < 0.5 ? -1 : 1) + 4) % 4; // Turn left or right
    }

    // Move in the current direction
    if (direction === 0) startY--; // Up
    if (direction === 1) startX++; // Right
    if (direction === 2) startY++; // Down
    if (direction === 3) startX--; // Left
  }
}

function drawStreets() {
  for (let i = 0; i < cols; i++) {
    for (let j = 0; j < rows; j++) {
      // Draw roads
      if (grid[i][j].road) {
        fill(roadColor);
        rect(i * w, j * w, w, w);
      }
      
      // Draw walls
      if (grid[i][j].wallLeft) {
        fill(wallColor);
        rect(i * w, j * w, 2, w); // Wall on the left
      }
      if (grid[i][j].wallRight) {
        fill(wallColor);
        rect(i * w + w - 2, j * w, 2, w); // Wall on the right
      }
    }
  }
}