Jump to content

Rapidly change state without repeated calls to create()?


SoulBeaver
 Share

Recommended Posts

Hello,

 

 

I'm programming a roguelike and I've divided the main game into three distinct states:  PlayerTurn, Animating, EnemyTurn. I do this to separate game logic and not have a humongous God class. Each state calls the other in a loop:  PlayerTurn -> Animating -> EnemyTurn -> Animating -> PlayerTurn

 

PlayerTurn:

        update() {            var keyboard = this.game.input.keyboard;                            for (var inputCommand in this.actionMap) {                var keyCode = toKeyCode(inputCommand);                if (keyboard.isDown(keyCode)) {                    this.actionMap[inputCommand]();                    this.switchToAnimatingState();                }            }        }

AnimatingState:

        init(args: any) {            console.log("In AnimatingState.");            this.view = args[0];            this.map = args[1];            this.player = args[2];            this.nextState = args[3];            this.view.onTweensFinished.addOnce(() => this.switchToNextState(), this);            this.view.play();        }

EnemyTurn:

        update() {            // No logic yet.            this.game.state.start(State.AnimatingState, false, false, [this.view, this.map, this.player, State.PlayerState]);        }

Anyway, all three States have the init(args) function. What I also noticed is that each game.state.start() call destroys, preloads, and creates every state new again. I don't really need, nor do I want, to perform my initialization logic twice. I have my fully constructed view, tilemap, and player class that I'm passing around.

 

In order to avoid creating my class twice and returning to the beginning game state, I have a static (global in js) variable:

        create() {            if (!PlayerState.hasBeenCreated) {                this.game.stage.backgroundColor = "#000000";                this.initializeView();                this.initializeInputBindings();                this.initializeMap();                this.initializePlayer();                PlayerState.hasBeenCreated = true;            }        }

That's pretty ugly and I wish it'd go away. Is it possible to switch states without destruction/update? My other solution was to have one GameState, and it controls which substate to process at the moment.

Link to comment
Share on other sites

This isn't really how States have been designed. You could still do this but I wouldn't use any of the pre-defined methods (create, preload, etc) and instead would just call my own custom functions as needed, keeping track of it myself. Which isn't far off what you've got. Also you could just do:

create() {  if (PlayerState.hasBeenCreated) { return; }  ....}
Link to comment
Share on other sites

Thanks for the heads-up! Yeah, I figured that I was trying to abuse the Phaser States. I've since refactored Player, Enemy and Animating into SubStates that my InGame state controls. Works a lot better that way, and I don't have to destroy or recreate the SubStates.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...