binary encode
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { _decorator, instantiate, ProgressBar, Label } from 'cc';
|
||||
import { EntityManager } from '../../Base/EntityManager';
|
||||
import { ApiMsgEnum, EntityTypeEnum, IActor, InputTypeEnum, IVec2 } from '../../Common';
|
||||
import { ApiMsgEnum, EntityTypeEnum, IActor, InputTypeEnum, IVec2, toFixed } from '../../Common';
|
||||
import { EntityStateEnum } from '../../Enum';
|
||||
import DataManager from '../../Global/DataManager';
|
||||
import NetworkManager from '../../Global/NetworkManager';
|
||||
import { rad2Angle, toFixed } from '../../Utils';
|
||||
import { rad2Angle } from '../../Utils';
|
||||
import { WeaponManager } from '../Weapon/WeaponManager';
|
||||
import { PlayerStateMachine } from './ActorStateMachine';
|
||||
const { ccclass } = _decorator;
|
||||
@@ -59,9 +59,9 @@ export class ActorManager extends EntityManager implements IActor {
|
||||
return
|
||||
}
|
||||
|
||||
const { x, y } = DataManager.Instance.jm.input
|
||||
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
|
||||
input: {
|
||||
if (DataManager.Instance.jm.input.length()) {
|
||||
const { x, y } = DataManager.Instance.jm.input
|
||||
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
|
||||
type: InputTypeEnum.ActorMove,
|
||||
id: this.id,
|
||||
direction: {
|
||||
@@ -69,8 +69,9 @@ export class ActorManager extends EntityManager implements IActor {
|
||||
y: toFixed(y),
|
||||
},
|
||||
dt: toFixed(dt)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
render(data: IActor) {
|
||||
|
@@ -1,11 +1,10 @@
|
||||
import { _decorator, Node, Vec2, UITransform } from 'cc'
|
||||
import { EntityManager } from '../../Base/EntityManager'
|
||||
import { ApiMsgEnum, EntityTypeEnum, InputTypeEnum } from '../../Common'
|
||||
import { ApiMsgEnum, EntityTypeEnum, InputTypeEnum, toFixed } from '../../Common'
|
||||
import { EntityStateEnum, EventEnum } from '../../Enum'
|
||||
import DataManager from '../../Global/DataManager'
|
||||
import EventManager from '../../Global/EventManager'
|
||||
import NetworkManager from '../../Global/NetworkManager'
|
||||
import { toFixed } from '../../Utils'
|
||||
import { WeaponStateMachine } from './WeaponStateMachine'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@@ -62,18 +61,16 @@ export class WeaponManager extends EntityManager {
|
||||
const directionVec2 = new Vec2(pointWorldPos.x - anchorWorldPos.x, pointWorldPos.y - anchorWorldPos.y).normalize()
|
||||
|
||||
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
|
||||
input: {
|
||||
type: InputTypeEnum.WeaponShoot,
|
||||
owner: this.owner,
|
||||
position: {
|
||||
x: toFixed(pointStagePos.x),
|
||||
y: toFixed(pointStagePos.y),
|
||||
},
|
||||
direction: {
|
||||
x: toFixed(directionVec2.x),
|
||||
y: toFixed(directionVec2.y),
|
||||
},
|
||||
}
|
||||
type: InputTypeEnum.WeaponShoot,
|
||||
owner: this.owner,
|
||||
position: {
|
||||
x: toFixed(pointStagePos.x),
|
||||
y: toFixed(pointStagePos.y),
|
||||
},
|
||||
direction: {
|
||||
x: toFixed(directionVec2.x),
|
||||
y: toFixed(directionVec2.y),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import { Node, Prefab, SpriteFrame } from 'cc'
|
||||
import Singleton from '../Base/Singleton'
|
||||
import { EntityTypeEnum, IBullet, IClientInput, InputTypeEnum, IRoom, IState } from '../Common'
|
||||
import { EntityTypeEnum, IBullet, IClientInput, InputTypeEnum, IRoom, IState, toFixed } from '../Common'
|
||||
import { ActorManager } from '../Entity/Actor/ActorManager'
|
||||
import { BulletManager } from '../Entity/Bullet/BulletManager'
|
||||
import { EventEnum } from '../Enum'
|
||||
@@ -82,8 +82,8 @@ export default class DataManager extends Singleton {
|
||||
return
|
||||
}
|
||||
|
||||
player.position.x += x * PLAYER_SPEED * dt
|
||||
player.position.y += y * PLAYER_SPEED * dt
|
||||
player.position.x += toFixed(x * PLAYER_SPEED * dt)
|
||||
player.position.y += toFixed(y * PLAYER_SPEED * dt)
|
||||
player.direction = { x, y }
|
||||
break
|
||||
}
|
||||
@@ -112,8 +112,8 @@ export default class DataManager extends Singleton {
|
||||
const player = players[j];
|
||||
if (((player.position.x - bullet.position.x) ** 2 + (player.position.y - bullet.position.y) ** 2) < (PLAYER_RADIUS + BULLET_RADIUS) ** 2) {
|
||||
EventManager.Instance.emit(EventEnum.ExplosionBorn, bullet.id, {
|
||||
x: (player.position.x + bullet.position.x) / 2,
|
||||
y: (player.position.y + bullet.position.y) / 2,
|
||||
x: toFixed((player.position.x + bullet.position.x) / 2),
|
||||
y: toFixed((player.position.y + bullet.position.y) / 2),
|
||||
})
|
||||
|
||||
player.hp -= WEAPON_DAMAGE
|
||||
@@ -132,8 +132,8 @@ export default class DataManager extends Singleton {
|
||||
}
|
||||
|
||||
for (const bullet of this.state.bullets) {
|
||||
bullet.position.x += bullet.direction.x * BULLET_SPEED * dt
|
||||
bullet.position.y += bullet.direction.y * BULLET_SPEED * dt
|
||||
bullet.position.x += toFixed(bullet.direction.x * BULLET_SPEED * dt)
|
||||
bullet.position.y += toFixed(bullet.direction.y * BULLET_SPEED * dt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
import Singleton from '../Base/Singleton'
|
||||
import { IModel } from '../Common';
|
||||
import { ApiMsgEnum, IModel, strdecode, strencode } from '../Common';
|
||||
import { binaryEncode } from '../Common/Binary';
|
||||
import { binaryDecode } from '../Utils';
|
||||
|
||||
const TIMEOUT = 5000
|
||||
|
||||
@@ -17,7 +19,7 @@ export default class NetworkManager extends Singleton {
|
||||
|
||||
ws: WebSocket
|
||||
port = 8888
|
||||
cbs: Map<string, Function[]> = new Map()
|
||||
maps: Map<ApiMsgEnum, Function[]> = new Map()
|
||||
isConnected = false
|
||||
|
||||
connect() {
|
||||
@@ -27,6 +29,8 @@ export default class NetworkManager extends Singleton {
|
||||
return
|
||||
}
|
||||
this.ws = new WebSocket(`ws://localhost:${this.port}`)
|
||||
|
||||
this.ws.binaryType = 'arraybuffer';
|
||||
this.ws.onopen = () => {
|
||||
console.log("ws onopen")
|
||||
this.isConnected = true
|
||||
@@ -45,15 +49,15 @@ export default class NetworkManager extends Singleton {
|
||||
|
||||
this.ws.onmessage = (e) => {
|
||||
try {
|
||||
const json = JSON.parse(e.data)
|
||||
const json = binaryDecode(e.data)
|
||||
const { name, data } = json
|
||||
try {
|
||||
if (this.cbs.has(name) && this.cbs.get(name).length) {
|
||||
if (this.maps.has(name) && this.maps.get(name).length) {
|
||||
console.log(json);
|
||||
this.cbs.get(name).forEach(cb => cb(data))
|
||||
this.maps.get(name).forEach(cb => cb(data))
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("this.cbs.get(name).forEach(cb => cb(restData))", error)
|
||||
console.log("this.maps.get(name).forEach(cb => cb(restData))", error)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -80,7 +84,7 @@ export default class NetworkManager extends Singleton {
|
||||
}
|
||||
this.listenMsg(name as any, cb)
|
||||
|
||||
this.ws.send(JSON.stringify({ name, data }))
|
||||
this.sendMsg(name as any, data)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
resolve({ success: false, error: error as Error })
|
||||
@@ -89,21 +93,24 @@ export default class NetworkManager extends Singleton {
|
||||
}
|
||||
|
||||
sendMsg<T extends keyof IModel['msg']>(name: T, data: IModel['msg'][T]) {
|
||||
this.ws.send(JSON.stringify({ name, data }))
|
||||
const view = binaryEncode(name, data)
|
||||
console.log("view", view.buffer);
|
||||
|
||||
this.ws.send(view.buffer)
|
||||
}
|
||||
|
||||
listenMsg<T extends keyof IModel['msg']>(name: T, cb: (args: IModel['msg'][T]) => void) {
|
||||
if (this.cbs.has(name)) {
|
||||
this.cbs.get(name).push(cb)
|
||||
if (this.maps.has(name)) {
|
||||
this.maps.get(name).push(cb)
|
||||
} else {
|
||||
this.cbs.set(name, [cb])
|
||||
this.maps.set(name, [cb])
|
||||
}
|
||||
}
|
||||
|
||||
unlistenMsg(name: string, cb: Function) {
|
||||
if (this.cbs.has(name)) {
|
||||
const index = this.cbs.get(name).indexOf(cb)
|
||||
index > -1 && this.cbs.get(name).splice(index, 1)
|
||||
unlistenMsg(name: ApiMsgEnum, cb: Function) {
|
||||
if (this.maps.has(name)) {
|
||||
const index = this.maps.get(name).indexOf(cb)
|
||||
index > -1 && this.maps.get(name).splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,6 @@ import NetworkManager from '../Global/NetworkManager';
|
||||
import ObjectPoolManager from '../Global/ObjectPoolManager';
|
||||
import { BulletManager } from '../Entity/Bullet/BulletManager';
|
||||
import { ApiMsgEnum, EntityTypeEnum, IMsgServerSync, InputTypeEnum } from '../Common';
|
||||
import { toFixed } from '../Utils';
|
||||
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@@ -86,7 +85,7 @@ export class BattleManager extends Component {
|
||||
map.setParent(this.stage)
|
||||
}
|
||||
|
||||
handleSync({ inputs }: IMsgServerSync) {
|
||||
handleSync(inputs: IMsgServerSync) {
|
||||
for (const input of inputs) {
|
||||
DataManager.Instance.applyInput(input)
|
||||
}
|
||||
@@ -102,7 +101,7 @@ export class BattleManager extends Component {
|
||||
|
||||
tick(dt: number) {
|
||||
this.tickPlayer(dt)
|
||||
this.tickGlobal(dt)
|
||||
// this.tickGlobal(dt)
|
||||
}
|
||||
|
||||
tickPlayer(dt: number) {
|
||||
@@ -115,14 +114,14 @@ export class BattleManager extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
tickGlobal(dt: number) {
|
||||
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
|
||||
input: {
|
||||
type: InputTypeEnum.TimePast,
|
||||
dt: toFixed(dt),
|
||||
}
|
||||
})
|
||||
}
|
||||
// tickGlobal(dt: number) {
|
||||
// NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
|
||||
// input: {
|
||||
// type: InputTypeEnum.TimePast,
|
||||
// dt: toFixed(dt),
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
|
||||
render() {
|
||||
this.renderPlayer()
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { SpriteFrame } from "cc"
|
||||
import { ApiMsgEnum, InputTypeEnum, strdecode } from "../Common"
|
||||
|
||||
const INDEX_REG = /\((\d+)\)/
|
||||
|
||||
@@ -9,4 +10,78 @@ export const sortSpriteFrame = (spriteFrame: Array<SpriteFrame>) =>
|
||||
|
||||
export const rad2Angle = (rad: number) => rad / Math.PI * 180
|
||||
|
||||
export const toFixed = (num: number, digit: number = 4): number => Math.floor(num * 10 ** digit) / 10 ** digit
|
||||
export const binaryDecode = (buffer: ArrayBuffer) => {
|
||||
let index = 0
|
||||
const view = new DataView(buffer)
|
||||
const type = view.getUint8(index++)
|
||||
|
||||
if (type === ApiMsgEnum.MsgClientSync) {
|
||||
const inputType = view.getUint8(index++)
|
||||
if (inputType === InputTypeEnum.ActorMove) {
|
||||
const id = view.getUint8(index++)
|
||||
const directionX = view.getFloat32(index)
|
||||
index += 4
|
||||
const directionY = view.getFloat32(index)
|
||||
index += 4
|
||||
const dt = view.getFloat32(index)
|
||||
index += 4
|
||||
const msg = {
|
||||
name: ApiMsgEnum.MsgClientSync,
|
||||
data: {
|
||||
type: InputTypeEnum.ActorMove,
|
||||
id,
|
||||
direction: {
|
||||
x: directionX,
|
||||
y: directionY,
|
||||
},
|
||||
dt
|
||||
}
|
||||
}
|
||||
|
||||
return msg
|
||||
} else if (inputType === InputTypeEnum.WeaponShoot) {
|
||||
const id = view.getUint8(index++)
|
||||
const positionX = view.getFloat32(index)
|
||||
index += 4
|
||||
const positionY = view.getFloat32(index)
|
||||
index += 4
|
||||
const directionX = view.getFloat32(index)
|
||||
index += 4
|
||||
const directionY = view.getFloat32(index)
|
||||
index += 4
|
||||
const msg = {
|
||||
name: ApiMsgEnum.MsgClientSync,
|
||||
data: {
|
||||
type: InputTypeEnum.WeaponShoot,
|
||||
id,
|
||||
position: {
|
||||
x: positionX,
|
||||
y: positionY,
|
||||
},
|
||||
direction: {
|
||||
x: directionX,
|
||||
y: directionY,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return msg
|
||||
} else {
|
||||
const dt = view.getFloat32(index)
|
||||
index += 4
|
||||
const msg = {
|
||||
name: ApiMsgEnum.MsgClientSync,
|
||||
data: {
|
||||
type: InputTypeEnum.TimePast,
|
||||
dt,
|
||||
}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
name: type,
|
||||
data: JSON.parse(strdecode(new Uint8Array(buffer.slice(1))))
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user