Пример #1
0
    /// <summary>
    /// Updates the generation data.
    /// If there are finished chunk generations their data will get added to the chunk object.
    /// </summary>
    private void UpdateGenerationData()
    {
        lock (this.generationLockObject)
        {
            List <ListIndex <int> > jobsToDelete = new List <ListIndex <int> >();
            foreach (KeyValuePair <ListIndex <int>, ChunkGenerationJob> job in this.generationJobs.listSource)
            {
                if (job.Value.done)
                {
                    // Check if chunk object is in the chunk objects list
                    if (this.chunkObjects.ContainsKey(job.Key.x, job.Key.y, job.Key.z))
                    {
                        // Set chunk data
                        CubicTerrainChunk chunk = this.chunkObjects[job.Key].GetComponent <CubicTerrainChunk>();
                        chunk.master    = this;
                        chunk.chunkData = job.Value.terrainChunkData;

                        // Mark job for removal
                        jobsToDelete.Add(job.Key);
                    }
                }
            }

            // Delete jobs marked for removal
            foreach (ListIndex <int> t in jobsToDelete)
            {
                this.generationJobs.Remove(t);
            }
        }
    }
Пример #2
0
    /// <summary>
    /// Generates the chunk generation job for x|z.
    ///
    /// One Chunk generation job generates a all chunks on y-axis for x|z.
    /// </summary>
    /// <param name="x">The x coordinate.</param>
    /// <param name="z">The z coordinate.</param>
    private void GenerateChunk(int x, int z)
    {
        for (int y = 0; y < this.chunksOnYAxis; y++)
        {
            if (this.generationJobs.ContainsKey(x, y, z) || this.chunkData.ContainsKey(x, y, z))
            {
                return;
            }

            // Create gameobject
            Vector3 chunkPosition = this.transform.position + new Vector3
                                    (
                x * this.chunkWidth,
                y * this.chunkHeight,
                z * this.chunkDepth
                                    );

            GameObject chunkObject = new GameObject("Chunk (" + x + "|" + y + "|" + z + ")");
            chunkObject.transform.position = chunkPosition;
            chunkObject.transform.parent   = this.transform;
            chunkObject.layer = this.gameObject.layer;

            CubicTerrainChunk terrainChunk = chunkObject.AddComponent <CubicTerrainChunk> ();
            terrainChunk.chunkPosition = new Vector3(x, y, z);

            lock (this.generationLockObject)
            {
                this.chunkObjects.Add(x, y, z, chunkObject);
                this.generationJobs.Add(x, y, z, new ChunkGenerationJob(new CubicTerrainData(this, this.chunkWidth, this.chunkHeight, this.chunkDepth), chunkPosition));
            }
        }
    }
Пример #3
0
    public void Update()
    {
        // Right mouse button.
        if (Input.GetMouseButtonDown(1))
        {
            // The unity physics way

            if (CubicTerrain.GetInstance().useMeshColliders)
            {
                // Get ready to perform the raycast.
                RaycastHit hitInfo   = new RaycastHit();
                Ray        cameraRay = this.playerCamera.GetComponent <Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
                Debug.DrawRay(cameraRay.origin, cameraRay.direction, Color.red, 100.0f);

                // Perform the raycast
                if (Physics.Raycast(cameraRay, out hitInfo, 50, this.detectionMask.value))
                {
                    if (hitInfo.collider == null)
                    {
                        return;
                    }

                    // get collider parent
                    Transform chunkTransform = hitInfo.collider.transform.parent;

                    if (chunkTransform != null)
                    {
                        // Chunk hit?
                        CubicTerrainChunk chunk = chunkTransform.GetComponent <CubicTerrainChunk>();
                        if (chunk != null && !chunk.isDirty)
                        {
                            // Yes, chunk hit!
                            // Delete the clicked block
                            Vector3 block = chunk.GetBlockPosition(hitInfo, -0.5f);

                            chunk.chunkData.SetVoxel((int)block.x, (int)block.y, (int)block.z, -1);
                        }
                    }
                }
            }
            else
            {
                // Cubic World Physics way
                CubicRaycastHitInfo hitInfo = new CubicRaycastHitInfo();
                if (CubicPhysics.TerrainRaycastUnprecise(this.playerCamera.GetComponent <Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2)), 5.0f, out hitInfo))
                {
                    // Debug.Log ("Hit: " + hitInfo.hitPoint + ", Block: " + hitInfo.blockHit + ", Face: " + hitInfo.faceHit);
                    // Hit block
                    CubicTerrain.GetInstance().SetBlock((int)hitInfo.blockHit.x, (int)hitInfo.blockHit.y, (int)hitInfo.blockHit.z, -1);
                }
            }
        }
    }
