MZ How to draw code from an array struct?

Hello, long time no see, right? Anyway, I found a way to store tiles in an array struct to form the fossil rocks and the fossils inside for my RPG Maker MZ Love letter to the Fossil Fighters series, Dragon Bones. All I need to know is how you draw them from an array struct using a JS Draw Code. I plan to do it similarly to how the CCG Plugin was programmed. In this case, I plan to use notes created directly from my plugin.

Below is the code I'm currently using for the plugin, credit goes to ct_bolt for giving me the template to help me get started.

JavaScript:
//=============================================================================
// RPG Maker MZ - GreenTree Studios' Fossil Cleaning Core Plugin
//=============================================================================
/*~struct~BGM:
 * @param name
 * @text Filename
 * @type file
 * @dir audio/bgm/
 * @default
 *
 * @param volume
 * @text Volume
 * @type number
 * @default 90
 *
 * @param pitch
 * @text Pitch
 * @type number
 * @default 100
 *
 * @param pan
 * @text Pan
 * @type number
 * @default 0
 *
 */
/*~struct~RockTemplate:
 * @param rockImages
 * @text Rock Graphics
 * @default
 *
 * @param Layer1
 * @text Top Layer
 * @parent rockImages
 * @type file
 * @dir img/rocks
 * @desc Tileset for the top layer of the fossil rock
 *
 * @param Layer2
 * @text Middle Layer
 * @parent rockImages
 * @type file
 * @dir img/rocks
 * @desc Tileset for the middle layer of the fossil rock
 *
 * @param Layer3
 * @text Bottom Layer
 * @parent rockImages
 * @type file
 * @dir img/rocks
 * @desc Tileset for the bottom layer of the fossil rock
 *
 * @param RockDrawCode:func
 * @text JS: Rock Draw Code
 * @type note
 * @desc Code used to draw coordinates on the rock tiles.
 */
/*:
 * @target MZ
 * @plugindesc [RPG Maker MZ] [Tier 1] [Version 1.00] [GT_Studios - Fossil Cleaning Core]
 * @author GT_Studios
 * @url https://greentree-studios.itch.io/
 *
 * @param RockTemplates:ArrayStruct
 * @text Rock Shapes
 * @type struct<RockTemplate>[]
 * @desc A list of the templates used for the rock layers and how they are visibly drawn.
 *
 * @param Templates:ArrayStruct
 * @text Fossil Templates
 * @type struct<FossilTemplate>[]
 * @desc A list of the templates used for the fossils and how they are visibly drawn.
 *
 * @param BGM
 * @text Default BGM
 * @type struct<BGM>
 * @desc Default BGM (Optional)
 * @default
 *
 * @param HoleMaskTile
 * @text Hole Mask Tileset
 * @type file
 * @dir img/layers/
 * @desc the tileset used to mask out the layers, thus making holes.
 *
 * @param TimeAmount
 * @text Timer
 * @type number
 * @desc The Time Limit (in seconds)
 * @default 120
 *
 * @param cleaningBackground
 * @text Cleaning Background
 * @type file
 * @dir img/cleaningbackground/
 * @desc The background graphic for the cleaning scene.
 * @default Cleaning_BKG
 */


