excaliburjs / Excalibur

🎮 Your friendly TypeScript 2D game engine for the web 🗡️
https://excaliburjs.com
BSD 2-Clause "Simplified" License
1.83k stars 192 forks source link

Add utility method to determine if Engine is paused or not #750

Closed jedeen closed 7 years ago

jedeen commented 7 years ago

Context

Currently, you can stop/start (i.e. pause/resume) a game with Engine.start() and Engine.stop(). However, in order to implement pause logic, you need to manage your own boolean:

var paused = false;
game.on('p', () => {
   if (!paused) {
      game.stop();
   } else {
      game.start();
   }
   paused != paused;
});

Proposal

The Engine already has a private _hasStarted variable that appears to be maintaining, but not exposing, this status.

It would be helpful to have an isStopped() or isPaused() method to return the current state of the game, so the above code could be simplified to:

game.on('p', () => {
   if (!game.isPaused()) {
      game.stop();
   } else {
      game.start();
   }
});
kamranayub commented 7 years ago

If you implement your game UI as all HTML this also works really well, since if the game is stopped no updates/processing will happen but HTML will work fine.

SurajGoel commented 7 years ago

Can I take this ?

eonarheim commented 7 years ago

@SurajGoel For sure! Please do, let us know if you need any assisstance :)

SurajGoel commented 7 years ago

@eonarheim Sorry, didn't see your post. On it now, will update you soon.

SurajGoel commented 7 years ago

@eonarheim How should I write a unit test for this ? A simple idea :- Maybe run the engine first, check the status, stop the engine, check the status again. And should I put the test in Utility Functions test suite ?

jedeen commented 7 years ago

@SurajGoel Sounds like a good way to do it! You'll probably want to test that it can be un-paused as well.

This is Engine functionality, so I'd recommend putting the test in EngineSpec.ts

Now that I'm thinking about it, this is more of a test of the Engine.start() and Engine.stop() methods. @excaliburjs/core-contributors, what do you think?