protected override void OnUpdate() { EntityCommandBuffer commandBuffer = CommandBufferSystem.CreateCommandBuffer(); NativeArray <LeoPlayerGameStatus> leoPlayerGameStatuses = leoPlayerGameStatusesQuery.ToComponentDataArray <LeoPlayerGameStatus>(Allocator.TempJob); /*NativeArray<ReceiveRpcCommandRequestComponent> leoPlayerConnectReveive // 由于发送请求需要对于每个人都发所以可以直接通过系统获取 * = leoPlayerGameStatusesQuery.ToComponentDataArray<ReceiveRpcCommandRequestComponent>(Allocator.TempJob);*/ NativeArray <LeoGameStatus> leoGameStatusesArray = new NativeArray <LeoGameStatus>(new LeoGameStatus[] { new LeoGameStatus { theGameStatus = TheGameStatus.NoReady } }, Allocator.TempJob); // 删除标识防止一直执行 commandBuffer.DestroyEntity(GetSingletonEntity <ServerGameStatusSendSystemController>()); // 遍历每个玩家的状态进行状态判断 var handle1 = Entities .WithoutBurst() .ForEach((Entity entity, in LeoGameStatus gameStatus) => { var countPlayer = leoPlayerGameStatuses.Length; if (leoPlayerGameStatuses.Count(s => s.playerGameStatus == PlayerGameStatus.NotReady) == countPlayer) { leoGameStatusesArray[0] = new LeoGameStatus { theGameStatus = TheGameStatus.NoReady }; //commandBuffer.SetComponent<LeoGameStatus>(entity,new LeoGameStatus { theGameStatus= TheGameStatus.NoReady }); } else if (leoPlayerGameStatuses.Count(s => s.playerGameStatus == PlayerGameStatus.Ready) < countPlayer) { leoGameStatusesArray[0] = new LeoGameStatus { theGameStatus = TheGameStatus.PartReady }; //commandBuffer.SetComponent<LeoGameStatus>(entity, new LeoGameStatus { theGameStatus = TheGameStatus.PartReady }); } else if (countPlayer < 2) // 解决只有一个玩家的时候可能提示游戏已经准备完成的问题 { leoGameStatusesArray[0] = new LeoGameStatus { theGameStatus = TheGameStatus.PartReady }; } else if (leoPlayerGameStatuses.Count(s => s.playerGameStatus == PlayerGameStatus.Ready) == countPlayer) { leoGameStatusesArray[0] = new LeoGameStatus { theGameStatus = TheGameStatus.AllReady }; //commandBuffer.SetComponent<LeoGameStatus>(entity, new LeoGameStatus { theGameStatus = TheGameStatus.AllReady }); // AllReady 会触发球生成 /*World.GetOrCreateSystem<BallSpawnerSystem>(); // 这个只能在服务端添加; 出现不能在ForEach中添加动态代码的错误*/ var tEnt = commandBuffer.CreateEntity(); commandBuffer.AddComponent <BallSpawnerSystemController>(tEnt); } else if (leoPlayerGameStatuses.Count(s => s.playerGameStatus == PlayerGameStatus.Playing) == countPlayer) { leoGameStatusesArray[0] = new LeoGameStatus { theGameStatus = TheGameStatus.Playing }; //commandBuffer.SetComponent<LeoGameStatus>(entity, new LeoGameStatus { theGameStatus = TheGameStatus.Playing }); } commandBuffer.SetComponent <LeoGameStatus>(entity, leoGameStatusesArray[0]); }).Schedule(Dependency); handle1.Complete(); //完成游戏状态更新后,同步状态信息给客户端 var handle2 = Entities .ForEach((Entity ent, ref NetworkIdComponent id) => { var entity = commandBuffer.CreateEntity(); commandBuffer.AddComponent <LeoGameStatus>(entity, leoGameStatusesArray[0]); commandBuffer.AddComponent <SendRpcCommandRequestComponent>(entity, new SendRpcCommandRequestComponent { TargetConnection = ent }); }).Schedule(handle1); handle2.Complete(); Dependency = leoPlayerGameStatuses.Dispose(handle2); Dependency = leoGameStatusesArray.Dispose(Dependency); //Job.WithCode(() => //{ // for(var i=0;i< leoPlayerGameStatuses.Length; i++) // { // var entity= commandBuffer.CreateEntity(); // commandBuffer.AddComponent<LeoGameStatus>(entity, leoGameStatusesArray[0]); // commandBuffer.AddComponent<SendRpcCommandRequestComponent>(entity, // new SendRpcCommandRequestComponent // { // TargetConnection= leoPlayerConnectReveive[i].SourceConnection // }); // } //}).Schedule(); }
protected override void OnUpdate() { if (gameOverObject == null) { gameOverObject = Resources.Load <GameObject>("HintPrepare"); gameOverObject.GetComponent <TextMesh>().text = ""; gameOverObject = GameObject.Instantiate(gameOverObject); } EntityCommandBuffer commandBuffer = CommandBufferSystem.CreateCommandBuffer(); NativeArray <ServerGameOverSystemController> serverGameOvers = entityQueryReceive.ToComponentDataArray <ServerGameOverSystemController>(Allocator.TempJob); NativeArray <Entity> serverGameOversEntities = entityQueryReceive.ToEntityArray(Allocator.TempJob); Entities.WithoutBurst() .ForEach((Entity ent, ref LeoGameStatus gameStatus, ref LeoPlayerGameStatus playerGameStatus) => { gameStatus = new LeoGameStatus { theGameStatus = TheGameStatus.Over }; if (serverGameOvers[0].WinPlayerId == playerGameStatus.playerId) { gameOverObject.GetComponent <TextMesh>().text = "You Win!"; } else if (playerGameStatus.playerId <= 1) { gameOverObject.GetComponent <TextMesh>().text = "You Lost!"; } else { gameOverObject.GetComponent <TextMesh>().text = serverGameOvers[0].WinPlayerId == 0 ? "Left Win!" : "Right Win!"; } GameObject.Destroy(gameOverObject, 5); // 删除对象 // gameOverObject.GetComponent<TextMesh>().text = ""; // 重新准备 playerGameStatus = new LeoPlayerGameStatus { playerGameStatus = PlayerGameStatus.NotReady, playerId = playerGameStatus.playerId }; // 同步客户端状态修改到服务端,又可以开始新的一局游戏了 if (GetEntityQuery(new ComponentType[] { typeof(ClientGameStatusSendSystemController) }).CalculateChunkCount() == 0) { var entity = commandBuffer.CreateEntity(); commandBuffer.AddComponent <ClientGameStatusSendSystemController>(entity); } if (GetEntityQuery(new ComponentType[] { typeof(PlayerInitSystemController) }).CalculateChunkCount() == 0) { var playerInitEntity = commandBuffer.CreateEntity(); commandBuffer.AddComponent <PlayerInitSystemController>(playerInitEntity); } // commandBuffer.DestroyEntity(GetSingletonEntity<ServerGameOverSystemController>()); //Entities.ForEach((Entity theEntity, in ServerGameOverSystemController controller, in ReceiveRpcCommandRequestComponent receive) => //{ // commandBuffer.DestroyEntity(theEntity); //}).Run(); for (var i = 0; i < serverGameOversEntities.Length; i++) { commandBuffer.DestroyEntity(serverGameOversEntities[i]); } }).Run(); var handle11 = Job.WithCode(() => { for (var i = 0; i < serverGameOversEntities.Length; i++) { commandBuffer.DestroyEntity(serverGameOversEntities[i]); } }).Schedule(Dependency); handle11.Complete(); var handle2 = Entities .WithNone <SendRpcCommandRequestComponent>() .ForEach( (Entity reqEnt, ref ServerGameOverSystemController controller, ref ReceiveRpcCommandRequestComponent reqSrc) => { //删除完成的请求 commandBuffer.DestroyEntity(reqEnt); }).Schedule(handle11); handle2.Complete(); Dependency = serverGameOvers.Dispose(handle2); Dependency = serverGameOversEntities.Dispose(Dependency); }