Beispiel #1
0
        // ---------------------------------------------------------------------------------------------
        //
        // ---------------------------------------------------------------------------------------------
        private VoxelUtils.VoxelChunk createCutVoxelChunk(VoxelUtils.VoxelVector3Int p, int w, int h, int d)
        {
            //if (_isExperimentalChunk) {
            //Debug.Log ("createCutVoxelChunk " + p.ToString () + ", " + w + ", " + h + ", " + d);
            //}

            float width  = w * VoxelUtils.CHUNK_SIZE;
            float height = h * VoxelUtils.CHUNK_SIZE;
            float depth  = d * VoxelUtils.CHUNK_SIZE;

            Vector3 pos = new Vector3((p.x * VoxelUtils.CHUNK_SIZE) + (width / 2f), (p.y * VoxelUtils.CHUNK_SIZE) + (height / 2f), (p.z * VoxelUtils.CHUNK_SIZE) + (depth / 2f));

            Bounds b = new Bounds();              //coll.bounds;

            b.size   = new Vector3(width - VoxelUtils.CHUNK_SIZE, height - VoxelUtils.CHUNK_SIZE, depth - VoxelUtils.CHUNK_SIZE);
            b.center = pos;

            VoxelUtils.VoxelChunk vc = new VoxelUtils.VoxelChunk();
            vc.pos           = p;
            vc.size          = new VoxelUtils.VoxelVector3Int(w, h, d);
            vc.bounds        = b;
            vc.corners       = VoxelUtils.createVoxelCorners(p, w, h, d);
            vc.materialIndex = 0;
            vc.meshCreated   = false;

            return(vc);
        }
Beispiel #2
0
        // ---------------------------------------------------------------------------------------------
        private void createDefaultLevel()
        {
            VoxelUtils.VoxelVector3Int pos = VoxelUtils.convertVector3ToVoxelVector3Int(Vector3.zero);
            VoxelUtils.VoxelChunk      vc;

            if (_isExperimentalChunk)
            {
                // create the full chunk voxel
                vc = createVoxelChunk(pos, VoxelUtils.MAX_CHUNK_UNITS, VoxelUtils.MAX_CHUNK_UNITS, VoxelUtils.MAX_CHUNK_UNITS);
                _aVoxelChunks.Add(vc);
                setVoxelChunkMesh(vc);
            }
            else
            {
                int half = VoxelUtils.MAX_CHUNK_UNITS / 2;

                vc = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.z += half;
                vc     = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.x += half;
                vc     = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.z = 0;
                vc    = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.x  = 0;
                pos.y += half;

                vc = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.z += half;
                vc     = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.x += half;
                vc     = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.z = 0;
                vc    = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                // create room in center of level
                //subtractChunk (new Vector3 (32, 34, 32), new Vector3 (8, 4, 8));
                subtractChunk(new Vector3(64, 68, 64), new Vector3(16, 8, 16));
            }
        }
Beispiel #3
0
        // ---------------------------------------------------------------------------------------------
        public void paint(RaycastHit hit, Vector3 chunkSize, int materialIndex)
        {
            //if (AppController.Instance.appState == AppState.Paint) {
            //chunkSize.z = 1;
            //}

            dig(hit, chunkSize);

            VoxelUtils.VoxelChunk vc = createVoxelChunk(VoxelUtils.convertVector3ToVoxelVector3Int(_lastChunkPos), (int)chunkSize.x, (int)chunkSize.y, (int)chunkSize.z);
            vc.materialIndex = materialIndex;

            _aVoxelChunks.Add(vc);
            setVoxelChunkMesh(vc);
        }
Beispiel #4
0
        // ---------------------------------------------------------------------------------------------
        public void build(RaycastHit hit, Vector3 chunkSize, int materialIndex)
        {
            _lastChunkPos = getHitChunkPos(hit);

            if (hit.normal.x != 0)
            {
                if (hit.normal.x > 0)
                {
                    _lastChunkPos.x += 1;
                }
                else
                {
                    _lastChunkPos.x -= chunkSize.x;
                }
            }
            else if (hit.normal.y != 0)
            {
                if (hit.normal.y > 0)
                {
                    _lastChunkPos.y += 1;
                }
                else
                {
                    _lastChunkPos.y -= chunkSize.y;
                }
            }
            else if (hit.normal.z != 0)
            {
                if (hit.normal.z > 0)
                {
                    _lastChunkPos.z += 1;
                }
                else
                {
                    _lastChunkPos.z -= chunkSize.z;
                }
            }

            subtractChunk(_lastChunkPos, chunkSize);

            VoxelUtils.VoxelChunk vc = createVoxelChunk(VoxelUtils.convertVector3ToVoxelVector3Int(_lastChunkPos), (int)chunkSize.x, (int)chunkSize.y, (int)chunkSize.z);
            vc.materialIndex = materialIndex;

            _aVoxelChunks.Add(vc);
            setVoxelChunkMesh(vc);
        }