Пример #4
0
    public void Update()
    {
        // Right mouse button.
        if (Input.GetKeyDown(KeyCode.R))
        {
            // Get ready to perform the raycast.
            RaycastHit hitInfo   = new RaycastHit();
            Ray        cameraRay = this.playerCamera.GetComponent <Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
            Debug.DrawRay(cameraRay.origin, cameraRay.direction, Color.red, 100.0f);

            // Perform the raycast
            if (Physics.Raycast(cameraRay, out hitInfo, 50, this.detectionMask.value))
            {
                if (hitInfo.collider == null)
                {
                    return;
                }

                // get collider parent
                Transform chunkTransform = hitInfo.collider.transform.parent;

                if (chunkTransform != null)
                {
                    // Chunk hit?
                    CubicTerrainChunk chunk = chunkTransform.GetComponent <CubicTerrainChunk>();
                    if (chunk != null && !chunk.isDirty)
                    {
                        // Yes, chunk hit!
                        // Rotate the clicked block
                        Vector3 block = chunk.GetBlockPosition(hitInfo, -0.5f);


                        byte rotation = (byte)Mathf.Repeat(chunk.chunkData.GetVoxel((int)block.x, (int)block.y, (int)block.z).rotation + 1, 4);
                        chunk.chunkData.GetVoxel((int)block.x, (int)block.y, (int)block.z).rotation = rotation;
                        chunk.chunkData.DataUpdated();
                        Debug.Log("Set rotation to " + rotation + " at " + new Vector3((int)block.x, (int)block.y, (int)block.z));
                    }
                }
            }
        }
    }
