Exemplo n.º 1
0
 // Use this for initialization
 void Start()
 {
     xRotation = 0;
     yRotation = 0;
     xOldChunk = ChunkCoordinates2D.ConvertSpace((int)transform.position.x, CoordinateSpace.World);
     zOldChunk = ChunkCoordinates2D.ConvertSpace((int)transform.position.z, CoordinateSpace.World);
     _onChunkBorderCrossed.Invoke();
 }
Exemplo n.º 2
0
    void Update()
    {
        int xChunk = ChunkCoordinates2D.ConvertSpace((int)transform.position.x, CoordinateSpace.World);
        int zChunk = ChunkCoordinates2D.ConvertSpace((int)transform.position.z, CoordinateSpace.World);

        if (Math.Abs(this.xOldChunk - xChunk) >= borderCrossEventBorderCount ||
            Math.Abs(this.zOldChunk - zChunk) >= borderCrossEventBorderCount)
        {
            xOldChunk = xChunk;
            zOldChunk = zChunk;
            _onChunkBorderCrossed.Invoke();
        }

        if (Input.GetKey(KeyCode.W))
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                transform.position += transform.forward * turboMovementSpeed * Time.deltaTime;
            }
            else
            {
                transform.position += transform.forward * movementSpeed * Time.deltaTime;
            }
        }

        if (Input.GetKey(KeyCode.S))
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                transform.position -= transform.forward * turboMovementSpeed * Time.deltaTime;
            }
            else
            {
                transform.position -= transform.forward * movementSpeed * Time.deltaTime;
            }
        }

        if (Input.GetKey(KeyCode.A))
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                transform.position -= transform.right * turboMovementSpeed * Time.deltaTime;
            }
            else
            {
                transform.position -= transform.right * movementSpeed * Time.deltaTime;
            }
        }

        if (Input.GetKey(KeyCode.D))
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                transform.position += transform.right * turboMovementSpeed * Time.deltaTime;
            }
            else
            {
                transform.position += transform.right * movementSpeed * Time.deltaTime;
            }
        }

        if (Input.GetKey(KeyCode.E))
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                transform.position += transform.up * turboMovementSpeed * Time.deltaTime;
            }
            else
            {
                transform.position += transform.up * movementSpeed * Time.deltaTime;
            }
        }

        if (Input.GetKey(KeyCode.Q))
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                transform.position -= transform.up * turboMovementSpeed * Time.deltaTime;
            }
            else
            {
                transform.position -= transform.up * movementSpeed * Time.deltaTime;
            }
        }

        if (Input.GetButton("Fire1"))
        {
            xRotation         += mouseSensitivity * Input.GetAxis("Mouse X");
            yRotation         -= mouseSensitivity * Input.GetAxis("Mouse Y");
            yRotation          = Mathf.Clamp(yRotation, -90, 90);
            transform.rotation = Quaternion.Euler(yRotation, xRotation, 0);
        }
    }
    private void UpdateChunkRemoval()
    {
        int playerX = ChunkCoordinates2D.ConvertSpace((int)origin.transform.position.x, CoordinateSpace.World);
        int playerZ = ChunkCoordinates2D.ConvertSpace((int)origin.transform.position.z, CoordinateSpace.World);

        for (int i = chunksToGenerate.Count - 1; i >= 0; i--)
        {
            ChunkCoordinates2D coordinates = chunksToGenerate[i].coordinates;
            int x = coordinates.GetX(CoordinateSpace.Chunk);
            int z = coordinates.GetZ(CoordinateSpace.Chunk);

            if (Vector2.Distance(new Vector2(x, z), new Vector2(playerX, playerZ)) > playerController.viewDistanceFurthest)
            {
                chunksToRemove.Add(chunksToGenerate[i]);
                chunksToGenerate.RemoveAt(i);
            }
        }

        for (int i = chunksGenerated.Count - 1; i >= 0; i--)
        {
            ChunkCoordinates2D coordinates = chunksGenerated[i].coordinates;
            int x = coordinates.GetX(CoordinateSpace.Chunk);
            int z = coordinates.GetZ(CoordinateSpace.Chunk);

            if (Vector2.Distance(new Vector2(x, z), new Vector2(playerX, playerZ)) > playerController.viewDistanceFurthest)
            {
                chunksToRemove.Add(chunksGenerated[i]);
                chunksGenerated.RemoveAt(i);
            }
        }

        int chunksRemoved = 0;

        while (chunksToRemove.Count > 0 && chunksRemoved < chunkRemoveCount)
        {
            chunksToRemove[0].Destroy();
            chunksToRemove.RemoveAt(0);
            chunksRemoved++;
        }

        //List<ulong> chunksToRemove = new List<ulong>();

        //foreach (KeyValuePair<ulong, Chunk> pair in Chunk.chunks) {
        //    ChunkCoordinates2D coordinates = pair.Value.coordinates;
        //    int x = coordinates.GetX(CoordinateSpace.Chunk);
        //    int z = coordinates.GetZ(CoordinateSpace.Chunk);
        //    int playerX = ChunkCoordinates2D.ConvertSpace((int)origin.transform.position.x, CoordinateSpace.World);
        //    int playerZ = ChunkCoordinates2D.ConvertSpace((int)origin.transform.position.z, CoordinateSpace.World);

        //    if (Vector2.Distance(new Vector2(x, z), new Vector2(playerX, playerZ)) > playerController.viewDistanceFurthest) {
        //        chunksToRemove.Add(pair.Key);
        //    }
        //}

        //int counter = 0;

        //foreach (ulong hash in chunksToRemove) {
        //    if (counter >= removeChunkCount) {
        //        break;
        //    }

        //    Chunk chunk;
        //    Chunk.chunks.TryGetValue(hash, out chunk);

        //    if (chunk != null) {
        //        chunk.Destroy();
        //        counter++;
        //    } else {
        //        Debug.Log("Tried retrieving a chunk with hash '" + hash + "', but got null.");
        //    }
        //}
    }