Beispiel #5
0
        // ---------------------------------------------------------------------------------------------
        // create single voxel
        // ---------------------------------------------------------------------------------------------
        public VoxelUtils.VoxelChunk createVoxelChunk(VoxelUtils.VoxelVector3Int p, int w, int h, int d)
        {
            //if (_isExperimentalChunk) {
            //	Debug.Log ("createVoxelChunk " + p.ToString () + ", " + w + ", " + h + ", " + d);
            //}

            GameObject cube = AssetFactory.Instance.createVoxelChunkClone();

            cube.transform.SetParent(_trfmVoxels);
            _iVoxelCount++;
            cube.name = "voxchunk_" + _iVoxelCount.ToString();

            //if (_isExperimentalChunk) {
            //	Debug.Log ("cube: " + cube.name + ", " + _trfmVoxels.name);
            //}

            float width  = w * VoxelUtils.CHUNK_SIZE;
            float height = h * VoxelUtils.CHUNK_SIZE;
            float depth  = d * VoxelUtils.CHUNK_SIZE;

            Vector3 pos = new Vector3((p.x * VoxelUtils.CHUNK_SIZE) + (width / 2f), (p.y * VoxelUtils.CHUNK_SIZE) + (height / 2f), (p.z * VoxelUtils.CHUNK_SIZE) + (depth / 2f));

            Bounds b = new Bounds();

            b.size   = new Vector3(width, height, depth);
            b.center = pos;

            VoxelUtils.VoxelChunk vc = new VoxelUtils.VoxelChunk();
            vc.go            = cube;
            vc.goPos         = pos;
            vc.pos           = p;
            vc.size          = new VoxelUtils.VoxelVector3Int(w, h, d);
            vc.bounds        = b;
            vc.corners       = VoxelUtils.createVoxelCorners(p, w, h, d);
            vc.materialIndex = 0;
            vc.meshCreated   = false;

            return(vc);
        }
