Jump to content

The art of waiting...


FlashbackJon
 Share

Recommended Posts

Long story short: new to Phaser, making a roguelike.  Turn-based movement on a tilemap grid, etc.

 

Obviously this ground has been covered before, but it seems that every example of a roguelike or RPG that I've found has basically just set the x/y of the sprite every time a movement occurs, and I'd like to be animating it.  I want to wait for user input, then shut off listening for input, and wait for the chosen action/move (if valid) to finish before the game continues processing the turn.  I've got it tweening well, but I'm not sure, conceptually, what the best practice is for "waiting."  A few games I've seen that use turn-based structure set a variable and then check time in update() to see if sufficient time has passed.  I tried using a timer at first, but ran into unexpected behavior (I'm sure I could fix it, but I'm more interested in knowing what I ought to do)...

 

What I have now (experimenting with the former), but not overly enamored with it:

function update() {    if (acceptInput) {        if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {            acceptInput = false;            moveTimer = game.time.time;            game.add.tween(player.body).to({ x: '-' + TILEWIDTH }, MOVEDURATION, Phaser.Easing.Linear.None, true);        } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {            acceptInput = false;            moveTimer = game.time.time;            game.add.tween(player.body).to({ x: '+' + TILEWIDTH }, MOVEDURATION, Phaser.Easing.Linear.None, true);        } else if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) {            acceptInput = false;            moveTimer = game.time.time;            game.add.tween(player.body).to({ y: '-' + TILEHEIGHT }, MOVEDURATION, Phaser.Easing.Linear.None, true);        } else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {            moveTimer = game.time.time;            acceptInput = false;            game.add.tween(player.body).to({ y: '+' + TILEHEIGHT }, MOVEDURATION, Phaser.Easing.Linear.None, true);        }    }    else     {        if (game.time.time > moveTimer + MOVEDURATION) { acceptInput = true; }    }}

Thoughts?

Link to comment
Share on other sites

Maybe you can add a callback for the moment when tweening ends? And there reset `acceptInput` to true?

Also for this case I would more likely use adding callbacks for key presses with onDown.add rather than checking in update. But maybe that's just my taste.

Link to comment
Share on other sites

Excellent feedback! I also noted the use of sprite.input.disable/enable in another thread, which might make my genetic boolean redundant.

As a related followup, how about just waiting for tweens/animation generically? E.g. I have a dozen NPCs to take their returns before I allow player input again. I process the logic for NPC 6, it has to make a series of moves (chaining tweens), then attack (animation), do I set timers or just use appropriate callback functions for each discrete unit of work my objects are doing?

Link to comment
Share on other sites

I would use a series of events, not timers, so your game knows exactly which stage it is at, at any given time. So each object would emit an event "taking move", "animating", "scanning", whatever, and you'd have a core game loop that knows what to listen for, in what order and how to make the game advance.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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