Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changing the area of sound and video #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions client/src/characters/OtherPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// OtherPlayer.ts
import Phaser from 'phaser'
import Player from './Player'
import MyPlayer from './MyPlayer'
Expand Down Expand Up @@ -32,20 +33,47 @@ export default class OtherPlayer extends Player {
makeCall(myPlayer: MyPlayer, webRTC: WebRTC) {
this.myPlayer = myPlayer
const myPlayerId = myPlayer.playerId

const isInSameRoom = this.isInSameRoom(myPlayer);
const isInHallway = this.isInHallway();
const myPlayerIsInHallway = myPlayer.isInHallway();

if (
!this.connected &&
this.connectionBufferTime >= 750 &&
myPlayer.readyToConnect &&
this.readyToConnect &&
myPlayer.videoConnected &&
myPlayerId > this.playerId
myPlayerId > this.playerId &&
((isInSameRoom && !isInHallway && !myPlayerIsInHallway) || (isInHallway && myPlayerIsInHallway))
) {
webRTC.connectToNewUser(this.playerId)
this.connected = true
this.connectionBufferTime = 0
}
}

private isInSameRoom(myPlayer: MyPlayer): boolean {
// بررسی کنید که آیا هر دو بازیکن در یک اتاق هستند یا خیر
const roomId = this.getRoomId(this.x, this.y);
const myPlayerRoomId = this.getRoomId(myPlayer.x, myPlayer.y);
return roomId === myPlayerRoomId;
}

private isInHallway(): boolean {
// بررسی کنید که آیا این بازیکن در راهرو است یا خیر
return this.getRoomId(this.x, this.y) === 'hallway';
}

private getRoomId(x: number, y: number): string {
// شناسایی اتاق بر اساس موقعیت x و y
if (x < 400 && y < 400) return 'room1';
if (x >= 400 && y < 400) return 'room2';
if (x < 400 && y >= 400) return 'room3';
if (x >= 400 && y >= 400) return 'room4';
return 'hallway';
}

updateOtherPlayer(field: string, value: number | string | boolean) {
switch (field) {
case 'name':
Expand Down Expand Up @@ -96,8 +124,6 @@ export default class OtherPlayer extends Player {
preUpdate(t: number, dt: number) {
super.preUpdate(t, dt)

// if Phaser has not updated the canvas (when the game tab is not active) for more than 1 sec
// directly snap player to their current locations
if (this.lastUpdateTimestamp && t - this.lastUpdateTimestamp > 750) {
this.lastUpdateTimestamp = t
this.x = this.targetPosition[0]
Expand All @@ -108,24 +134,22 @@ export default class OtherPlayer extends Player {
}

this.lastUpdateTimestamp = t
this.setDepth(this.y) // change player.depth based on player.y
this.setDepth(this.y)
const animParts = this.anims.currentAnim.key.split('_')
const animState = animParts[1]
if (animState === 'sit') {
const animDir = animParts[2]
const sittingShift = sittingShiftData[animDir]
if (sittingShift) {
// set hardcoded depth (differs between directions) if player sits down
this.setDepth(this.depth + sittingShiftData[animDir][2])
}
}

const speed = 200 // speed is in unit of pixels per second
const delta = (speed / 1000) * dt // minimum distance that a player can move in a frame (dt is in unit of ms)
const speed = 200
const delta = (speed / 1000) * dt
let dx = this.targetPosition[0] - this.x
let dy = this.targetPosition[1] - this.y

// if the player is close enough to the target position, directly snap the player to that position
if (Math.abs(dx) < delta) {
this.x = this.targetPosition[0]
this.playerContainer.x = this.targetPosition[0]
Expand All @@ -137,23 +161,18 @@ export default class OtherPlayer extends Player {
dy = 0
}

// if the player is still far from target position, impose a constant velocity towards it
let vx = 0
let vy = 0
if (dx > 0) vx += speed
else if (dx < 0) vx -= speed
if (dy > 0) vy += speed
else if (dy < 0) vy -= speed

// update character velocity
this.setVelocity(vx, vy)
this.body.velocity.setLength(speed)
// also update playerNameContainer velocity
this.playContainerBody.setVelocity(vx, vy)
this.playContainerBody.velocity.setLength(speed)

// while currently connected with myPlayer
// if myPlayer and the otherPlayer stop overlapping, delete video stream
this.connectionBufferTime += dt
if (
this.connected &&
Expand Down