From a6d22663b86aee5a17600ab666225516262d2834 Mon Sep 17 00:00:00 2001 From: Marty Killian Date: Wed, 29 Nov 2023 19:03:50 +0100 Subject: [PATCH] update dt --- js/class.js | 20 ++++++++++---------- js/game.js | 8 +++++++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/js/class.js b/js/class.js index 0c17370..2ba5648 100644 --- a/js/class.js +++ b/js/class.js @@ -1,7 +1,7 @@ let mapWidth = 210.; let mapHeith = 100.; const playerSize = 50.; -const playerSpeed=2.; +const playerSpeed=.2; const halfSqrtTwo=0.70710678118; class Player { @@ -55,7 +55,7 @@ class Player //console.log("Should send: {type='update', dir=",this.dir,", x=",this.x,", y=",this.y,"}"); } - update(squares,circles)//update position + update(squares,circles, dt)//update position { if(this.dir==0) return; @@ -63,14 +63,14 @@ class Player let dx,dy; switch(this.dir) { - case 1: dx=0.;dy=-playerSpeed;break; - case 2: dx=halfSqrtTwo*playerSpeed;dy=-halfSqrtTwo*playerSpeed;break; - case 3: dx=playerSpeed;dy=0.;break; - case 4: dx=halfSqrtTwo*playerSpeed;dy=halfSqrtTwo*playerSpeed;break; - case 5: dx=0.;dy=playerSpeed;break; - case 6: dx=-halfSqrtTwo*playerSpeed;dy=halfSqrtTwo*playerSpeed;break; - case 7: dx=-playerSpeed;dy=0.;break; - case 8: dx=-halfSqrtTwo*playerSpeed;dy=-halfSqrtTwo*playerSpeed;break; + case 1: dx=0.;dy=-playerSpeed*dt;break; + case 2: dx=halfSqrtTwo*playerSpeed*dt;dy=-halfSqrtTwo*playerSpeed*dt;break; + case 3: dx=playerSpeed*dt;dy=0.;break; + case 4: dx=halfSqrtTwo*playerSpeed*dt;dy=halfSqrtTwo*playerSpeed*dt;break; + case 5: dx=0.;dy=playerSpeed*dt;break; + case 6: dx=-halfSqrtTwo*playerSpeed*dt;dy=halfSqrtTwo*playerSpeed*dt;break; + case 7: dx=-playerSpeed*dt;dy=0.;break; + case 8: dx=-halfSqrtTwo*playerSpeed*dt;dy=-halfSqrtTwo*playerSpeed*dt;break; default: } this.x += dx; diff --git a/js/game.js b/js/game.js index 8c95fc1..5042ae8 100644 --- a/js/game.js +++ b/js/game.js @@ -13,8 +13,10 @@ playerId=Net.playerId; player=Net.clientPlayer; players=Net.getPlayersToAdd(); +let dt = 0; +let currentTime = new Date(); function game() { if(playerId==null) { @@ -51,13 +53,17 @@ function game() { } } } - player.update([],[]); + player.update([],[], dt); for (var i = players.length - 1; i >= 0; i--) { players[i].update([],[]);//squares, circles } //console.log(Inp.calculateAngle(500,500)) Renderer.ReloadAff(); } + + let newTime = new Date(); + dt=newTime - currentTime; + currentTime=newTime } Net.connect(); //connect to server, create a player, and retrieve all players info