Пример #5
0
    public void Update()
    {
        // Right mouse button.
        if (Input.GetMouseButtonDown(0))
        {
            // Unity physics
            if (CubicTerrain.GetInstance().useMeshColliders)
            {
                // Get ready to perform the raycast.
                RaycastHit hitInfo   = new RaycastHit();
                Ray        cameraRay = this.playerCamera.GetComponent <Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
                Debug.DrawRay(cameraRay.origin, cameraRay.direction, Color.red, 100.0f);

                // Perform the raycast
                if (Physics.Raycast(cameraRay, out hitInfo, 5, this.detectionMask.value))
                {
                    if (hitInfo.collider == null)
                    {
                        return;
                    }

                    // get collider parent
                    Transform chunkTransform = hitInfo.collider.transform.parent;

                    if (chunkTransform != null)
                    {
                        // Chunk hit?
                        CubicTerrainChunk chunk = chunkTransform.GetComponent <CubicTerrainChunk>();
                        if (chunk != null && !chunk.isDirty)
                        {
                            // Yes, chunk hit!
                            BlockHitInfo blockHitInfo = chunk.GetBlockHitInfo(hitInfo);
                            int          x            = (int)blockHitInfo.hitBlock.x;
                            int          y            = (int)blockHitInfo.hitBlock.y;
                            int          z            = (int)blockHitInfo.hitBlock.z;

                            // Which face was hit? calculate target position for the new block
                            switch (blockHitInfo.hitFace)
                            {
                            case BlockFace.LEFT:
                                x -= 1;
                                break;

                            case BlockFace.RIGHT:
                                x += 1;
                                break;

                            case BlockFace.TOP:
                                y += 1;
                                break;

                            case BlockFace.BOTTOM:
                                y -= 1;
                                break;

                            case BlockFace.FRONT:
                                z += 1;
                                break;

                            case BlockFace.BACK:
                                z -= 1;
                                break;
                            }

                            Vector3 chunkPos = chunk.chunkPosition;

                            // Get chunk we want to place the block on
                            if (x < 0)
                            {
                                chunkPos.x -= 1;
                                x           = chunk.master.chunkWidth - 1;
                            }
                            if (x >= chunk.master.chunkWidth)
                            {
                                chunkPos.x += 1;
                                x           = 0;
                            }
                            if (y < 0)
                            {
                                chunkPos.y -= 1;
                                y           = chunk.master.chunkDepth - 1;
                            }
                            if (z >= chunk.master.chunkHeight)
                            {
                                chunkPos.y += 1;
                                y           = 0;
                            }
                            if (z < 0)
                            {
                                chunkPos.z -= 1;
                                z           = chunk.master.chunkDepth - 1;
                            }
                            if (z >= chunk.master.chunkDepth)
                            {
                                chunkPos.z += 1;
                                z           = 0;
                            }

                            // Finally place the object
                            GameObject chunkObject = chunk.master.GetChunkObject((int)chunkPos.x, (int)chunkPos.y, (int)chunkPos.z);
                            chunkObject.GetComponent <CubicTerrainChunk>().chunkData.SetVoxel(x, y, z, (short)this.blockId);
                        }
                    }
                }
            }
            else
            {
                // Cubic physics
                CubicRaycastHitInfo hitInfo = new CubicRaycastHitInfo();
                if (CubicPhysics.TerrainRaycastPrecise(this.playerCamera.GetComponent <Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2)), 5.0f, out hitInfo))
                {
                    Debug.Log("Hit: " + hitInfo.hitPoint + ", Block: " + hitInfo.blockHit + ", Face: " + hitInfo.faceHit);

                    // Get block to place position
                    int x = (int)hitInfo.blockHit.x;
                    int y = (int)hitInfo.blockHit.y;
                    int z = (int)hitInfo.blockHit.z;

                    // Which face was hit? calculate target position for the new block
                    switch (hitInfo.faceHit)
                    {
                    case BlockFace.LEFT:
                        x -= 1;
                        break;

                    case BlockFace.RIGHT:
                        x += 1;
                        break;

                    case BlockFace.TOP:
                        y += 1;
                        break;

                    case BlockFace.BOTTOM:
                        y -= 1;
                        break;

                    case BlockFace.FRONT:
                        z += 1;
                        break;

                    case BlockFace.BACK:
                        z -= 1;
                        break;
                    }

                    CubicTerrain.GetInstance().SetBlock(x, y, z, (short)this.blockId);
                }
            }
        }
    }
