stolsky / the-smoking-mirror

GNU General Public License v3.0
0 stars 0 forks source link

create statestack related game loop #20

Closed stolsky closed 2 years ago

stolsky commented 2 years ago

index.js


import Tick from "";
import Application from "";
import { getDefaultRendererContext } from "";

import GameStates from "./game_states.js";
import * as SetupState from "./setup_state.js";

const app = new Application();
const ctx = getDefaultRendererContext();

// push initial state(s) to gameStates
GameStates.push(SetupState);

// game loop
Tick.start((dt) => {

    GameStates.update(dt);
    GameStates.render(ctx);

    if (GameStates.isEmpty()) {
        Tick.stop();
        throw new Error("The game states stack is empty.");
    }
});
stolsky commented 2 years ago

game_states.js

import StateStack from "";

const GameStates = new StateStack();

export default GameStates;
stolsky commented 2 years ago

setup_state.js


let ready = false;

// ...

const enter = () => {
    // load resources
};

const update = () => {
    if (ready) {
        GameStates.pop();
        GameStates.push(...);
    }
};

export {
    update,
    render,
    enter,
    exit
}