@@ -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