Beispiel #6
0
        // ---------------------------------------------------------------------------------------------
        // cut a hole!
        // ---------------------------------------------------------------------------------------------
        public bool subtractChunk(Vector3 v3Pos, Vector3 v3Size, bool allowUndo = true)
        {
            bool success = true;

            float timer = Time.realtimeSinceStartup;

            if (allowUndo && !_isExperimentalChunk)
            {
                LevelEditor.Instance.resetUndoActions();
                saveCurrentVoxelChunks();
                MainMenu.Instance.setUndoButton(true);
            }

            VoxelUtils.VoxelVector3Int pos   = VoxelUtils.convertVector3ToVoxelVector3Int(v3Pos);
            VoxelUtils.VoxelChunk      vsCut = createCutVoxelChunk(pos, (int)v3Size.x, (int)v3Size.y, (int)v3Size.z);

            //if (!_isExperimentalChunk) {
            _levelMap.addCube(pos, new Vector3((int)v3Size.x, (int)v3Size.y, (int)v3Size.z));
            //}

            // does the new voxel intersect with any existing voxels?
            bool splittage = splitVoxels(vsCut);
            int  loops     = 0;

            while (splittage && loops < 1000)
            {
                splittage = splitVoxels(vsCut);
                loops++;
            }

            if (loops >= 1000)
            {
                Debug.LogWarning("looks like we got ourselves an endless loop here!");
                success = false;
            }
            else
            {
                VoxelUtils.VoxelChunk vc;
                int i, len = _aVoxelChunks.Count;
                //int count = 0;
                for (i = 0; i < len; ++i)
                {
                    vc = _aVoxelChunks [i];
                    if (!vc.meshCreated)
                    {
                        setVoxelChunkMesh(vc);
                        vc.meshCreated = true;
                        //count++;
                        _aVoxelChunks [i] = vc;
                    }
                }

                //if (_isExperimentalChunk) {
                //	Debug.Log ("num voxels: " + len + " - loops: " + loops + " - meshes created: " + count);
                //}
            }

            //if (_isExperimentalChunk) {
            //	Debug.Log ("Time to create chunk(s): " + (Time.realtimeSinceStartup - timer).ToString ());
            //}

            if (!_isExperimentalChunk)
            {
                MainMenu.Instance.setCubeCountText("Voxel Chunks: " + _aVoxelChunks.Count.ToString());
            }

            return(success);
        }
        //
        private void createLevel(LevelFile levelFile)
        {
            if (levelFile.fileFormatVersion != Globals.levelSaveFormatVersion)
            {
                AppController.Instance.showPopup(PopupMode.Notification, "Warning", Globals.warningObsoleteFileFormat);
                return;
            }

            currentLevelId = levelFile.levelId;

            MainMenu.Instance.setLevelNameText(levelFile.levelName);
            lastLevelName = levelFile.levelName;

            LevelEditor      levelEditor  = LevelEditor.Instance;
            PropsManager     propsManager = PropsManager.Instance;
            VoxelsLevelChunk levelChunk   = levelEditor.curVoxelsLevelChunk;

            levelEditor.resetAll();

            Vector3 savedPos = new Vector3(levelFile.playerPosition.x, levelFile.playerPosition.y, levelFile.playerPosition.z);
            Vector3 savedRot = new Vector3(levelFile.playerEuler.x, levelFile.playerEuler.y, levelFile.playerEuler.z);

            levelChunk.setStartPos(savedPos, savedRot);
            FlyCam.Instance.setNewInitialPosition(savedPos, savedRot);
            FlyCam.Instance.reset();

            //GameObject goQuadrant;
            //Transform trfmContainer;
            //GameObject container;
            Vector3 pos = Vector3.zero;

            int   quadLen   = levelEditor.cubesPerQuadrant;
            float fRockSize = levelEditor.fRockSize;

            int i, len = levelFile.levelVoxelChunks.Count;

            Debug.Log("levelFile.levelVoxelChunks.Count: " + levelFile.levelVoxelChunks.Count);
            for (i = 0; i < len; ++i)
            {
                pos.x = (int)levelFile.levelVoxelChunks[i].position.x;
                pos.y = (int)levelFile.levelVoxelChunks[i].position.y;
                pos.z = (int)levelFile.levelVoxelChunks[i].position.z;

                VoxelUtils.VoxelChunk vc = levelEditor.curVoxelsLevelChunk.createVoxelChunk(
                    VoxelUtils.convertVector3ToVoxelVector3Int(pos),
                    (int)levelFile.levelVoxelChunks[i].size.x,
                    (int)levelFile.levelVoxelChunks[i].size.y,
                    (int)levelFile.levelVoxelChunks[i].size.z
                    );

                vc.materialIndex = levelFile.levelVoxelChunks [i].materialId;

                levelEditor.curVoxelsLevelChunk.aVoxelChunks.Add(vc);

                levelEditor.curVoxelsLevelChunk.setVoxelChunkMesh(vc);
            }

            MainMenu.Instance.setCubeCountText("Voxel Chunks: " + levelEditor.curVoxelsLevelChunk.aVoxelChunks.Count.ToString());

            if (levelFile.levelProps != null)
            {
                LevelProp  levelProp;
                GameObject goProp;
                Quaternion rotation = Quaternion.identity;
                propDef    prop;
                string     name;

                len = levelFile.levelProps.Count;
                for (i = 0; i < len; ++i)
                {
                    levelProp = levelFile.levelProps [i];

                    pos.x = levelProp.position.x;
                    pos.y = levelProp.position.y;
                    pos.z = levelProp.position.z;

                    prop = propsManager.getPropDefForId(levelProp.id);
                    if (prop.id != -1)
                    {
                        name   = prop.name + "_" + levelChunk.trfmProps.childCount;
                        goProp = propsManager.createProp(prop, pos, name, levelChunk.trfmProps, prop.useCollider, prop.useGravity);

                        rotation.w = levelProp.rotation.w;
                        rotation.x = levelProp.rotation.x;
                        rotation.y = levelProp.rotation.y;
                        rotation.z = levelProp.rotation.z;
                        goProp.transform.rotation = rotation;

                        levelChunk.addWorldProp(prop.id, goProp);
                    }
                }
            }
        }