update dt

This commit is contained in:
Marty Killian 2023-11-29 19:03:50 +01:00
parent 374dceda99
commit a6d22663b8
2 changed files with 17 additions and 11 deletions

View file

@ -1,7 +1,7 @@
let mapWidth = 210.; let mapWidth = 210.;
let mapHeith = 100.; let mapHeith = 100.;
const playerSize = 50.; const playerSize = 50.;
const playerSpeed=2.; const playerSpeed=.2;
const halfSqrtTwo=0.70710678118; const halfSqrtTwo=0.70710678118;
class Player class Player
{ {
@ -55,7 +55,7 @@ class Player
//console.log("Should send: {type='update', dir=",this.dir,", x=",this.x,", y=",this.y,"}"); //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) if(this.dir==0)
return; return;
@ -63,14 +63,14 @@ class Player
let dx,dy; let dx,dy;
switch(this.dir) switch(this.dir)
{ {
case 1: dx=0.;dy=-playerSpeed;break; case 1: dx=0.;dy=-playerSpeed*dt;break;
case 2: dx=halfSqrtTwo*playerSpeed;dy=-halfSqrtTwo*playerSpeed;break; case 2: dx=halfSqrtTwo*playerSpeed*dt;dy=-halfSqrtTwo*playerSpeed*dt;break;
case 3: dx=playerSpeed;dy=0.;break; case 3: dx=playerSpeed*dt;dy=0.;break;
case 4: dx=halfSqrtTwo*playerSpeed;dy=halfSqrtTwo*playerSpeed;break; case 4: dx=halfSqrtTwo*playerSpeed*dt;dy=halfSqrtTwo*playerSpeed*dt;break;
case 5: dx=0.;dy=playerSpeed;break; case 5: dx=0.;dy=playerSpeed*dt;break;
case 6: dx=-halfSqrtTwo*playerSpeed;dy=halfSqrtTwo*playerSpeed;break; case 6: dx=-halfSqrtTwo*playerSpeed*dt;dy=halfSqrtTwo*playerSpeed*dt;break;
case 7: dx=-playerSpeed;dy=0.;break; case 7: dx=-playerSpeed*dt;dy=0.;break;
case 8: dx=-halfSqrtTwo*playerSpeed;dy=-halfSqrtTwo*playerSpeed;break; case 8: dx=-halfSqrtTwo*playerSpeed*dt;dy=-halfSqrtTwo*playerSpeed*dt;break;
default: default:
} }
this.x += dx; this.x += dx;

View file

@ -13,8 +13,10 @@ playerId=Net.playerId;
player=Net.clientPlayer; player=Net.clientPlayer;
players=Net.getPlayersToAdd(); players=Net.getPlayersToAdd();
let dt = 0;
let currentTime = new Date();
function game() { function game() {
if(playerId==null) if(playerId==null)
{ {
@ -51,13 +53,17 @@ function game() {
} }
} }
} }
player.update([],[]); player.update([],[], dt);
for (var i = players.length - 1; i >= 0; i--) { for (var i = players.length - 1; i >= 0; i--) {
players[i].update([],[]);//squares, circles players[i].update([],[]);//squares, circles
} }
//console.log(Inp.calculateAngle(500,500)) //console.log(Inp.calculateAngle(500,500))
Renderer.ReloadAff(); Renderer.ReloadAff();
} }
let newTime = new Date();
dt=newTime - currentTime;
currentTime=newTime
} }
Net.connect(); //connect to server, create a player, and retrieve all players info Net.connect(); //connect to server, create a player, and retrieve all players info