Пример #6
0
    public void Update()
    {
        bool firstPoint  = Input.GetKeyDown(KeyCode.P);
        bool lastPoint   = Input.GetKeyDown(KeyCode.L);
        bool definePoint = firstPoint || lastPoint;

        if (this.path != null)
        {
            if (this.path.isReady)
            {
                Debug.Log("Path found in " + path.runtime + " milliseconds!");
                foreach (Vector3 blockPos in this.path.pathData)
                {
                    CubicTerrain.GetInstance().SetBlock((int)blockPos.x, (int)blockPos.y - 1, (int)blockPos.z, -1);
                }
                this.path = null;
            }
            else if (!this.path.foundPath)
            {
                Debug.Log("Path not found :-( after search for " + path.runtime + " milliseconds!");
            }
        }

        // Right mouse button.
        if (definePoint)
        {
            if (CubicTerrain.GetInstance().useMeshColliders)
            {
                // Get ready to perform the raycast.
                RaycastHit hitInfo   = new RaycastHit();
                Ray        cameraRay = this.playerCamera.GetComponent <Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
                Debug.DrawRay(cameraRay.origin, cameraRay.direction, Color.red, 100.0f);

                // Perform the raycast
                if (Physics.Raycast(cameraRay, out hitInfo, 5, this.detectionMask.value))
                {
                    if (hitInfo.collider == null)
                    {
                        return;
                    }

                    // get collider parent
                    Transform chunkTransform = hitInfo.collider.transform.parent;

                    if (chunkTransform != null)
                    {
                        // Chunk hit?
                        CubicTerrainChunk chunk = chunkTransform.GetComponent <CubicTerrainChunk>();
                        if (chunk != null && !chunk.isDirty)
                        {
                            // Yes, chunk hit!
                            BlockHitInfo blockHitInfo = chunk.GetBlockHitInfo(hitInfo);
                            int          x            = (int)blockHitInfo.hitBlock.x;
                            int          y            = (int)blockHitInfo.hitBlock.y;
                            int          z            = (int)blockHitInfo.hitBlock.z;

                            // Which face was hit? calculate target position for the new block
                            switch (blockHitInfo.hitFace)
                            {
                            case BlockFace.LEFT:
                                x -= 1;
                                break;

                            case BlockFace.RIGHT:
                                x += 1;
                                break;

                            case BlockFace.TOP:
                                y += 1;
                                break;

                            case BlockFace.BOTTOM:
                                y -= 1;
                                break;

                            case BlockFace.FRONT:
                                z += 1;
                                break;

                            case BlockFace.BACK:
                                z -= 1;
                                break;
                            }

                            Vector3 chunkPos = chunk.chunkPosition;

                            // Get chunk we want to place the block on
                            if (x < 0)
                            {
                                chunkPos.x -= 1;
                                x           = chunk.master.chunkWidth - 1;
                            }
                            if (x >= chunk.master.chunkWidth)
                            {
                                chunkPos.x += 1;
                                x           = 0;
                            }
                            if (z < 0)
                            {
                                chunkPos.z -= 1;
                                z           = chunk.master.chunkDepth - 1;
                            }
                            if (z >= chunk.master.chunkDepth)
                            {
                                chunkPos.z += 1;
                                z           = 0;
                            }

                            // Finally place the object
                            GameObject chunkObject = chunk.master.GetChunkObject((int)chunkPos.x, (int)chunkPos.y, (int)chunkPos.z);
                            chunk = chunkObject.GetComponent <CubicTerrainChunk>();

                            // Get absolute position
                            Vector3 absoluteVoxelspace = chunk.GetAbsolutePosition(new Vector3(x, y, z));

                            Debug.Log("Point: " + absoluteVoxelspace);
                            if (firstPoint)
                            {
                                startPoint = absoluteVoxelspace;
                            }
                            else if (lastPoint)
                            {
                                goalPoint = absoluteVoxelspace;
                            }
                        }
                    }
                }
            }
            else
            {
                // Cubic World Physics way
                CubicRaycastHitInfo hitInfo = new CubicRaycastHitInfo();
                if (CubicPhysics.TerrainRaycastUnprecise(this.playerCamera.GetComponent <Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2)), 5.0f, out hitInfo))
                {
                    // Debug.Log ("Hit: " + hitInfo.hitPoint + ", Block: " + hitInfo.blockHit + ", Face: " + hitInfo.faceHit);
                    // Hit block
                    Vector3 topBlock = hitInfo.blockHit + Vector3.up;
                    Debug.Log("Top Block: " + topBlock);
                    if (firstPoint)
                    {
                        startPoint = topBlock;
                    }
                    else if (lastPoint)
                    {
                        goalPoint = topBlock;
                    }
                }
            }
        }

        if (startPoint != Vector3.zero && goalPoint != Vector3.zero)
        {
            Debug.Log("Starting A* path finding. Distance: " + Vector3.Distance(startPoint, goalPoint));

            // Start pathfinding
            CubicPathfinding pathfinder = CubicPathfinding.GetInstance();
            this.path = pathfinder.GetPath(startPoint, goalPoint, true);

            startPoint = Vector3.zero;
            goalPoint  = Vector3.zero;
        }
    }