game_space_space_manager.js

import Space from "./space.js";

/**
 * @module SpaceManager
 * @fileoverview Global manager of spaces.
 */

/**
 * @class
 * Global manager for spaces.
 */
class SpaceManager {
    /**
     * Create a new space manager.
     * @constructor
     */
    constructor() {
        this._spaces = new Map();
    }


    //
    // Management
    //

    /**
     * Create and add a new space.
     * @param {string} id Identifier of space
     * @param type Type of space
     */
    new(id, type = Space) {
        this._spaces.set(id, new type());
    }

    /**
     * Add a space.
     * @param {string} id Identifier of space
     * @param {Space} space Space to add
     * @returns {Space} Space that was added.
     */
    add(id, space) {
        this._spaces.set(id, space);

        space.id = id;

        return space;
    }

    /**
     * Remove a specific space.
     * @param {string} id Identifier of space to remove
     */
    remove(id) {
        this._spaces.delete(id);
    }


    //
    // Access & Usage
    //

    /**
     * Get a space by its identifier.
     * @param {string} id Identifier of space
     * @returns {Space|null} Space with specified identifier, or null if it does not exist.
     */
    get(id) {
        if (!this._spaces.has(id))
            return null;

        return this._spaces.get(id);
    }

    /**
     * Open a space by its identifier.
     * @param {string} id Identifier of space to open
     */
    open(id) {
        if (!this._spaces.has(id))
            return;

        $$.app.space = this._spaces.get(id);
    }
}

export default SpaceManager;