binary encode

This commit is contained in:
sli97
2022-12-04 22:10:30 +08:00
parent a678a5b3fc
commit c31af6b02a
23 changed files with 1089 additions and 745 deletions

View File

@@ -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)
}
}
}

View File

@@ -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)
}
}
}