Skip to content

Commit da9d456

Browse files
committed
Added tick function that uses delta time
1 parent ae3d4a3 commit da9d456

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

engine.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ export function sprite(options) {
9797
this.body.style.transform += "perspective("+ this.rot.p +"px) "
9898
}
9999
this.body.style.transform += "translate("+ this.pos.x +"px, "+ this.pos.y +"px) " // X Y
100-
this.body.style.transform += "rotateX("+ this.rot.x +"deg) "
101-
this.body.style.transform += "rotateY("+ this.rot.y +"deg) "
102-
this.body.style.transform += "rotateZ("+ this.rot.z +"deg) "
100+
this.body.style.transform += "rotateX("+ this.rot.x +"deg) " // Rotation X
101+
this.body.style.transform += "rotateY("+ this.rot.y +"deg) " // Rotation Y
102+
this.body.style.transform += "rotateZ("+ this.rot.z +"deg) " // Rotation Z (2D)
103103
this.body.style.transformStyle = ""
104-
this.body.style.transform += "scale("+ this.scale +") "
104+
this.body.style.transform += "scale("+ this.scale +") " // Scale
105105
//this.body.style.transformOrigin = "bottom"
106106

107-
this.body.style.width = this.size.x +"px"
108-
this.body.style.height = this.size.y +"px"
107+
this.body.style.width = this.size.x +"px" // Width
108+
this.body.style.height = this.size.y +"px" // Height
109109
if (this.texture != null) {
110110
this.body.style.background = "url("+ this.texture.src +")"
111111
}
112112

113-
this.body.style.transform += "translate("+ this.offset.x +"px, "+ this.offset.y +"px) "
114-
this.body.style.transform += "translateZ(0) "
113+
this.body.style.transform += "translate("+ this.offset.x +"px, "+ this.offset.y +"px) " // Offset
114+
//this.body.style.transform += "translateZ(0) "
115115
this.body.style.transformOrigin = (-this.offset.x+this.body.style.width/2) +"px "+ (-this.offset.y+this.body.style.height/2) +"px"
116116
this.body.style.imageRendering = "pixelated"
117117

script.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ window.onload = function() {
2020

2121
const world = new sprite({
2222
size: {
23-
x: 10000,
24-
y: 10000
23+
x: 1000,
24+
y: 1000
2525
},
2626
offset: {
2727
x: 0,
2828
y: 0
2929
},
3030
rot: {
31-
x: 45,
31+
x: 0,
3232
y: 0,
3333
z: 0,
34-
p: 500
34+
p: 0
3535
},
3636
texture: "background.png"
3737
})
38-
/* const child = new sprite({
38+
const child = new sprite({
3939
parent: world,
4040
texture: "botWalk.gif",
4141
pos: {
@@ -55,7 +55,7 @@ window.onload = function() {
5555
x: 0,
5656
y: 0
5757
}
58-
}) */
58+
})
5959

6060
const canvas = document.getElementById("canvas")
6161
canvas.style.width = window.innerWidth +"px"
@@ -72,10 +72,13 @@ window.onload = function() {
7272

7373
const debugInfo = this.document.getElementById("debugInfo")
7474

75-
let lastTime
75+
let lastTime = Date.now()
7676

7777
const camPos = {x:0, y:0}
7878
function render() {
79+
let delta = Date.now() - lastTime // delta = time last frame took
80+
lastTime = Date.now()
81+
7982
let camSpeed = 12
8083
if (heldKeys["ShiftLeft"]) {camSpeed *= 2}
8184
if (heldKeys["KeyW"]) {camPos.y += camSpeed / world.scale}
@@ -95,16 +98,23 @@ window.onload = function() {
9598
debugInfo.innerText = ""
9699
debugInfo.innerText += "Cam X Y: "+ camPos.x.toFixed(2) +" "+ camPos.y.toFixed(2) +"\n"
97100
debugInfo.innerText += "Cam zoom: "+ camZoom +" ("+ world.scale.toFixed(2) +")\n"
98-
99-
//child.setPos({x: child.pos.x - 0.01, y: 0})
100-
debugInfo.innerText += "Render: "+ (Date.now() - lastTime) +" ms"
101-
lastTime = Date.now()
102-
103-
child.setPos({x: child.pos.x % 50 - 0.2, y: 0})
101+
debugInfo.innerText += "Render: "+ delta +" ms"
102+
103+
tick(delta/16) // 16 milliseconds =~ 60fps
104104

105105
requestAnimationFrame(render)
106106
}
107107

108+
let oog = Date.now()
109+
function tick(d) {
110+
child.setPos({x: child.pos.x - 0.2*d, y: 0})
111+
if (child.pos.x < -50) {
112+
child.setPos({x: 0, y: 0})
113+
console.log("Took me "+ (Date.now() - oog))
114+
oog = Date.now()
115+
}
116+
}
117+
108118
requestAnimationFrame(render)
109119
}
110120

0 commit comments

Comments
 (0)