Skip to content

Commit

Permalink
fixed multiple players issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gdsc0301 committed Sep 13, 2023
1 parent 4aa616a commit ee98a17
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<canvas id="app" width="640" height="360"></canvas>
<form id="loginForm">
<span class="welcomeMessage"></span>
<input type="text" placeholder="ip-address:port" value="https://best-multiplayer-game-possible-pvenbf56dq-uc.a.run.app">
<input id="ip-field" type="text" placeholder="ip-address:port" value="https://best-multiplayer-game-possible-pvenbf56dq-uc.a.run.app">
<!-- <input type="text" placeholder="ip-address:port" value="http://localhost:8080"> -->
<input type="text" autocomplete="false" placeholder="Your username"><input type="submit" value="GO!">
<span class="error"></span>
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class App {

this.username = 'Guest';

this.loginForm['elements'][0].value = window.location.hostname === 'localhost' ? 'http://localhost:8080' : 'https://best-multiplayer-game-possible-pvenbf56dq-uc.a.run.app';

this.loginForm.addEventListener('submit', e => {
e.preventDefault();

Expand Down Expand Up @@ -163,20 +165,18 @@ class App {

parseRoomPlayers(newRoomData: Room) {
for (const player_id in newRoomData.players) {
if(player_id === this.localPlayer.username) {
continue;
}

const newPos = new Vector3(newRoomData.players[player_id].position._x, newRoomData.players[player_id].position._y, 0);
const newRot = new Vector3(newRoomData.players[player_id].rotation._x, newRoomData.players[player_id].rotation._y, newRoomData.players[player_id].rotation._z);

// Player already exists
if(this.currentRoom.players[player_id] && this.currentRoom.players[player_id] instanceof Player) {
this.currentRoom.players[player_id].update(newPos, newRot);
return;
continue;
}

const newPlayer = Object.assign(new Player(player_id, this.scene, false), newRoomData.players[player_id]);
const isLocal = player_id === this.localPlayer.username;
let newPlayer = new Player(player_id, !isLocal ? this.scene : undefined, isLocal);
newPlayer = Object.assign(newPlayer, newRoomData.players[player_id]);
newPlayer.update(newPos, newRot);

this.currentRoom.players[player_id] = newPlayer;
Expand Down

0 comments on commit ee98a17

Please sign in to comment.