/*~struct~FossilTemplate:
 * @param Name:str
 * @text Name
 * @desc The name of this template. Used in code, too.
 * @default None
 *
 * @param FossilTiles
 * @text Fossil Graphics
 *
 * @param HeadFossilTiles:str
 * @text Head
 * @parent FossilTiles
 * @type file
 * @dir img/fossils
 * @desc Tileset file for the head fossil
 * @default
 *
 * @param HeadRockID:num
 * @text Rock Shape
 * @parent HeadFossilTiles:str
 * @type number
 * @desc Fossil Rock Shape ID (Head)
 * @default 1
 *
 * @param FossilHeadDamageLayer:str
 * @text Damage Layer (Head)
 * @parent HeadFossilTiles:str
 * @type file
 * @dir img/fossils
 * @desc Fossil Damage Layer (Head)
 * @default
 *
 * @param BodyFossilTiles:str
 * @text Body
 * @parent FossilTiles
 * @type file
 * @dir img/fossils
 * @desc Tileset file for the body fossil
 * @default
 *
 * @param BodyRockID:num
 * @text Rock Shape
 * @parent BodyFossilTiles:str
 * @type number
 * @desc Fossil Rock Shape ID (Body)
 * @default 1
 *
 * @param FossilBodyDamageLayer:str
 * @text Damage Layer (Body)
 * @parent BodyFossilTiles:str
 * @type file
 * @dir img/fossils
 * @desc Fossil Damage Layer (Body)
 * @default
 *
 * @param ArmsFossilTiles:str
 * @text Arms
 * @parent FossilTiles
 * @type file
 * @dir img/fossils
 * @desc Tileset file for the arm fossils
 * @default
 *
 * @param ArmsRockID:num
 * @text Rock Shape
 * @parent ArmsFossilTiles:str
 * @type number
 * @desc Fossil Rock Shape ID (Arms)
 * @default 1
 *
 * @param LegsFossilTiles:str
 * @text Legs
 * @parent FossilTiles
 * @type file
 * @dir img/fossils
 * @desc Tileset file for the leg fossils
 * @default
 *
 * @param LegsRockID:num
 * @text Rock Shape
 * @parent LegsFossilTiles:str
 * @type number
 * @desc Fossil Rock Shape ID (Legs)
 * @default 1
 *
 * @param TailFossilTiles:str
 * @text Tail
 * @parent FossilTiles
 * @type file
 * @dir img/fossils
 * @desc Tileset file for the tail fossil
 * @default
 *
 * @param TailRockID:num
 * @text Rock Shape
 * @parent TailFossilTiles:str
 * @type number
 * @desc Fossil Rock Shape ID (Tail)
 * @default 1
 *
 * @param FullBodyFossilTiles:str
 * @text Full Body
 * @parent FossilTiles
 * @type file
 * @dir img/fossils
 * @desc Tileset file for the full body fossil
 * @default
 *
 * @param FullBodyRockID:num
 * @text Rock Shape
 * @parent FullBodyFossilTiles:str
 * @type number
 * @desc Fossil Rock Shape ID (Full Body)
 * @default 1
 *
 * @param FossilDrawCode:func
 * @text JS: Fossil Draw Code
 * @type note
 * @desc Code used to draw coordinates on the fossil rock tiles.
 */

var GTS = GTS || {}; GTS.FossilCleaning  = GTS.FossilCleaning || {};
var Imported = Imported || {}; Imported["GreenTreeStudios_FossilCleaning"] = 1.00;

