Ari Thull's Recent Forum Activity

  • I have a array that I made in javascript and I want to the put it into the array object, how would I do that and what type // format of string does JSON load need.

    I've tried making it the same format as it would be when loaded from a fiie

    is their a way so that I can take my array from javascript file and then put it into the array object

    Array Object I want the Javascript array to go into

    Code:

    	export let buildingGrid = [];
    
    export function createBuildingGrid(severity, width = 30, height = 20) {
     const styles = [
     "default", "cabin", "skyscraper", "pillars", "roman",
     "stripes", "windows", "battlements", "zigzag", "tower",
     "step-pyramid", "arch", "roof", "blocky", "industrial",
     "grid", "frame", "modern", "lighthouse", "castle",
     "barn", "hut", "crenellations", "glass", "hex"
     ];
    
     const style = styles[Math.floor(Math.random() * styles.length)];
    
     const grid = Array.from({ length: height }, () =>
     Array.from({ length: width }, () => 0)
     );
    
     const fillPoints = [];
    
     // Helper to queue fill points based on style logic
     function draw(fx) {
     for (let y = 0; y < height; y++) {
     for (let x = 0; x < width; x++) {
     if (fx(x, y)) {
     fillPoints.push([y, x]);
     }
     }
     }
     }
    
     const stylesMap = {
     default: () => draw(() => true),
     cabin: () => draw((x, y) => x > 5 && x < 24 && y > 10 && y < 18),
     skyscraper: () => draw((x, y) => x === 14 || x === 15 || y % 2 === 0),
     pillars: () => draw((x, y) => x % 3 === 0),
     roman: () => draw((x, y) => y === 0 || y === height - 1 || x % 4 === 0),
     stripes: () => draw((x, y) => y % 2 === 0),
     windows: () => draw((x, y) => x % 3 === 1 && y % 2 === 1),
     battlements: () => draw((x, y) => y === 0 || (y === 1 && x % 2 === 0)),
     zigzag: () => draw((x, y) => (x + y) % 5 === 0),
     tower: () => draw((x, y) => x === 14 || x === 15),
     "step-pyramid": () => draw((x, y) => x >= y && x < width - y),
     arch: () => draw((x, y) => y === height - 1 || (y < 3 && x % 4 === 0)),
     roof: () => draw((x, y) => y >= 18 || y === Math.abs(15 - x)),
     blocky: () => draw((x, y) => x % 2 === 0 && y % 2 === 0),
     industrial: () => draw((x, y) => x % 4 === 0 && y % 3 === 0),
     grid: () => draw((x, y) => x % 2 === 0 || y % 2 === 0),
     frame: () => draw((x, y) => x === 0 || x === width - 1 || y === 0 || y === height - 1),
     modern: () => draw((x, y) => (x + y) % 3 === 0),
     lighthouse: () => draw((x, y) => x === Math.floor(width / 2)),
     castle: () => draw((x, y) => y === height - 1 || (y === 0 && x % 3 !== 1)),
     barn: () => draw((x, y) => y > 12 && y < 18 && x > 10 && x < 20),
     hut: () => draw((x, y) => y > 14 && x === Math.floor(width / 2)),
     crenellations: () => draw((x, y) => y === height - 1 || (y === 0 && x % 2 === 0)),
     glass: () => draw((x, y) => (x + y) % 2 === 0),
     hex: () => draw((x, y) => (x + y) % 6 === 0 || (x - y + width) % 6 === 0)
     };
    
     if (stylesMap[style]) stylesMap[style]();
    
     // Shuffle and limit the fill points to match severity
     fillPoints.sort(() => Math.random() - 0.5);
     const limitedPoints = fillPoints.slice(0, severity);
    
     for (const [y, x] of limitedPoints) {
     grid[y][x] = 1;
     }
    
     buildingGrid = grid;
    
     console.log(`Generated style: ${style}, 1s placed: ${limitedPoints.length}`);
     
     
    
    //Replace items chars in grid string until its in correct format
    var grid_Out
    grid_Out = '{"c2array":true,"size":[30,20,1],"data":' + (JSON.stringify(grid)) +"}";
    console.log(grid_Out);
     return (grid_Out);
    
    }
    
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
Ari Thull's avatar

Ari Thull

Member since 3 Sep, 2024

Twitter
Ari Thull has 1 followers

Trophy Case

  • RTFM Read the fabulous manual
  • Email Verified

Progress

2/44
How to earn trophies