Search the Community
Showing results for tags 'phaser tilemap roguelike'.
-
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.