This commit is contained in:
sli97
2022-12-03 21:28:38 +08:00
parent f3dc3ef7ba
commit a678a5b3fc
10 changed files with 76 additions and 32 deletions

View File

@@ -4,7 +4,7 @@ import { ApiMsgEnum, EntityTypeEnum, IActor, InputTypeEnum, IVec2 } from '../../
import { EntityStateEnum } from '../../Enum';
import DataManager from '../../Global/DataManager';
import NetworkManager from '../../Global/NetworkManager';
import { rad2Angle } from '../../Utils';
import { rad2Angle, toFixed } from '../../Utils';
import { WeaponManager } from '../Weapon/WeaponManager';
import { PlayerStateMachine } from './ActorStateMachine';
const { ccclass } = _decorator;
@@ -61,13 +61,15 @@ export class ActorManager extends EntityManager implements IActor {
const { x, y } = DataManager.Instance.jm.input
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
type: InputTypeEnum.ActorMove,
id: this.id,
direction: {
x,
y
},
dt
input: {
type: InputTypeEnum.ActorMove,
id: this.id,
direction: {
x: toFixed(x),
y: toFixed(y),
},
dt: toFixed(dt)
}
})
}

View File

@@ -5,6 +5,7 @@ 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
@@ -61,16 +62,18 @@ export class WeaponManager extends EntityManager {
const directionVec2 = new Vec2(pointWorldPos.x - anchorWorldPos.x, pointWorldPos.y - anchorWorldPos.y).normalize()
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
type: InputTypeEnum.WeaponShoot,
owner: this.owner,
position: {
x: pointStagePos.x,
y: pointStagePos.y
},
direction: {
x: directionVec2.x,
y: directionVec2.y
},
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),
},
}
})
}
}

View File

@@ -22,6 +22,10 @@ export default class NetworkManager extends Singleton {
connect() {
return new Promise((resolve, reject) => {
if (this.isConnected) {
resolve(true)
return
}
this.ws = new WebSocket(`ws://localhost:${this.port}`)
this.ws.onopen = () => {
console.log("ws onopen")

View File

@@ -7,7 +7,8 @@ import { PrefabPathEnum, TexturePathEnum } from '../Enum';
import NetworkManager from '../Global/NetworkManager';
import ObjectPoolManager from '../Global/ObjectPoolManager';
import { BulletManager } from '../Entity/Bullet/BulletManager';
import { EntityTypeEnum, InputTypeEnum } from '../Common';
import { ApiMsgEnum, EntityTypeEnum, IMsgServerSync, InputTypeEnum } from '../Common';
import { toFixed } from '../Utils';
const { ccclass } = _decorator;
@@ -26,6 +27,7 @@ export class BattleManager extends Component {
await this.loadRes()
this.initScene()
await this.connectServer()
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgServerSync, this.handleSync);
this.isInited = true
}
@@ -84,6 +86,12 @@ export class BattleManager extends Component {
map.setParent(this.stage)
}
handleSync({ inputs }: IMsgServerSync) {
for (const input of inputs) {
DataManager.Instance.applyInput(input)
}
}
update(dt: number) {
if (!this.isInited) {
return
@@ -108,9 +116,11 @@ export class BattleManager extends Component {
}
tickGlobal(dt: number) {
DataManager.Instance.applyInput({
type: InputTypeEnum.TimePast,
dt
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
input: {
type: InputTypeEnum.TimePast,
dt: toFixed(dt),
}
})
}

View File

@@ -9,3 +9,4 @@ 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