示例#1
0
 protected override void OnUpdate()
 {
     Entities.ForEach((Entity e, ref PlayerControl pc) => {
         if (!pc.initialized)
         {
             Entities.ForEach((ref Communicator Com) => {
                 com = Com;
             });
             StartSystem();
             EntityManager.SetComponentData(e, new PlayerControl {
                 run = true, initialized = true
             });
         }
         if (pc.run)
         {
             //player movement
             if (Input.GetKeyDown(KeyCode.W))
             {
                 direction = Vector3.forward;
             }
             if (Input.GetKeyDown(KeyCode.A))
             {
                 direction = -Vector3.right;
             }
             if (Input.GetKeyDown(KeyCode.D))
             {
                 direction = Vector3.right;
             }
             if (Input.GetKeyDown(KeyCode.S))
             {
                 direction = -Vector3.forward;
             }
             if (Input.GetKeyDown(KeyCode.Backspace))
             {
                 BackToMenu();
             }
             //check if can move to the position if so move and check if it has a collectible as well
             Entities.ForEach((ref Player p, ref Translation playerTranslation) => {
                 SmoothFollow.target = playerTranslation.Value;
                 if (!(direction.x == 0 && direction.z == 0))
                 {
                     int index = (int)(playerTranslation.Value.z + direction.z) * com.width + (int)(playerTranslation.Value.x + direction.x);
                     if (!RecursiveBacktracking.hashGrid.ContainsKey(index))
                     {
                         player = playerTranslation.Value + direction;
                         playerTranslation.Value = player;
                     }
                     NativeArray <bool> result = new NativeArray <bool>(1, Allocator.TempJob);
                     Collect collect           = new Collect {
                         CommandBuffer = endSimCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
                         result        = result,
                         x             = (int)player.x,
                         z             = (int)player.z
                     };
                     JobHandle collectJobHandle = collect.Schedule(this);
                     collectJobHandle.Complete();
                     if (result[0])
                     {
                         SetupCollectible.PlayCollect();
                     }
                     result.Dispose();
                 }
                 direction = float3.zero;
                 if (Time.time - startTime > 3)
                 {
                     //don't let the player die in the first 3 sec so if it spawn in a bad position it has a chance
                     CheckEnemy();
                 }
             });
         }
     });
 }