This commit is contained in:
sli97 2022-12-14 20:25:40 +08:00
parent 2004a33531
commit 1ade200675
4 changed files with 62 additions and 63 deletions

View File

@ -1,19 +1,19 @@
import { _decorator, Component, Node, Label } from "cc";
import DataManager from "../Global/DataManager";
const { ccclass, property } = _decorator;
import { _decorator, Component, Node, Label } from "cc"
import DataManager from "../Global/DataManager"
const { ccclass, property } = _decorator
@ccclass("PlayerManager")
export class PlayerManager extends Component {
init({ id, nickname, rid }: { id: number; nickname: string; rid: number }) {
const label = this.getComponent(Label);
let str = nickname;
const label = this.getComponent(Label)
let str = nickname
if (DataManager.Instance.myPlayerId === id) {
str += `(我)`;
str += `(我)`
}
if (rid !== -1) {
str += `(房间${rid})`;
if (rid) {
str += `(房间${rid})`
}
label.string = str;
this.node.active = true;
label.string = str
this.node.active = true
}
}

View File

@ -1,16 +1,14 @@
import { Connection } from "../Core";
import { Connection } from "../Core"
export default class Player {
id: number;
nickname: string;
connection: Connection;
rid: number;
id: number
nickname: string
connection: Connection
rid: number
constructor({ id, nickname, connection }: Pick<Player, "id" | "nickname" | "connection">) {
this.id = id;
this.nickname = nickname;
this.connection = connection;
this.connection.playerId = this.id;
this.rid = -1;
this.id = id
this.nickname = nickname
this.connection = connection
}
}

View File

@ -1,57 +1,57 @@
import { ApiMsgEnum, EntityTypeEnum, IClientInput, IMsgClientSync, InputTypeEnum, IState, toFixed } from "../Common";
import { Connection } from "../Core";
import type Player from "./Player";
import PlayerManager from "./PlayerManager";
import RoomManager from "./RoomManager";
import { ApiMsgEnum, EntityTypeEnum, IClientInput, IMsgClientSync, InputTypeEnum, IState, toFixed } from "../Common"
import { Connection } from "../Core"
import type Player from "./Player"
import PlayerManager from "./PlayerManager"
import RoomManager from "./RoomManager"
export default class Room {
id: number;
players: Set<Player> = new Set();
id: number
players: Set<Player> = new Set()
private lastTime?: number;
private timers: NodeJS.Timer[] = [];
private pendingInput: Array<IClientInput> = [];
private lastPlayerFrameIdMap: Map<number, number> = new Map();
private lastTime?: number
private timers: NodeJS.Timer[] = []
private pendingInput: Array<IClientInput> = []
private lastPlayerFrameIdMap: Map<number, number> = new Map()
constructor(rid: number) {
this.id = rid;
this.id = rid
}
join(uid: number) {
const player = PlayerManager.Instance.getPlayerById(uid);
const player = PlayerManager.Instance.getPlayerById(uid)
if (player) {
player.rid = this.id;
this.players.add(player);
player.rid = this.id
this.players.add(player)
}
}
leave(uid: number) {
const player = PlayerManager.Instance.getPlayerById(uid);
const player = PlayerManager.Instance.getPlayerById(uid)
if (player) {
player.rid = -1;
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this);
this.players.delete(player);
player.rid = undefined
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this)
this.players.delete(player)
if (!this.players.size) {
RoomManager.Instance.closeRoom(this.id);
RoomManager.Instance.closeRoom(this.id)
}
}
}
close() {
this.timers.forEach((t) => clearInterval(t));
this.timers.forEach((t) => clearInterval(t))
for (const player of this.players) {
player.rid = -1;
player.connection.sendMsg(ApiMsgEnum.MsgGameEnd, {});
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this);
player.rid = undefined
player.connection.sendMsg(ApiMsgEnum.MsgGameEnd, {})
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this)
}
this.players.clear();
this.players.clear()
}
sync() {
for (const player of this.players) {
player.connection.sendMsg(ApiMsgEnum.MsgRoom, {
room: RoomManager.Instance.getRoomView(this),
});
})
}
}
@ -75,47 +75,47 @@ export default class Room {
})),
bullets: [],
nextBulletId: 1,
};
}
for (const player of this.players) {
player.connection.sendMsg(ApiMsgEnum.MsgGameStart, {
state,
});
player.connection.listenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this);
})
player.connection.listenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this)
}
let t1 = setInterval(() => {
this.sendServerMsg();
}, 100);
this.sendServerMsg()
}, 100)
let t2 = setInterval(() => {
this.timePast();
}, 16);
this.timers = [t1, t2];
this.timePast()
}, 16)
this.timers = [t1, t2]
}
getClientMsg(connection: Connection, { frameId, input }: IMsgClientSync) {
this.lastPlayerFrameIdMap.set(connection.playerId, frameId);
this.pendingInput.push(input);
this.lastPlayerFrameIdMap.set(connection.playerId, frameId)
this.pendingInput.push(input)
}
sendServerMsg() {
const pendingInput = this.pendingInput;
this.pendingInput = [];
const pendingInput = this.pendingInput
this.pendingInput = []
for (const player of this.players) {
player.connection.sendMsg(ApiMsgEnum.MsgServerSync, {
lastFrameId: this.lastPlayerFrameIdMap.get(player.id) ?? 0,
inputs: pendingInput,
});
})
}
}
timePast() {
let now = process.uptime();
const dt = now - (this.lastTime ?? now);
let now = process.uptime()
const dt = now - (this.lastTime ?? now)
this.pendingInput.push({
type: InputTypeEnum.TimePast,
dt: toFixed(dt),
});
this.lastTime = now;
})
this.lastTime = now
}
}

View File

@ -50,6 +50,7 @@ server.setApi(ApiMsgEnum.ApiPlayerList, (connection: Connection, data: IApiPlaye
server.setApi(ApiMsgEnum.ApiPlayerJoin, (connection: Connection, { nickname }: IApiPlayerJoinReq): IApiPlayerJoinRes => {
const player = PlayerManager.Instance.createPlayer({ connection, nickname })
connection.playerId = player.id
PlayerManager.Instance.syncPlayers()
return {
player: PlayerManager.Instance.getPlayerView(player),