Skip to content

Commit

Permalink
Merge pull request #30 from Crazy-Cow/feature/CD-76
Browse files Browse the repository at this point in the history
fix: Game Interval 1번만 켜지도록 수정 [CD-76]
  • Loading branch information
ddubbu-dev authored Nov 15, 2024
2 parents 1b6664e + 683a43e commit 6027d61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
18 changes: 17 additions & 1 deletion src/game/maps/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ const GROUND_POS = {
}

export class CommonMap {
updateInterval: number
private updateInterval: number
world: CANNON.World
characters: Character[] = []
characterMaterial: CANNON.Material
groundMaterial: CANNON.Material
private intervalId?: NodeJS.Timeout

constructor() {
this.updateInterval = 1 / 60 // 60 FPS
Expand Down Expand Up @@ -135,4 +136,19 @@ export class CommonMap {
this.checkAndUpdatePosition(character)
})
}

startGameLoop(emitter: (data: unknown) => void) {
this.intervalId = setInterval(() => {
this.updateGameState()
const gameState = this.convertGameState()
emitter(gameState) // 특정 방이나 전체에 상태를 브로드캐스트
}, 1000 * this.updateInterval)
}

stopGameLoop() {
if (this.intervalId) {
clearInterval(this.intervalId)
this.intervalId = undefined
}
}
}
16 changes: 7 additions & 9 deletions src/game/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class SocketImplement {
this.socket = socket
this.register()
this.handleConnect()
this.updateGameState() // TODO: 게임방별로 돌아야함
}

private register = () => {
Expand Down Expand Up @@ -55,14 +54,6 @@ class SocketImplement {
this.socket.emit('characters', gameWorld.convertGameState())
}

updateGameState = () => {
setInterval(() => {
gameWorld.updateGameState()
// Q. 업데이트 주기에만 쏘면되지 않나??
this.socket.emit('characters', gameWorld.convertGameState()) // TODO: broadcast only room
}, 1000 * gameWorld.updateInterval)
}

public logger = (msg: string, args?: SocketOnEvtData) => {
console.log(
`[${this.socket.id}] ${msg} ${args ? JSON.stringify(args) : ''}`
Expand All @@ -73,6 +64,13 @@ class SocketImplement {
export function initInGmaeSocket(io: Server): void {
const root = io.of('/')

// 게임 서버 interval ON
gameWorld.startGameLoop((gameState) => {
root.emit('characters', gameState) // TODO: broadcast only room
})

// TODO: OFF

root.on(SOCKET_ON_EVT_TYPE.CONNECT, (socket: Socket) => {
const instance = new SocketImplement(socket)
instance.logger('complete connection')
Expand Down

0 comments on commit 6027d61

Please sign in to comment.