(($_$) => {
    const NAMESPACE   = 'FossilCleaning';
    const PLUGIN_NAME = 'GTS_' + NAMESPACE;

    function getPluginParameters() {var a = document.currentScript || (function() { var b = document.getElementsByTagName('script'); return b[b.length - 1]; })(); return PluginManager.parameters(a.src.substring((a.src.lastIndexOf('/') + 1), a.src.indexOf('.js')));} $_$.params = getPluginParameters();
  
    ['BGM'].forEach(function(v){
        $_$.params[v] = $_$.params[v] || '""';
        $_$.params[v] = JSON.parse($_$.params[v]);
    });
  
    $_$.bgm = $_$.params['BGM'];
    $_$.fossilRock = $_$.params['RockTemplates:ArrayStruct'];
    $_$.rockDrawCode = $_$.params['RockDrawCode:func'];
    $_$.fossil = $_$.params['Templates:ArrayStruct'];
    $_$.fossilDrawCode = $_$.params['FossilDrawCode:func'];
    $_$.holeTiles = $_$.params['HoleMaskTile'];
    $_$.cleaningIMG = $_$.params['cleaningBackground'];



    //-----------------------------------------------------------------------------
    // FossilCleaningManager
    //
    // The static class that assists with the Slot Game progress.

    function FossilCleaningManager() {
        throw new Error("This is a static class");
    }

    FossilCleaningManager.setup = function() {
      
    };

    FossilCleaningManager.saveBgmAndBgs = function() {
        this._mapBgm = AudioManager.saveBgm();
        this._mapBgs = AudioManager.saveBgs();
    };

    FossilCleaningManager.replayBgmAndBgs = function() {
        if (this._mapBgm) { 
            AudioManager.replayBgm(this._mapBgm);
        } else {
            AudioManager.stopBgm();
        }
        if (this._mapBgs) {
            AudioManager.replayBgs(this._mapBgs);
        }
    };

    FossilCleaningManager.playBgm = function(audio) {
        audio = audio || $_$.bgm || {};
        console.log(audio);
        AudioManager.playBgm(audio);
        AudioManager.stopBgs();
    };

    FossilCleaningManager.stopAudioOnStart = function(audio) {
        audio = audio || GTS.FossilCleaning.bgm || {};
        if (!AudioManager.isCurrentBgm(audio)) {
            AudioManager.stopBgm();
        };
        AudioManager.stopBgs();
        AudioManager.stopMe();
        AudioManager.stopSe();
    };

    //-----------------------------------------------------------------------------
    // FossilDamageSprite
    //
    function FossilDamageSprite() {
        this.initialize.apply(this, arguments);
    }

    FossilDamageSprite.prototype = Object.create(Sprite.prototype);
    FossilDamageSprite.prototype.constructor = FossilDamageSprite;

    FossilDamageSprite.prototype.initialize = function (bitmap) {
        Sprite.prototype.initialize.call(this, bitmap);
    };

    //-----------------------------------------------------------------------------
    // CleaningBackgroundSprite
    //

    function CleaningBackgroundSprite() {
        this.initialize.apply(this, arguments);
    }

    CleaningBackgroundSprite.prototype = Object.create(Sprite.prototype);
    CleaningBackgroundSprite.prototype.constructor = CleaningBackgroundSprite;

    CleaningBackgroundSprite.prototype.initialize = function (bitmap) {
        Sprite.prototype.initialize.call(this, bitmap);
    };


    //-----------------------------------------------------------------------------
    // FossilSprite
    //

    function FossilSprite() {
        this.initialize.apply(this, arguments);
    }

    FossilSprite.prototype = Object.create(Sprite.prototype);
    FossilSprite.prototype.constructor = FossilSprite;

    FossilSprite.prototype.initialize = function (bitmap) {
        Sprite.prototype.initialize.call(this, bitmap);
    };

    //-----------------------------------------------------------------------------
    // RockSprite
    //

    function RockSprite() {
        this.initialize.apply(this, arguments);
    }

    RockSprite.prototype = Object.create(Sprite.prototype);
    RockSprite.prototype.constructor = RockSprite;

    RockSprite.prototype.initialize = function (bitmap) {
        Sprite.prototype.initialize.call(this, bitmap);
    };

    //------------------------------------------------------------------------------
    // Scene FossilCleaning
    //

    function Scene_FossilCleaning() {
        this.initialize(...arguments);
    }

    Scene_FossilCleaning.prototype = Object.create(Scene_MenuBase.prototype);
    Scene_FossilCleaning.prototype.constructor = Scene_FossilCleaning;

    $_$['Scene_FossilCleaning.prototype.isReady'] = Scene_FossilCleaning.prototype.isReady;
    Scene_FossilCleaning.prototype.isReady = function() {
        return $_$['Scene_FossilCleaning.prototype.isReady'].apply(this, arguments) && this._imagesLoaded >= this._imagesLoadCount;
    };

    Scene_FossilCleaning.prototype.imageLoaded = function () {
        this._imagesLoaded++;
    };

    Scene_FossilCleaning.prototype.initialize = function () {
        this._imagesLoaded = 0;
        this._imagesLoadCount = 2;
      
        $_$._cleaningBackgroundBitmap = ImageManager.loadBitmap($_$.cleaningIMG);
        $_$._cleaningBackgroundBitmap.addLoadListener(this.imageLoaded.bind(this)); 
        $_$._fossilBitmap = ImageManager.loadBitmap();
        $_$._fossilBitmap.addLoadListener(this.imageLoaded.bind(this));     
        $_$._rockBitmap = ImageManager.loadBitmap();
        $_$._rockBitmap.addLoadListener(this.imageLoaded.bind(this));
      
        Scene_MenuBase.prototype.initialize.call(this);
    };

    Scene_FossilCleaning.prototype.create = function () {
        this.createBackground();
        this.createFossil();
        this.createRock();
    };

    Scene_FossilCleaning.prototype.createBackground = function () {     
        this._backgroundSprite = new CleaningBackgroundSprite($_$._cleaningBackgroundBitmap);
        this.addChild(this._backgroundSprite);
    };

    Scene_FossilCleaning.prototype.createFossil = function() {
        this._fossilPieces = [];
      
        // sprite settings
        const cellSize = 24;
        const cols  = $_$._fossilBitmap.width  / cellSize;
        const rows  = $_$._fossilBitmap.height / cellSize;
  
        for (var i = 0; i < cols*rows; i++) {
            var row = Math.floor(i / cols);
            var col = i % cols;
            this._fossilPieces[i] = new FossilSprite($_$._fossilBitmap);
            this._fossilPieces[i].setFrame(col*cellSize, row*cellSize, cellSize, cellSize);
            this._fossilPieces[i].x = col * cellSize;
            this._fossilPieces[i].y = row * cellSize;
            this._fossilPieces[i].visible = false;
            this.addChild(this._fossilPieces[i]);
        };
      
    }

    Scene_FossilCleaning.prototype.createRock = function() {
        this._rockPieces = [];
      
        // sprite settings
        const cellSize = 24;
        const cols  = $_$._rockBitmap.width  / cellSize;
        const rows  = $_$._rockBitmap.height / cellSize;
  
        for (var i = 0; i < cols*rows; i++) {
            var row = Math.floor(i / cols);
            var col = i % cols;
            this._rockPieces[i] = new RockSprite($_$._rockBitmap);
            this._rockPieces[i].setFrame(col*cellSize, row*cellSize, cellSize, cellSize);
            this._rockPieces[i].x = col * cellSize;
            this._rockPieces[i].y = row * cellSize;
            this._rockPieces[i].visible = false;
            this.addChild(this._rockPieces[i]);
        }
    };

    //-----------------------------------------------------------------------------
    // GTS.FossilsMZ
    //-----------------------------------------------------------------------------
  
    GTS.FossilCleaning.Play = function(){
        FossilCleaningManager.saveBgmAndBgs();
        FossilCleaningManager.stopAudioOnStart();
      
        SceneManager.push(Scene_FossilCleaning); 
    };

})(GTS.FossilCleaning, this);

