extra-game-loop 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16627,22 +16627,18 @@ var State;
16627
16627
  State["Stopped"] = "stopped";
16628
16628
  State["Running"] = "running";
16629
16629
  })(State || (State = {}));
16630
- var Event;
16631
- (function (Event) {
16632
- Event["Start"] = "start";
16633
- Event["Stop"] = "stop";
16634
- })(Event || (Event = {}));
16635
16630
  const schema = {
16636
- [State.Stopped]: { [Event.Start]: State.Running },
16637
- [State.Running]: { [Event.Stop]: State.Stopped }
16631
+ [State.Stopped]: { start: State.Running },
16632
+ [State.Running]: { stop: State.Stopped }
16638
16633
  };
16639
16634
  class GameLoop {
16640
16635
  constructor(options) {
16641
16636
  this.fsm = new es2018$e.FiniteStateMachine(schema, State.Stopped);
16642
- this.frames = 0;
16643
16637
  this.deltaTimeAccumulator = 0;
16638
+ this.lastDeltaTime = 0;
16644
16639
  this.loop = (timestamp) => {
16645
16640
  const deltaTime = timestamp - this.lastTimestamp;
16641
+ this.lastDeltaTime = deltaTime;
16646
16642
  this.lastTimestamp = timestamp;
16647
16643
  this.nextFrame(deltaTime);
16648
16644
  this.requstId = requestAnimationFrame(this.loop);
@@ -16655,21 +16651,22 @@ class GameLoop {
16655
16651
  this.render = options.render;
16656
16652
  }
16657
16653
  start() {
16658
- this.fsm.send(Event.Start);
16654
+ this.fsm.send('start');
16659
16655
  const timestamp = performance.now();
16660
- this.loopStartTimestmap = timestamp;
16661
16656
  this.lastTimestamp = timestamp;
16662
16657
  this.loop(timestamp);
16663
16658
  }
16664
16659
  stop() {
16665
- this.fsm.send(Event.Stop);
16660
+ this.fsm.send('stop');
16666
16661
  cancelAnimationFrame(this.requstId);
16662
+ delete this.requstId;
16663
+ delete this.lastTimestamp;
16664
+ this.lastDeltaTime = 0;
16667
16665
  }
16668
16666
  getFramesOfSecond() {
16669
16667
  if (this.fsm.matches(State.Running)) {
16670
- const elapsedMs = performance.now() - this.loopStartTimestmap;
16671
- return elapsedMs !== 0
16672
- ? this.frames / (elapsedMs / 1000)
16668
+ return this.lastDeltaTime !== 0
16669
+ ? 1000 / this.lastDeltaTime
16673
16670
  : 0;
16674
16671
  }
16675
16672
  else {
@@ -16691,7 +16688,6 @@ class GameLoop {
16691
16688
  }
16692
16689
  this.update(deltaTime);
16693
16690
  this.render();
16694
- this.frames++;
16695
16691
  }
16696
16692
  }
16697
16693