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

chore: tg client players on load #288

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions packages/game/src/lib/baseGameAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,28 @@ export class BaseGameAddon extends Container implements GameAddon {

async play() {}

createObject(type: GameObject['type'], id: string, x: number, zIndex?: number) {
createObject(data: { type: GameObject['type'], id: string, x: number, zIndex?: number, telegramId?: string }) {
// Check, if already exists
if (this.findObject(id)) {
if (this.findObject(data.id)) {
return
}

if (type === 'WAGON' && !this.wagon) {
this.wagon = new BaseWagonObject({ addon: this, x, y: this.bottomY })
if (data.type === 'PLAYER') {
if (data?.telegramId && this.player?.telegramId !== data.telegramId) {
this.playerService.createPlayer({ id: data.id, telegramId: data.telegramId, x: data.x })
}
}
if (data.type === 'WAGON' && !this.wagon) {
this.wagon = new BaseWagonObject({ addon: this, x: data.x, y: this.bottomY })
this.app.stage.addChild(this.wagon)
this.addChild(this.wagon)

if (this.client === 'WAGON_CLIENT') {
this.cameraTarget = this.wagon
}
}
if (type === 'TREE') {
const tree = new TreeObject({ id, addon: this, x, y: this.bottomY, size: getRandomInRange(50, 100), zIndex })
if (data.type === 'TREE') {
const tree = new TreeObject({ id: data.id, addon: this, x: data.x, y: this.bottomY, size: getRandomInRange(50, 100), zIndex: data?.zIndex })
this.app.stage.addChild(tree)
this.addChild(tree)
}
Expand Down
28 changes: 12 additions & 16 deletions packages/game/src/lib/services/baseWebSocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,14 @@ export class BaseWebSocketService implements WebSocketService {
const { id, type, objects } = message.data

for (const obj of objects) {
this.addon.createObject(obj.type, obj.id, obj.x, obj.zIndex)
this.addon.createObject({ type: obj.type, id: obj.id, x: obj.x, zIndex: obj?.zIndex, telegramId: obj?.telegramId })
}

if (type === 'PLAYER') {
if (type === 'PLAYER' && this.addon.player) {
const player = objects.find((obj) => obj.type === 'PLAYER' && obj.id === id)
if (player) {
// Me?
const telegramId = player.telegramId
if (telegramId) {
if (telegramId !== this.addon.player?.telegramId) {
this.addon.playerService.createPlayer({ id, telegramId, x: player.x })
} else {
if (this.addon.client === 'TELEGRAM_CLIENT') {
this.addon.player.id = id
this.addon.player.x = player.x
}
}
}
if (player && player?.telegramId === this.addon.player?.telegramId) {
this.addon.player.id = id
this.addon.player.x = player.x
}
}
}
Expand All @@ -84,9 +74,15 @@ export class BaseWebSocketService implements WebSocketService {
const { x } = message.data
this.addon.wagon?.createFlagAndMove(x)
}
if (message.type === 'NEW_PLAYER_TARGET') {
const { id, x } = message.data
if (this.addon.player?.id !== id) {
this.addon.playerService.movePlayer({ id, x })
}
}
if (message.type === 'NEW_TREE') {
const { id, x, zIndex } = message.data
this.addon.createObject('TREE', id, x, zIndex)
this.addon.createObject({ type: 'TREE', id, x, zIndex })
}
if (message.type === 'DESTROY_TREE') {
const { id } = message.data
Expand Down
2 changes: 1 addition & 1 deletion packages/game/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface GameAddon extends Container {
play: () => void
checkIfThisFlagIsTarget: (id: string) => boolean
findObject: (id: string) => GameObject | undefined
createObject: (type: GameObject['type'], id: string, x: number, zIndex?: number) => void
createObject: (data: { type: GameObject['type'], id: string, x: number, zIndex?: number, telegramId?: string }) => void
removeObject: (id: string) => void
rebuildScene: () => Promise<void>
}
Expand Down
Loading