Skip to content

Commit

Permalink
Merge pull request #55 from SaltieRL/pa-delta
Browse files Browse the repository at this point in the history
Pass delta into frame callbacks
  • Loading branch information
Abbondanzo committed Aug 17, 2019
2 parents 151fb76 + ffed5c5 commit 9cfb7fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/eventbus/events/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EventBus from "../EventBus"
* Fires each time the global game clock advances a frame or updates its current frame.
*/
export interface FrameEvent {
delta: number
frame: number
elapsedTime: number
}
Expand Down
4 changes: 1 addition & 3 deletions src/managers/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export class GameManager {
paused ? this.clock.pause() : this.clock.play()
}

animate({ }: FrameEvent) {
const delta = this.clock.getDelta()

animate({ delta }: FrameEvent) {
if (delta) {
AnimationManager.getInstance().updateAnimationClips(delta)
this.render()
Expand Down
8 changes: 6 additions & 2 deletions src/managers/SceneManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Scene } from "three"

import { addFrameListener, removeFrameListener } from "../eventbus/events/frame"
import {
addFrameListener,
FrameEvent,
removeFrameListener,
} from "../eventbus/events/frame"
import BallManager from "./models/BallManager"
import FieldManager from "./models/FieldManager"
import PlayerManager from "./models/PlayerManager"
Expand All @@ -27,7 +31,7 @@ export default class SceneManager {
addFrameListener(this.update)
}

private readonly update = () => {
private readonly update = ({ delta }: FrameEvent) => {
for (const player of this.players) {
if (player.carGroup.position.y < 0) {
player.carGroup.visible = false
Expand Down
17 changes: 9 additions & 8 deletions src/utils/FPSClock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ export default class FPSClock {
this.timeout(false)
}

/**
* Returns the elapsed time in milliseconds.
*/
public getElapsedTime() {
return this.frameToDuration[this.currentFrame]
}

/**
* Returns the number of seconds elapsed since the last time getDelta was called. This function
* uses a combination of the performance.now() functionality when animations are rolling,
Expand All @@ -87,7 +94,7 @@ export default class FPSClock {
*
* @returns {number} seconds
*/
public getDelta(): number {
private getDelta(): number {
const now = performance.now()
// Initialize empty delta
if (!this.lastDelta) {
Expand All @@ -111,13 +118,6 @@ export default class FPSClock {
return delta / 1000
}

/**
* Returns the elapsed time in milliseconds.
*/
public getElapsedTime() {
return this.frameToDuration[this.currentFrame]
}

private update() {
if (!this.paused) {
this.getElapsedFrames()
Expand Down Expand Up @@ -147,6 +147,7 @@ export default class FPSClock {

private doCallbacks() {
dispatchFrameEvent({
delta: this.getDelta(),
frame: this.currentFrame,
elapsedTime: this.getElapsedTime(),
})
Expand Down

0 comments on commit 9cfb7fb

Please sign in to comment.