115 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-12-04 22:10:30 +08:00
import { ApiMsgEnum, EntityTypeEnum, IClientInput, InputTypeEnum, IState, toFixed } from '../Common'
2022-12-01 22:26:41 +08:00
import type Player from './Player'
import PlayerManager from './PlayerManager'
import RoomManager from './RoomManager'
export default class Room {
id: number
players: Set<Player> = new Set()
2022-12-04 22:10:30 +08:00
lastSyncTime?: number
2022-12-07 22:24:46 +08:00
private timers: NodeJS.Timer[] = []
2022-12-04 22:10:30 +08:00
private inputs: Array<IClientInput> = []
2022-12-01 22:26:41 +08:00
constructor(rid: number) {
this.id = rid
}
join(uid: number) {
const player = PlayerManager.Instance.getPlayerById(uid)
if (player) {
player.rid = this.id
this.players.add(player)
}
}
leave(uid: number) {
const player = PlayerManager.Instance.getPlayerById(uid)
if (player) {
player.rid = -1
2022-12-07 22:24:46 +08:00
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getInput, this)
2022-12-01 22:26:41 +08:00
this.players.delete(player)
if (!this.players.size) {
RoomManager.Instance.closeRoom(this.id)
}
}
}
2022-12-07 22:24:46 +08:00
close() {
this.timers.forEach(t => clearInterval(t))
for (const player of this.players) {
player.connection.sendMsg(ApiMsgEnum.MsgGameEnd, {})
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getInput, this)
}
this.players.clear()
}
2022-12-01 22:26:41 +08:00
sync() {
for (const player of this.players) {
2022-12-03 20:06:57 +08:00
player.connection.sendMsg(ApiMsgEnum.MsgRoom, {
room: RoomManager.Instance.getRoomView(this)
})
}
}
start() {
const state: IState = {
players: [...this.players].map((player, index) => ({
id: player.id,
nickname: player.nickname,
position: {
x: -150 + index * 300,
y: -150 + index * 300,
},
direction: {
x: 1,
y: 0
},
hp: 100,
2022-12-06 23:10:03 +08:00
type: EntityTypeEnum.Actor2,
weaponType: EntityTypeEnum.Weapon2,
bulletType: EntityTypeEnum.Bullet2,
2022-12-03 20:06:57 +08:00
})),
bullets: [],
nextBulletId: 1
}
for (const player of this.players) {
player.connection.sendMsg(ApiMsgEnum.MsgGameStart, {
state
})
2022-12-07 22:24:46 +08:00
player.connection.listenMsg(ApiMsgEnum.MsgClientSync, this.getInput, this)
2022-12-03 20:06:57 +08:00
}
2022-12-06 23:10:03 +08:00
let t1 = setInterval(() => {
2022-12-07 22:24:46 +08:00
this.sendInput()
2022-12-04 22:10:30 +08:00
}, 100)
2022-12-06 23:10:03 +08:00
let t2 = setInterval(() => {
2022-12-04 22:10:30 +08:00
this.timePast()
}, 16)
2022-12-06 23:10:03 +08:00
this.timers = [t1, t2]
2022-12-03 20:06:57 +08:00
}
2022-12-07 22:24:46 +08:00
getInput(input: IClientInput) {
this.inputs.push(input)
2022-12-03 21:28:38 +08:00
}
2022-12-07 22:24:46 +08:00
sendInput() {
2022-12-03 21:28:38 +08:00
const inputs = this.inputs
this.inputs = []
2022-12-04 22:10:30 +08:00
2022-12-03 21:28:38 +08:00
for (const player of this.players) {
2022-12-04 22:10:30 +08:00
player.connection.sendMsg(ApiMsgEnum.MsgServerSync, inputs)
2022-12-01 22:26:41 +08:00
}
}
2022-12-04 22:10:30 +08:00
timePast() {
let now = process.uptime();
const dt = now - (this.lastSyncTime ?? now)
this.inputs.push({
type: InputTypeEnum.TimePast,
dt: toFixed(dt)
})
this.lastSyncTime = now;
}
2022-12-01 22:26:41 +08:00
}