Below is a sample image of a fossil Rock I created in Krita if you need an example to work from:
Rock_15.png

Plus the fossil tiles:
1_H.png

You will need to create certain folders in the img folder: fossils and rocks respectively.

Thank you in advance.
One more thing I forgot to mention, below is the draw code I use for the fossil tiles:


JavaScript:
for (var i = 0; i < cols*rows; i++) {
    var row = Math.floor(i / cols);
    var col = i % cols;
    this._fossilPieces[i] = new FossilSprite($_$._fossilBitmap);
    this._fossilPieces[i].setFrame(col*cellSize, row*cellSize, cellSize, cellSize);
    this._fossilPieces[i].x = col * cellSize;
    this._fossilPieces[i].y = row * cellSize;
    this._fossilPieces[i].visible = false;
    this.addChild(this._fossilPieces[i]);
};

for (var i = 0; i < 3; i++) {
    this._fossilPieces[i].x = (cellSize*21)+(cellSize*i);
    this._fossilPieces[i].visible = true;
  }
  for (var i = 3; i < 9; i++) {
    this._fossilPieces[i].x = (cellSize*15)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*2);
  }
  for (var i = 9; i < 21; i++) {
    this._fossilPieces[i].x = (cellSize*13)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*3);
  }
  for (var i = 21; i < 36; i++) {
    this._fossilPieces[i].x = (cellSize*11)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*4);
  }
 
  for (var i = 36; i < 57; i++) {
    this._fossilPieces[i].x = (cellSize*9)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*5);
  }
 
  for (var i = 57; i < 80; i++) {
    this._fossilPieces[i].x = (cellSize*5)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*6);
  }
 
  for (var i = 80; i < 105; i++) {
    this._fossilPieces[i].x = (cellSize*3)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*7);
  }
 
  for (var i = 105; i < 131; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*8);
  }
 
  for (var i = 131; i < 155; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*9);
  }
 
  for (var i = 155; i < 180; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*10);
  }
 
  for (var i = 180; i < 198; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*11);
  }
 
  for (var i = 198; i < 221; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*12);
  }
 
  for (var i = 221; i < 243; i++) {
    this._fossilPieces[i].x = (cellSize*2)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*13);
  }
 
  for (var i = 243; i < 265; i++) {
    this._fossilPieces[i].x = (cellSize*4)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*14);
  }
 
  for (var i = 265; i < 286; i++) {
    this._fossilPieces[i].x = (cellSize*2)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*15);
  }
 
  for (var i = 286; i < 307; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*16);
  }
 
  for (var i = 307; i < 325; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*17);
  }
 
  for (var i = 325; i < 341; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*18);
  }
  for (var i = 341; i < 348; i++) {
    this._fossilPieces[i].x = (cellSize*1)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*19);
  }
