extra-game-loop 0.1.0 → 0.1.1

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.
@@ -16633,22 +16633,18 @@
16633
16633
  State["Stopped"] = "stopped";
16634
16634
  State["Running"] = "running";
16635
16635
  })(State || (State = {}));
16636
- var Event;
16637
- (function (Event) {
16638
- Event["Start"] = "start";
16639
- Event["Stop"] = "stop";
16640
- })(Event || (Event = {}));
16641
16636
  const schema = {
16642
- [State.Stopped]: { [Event.Start]: State.Running },
16643
- [State.Running]: { [Event.Stop]: State.Stopped }
16637
+ [State.Stopped]: { start: State.Running },
16638
+ [State.Running]: { stop: State.Stopped }
16644
16639
  };
16645
16640
  class GameLoop {
16646
16641
  constructor(options) {
16647
16642
  this.fsm = new es2018$e.FiniteStateMachine(schema, State.Stopped);
16648
- this.frames = 0;
16649
16643
  this.deltaTimeAccumulator = 0;
16644
+ this.lastDeltaTime = 0;
16650
16645
  this.loop = (timestamp) => {
16651
16646
  const deltaTime = timestamp - this.lastTimestamp;
16647
+ this.lastDeltaTime = deltaTime;
16652
16648
  this.lastTimestamp = timestamp;
16653
16649
  this.nextFrame(deltaTime);
16654
16650
  this.requstId = requestAnimationFrame(this.loop);
@@ -16661,21 +16657,22 @@
16661
16657
  this.render = options.render;
16662
16658
  }
16663
16659
  start() {
16664
- this.fsm.send(Event.Start);
16660
+ this.fsm.send('start');
16665
16661
  const timestamp = performance.now();
16666
- this.loopStartTimestmap = timestamp;
16667
16662
  this.lastTimestamp = timestamp;
16668
16663
  this.loop(timestamp);
16669
16664
  }
16670
16665
  stop() {
16671
- this.fsm.send(Event.Stop);
16666
+ this.fsm.send('stop');
16672
16667
  cancelAnimationFrame(this.requstId);
16668
+ delete this.requstId;
16669
+ delete this.lastTimestamp;
16670
+ this.lastDeltaTime = 0;
16673
16671
  }
16674
16672
  getFramesOfSecond() {
16675
16673
  if (this.fsm.matches(State.Running)) {
16676
- const elapsedMs = performance.now() - this.loopStartTimestmap;
16677
- return elapsedMs !== 0
16678
- ? this.frames / (elapsedMs / 1000)
16674
+ return this.lastDeltaTime !== 0
16675
+ ? 1000 / this.lastDeltaTime
16679
16676
  : 0;
16680
16677
  }
16681
16678
  else {
@@ -16697,7 +16694,6 @@
16697
16694
  }
16698
16695
  this.update(deltaTime);
16699
16696
  this.render();
16700
- this.frames++;
16701
16697
  }
16702
16698
  }
16703
16699