private static async ETVoid CreateOtherCube(int account, Vector3 InitPosition, ETTaskCompletionSource <OtherCube> tcs) { try { GameObject resObj = await ETModel.Game.Scene.GetComponent <ResourcesAsyncComponent>().LoadAssetAsync <GameObject>(Assets.LoadAssetAsync("Assets/Bundles/Prefab/OtherCube.prefab", typeof(GameObject))); ReferenceCollector rc = resObj.GetComponent <ReferenceCollector>(); GameObject otherCubeObj = GameObject.Instantiate <GameObject>(rc.Get <GameObject>("OtherCube")); GameObject otherDirCubeObj = GameObject.Instantiate <GameObject>(rc.Get <GameObject>("OtherDirCube")); GameObject ganFire = GameObject.Instantiate <GameObject>(rc.Get <GameObject>("gunFire")); OtherCube otherCube = ComponentFactory.Create <OtherCube, int, GameObject, GameObject>(account, otherCubeObj, otherDirCubeObj, false); //添加攻击脚本 OtherCubeAttackComponent otherCubeAttackComponent = otherCube.AddComponent <OtherCubeAttackComponent, GameObject>(ganFire); //添加网络同步组件 OtherCubeNetSyncComponent otherCubeNetSyncComponent = otherCube.AddComponent <OtherCubeNetSyncComponent, int, Vector3>(account, InitPosition); tcs.SetResult(otherCube); } catch (Exception e) { Log.Error(e); tcs.SetResult(null); } }
/// <summary> /// 添加其它cube玩家的网络同步组件 /// </summary> public void AddNetSyncComponentByOtherCubeAccount(int Account, OtherCubeNetSyncComponent netSyncComponent) { if (!otherCubeAccountToNetSyncComponent.ContainsKey(Account)) { otherCubeAccountToNetSyncComponent.Add(Account, netSyncComponent); } else { Log.Info("其它玩家已经存在:" + Account); } }
protected override async ETTask Run(ETModel.Session session, G2C_OtherPlayerEnterMap message) { Log.Info("其它玩家进入Map: " + message.Account); OtherCubeNetSyncComponent NetSyncComponent = Game.Scene.GetComponent <OtherCubeManagerComponent>() .GetNetSyncComponentByOtherCubeAccount(message.Account); if (NetSyncComponent != null) { NetSyncComponent.NetWorkAsyncPosition(new Vector3(message.PositionX, message.PositionY, message.PositionZ), Quaternion.identity, Vector3.zero); } else { OtherCube otherCube = await OtherCubeFactory.Create(message.Account, new Vector3(message.PositionX, message.PositionY, message.PositionZ)); } await ETTask.CompletedTask; }
protected override async ETTask Run(ETModel.Session session, G2C_OtherPlayerPosition message) { if (otherCubeManagerComponent == null) { otherCubeManagerComponent = Game.Scene.GetComponent <OtherCubeManagerComponent>(); } if (message.ServerTime > serverTime) { serverTime = message.ServerTime; int[] DirAccount = message.DirAccount.array; float[] PositionX = message.PositionX.array; float[] PositionY = message.PositionY.array; float[] PositionZ = message.PositionZ.array; float[] RotationX = message.RotationX.array; float[] RotationY = message.RotationY.array; float[] RotationZ = message.RotationZ.array; float[] RotationW = message.RotationW.array; float[] VelocityX = message.VelocityX.array; float[] VelocityY = message.VelocityY.array; float[] VelocityZ = message.VelocityZ.array; bool[] Fire = message.Fire.array; for (int i = 0; i < DirAccount.Length; i++) { OtherCubeNetSyncComponent otherCubeNetSyncComponent = otherCubeManagerComponent.GetNetSyncComponentByOtherCubeAccount(DirAccount[i]); if (otherCubeNetSyncComponent != null) { otherCubeNetSyncComponent.NetWorkAsyncPosition(new Vector3(PositionX[i], PositionY[i], PositionZ[i]), new Quaternion(RotationX[i], RotationY[i], RotationZ[i], RotationW[i]), new Vector3(VelocityX[i], VelocityY[i], VelocityZ[i])); //Log.Info("同步一次位置:" + DirAccount[i]); otherCubeNetSyncComponent.NetWorkAsyncFire(Fire[i]); } } PlayerInfoComponent playerInfoComponent = Game.Scene.GetComponent <PlayerInfoComponent>(); //if (message.Bullets.array.Length != 0) //{ // Debug.LogError("子弹数量:" + message.Bullets.count); //} //同步子弹数量 int count = message.Bullets.count; for (int i = 0; i < count; i++) { //不是自己的子弹才需要创建同步 if (message.Bullets[i].Account != playerInfoComponent.account) { CubeBullet cubeBullet = CubeBulletFactory.CreateCubeBullet(); cubeBullet.SyncBullet(new Vector3(message.Bullets[i].PositionX, message.Bullets[i].PositionY, message.Bullets[i].PositionZ), new Quaternion(message.Bullets[i].RotationX, message.Bullets[i].RotationY, message.Bullets[i].RotationZ, message.Bullets[i].RotationW), new Vector3(message.Bullets[i].VelocityX, message.Bullets[i].VelocityY, message.Bullets[i].VelocityZ)); //Debug.LogError("创建一颗子弹"); } } } else { Debug.LogError("丢包了: " + message.ServerTime + " || " + serverTime); } await ETTask.CompletedTask; }