Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#577] [#1170] Implement new Clock API #2122

Merged
merged 20 commits into from
Nov 28, 2021
Merged

feat: [#577] [#1170] Implement new Clock API #2122

merged 20 commits into from
Nov 28, 2021

Conversation

eonarheim
Copy link
Member

@eonarheim eonarheim commented Nov 23, 2021

===:clipboard: PR Checklist :clipboard:===

  • 📌 issue exists in github for these changes
  • 🔬 existing tests still pass
  • 🙈 code conforms to the style guide
  • 📐 new tests written and passing / old tests updated with new scenario(s)
  • 📄 changelog entry added (or not needed)

==================

Closes: #1170
Closes: #577

This PR implements a new Clock api to manage the core main loop. Clocks hide the implementation detail of how the mainloop runs, users just knows that it ticks somehow. Clocks additionally encapsulate any related browser timing, like performance.now()

  1. StandardClock encapsulates the existing requestAnimationFrame api logic
  2. TestClock allows a user to manually step the mainloop, this can be useful for frame by frame debugging Add Engine frame step debugging feature #1170
  3. The base abstract clock implements the specifics of elapsed time
  • The new maxFps option is added to the engine constructor options. This is achieved by exiting the mainloop early in clock if the current elapsed time does not match the desired fps. This can be useful when you want to deliver a consistent experience across devices.

Some fixed bugs that were illuminated as part of this

  • Excalibur's initialize flow was flawed, this PR refactors the start() to mean "ready for game code to run", before it actually meant resources loaded and player clicked play
  • Play button wasn't positioned until first update, now positioned on init
  • Systems were not initialized until first update, now initialized with the scene

Current game start flow

After this PR (consolidate init before start resolves)

  1. game.start(loader)
  2. clock started
  3. load resources
  4. compute resolution
  5. run engine/scene/ecs system initialize
  6. emit ready
  7. start the game

Before

  1. game.start(loader);
  2. mainloop raf started
  3. load resources
  4. run some initialize logic
  5. compute resolution
  6. start game
  7. run initialize logic

Changes:

  • New Clock API for managing the mainloop
  • Added maxFps engine option to constrain to a maximum fps
  • Updated fps sampling to be more useful
  • Updates the tests to use the new TestClock (fun side-effect tests are ~20 seconds faster in CI!)
  • Some small bug fixes that are related to clock timings

@eonarheim eonarheim added the work-in-progress A work in progress pull request, will not be merged until this is removed label Nov 23, 2021
@github-actions github-actions bot added the enhancement Label applied to enhancements or improvements to existing features label Nov 23, 2021
@eonarheim eonarheim changed the title feat: Implement maxFps Engine option feat: [#577] [#1170] Implement new Clock API Nov 23, 2021
@eonarheim eonarheim removed the work-in-progress A work in progress pull request, will not be merged until this is removed label Nov 24, 2021
@eonarheim eonarheim added the work-in-progress A work in progress pull request, will not be merged until this is removed label Nov 25, 2021
@eonarheim
Copy link
Member Author

I want to have the tests moved over to the TestClock before merging, should speed up the test runs

@eonarheim eonarheim removed the work-in-progress A work in progress pull request, will not be merged until this is removed label Nov 27, 2021
src/engine/Util/Clock.ts Outdated Show resolved Hide resolved
src/engine/Util/Clock.ts Outdated Show resolved Hide resolved
@eonarheim eonarheim merged commit c19e8da into main Nov 28, 2021
@eonarheim eonarheim deleted the feature/max-fps branch November 28, 2021 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Label applied to enhancements or improvements to existing features
Projects
None yet
2 participants