81 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-12-03 20:06:57 +08:00
import { ApiMsgEnum, EntityTypeEnum, IState } 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()
constructor(rid: number) {
this.id = rid
}
2022-12-03 20:06:57 +08:00
private inputs = []
2022-12-01 22:26:41 +08:00
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
this.players.delete(player)
if (!this.players.size) {
RoomManager.Instance.closeRoom(this.id)
}
}
}
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,
type: EntityTypeEnum.Actor1,
weaponType: EntityTypeEnum.Weapon1,
bulletType: EntityTypeEnum.Bullet1,
})),
bullets: [],
nextBulletId: 1
}
for (const player of this.players) {
player.connection.sendMsg(ApiMsgEnum.MsgGameStart, {
state
})
}
this.listenPlayer()
}
listenPlayer() {
for (const player of this.players) {
player.connection.listenMsg(ApiMsgEnum.MsgClientSync, () => { }
)
2022-12-01 22:26:41 +08:00
}
}
}