game_space_world_world.js

import WorldGenerator from "./gen/world_generator.js";
import Vec2 from "../../vector/vec2.js";
import Space from "../space.js";

/**
 * @module World
 * @fileoverview Contains World class.
 */

/**
 * @class
 * @extends Space
 * Special space that uses a generator to automatically make chunk units.
 */
class World extends Space {
    /**
     * Create a new world.
     * @param {App} app Parent application
     * @param {number} chunkSize Number of units per chunk
     * @param {number} unitSize Number of pixels per unit
     * @constructor
     */
    constructor(app, chunkSize, unitSize) {
        super(app, chunkSize, unitSize);

        this.generator = new WorldGenerator();
    }

    chunkAdded(chunk) {
        const s = this.chunkSize * this.unitSize;
        const xo = chunk.x * s;
        const yo = chunk.y * s;

        for (let x = 0; x < chunk.units.length; x++) {
            for (let y = 0; y < chunk.units[x].length; y++) {
                chunk.units[x][y] = this.generator.get(chunk, xo + x * this.unitSize, yo + y * this.unitSize,
                    32);
            }
        }

        // if ($$.flags.chunks.cache) {
        //     $$.createImage(ctx => {
        //         for (let x = 0; x < chunk.units.length; x++) {
        //             for (let y = 0; y < chunk.units[x].length; y++)
        //                 chunk.units[x][y].draw(ctx);
        //         }
        //     }, image => chunk.cachedUnits = image, this.chunkSize, this.chunkSize);
        // }
    }
}

export default World;