render optimization
This commit is contained in:
		| @@ -55,6 +55,9 @@ export class ActorManager extends EntityManager implements IActor { | |||||||
|         weapon.setParent(this.node) |         weapon.setParent(this.node) | ||||||
|         this.weapon = weapon.addComponent(WeaponManager) |         this.weapon = weapon.addComponent(WeaponManager) | ||||||
|         this.weapon.init(data) |         this.weapon.init(data) | ||||||
|  |  | ||||||
|  |         this.targetPos = undefined | ||||||
|  |         this.node.active = false | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     tick(dt: number) { |     tick(dt: number) { | ||||||
| @@ -88,33 +91,32 @@ export class ActorManager extends EntityManager implements IActor { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     renderPosition(data: IActor) { |     renderPosition(data: IActor) { | ||||||
|         // this.node.setPosition(data.position.x, data.position.y) |  | ||||||
|  |  | ||||||
|         const newPos = new Vec3(data.position.x, data.position.y) |         const newPos = new Vec3(data.position.x, data.position.y) | ||||||
|         if (!this.targetPos) { |         if (!this.targetPos) { | ||||||
|  |             this.node.active = true | ||||||
|             this.node.setPosition(newPos) |             this.node.setPosition(newPos) | ||||||
|             this.targetPos = new Vec3() |             this.targetPos = new Vec3(newPos) | ||||||
|             this.targetPos.set(newPos) |  | ||||||
|         } else if (!this.targetPos.equals(newPos)) { |         } else if (!this.targetPos.equals(newPos)) { | ||||||
|             this.tw?.stop() |             this.tw?.stop() | ||||||
|             this.node.setPosition(this.targetPos) |             this.node.setPosition(this.targetPos) | ||||||
|             this.targetPos.set(newPos) |             this.targetPos.set(newPos) | ||||||
|  |             this.state = EntityStateEnum.Run | ||||||
|             this.tw = tween(this.node).to(0.1, { |             this.tw = tween(this.node).to(0.1, { | ||||||
|                 position: this.targetPos |                 position: this.targetPos | ||||||
|  |             }).call(() => { | ||||||
|  |                 this.state = EntityStateEnum.Idle | ||||||
|             }).start() |             }).start() | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         // this.node.setPosition(data.position.x, data.position.y) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     renderDirection(data: IActor) { |     renderDirection(data: IActor) { | ||||||
|         if (data.direction.x === 0 && data.direction.y === 0) { |  | ||||||
|             this.state = EntityStateEnum.Idle |  | ||||||
|             return |  | ||||||
|         } |  | ||||||
|         this.state = EntityStateEnum.Run |  | ||||||
|         const { x, y } = data.direction |         const { x, y } = data.direction | ||||||
|         if (x !== 0) { |         if (x !== 0) { | ||||||
|             this.node.setScale(x > 0 ? 1 : -1, 1) |             this.node.setScale(x > 0 ? 1 : -1, 1) | ||||||
|             this.label.node.setScale(x > 0 ? 1 : -1, 1) |             this.label.node.setScale(x > 0 ? 1 : -1, 1) | ||||||
|  |             this.hpBar.node.setScale(x > 0 ? 1 : -1, 1) | ||||||
|         } |         } | ||||||
|         const side = Math.sqrt(x * x + y * y) |         const side = Math.sqrt(x * x + y * y) | ||||||
|         const angle = rad2Angle(Math.asin(y / side)) |         const angle = rad2Angle(Math.asin(y / side)) | ||||||
|   | |||||||
| @@ -23,7 +23,7 @@ export class BulletManager extends EntityManager implements IBullet { | |||||||
|  |  | ||||||
|   private angle: number |   private angle: number | ||||||
|   private tw: Tween<any> |   private tw: Tween<any> | ||||||
|   private targetPos: Vec3 = new Vec3 |   private targetPos: Vec3 | ||||||
|  |  | ||||||
|   init({ id, owner, type }: IBullet) { |   init({ id, owner, type }: IBullet) { | ||||||
|     this.id = id |     this.id = id | ||||||
| @@ -34,6 +34,7 @@ export class BulletManager extends EntityManager implements IBullet { | |||||||
|     this.fsm.init(type) |     this.fsm.init(type) | ||||||
|     this.state = EntityStateEnum.Idle |     this.state = EntityStateEnum.Idle | ||||||
|     this.node.active = false |     this.node.active = false | ||||||
|  |     this.targetPos = undefined | ||||||
|  |  | ||||||
|     EventManager.Instance.on(EventEnum.ExplosionBorn, this.handleExplosion, this) |     EventManager.Instance.on(EventEnum.ExplosionBorn, this.handleExplosion, this) | ||||||
|   } |   } | ||||||
| @@ -62,10 +63,10 @@ export class BulletManager extends EntityManager implements IBullet { | |||||||
|  |  | ||||||
|   renderPosition(data: IBullet) { |   renderPosition(data: IBullet) { | ||||||
|     const newPos = new Vec3(data.position.x, data.position.y) |     const newPos = new Vec3(data.position.x, data.position.y) | ||||||
|     if (!this.node.active) { |     if (!this.targetPos) { | ||||||
|       this.node.active = true |       this.node.active = true | ||||||
|       this.node.setPosition(newPos) |       this.node.setPosition(newPos) | ||||||
|       this.targetPos.set(newPos) |       this.targetPos = new Vec3(newPos) | ||||||
|     } else if (!this.targetPos.equals(newPos)) { |     } else if (!this.targetPos.equals(newPos)) { | ||||||
|       this.tw?.stop() |       this.tw?.stop() | ||||||
|       this.node.setPosition(this.targetPos) |       this.node.setPosition(this.targetPos) | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| import { Node, Prefab, SpriteFrame } from 'cc' | import { Node, Prefab, SpriteFrame, clamp } from 'cc' | ||||||
| import Singleton from '../Base/Singleton' | import Singleton from '../Base/Singleton' | ||||||
| import { EntityTypeEnum, IBullet, IClientInput, InputTypeEnum, IRoom, IState, toFixed } from '../Common' | import { EntityTypeEnum, IBullet, IClientInput, InputTypeEnum, IRoom, IState, toFixed } from '../Common' | ||||||
| import { ActorManager } from '../Entity/Actor/ActorManager' | import { ActorManager } from '../Entity/Actor/ActorManager' | ||||||
| @@ -84,6 +84,10 @@ export default class DataManager extends Singleton { | |||||||
|  |  | ||||||
|         player.position.x += toFixed(x * PLAYER_SPEED * dt) |         player.position.x += toFixed(x * PLAYER_SPEED * dt) | ||||||
|         player.position.y += toFixed(y * 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.direction = { x, y } |         player.direction = { x, y } | ||||||
|         break |         break | ||||||
|       } |       } | ||||||
|   | |||||||
| @@ -91,8 +91,10 @@ export default class NetworkManager extends Singleton { | |||||||
|     }) |     }) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   sendMsg<T extends keyof IModel['msg']>(name: T, data: IModel['msg'][T]) { |   async sendMsg<T extends keyof IModel['msg']>(name: T, data: IModel['msg'][T]) { | ||||||
|     const view = binaryEncode(name, data) |     const view = binaryEncode(name, data) | ||||||
|  |     let networkLag = parseInt(new URLSearchParams(location.search).get('lag') || '0') || 0; | ||||||
|  |     await new Promise((r) => setTimeout(r, networkLag)) | ||||||
|     this.ws.send(view.buffer) |     this.ws.send(view.buffer) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -26,6 +26,7 @@ export class BattleManager extends Component { | |||||||
|         await this.loadRes() |         await this.loadRes() | ||||||
|         this.initScene() |         this.initScene() | ||||||
|         await this.connectServer() |         await this.connectServer() | ||||||
|  |         // 在场景初始化完毕之前,卡主别的玩家,准备好以后再告知服务器,等所有玩家都准备好以后才开始,这里就不做了 | ||||||
|         NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgServerSync, this.handleSync); |         NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgServerSync, this.handleSync); | ||||||
|         this.isInited = true |         this.isInited = true | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ export default class Room { | |||||||
|   id: number |   id: number | ||||||
|   players: Set<Player> = new Set() |   players: Set<Player> = new Set() | ||||||
|   lastSyncTime?: number |   lastSyncTime?: number | ||||||
|  |   timers: NodeJS.Timer[] = [] | ||||||
|  |  | ||||||
|   private inputs: Array<IClientInput> = [] |   private inputs: Array<IClientInput> = [] | ||||||
|  |  | ||||||
| @@ -30,6 +31,7 @@ export default class Room { | |||||||
|       player.rid = -1 |       player.rid = -1 | ||||||
|       this.players.delete(player) |       this.players.delete(player) | ||||||
|       if (!this.players.size) { |       if (!this.players.size) { | ||||||
|  |         this.timers.forEach(t => clearInterval(t)) | ||||||
|         RoomManager.Instance.closeRoom(this.id) |         RoomManager.Instance.closeRoom(this.id) | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
| @@ -57,9 +59,9 @@ export default class Room { | |||||||
|           y: 0 |           y: 0 | ||||||
|         }, |         }, | ||||||
|         hp: 100, |         hp: 100, | ||||||
|         type: EntityTypeEnum.Actor1, |         type: EntityTypeEnum.Actor2, | ||||||
|         weaponType: EntityTypeEnum.Weapon1, |         weaponType: EntityTypeEnum.Weapon2, | ||||||
|         bulletType: EntityTypeEnum.Bullet1, |         bulletType: EntityTypeEnum.Bullet2, | ||||||
|       })), |       })), | ||||||
|       bullets: [], |       bullets: [], | ||||||
|       nextBulletId: 1 |       nextBulletId: 1 | ||||||
| @@ -71,12 +73,13 @@ export default class Room { | |||||||
|       }) |       }) | ||||||
|     } |     } | ||||||
|     this.listenPlayer() |     this.listenPlayer() | ||||||
|     setInterval(() => { |     let t1 = setInterval(() => { | ||||||
|       this.syncInput() |       this.syncInput() | ||||||
|     }, 100) |     }, 100) | ||||||
|     setInterval(() => { |     let t2 = setInterval(() => { | ||||||
|       this.timePast() |       this.timePast() | ||||||
|     }, 16) |     }, 16) | ||||||
|  |     this.timers = [t1, t2] | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   listenPlayer() { |   listenPlayer() { | ||||||
| @@ -105,6 +108,5 @@ export default class Room { | |||||||
|       dt: toFixed(dt) |       dt: toFixed(dt) | ||||||
|     }) |     }) | ||||||
|     this.lastSyncTime = now; |     this.lastSyncTime = now; | ||||||
|  |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user