One more thing I forgot to mention, below is the draw code I use for the fossil tiles:

Just reading through the draw code, I see a couple of potential issues.
First, x and y coords setting. You're doing it in both the large loop where you create them:
Code:
for (var i = 0; i < cols*rows; i++) {
    ...
    this._fossilPieces[i].x = col * cellSize;
    this._fossilPieces[i].y = row * cellSize;
    this._fossilPieces[i].visible = false;
    this.addChild(this._fossilPieces[i]);
};

And in the subsequent smaller loops where, I guess, you set their precise position:
Code:
for (var i = 3; i < 9; i++) {
    this._fossilPieces[i].x = (cellSize*15)+(cellSize*i);
    this._fossilPieces[i].y = (cellSize*2);
  }

You're setting x and y twice and I don't understand why. Reading through the code, it would also seem to me that you set the Y bigger than you'd want.
The first row will be set at 0, second row at cellSize*2 while you'd want it at cellSize.

Nevertheless, there are bigger issues.
You use
Code:
+ (cellSize * i)
in the smaller for loops to set X coords. Which, given that i is equal to rows * cols, means that cellSize*i reaches absurdly high numbers very quickly and makes the sprite render off screen.
If you want it to render from the left, perhaps you wanted sth like
Code:
var min = 0;
for (var i = min; i < 3; i++) {
    this._fossilPieces[i].x = (cellSize*21)+(cellSize*(i-min));
  }
min = 3;
  for (var i = min; i < 9; i++) {
    this._fossilPieces[i].x = (cellSize*15)+(cellSize*(i-min));
    this._fossilPieces[i].y = cellSize;
  }
min = 9;
  for (var i = min; i < 21; i++) {
    this._fossilPieces[i].x = (cellSize*13)+(cellSize*(i-min));
    this._fossilPieces[i].y = (cellSize*2);
  }
?

Plus, the sprites won't render at all for the most part. You set their ._visible to false in the large for loop, but when you look at the smaller for loops, you set the ._visible tag to true only in the first of the small loops. Meaning, most of your sprites will not render at all.
If you're going to render the sprites immediately, don't set the visible to false only to then immediately set it to true - it's pointless.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-draw-code-from-an-array-struct.169641/