Skip to content

Commit cc45c8f

Browse files
committed
- change delta time and timestep calculation
- add throttling ability to application - reformat most files
1 parent 77f8d2a commit cc45c8f

File tree

40 files changed

+337
-278
lines changed

40 files changed

+337
-278
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ Components are attached to a GameObject like a sprite or a light. These have amo
239239
class PlayerMovement extends fox.Component {
240240
onCalc({timestep, object} = {}) {
241241
// move player according to arrow keys
242-
if (fox.Input.isKeyDown({key: "ArrowLeft"})) object.position.x -= 5
243-
if (fox.Input.isKeyDown({key: "ArrowRight"})) object.position.x += 5
244-
if (fox.Input.isKeyDown({key: "ArrowUp"})) object.position.y -= 5
245-
if (fox.Input.isKeyDown({key: "ArrowDown"})) object.position.y += 5
242+
if (fox.Input.isKeyDown({key: "ArrowLeft"})) object.position.x -= 5 * timestep
243+
if (fox.Input.isKeyDown({key: "ArrowRight"})) object.position.x += 5 * timestep
244+
if (fox.Input.isKeyDown({key: "ArrowUp"})) object.position.y -= 5 * timestep
245+
if (fox.Input.isKeyDown({key: "ArrowDown"})) object.position.y += 5 * timestep
246246
}
247247
}
248248

