示例#1
0
    public void BlinqShouldEqualLinqNativeArrayFirstPredicate([ArrayValues] int[] sourceArr)
    {
        var source   = new NativeArray <int>(sourceArr, Allocator.Persistent);
        var expected = ExceptionAndValue(() => Linq.First(source, EqualsZero.Invoke));
        var actual   = ExceptionAndValue(() => source.First(EqualsZero));

        AssertAreEqual(expected, actual);
        source.Dispose();
    }
    protected override void OnUpdate()
    {
        EntityManager entityManager = EntityManager;

        EntityCommandBuffer commandBuffer
            = CommandBufferSystem.CreateCommandBuffer();
        NativeArray <LeoPlayerGameStatus> leoPlayerStatusReceive
            = entityQueryReceive.ToComponentDataArray <LeoPlayerGameStatus>(Allocator.TempJob);

        //NativeArray<int> hasDonePlayerIds = new NativeArray<int>(leoPlayerStatusReceive.Length, Allocator.TempJob);

        //NativeArray<int> CountDonePlayerIds = new NativeArray<int>(new int[] { 0 }, Allocator.TempJob);

        /*NativeArray<ReceiveRpcCommandRequestComponent> leoPlayerConnectReveive
         *  = entityQueryReceive.ToComponentDataArray<ReceiveRpcCommandRequestComponent>(Allocator.TempJob);*/


        Debug.Log("服务端收到客户端Rpc连接 LeoPlayerGameStatus ");

        // 对已经存在服务端的客户端连接状态,直接修改就好了。对于找不到的就只能在外部修改了
        var handle1 = Entities
                      .WithoutBurst()
                      // .WithStructuralChanges() //  WithStructuralChanges only supper Run()
                      .WithNone <SendRpcCommandRequestComponent, ReceiveRpcCommandRequestComponent>()
                      .ForEach(
            (Entity reqEnt,
             ref LeoPlayerGameStatus leoPlayerGameStatus) =>
        {
            int currentPlayerId = leoPlayerGameStatus.playerId;
            // int PlayerId = EntityManager.GetComponentData<NetworkIdComponent>(reqSrc.SourceConnection).Value;
            // int currentIndex = 0;
            if (leoPlayerStatusReceive.Select(s => s.playerId).Contains(currentPlayerId))
            {
                commandBuffer.SetComponent <LeoPlayerGameStatus>(reqEnt,
                                                                 leoPlayerStatusReceive.First(s => s.playerId == currentPlayerId));

                //hasDonePlayerIds[currentIndex] = currentPlayerId;
                //currentIndex++;
                //CountDonePlayerIds[0] = currentIndex;
            }
            else
            {
                // 不存在自然不会更新
            }
        }).Schedule(Dependency);

        handle1.Complete();

        var handle2 = Entities.ForEach((Entity entity, ref LeoPlayerGameStatus leoPlayerGame, ref ReceiveRpcCommandRequestComponent receive) =>
        {
            commandBuffer.DestroyEntity(entity); // 删除已经使用过的请求对象
        }).Schedule(handle1);

        handle2.Complete();

        var controller = commandBuffer.CreateEntity();

        commandBuffer.AddComponent <ServerGameStatusSendSystemController>(controller); // 添加控制以执行更新游戏状态并同步给客户端

        Dependency = leoPlayerStatusReceive.Dispose(handle2);


        #region 注释添加玩家状态实体的部分,转到初始化时添加

        /*
         *      var handle2 = Job.WithCode(() =>
         *      {
         *          // 把多余的数据标识为-1 不参与后面的比对
         *          if (CountDonePlayerIds[0]!=0)
         *          {
         *              for (var j = CountDonePlayerIds[0] - 1; j < hasDonePlayerIds.Length; j++)
         *              {
         *                  hasDonePlayerIds[CountDonePlayerIds[0] - 1] = -1;
         *              }
         *
         *          }
         *
         *          // var hasDonePlayerIdsArray = hasDonePlayerIds.ToArray(); // DynamicBuffer to NativeArray
         *          for (var i= CountDonePlayerIds[0]; i< leoPlayerStatusReceive.Length; i++)
         *          {
         *              var thePlayId = leoPlayerStatusReceive[i].playerId;
         *              if (!hasDonePlayerIds.Contains(thePlayId))
         *              {
         *                  var the = commandBuffer.CreateEntity();
         *                  commandBuffer.AddComponent<LeoPlayerGameStatus>(the, leoPlayerStatusReceive[i]);
         *              }
         *
         *          }
         *
         *          var controller  = commandBuffer.CreateEntity();
         *          commandBuffer.AddComponent<ServerGameStatusSendSystemController>(controller); // 添加控制以执行更新游戏状态并同步给客户端
         *
         *      }).Schedule(handle1);
         *
         *      handle2.Complete();
         *
         *      Dependency = leoPlayerStatusReceive.Dispose(handle2);  // dispose
         *      Dependency = hasDonePlayerIds.Dispose(Dependency);
         *      Dependency = CountDonePlayerIds.Dispose(Dependency);
         */
        #endregion
    }