git @ Cat's Eye Technologies Cosmos-Boulders / master src / cosmos-boulders / main.js
master

Tree @master (Download .tar.gz)

main.js @masterraw · history · blame

// SPDX-FileCopyrightText: In 2019, Chris Pressey, the original author of this work, placed it into the public domain.
// SPDX-License-Identifier: Unlicense
// For more information, please refer to <https://unlicense.org/>


import { Hardware } from './hardware.js';
import { SCREEN_WIDTH, SCREEN_HEIGHT } from './utils.js';
import { updateInputContext } from './input-context.js';
import { drawGame } from './display.js';


/* --[ Main driver ]-------------------------------------------------------- */


function launch(config) {
    const canvas = document.createElement('canvas');
    canvas.width = SCREEN_WIDTH;
    canvas.height = SCREEN_HEIGHT;
    canvas.tabIndex = 0;
    config.container.appendChild(canvas);

    let store = null;
    (new Hardware()).init({
        canvas: canvas,
        onSetup: function(hardware) {
            store = Redux.createStore(updateInputContext);
            function render() {
                drawGame(store.getState().get("game"), hardware.screen.canvas, hardware.screen.ctx);
            }
            render();
            store.subscribe(render);
        },
        onFrameReady: function(hardware) {
            store.dispatch({
                type: "FRAME_READY",
                leftPressed: hardware.leftPressed,
                rightPressed: hardware.rightPressed,
                thrustPressed: hardware.thrustPressed,
                firePressed: hardware.firePressed,
                startPressed: hardware.startPressed
            });
        },
        onCoinInserted: function(hardware) {
            store.dispatch({
                type: "COIN_INSERTED"
            });
        }
    });

    canvas.focus();
}

export { launch };