src/packages/animator/animation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export class Animation {
2424
* @returns {*}
2525
*/
2626
getTexture({frame}) {
27-
if(!this.loop && frame >= this.animationLength){
27+
if (!this.loop && frame >= this.animationLength) {
2828
// returns the last frame
29-
return this.frames[this.frames.length-1].texture
30-
}else{
29+
return this.frames[this.frames.length - 1].texture
30+
} else {
3131
// calculate current texture
3232
let frameTime = frame % this.animationLength
3333

src/packages/animator/animator.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Component} from "../components/index.js";
33
/**
44
* An Animator is a GameObject-Component for Sprites that holds different animations and changes them accordingly.
55
*/
6-
export class Animator extends Component{
6+
export class Animator extends Component {
77
/**
88
* Creates an Animator object.
99
* @param {object} animations Animations mapped by name (e.g. [{"idleAnim":Animation}])
@@ -20,21 +20,22 @@ export class Animator extends Component{
2020

2121
/**
2222
* Called every frame, up to 60 times per second
23+
* @param {number} timestep
2324
* @param {object} object
2425
*/
25-
onCalc({object} = {}) {
26-
this.framecounter ++
26+
onCalc({timestep, object} = {}) {
27+
this.framecounter += timestep
2728

28-
object.texture = this.animations[this.activeAnimation].getTexture({frame:this.framecounter})
29+
object.texture = this.animations[this.activeAnimation].getTexture({frame: parseInt(this.framecounter)})
2930
object.applyTexture()
3031
}
3132

3233
/**
3334
* Changes the active animation by the name provided in the animations parameter of the constructor.
3435
* @param {string} name Name of the animation
3536
*/
36-
setActiveAnimation({name}){
37-
if(this.activeAnimation!==name){
37+
setActiveAnimation({name}) {
38+
if (this.activeAnimation !== name) {
3839
this.activeAnimation = name
3940
this.framecounter = 0
4041
}

src/packages/application/application.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ export class Application {
1111
* Construct method of the object
1212
* @returns Application
1313
*/
14-
constructor({width, height, renderer, logFPS} = {}) {
14+
constructor({width, height, renderer, logFPS, minFrameTime} = {}) {
1515
let _this = this
1616

1717
this.scenes = {
1818
all: [],
1919
active: undefined,
20-
activeName : undefined
20+
activeName: undefined
2121
}
2222

2323
//settings
@@ -34,15 +34,15 @@ export class Application {
3434
}
3535

3636
this.frames = {
37-
"frame": 0,
37+
frame: 0,
38+
minFrameTime: minFrameTime !== undefined ? Math.floor(1000 / minFrameTime) : undefined,
39+
lastTimestamp: undefined
3840
}
3941

40-
this.fps = {
41-
"starttime": undefined,
42-
"rate": 1000 / 60,
43-
"deltatime": 0,
44-
"timestep": 1,
45-
"maxskippingframes": 5
42+
this.time = {
43+
startTime: undefined,
44+
deltaTime: 0,
45+
timestep: 1
4646
}
4747

4848
if (logFPS) {
@@ -81,14 +81,22 @@ export class Application {
8181
*/
8282
render(timestamp, _this = this) {
8383
//calc fps rate
84-
if (_this.fps.starttime === undefined) {
85-
_this.fps.starttime = timestamp
86-
_this.fps.lastFrame = Math.round((timestamp - _this.fps.starttime) / _this.fps.rate)
84+
if (_this.time.startTime === undefined) {
85+
_this.time.startTime = timestamp
86+
_this.frames.lastTimestamp = timestamp
8787
} else {
88-
let currentFrame = Math.round((timestamp - _this.fps.starttime) / _this.fps.rate);
89-
_this.fps.deltatime = (currentFrame - _this.fps.lastFrame) * (1000 / 60);
90-
_this.fps.timestep = Math.min(_this.fps.deltatime / _this.fps.rate, _this.fps.maxskippingframes)
91-
_this.fps.lastFrame = currentFrame
88+
let deltaTime = timestamp - _this.frames.lastTimestamp
89+
_this.frames.lastTimestamp = timestamp
90+
_this.time.deltaTime += deltaTime
91+
92+
if (_this.time.minFrameTime !== undefined && _this.time.deltaTime < _this.frames.minFrameTime) {
93+
window.requestAnimationFrame(function (t) {
94+
_this.render(t, _this)
95+
})
96+
return
97+
}
98+
99+
_this.time.timestep = _this.time.deltaTime / (1000 / 60)
92100
}
93101

94102
// TODO: check every timestep needed?
@@ -98,13 +106,17 @@ export class Application {
98106
if (_this.project.logFPS) this.stats.begin()
99107

100108
// calc the next frame
101-
_this.scenes.active.calc({timestep: _this.fps.timestep})
109+
_this.scenes.active.calc({timestep: _this.time.timestep})
102110

103111
// render the camera-views to the offscreen canvases
104112
_this.scenes.active.render({app: _this})
105113

106114
if (_this.project.logFPS) this.stats.end()
107115
}
116+
117+
// reset delta time
118+
_this.time.deltaTime = 0
119+
108120
window.requestAnimationFrame(function (t) {
109121
_this.render(t, _this)
110122
})
@@ -140,16 +152,16 @@ export class Application {
140152
/**
141153
* Destructs the current scene
142154
*/
143-
destroyCurrentScene(){
144-
if(this.scenes.activeName){
155+
destroyCurrentScene() {
156+
if (this.scenes.activeName) {
145157
this.scenes.all[this.scenes.activeName].destroy()
146158
}
147159
}
148160

149161
/**
150162
* Re-inits the current scene
151163
*/
152-
reloadCurrentScene(){
164+
reloadCurrentScene() {
153165
// destroy current scene
154166
this.destroyCurrentScene()
155167

@@ -160,7 +172,7 @@ export class Application {
160172
/**
161173
* Re-inits the current scene
162174
*/
163-
getCurrentSceneName(){
175+
getCurrentSceneName() {
164176
// init scene
165177
return this.scenes.activeName
166178
}

src/packages/application/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import {Application} from './application.js'
2+
23
export {Application}

src/packages/assetmanager/assetmanager.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Represents the AssetManager of the game engine. It is responsible
2+
* Represents the AssetManager of the game engine. It is responsible
33
* for holding all necessary assets in a global and static scope.
44
* @class AssetManager
55
*/
@@ -47,10 +47,10 @@ export class AssetManager {
4747
* @returns {object}
4848
*/
4949
static getTexture({name} = {}) {
50-
if(AssetManager.objects.texture[name] !== undefined){
50+
if (AssetManager.objects.texture[name] !== undefined) {
5151
return AssetManager.objects.texture[name]
5252
}
53-
console.warn("src: assetmanager: the texture you are trying to load does not exists: \""+name+"\"")
53+
console.warn("src: assetmanager: the texture you are trying to load does not exists: \"" + name + "\"")
5454
}
5555

5656
/**
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/**
2-
* Represents the blueprint for all asset based objects, like images, audios, animations...
2+
* Represents the blueprint for all asset based objects, like images, audios, animations...
33
* @class Asset
44
*/
5-
export class Asset{
5+
export class Asset {
66
/**
7-
* Constructs the Asset Object
7+
* Constructs the Asset Object
88
9-
* @return Asset
10-
*/
11-
constructor(){
9+
* @return Asset
10+
*/
11+
constructor() {
1212
this.loaded = false
1313
}
14-
15-
14+
15+
1616
/**
17-
* Returns the raw data of the object
18-
* @return {object}
19-
*/
20-
getData(_this=this){
21-
17+
* Returns the raw data of the object
18+
* @return {object}
19+
*/
20+
getData(_this = this) {
21+
2222
}
2323
}

src/packages/assets/assets/audio.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ import {Asset} from './asset.js'
33
/**
44
* An Audio-Asset holds an audio file for playback.
55
*/
6-
export class Audio extends Asset{
6+
export class Audio extends Asset {
77
/**
88
* Creates an Audio object.
99
* @param src
1010
* @param volume
1111
*/
12-
constructor({src, volume}={}){
12+
constructor({src, volume} = {}) {
1313
super()
14-
15-
let _this = this
14+
15+
let _this = this
1616
this.loaded = false
1717

1818
this.src = src
19-
19+
2020
_this.data = new window.Audio(src);
21-
_this.data.onloadeddata = function(){
21+
_this.data.onloadeddata = function () {
2222
_this.loaded = true
2323
}
24-
_this.data.volume = volume==undefined ? 1 : volume/100
24+
_this.data.volume = volume == undefined ? 1 : volume / 100
2525
}
2626

2727
/**
@@ -30,14 +30,14 @@ export class Audio extends Asset{
3030
* @param {number} jumpToSecsAfterLoop
3131
* @param _this
3232
*/
33-
play({loop,jumpToSecsAfterLoop}={}, _this=this){
33+
play({loop, jumpToSecsAfterLoop} = {}, _this = this) {
3434
_this.data.currentTime = 0
3535
_this.data.play()
36-
if(loop){
37-
_this.data.addEventListener("timeupdate", function(){
36+
if (loop) {
37+
_this.data.addEventListener("timeupdate", function () {
3838
let buffer = .64
39-
if(this.currentTime > this.duration-buffer){
40-
this.currentTime = jumpToSecsAfterLoop==undefined ? 0 : jumpToSecsAfterLoop
39+
if (this.currentTime > this.duration - buffer) {
40+
this.currentTime = jumpToSecsAfterLoop == undefined ? 0 : jumpToSecsAfterLoop
4141
this.play()
4242
}
4343
})
@@ -47,15 +47,15 @@ export class Audio extends Asset{
4747
/**
4848
* Pauses audio playback.
4949
*/
50-
pause(){
50+
pause() {
5151
this.data.pause()
5252
}
5353

5454
/**
5555
* Sets the volume of the audio
5656
* @param volume
5757
*/
58-
setVolume({volume}={}){
59-
this.data.volume = volume/100
58+
setVolume({volume} = {}) {
59+
this.data.volume = volume / 100
6060
}
6161
}

src/packages/assets/assets/texture.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Texture extends Asset {
3636
}
3737

3838
this.rendererData = {
39-
texture : undefined
39+
texture: undefined
4040
}
4141

4242
if (src !== undefined) {
@@ -60,7 +60,7 @@ export class Texture extends Asset {
6060
}
6161
}
6262

63-
static fromCanvas({canvas, ctx}){
63+
static fromCanvas({canvas, ctx}) {
6464
let instance = new Texture({src: undefined, width: canvas.width, height: canvas.height})
6565
instance.rendering.canvas = canvas
6666
instance.rendering.ctx = ctx || canvas.getContext("2d")
@@ -74,7 +74,7 @@ export class Texture extends Asset {
7474
* Returns the id of the texture
7575
* @returns {string}
7676
*/
77-
getId(){
77+
getId() {
7878
return this.id
7979
}
8080

0 commit comments

Comments
 (0)