update
This commit is contained in:
parent
2004a33531
commit
1ade200675
@ -1,19 +1,19 @@
|
|||||||
import { _decorator, Component, Node, Label } from "cc";
|
import { _decorator, Component, Node, Label } from "cc"
|
||||||
import DataManager from "../Global/DataManager";
|
import DataManager from "../Global/DataManager"
|
||||||
const { ccclass, property } = _decorator;
|
const { ccclass, property } = _decorator
|
||||||
|
|
||||||
@ccclass("PlayerManager")
|
@ccclass("PlayerManager")
|
||||||
export class PlayerManager extends Component {
|
export class PlayerManager extends Component {
|
||||||
init({ id, nickname, rid }: { id: number; nickname: string; rid: number }) {
|
init({ id, nickname, rid }: { id: number; nickname: string; rid: number }) {
|
||||||
const label = this.getComponent(Label);
|
const label = this.getComponent(Label)
|
||||||
let str = nickname;
|
let str = nickname
|
||||||
if (DataManager.Instance.myPlayerId === id) {
|
if (DataManager.Instance.myPlayerId === id) {
|
||||||
str += `(我)`;
|
str += `(我)`
|
||||||
}
|
}
|
||||||
if (rid !== -1) {
|
if (rid) {
|
||||||
str += `(房间${rid})`;
|
str += `(房间${rid})`
|
||||||
}
|
}
|
||||||
label.string = str;
|
label.string = str
|
||||||
this.node.active = true;
|
this.node.active = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
import { Connection } from "../Core";
|
import { Connection } from "../Core"
|
||||||
|
|
||||||
export default class Player {
|
export default class Player {
|
||||||
id: number;
|
id: number
|
||||||
nickname: string;
|
nickname: string
|
||||||
connection: Connection;
|
connection: Connection
|
||||||
rid: number;
|
rid: number
|
||||||
|
|
||||||
constructor({ id, nickname, connection }: Pick<Player, "id" | "nickname" | "connection">) {
|
constructor({ id, nickname, connection }: Pick<Player, "id" | "nickname" | "connection">) {
|
||||||
this.id = id;
|
this.id = id
|
||||||
this.nickname = nickname;
|
this.nickname = nickname
|
||||||
this.connection = connection;
|
this.connection = connection
|
||||||
this.connection.playerId = this.id;
|
|
||||||
this.rid = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,57 +1,57 @@
|
|||||||
import { ApiMsgEnum, EntityTypeEnum, IClientInput, IMsgClientSync, InputTypeEnum, IState, toFixed } from "../Common";
|
import { ApiMsgEnum, EntityTypeEnum, IClientInput, IMsgClientSync, InputTypeEnum, IState, toFixed } from "../Common"
|
||||||
import { Connection } from "../Core";
|
import { Connection } from "../Core"
|
||||||
import type Player from "./Player";
|
import type Player from "./Player"
|
||||||
import PlayerManager from "./PlayerManager";
|
import PlayerManager from "./PlayerManager"
|
||||||
import RoomManager from "./RoomManager";
|
import RoomManager from "./RoomManager"
|
||||||
|
|
||||||
export default class Room {
|
export default class Room {
|
||||||
id: number;
|
id: number
|
||||||
players: Set<Player> = new Set();
|
players: Set<Player> = new Set()
|
||||||
|
|
||||||
private lastTime?: number;
|
private lastTime?: number
|
||||||
private timers: NodeJS.Timer[] = [];
|
private timers: NodeJS.Timer[] = []
|
||||||
private pendingInput: Array<IClientInput> = [];
|
private pendingInput: Array<IClientInput> = []
|
||||||
private lastPlayerFrameIdMap: Map<number, number> = new Map();
|
private lastPlayerFrameIdMap: Map<number, number> = new Map()
|
||||||
|
|
||||||
constructor(rid: number) {
|
constructor(rid: number) {
|
||||||
this.id = rid;
|
this.id = rid
|
||||||
}
|
}
|
||||||
|
|
||||||
join(uid: number) {
|
join(uid: number) {
|
||||||
const player = PlayerManager.Instance.getPlayerById(uid);
|
const player = PlayerManager.Instance.getPlayerById(uid)
|
||||||
if (player) {
|
if (player) {
|
||||||
player.rid = this.id;
|
player.rid = this.id
|
||||||
this.players.add(player);
|
this.players.add(player)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
leave(uid: number) {
|
leave(uid: number) {
|
||||||
const player = PlayerManager.Instance.getPlayerById(uid);
|
const player = PlayerManager.Instance.getPlayerById(uid)
|
||||||
if (player) {
|
if (player) {
|
||||||
player.rid = -1;
|
player.rid = undefined
|
||||||
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this);
|
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this)
|
||||||
this.players.delete(player);
|
this.players.delete(player)
|
||||||
if (!this.players.size) {
|
if (!this.players.size) {
|
||||||
RoomManager.Instance.closeRoom(this.id);
|
RoomManager.Instance.closeRoom(this.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
this.timers.forEach((t) => clearInterval(t));
|
this.timers.forEach((t) => clearInterval(t))
|
||||||
for (const player of this.players) {
|
for (const player of this.players) {
|
||||||
player.rid = -1;
|
player.rid = undefined
|
||||||
player.connection.sendMsg(ApiMsgEnum.MsgGameEnd, {});
|
player.connection.sendMsg(ApiMsgEnum.MsgGameEnd, {})
|
||||||
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this);
|
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this)
|
||||||
}
|
}
|
||||||
this.players.clear();
|
this.players.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
sync() {
|
sync() {
|
||||||
for (const player of this.players) {
|
for (const player of this.players) {
|
||||||
player.connection.sendMsg(ApiMsgEnum.MsgRoom, {
|
player.connection.sendMsg(ApiMsgEnum.MsgRoom, {
|
||||||
room: RoomManager.Instance.getRoomView(this),
|
room: RoomManager.Instance.getRoomView(this),
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,47 +75,47 @@ export default class Room {
|
|||||||
})),
|
})),
|
||||||
bullets: [],
|
bullets: [],
|
||||||
nextBulletId: 1,
|
nextBulletId: 1,
|
||||||
};
|
}
|
||||||
|
|
||||||
for (const player of this.players) {
|
for (const player of this.players) {
|
||||||
player.connection.sendMsg(ApiMsgEnum.MsgGameStart, {
|
player.connection.sendMsg(ApiMsgEnum.MsgGameStart, {
|
||||||
state,
|
state,
|
||||||
});
|
})
|
||||||
player.connection.listenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this);
|
player.connection.listenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this)
|
||||||
}
|
}
|
||||||
let t1 = setInterval(() => {
|
let t1 = setInterval(() => {
|
||||||
this.sendServerMsg();
|
this.sendServerMsg()
|
||||||
}, 100);
|
}, 100)
|
||||||
let t2 = setInterval(() => {
|
let t2 = setInterval(() => {
|
||||||
this.timePast();
|
this.timePast()
|
||||||
}, 16);
|
}, 16)
|
||||||
this.timers = [t1, t2];
|
this.timers = [t1, t2]
|
||||||
}
|
}
|
||||||
|
|
||||||
getClientMsg(connection: Connection, { frameId, input }: IMsgClientSync) {
|
getClientMsg(connection: Connection, { frameId, input }: IMsgClientSync) {
|
||||||
this.lastPlayerFrameIdMap.set(connection.playerId, frameId);
|
this.lastPlayerFrameIdMap.set(connection.playerId, frameId)
|
||||||
this.pendingInput.push(input);
|
this.pendingInput.push(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
sendServerMsg() {
|
sendServerMsg() {
|
||||||
const pendingInput = this.pendingInput;
|
const pendingInput = this.pendingInput
|
||||||
this.pendingInput = [];
|
this.pendingInput = []
|
||||||
|
|
||||||
for (const player of this.players) {
|
for (const player of this.players) {
|
||||||
player.connection.sendMsg(ApiMsgEnum.MsgServerSync, {
|
player.connection.sendMsg(ApiMsgEnum.MsgServerSync, {
|
||||||
lastFrameId: this.lastPlayerFrameIdMap.get(player.id) ?? 0,
|
lastFrameId: this.lastPlayerFrameIdMap.get(player.id) ?? 0,
|
||||||
inputs: pendingInput,
|
inputs: pendingInput,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
timePast() {
|
timePast() {
|
||||||
let now = process.uptime();
|
let now = process.uptime()
|
||||||
const dt = now - (this.lastTime ?? now);
|
const dt = now - (this.lastTime ?? now)
|
||||||
this.pendingInput.push({
|
this.pendingInput.push({
|
||||||
type: InputTypeEnum.TimePast,
|
type: InputTypeEnum.TimePast,
|
||||||
dt: toFixed(dt),
|
dt: toFixed(dt),
|
||||||
});
|
})
|
||||||
this.lastTime = now;
|
this.lastTime = now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,7 @@ server.setApi(ApiMsgEnum.ApiPlayerList, (connection: Connection, data: IApiPlaye
|
|||||||
|
|
||||||
server.setApi(ApiMsgEnum.ApiPlayerJoin, (connection: Connection, { nickname }: IApiPlayerJoinReq): IApiPlayerJoinRes => {
|
server.setApi(ApiMsgEnum.ApiPlayerJoin, (connection: Connection, { nickname }: IApiPlayerJoinReq): IApiPlayerJoinRes => {
|
||||||
const player = PlayerManager.Instance.createPlayer({ connection, nickname })
|
const player = PlayerManager.Instance.createPlayer({ connection, nickname })
|
||||||
|
connection.playerId = player.id
|
||||||
PlayerManager.Instance.syncPlayers()
|
PlayerManager.Instance.syncPlayers()
|
||||||
return {
|
return {
|
||||||
player: PlayerManager.Instance.getPlayerView(player),
|
player: PlayerManager.Instance.getPlayerView(player),
|
||||||
|
Loading…
Reference in New Issue
Block a user