refactor
This commit is contained in:
@@ -16,25 +16,36 @@ const WEAPON_DAMAGE = 5
|
||||
const PLAYER_RADIUS = 50
|
||||
const BULLET_RADIUS = 10
|
||||
|
||||
const mapW = 960
|
||||
const mapH = 640
|
||||
|
||||
export default class DataManager extends Singleton {
|
||||
static get Instance() {
|
||||
return super.GetInstance<DataManager>()
|
||||
}
|
||||
|
||||
//登陆数据
|
||||
myPlayerId = 1
|
||||
|
||||
//大厅数据
|
||||
roomInfo: IRoom
|
||||
|
||||
//游戏数据
|
||||
stage: Node
|
||||
jm: JoyStickManager
|
||||
|
||||
prefabMap: Map<string, Prefab> = new Map()
|
||||
textureMap: Map<string, SpriteFrame[]> = new Map()
|
||||
|
||||
actorMap: Map<number, ActorManager> = new Map()
|
||||
bulletMap: Map<number, BulletManager> = new Map()
|
||||
|
||||
myPlayerId = 1
|
||||
roomInfo: IRoom
|
||||
mapSize = {
|
||||
x: 960,
|
||||
y: 640,
|
||||
reset() {
|
||||
this.stage = null
|
||||
this.jm = null
|
||||
this.actorMap.clear()
|
||||
this.bulletMap.clear()
|
||||
this.prefabMap.clear()
|
||||
this.textureMap.clear()
|
||||
}
|
||||
|
||||
state: IState = {
|
||||
@@ -85,8 +96,8 @@ export default class DataManager extends Singleton {
|
||||
player.position.x += toFixed(x * PLAYER_SPEED * dt)
|
||||
player.position.y += toFixed(y * PLAYER_SPEED * dt)
|
||||
|
||||
player.position.x = clamp(player.position.x, -this.mapSize.x / 2, this.mapSize.x / 2)
|
||||
player.position.y = clamp(player.position.y, -this.mapSize.y / 2, this.mapSize.y / 2)
|
||||
player.position.x = clamp(player.position.x, -mapW / 2, mapW / 2)
|
||||
player.position.y = clamp(player.position.y, -mapH / 2, mapH / 2)
|
||||
|
||||
player.direction = { x, y }
|
||||
break
|
||||
@@ -124,7 +135,7 @@ export default class DataManager extends Singleton {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (Math.abs(bullet.position.x) > this.mapSize.x / 2 || Math.abs(bullet.position.y) > this.mapSize.y / 2) {
|
||||
if (Math.abs(bullet.position.x) > mapW / 2 || Math.abs(bullet.position.y) > mapH / 2) {
|
||||
EventManager.Instance.emit(EventEnum.ExplosionBorn, bullet.id, {
|
||||
x: bullet.position.x,
|
||||
y: bullet.position.y,
|
||||
|
@@ -2,7 +2,7 @@ import Singleton from '../Base/Singleton'
|
||||
import { EventEnum } from '../Enum';
|
||||
|
||||
interface IItem {
|
||||
func: Function;
|
||||
cb: Function;
|
||||
ctx: unknown;
|
||||
}
|
||||
|
||||
@@ -13,25 +13,25 @@ export default class EventManager extends Singleton {
|
||||
|
||||
private map: Map<EventEnum, Array<IItem>> = new Map();
|
||||
|
||||
on(event: EventEnum, func: Function, ctx?: unknown) {
|
||||
on(event: EventEnum, cb: Function, ctx: unknown) {
|
||||
if (this.map.has(event)) {
|
||||
this.map.get(event).push({ func, ctx });
|
||||
this.map.get(event).push({ cb, ctx });
|
||||
} else {
|
||||
this.map.set(event, [{ func, ctx }]);
|
||||
this.map.set(event, [{ cb, ctx }]);
|
||||
}
|
||||
}
|
||||
|
||||
off(event: EventEnum, func: Function, ctx?: unknown) {
|
||||
off(event: EventEnum, cb: Function, ctx: unknown) {
|
||||
if (this.map.has(event)) {
|
||||
const index = this.map.get(event).findIndex(i => func === i.func && i.ctx === ctx);
|
||||
const index = this.map.get(event).findIndex(i => cb === i.cb && i.ctx === ctx);
|
||||
index > -1 && this.map.get(event).splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
emit(event: EventEnum, ...params: unknown[]) {
|
||||
if (this.map.has(event)) {
|
||||
this.map.get(event).forEach(({ func, ctx }) => {
|
||||
ctx ? func.apply(ctx, params) : func(...params);
|
||||
this.map.get(event).forEach(({ cb, ctx }) => {
|
||||
cb.apply(ctx, params)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,14 @@
|
||||
import Singleton from '../Base/Singleton'
|
||||
import { ApiMsgEnum, IModel, strdecode, strencode } from '../Common';
|
||||
import { ApiMsgEnum, IModel } from '../Common';
|
||||
import { binaryEncode, binaryDecode } from '../Common/Binary';
|
||||
|
||||
const TIMEOUT = 5000
|
||||
|
||||
interface IItem {
|
||||
cb: Function;
|
||||
ctx: unknown;
|
||||
}
|
||||
|
||||
export interface ICallApiRet<T> {
|
||||
success: boolean;
|
||||
error?: Error;
|
||||
@@ -18,7 +23,7 @@ export default class NetworkManager extends Singleton {
|
||||
|
||||
ws: WebSocket
|
||||
port = 8888
|
||||
maps: Map<ApiMsgEnum, Function[]> = new Map()
|
||||
maps: Map<ApiMsgEnum, Array<IItem>> = new Map()
|
||||
isConnected = false
|
||||
|
||||
connect() {
|
||||
@@ -28,16 +33,17 @@ export default class NetworkManager extends Singleton {
|
||||
return
|
||||
}
|
||||
this.ws = new WebSocket(`ws://localhost:${this.port}`)
|
||||
|
||||
//onmessage接受的数据类型,只有在后端返回字节数组的时候才有效果
|
||||
this.ws.binaryType = 'arraybuffer';
|
||||
|
||||
this.ws.onopen = () => {
|
||||
console.log("ws onopen")
|
||||
this.isConnected = true
|
||||
resolve(true)
|
||||
}
|
||||
|
||||
this.ws.onerror = () => {
|
||||
this.ws.onerror = (e) => {
|
||||
this.isConnected = false
|
||||
console.log(e)
|
||||
reject("ws onerror")
|
||||
}
|
||||
|
||||
@@ -53,12 +59,11 @@ export default class NetworkManager extends Singleton {
|
||||
try {
|
||||
if (this.maps.has(name) && this.maps.get(name).length) {
|
||||
console.log(json);
|
||||
this.maps.get(name).forEach(cb => cb(data))
|
||||
this.maps.get(name).forEach(({ cb, ctx }) => cb.call(ctx, data))
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("this.maps.get(name).forEach(cb => cb(restData))", error)
|
||||
console.log("onmessage:", error)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log('解析失败,不是合法的JSON格式', error)
|
||||
}
|
||||
@@ -72,20 +77,19 @@ export default class NetworkManager extends Singleton {
|
||||
// 超时处理
|
||||
const timer = setTimeout(() => {
|
||||
resolve({ success: false, error: new Error('timeout') })
|
||||
this.unlistenMsg(name, cb)
|
||||
this.unlistenMsg(name as any, cb, null)
|
||||
}, TIMEOUT)
|
||||
|
||||
// 回调处理
|
||||
const cb = (res) => {
|
||||
resolve(res)
|
||||
clearTimeout(timer)
|
||||
this.unlistenMsg(name, cb)
|
||||
this.unlistenMsg(name as any, cb, null)
|
||||
}
|
||||
this.listenMsg(name as any, cb)
|
||||
this.listenMsg(name as any, cb, null)
|
||||
|
||||
this.sendMsg(name as any, data)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
resolve({ success: false, error: error as Error })
|
||||
}
|
||||
})
|
||||
@@ -98,18 +102,19 @@ export default class NetworkManager extends Singleton {
|
||||
this.ws.send(view.buffer)
|
||||
}
|
||||
|
||||
listenMsg<T extends keyof IModel['msg']>(name: T, cb: (args: IModel['msg'][T]) => void) {
|
||||
listenMsg<T extends keyof IModel['msg']>(name: T, cb: (args: IModel['msg'][T]) => void, ctx: unknown) {
|
||||
if (this.maps.has(name)) {
|
||||
this.maps.get(name).push(cb)
|
||||
this.maps.get(name).push({ ctx, cb })
|
||||
} else {
|
||||
this.maps.set(name, [cb])
|
||||
this.maps.set(name, [{ ctx, cb }])
|
||||
}
|
||||
}
|
||||
|
||||
unlistenMsg(name: ApiMsgEnum, cb: Function) {
|
||||
unlistenMsg<T extends keyof IModel['msg']>(name: T, cb: (args: IModel['msg'][T]) => void, ctx: unknown) {
|
||||
if (this.maps.has(name)) {
|
||||
const index = this.maps.get(name).indexOf(cb)
|
||||
index > -1 && this.maps.get(name).splice(index, 1)
|
||||
const items = this.maps.get(name)
|
||||
const index = items.findIndex(i => cb === i.cb && i.ctx === ctx);
|
||||
index > -1 && items.splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -15,6 +15,11 @@ export default class ObjectPoolManager extends Singleton {
|
||||
return objectName + 'Pool'
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.objectPool = null
|
||||
this.map.clear()
|
||||
}
|
||||
|
||||
get(objectName: EntityTypeEnum) {
|
||||
if (this.objectPool === null) {
|
||||
this.objectPool = new Node("ObjectPool")
|
||||
|
Reference in New Issue
Block a user