示例#1
0
 public virtual void Initialize(Voxelmetric vm)
 {
     this.vm    = vm;
     chunkPower = _chunkPower;
     chunkSize  = 1 << chunkPower;
     blockSize  = _blockSize;
 }
示例#2
0
 public override TerrainLayer[] GetLayers(Voxelmetric vm)
 {
     return(new TerrainLayer[] {
         new SimpleLayer {
             baseHeight = 20,
             frequency = 0.01f,
             amplitude = 16,
             absolute = true,
             blockId = vm.components.blockLoader.GetId("rock")
         },
         new SimpleLayer {
             baseHeight = 0,
             frequency = 0.1f,
             amplitude = 6,
             absolute = false,
             blockId = vm.components.blockLoader.GetId("rock")
         },
         new SimpleLayer {
             baseHeight = 20,
             frequency = 0.05f,
             amplitude = 2,
             absolute = true,
             blockId = vm.components.blockLoader.GetId("dirt")
         },
         new SimpleLayer {
             baseHeight = 2,
             frequency = 0.01f,
             amplitude = 2,
             absolute = false,
             blockId = vm.components.blockLoader.GetId("grass")
         }
     });
 }
        public void SaveAll()
        {
            if (saveProgress != null)
            {
                return;
            }

            saveProgress = new SaveProgress(Voxelmetric.SaveAll(world));
        }
示例#4
0
    public override void Initialize(Voxelmetric vm)
    {
        base.Initialize(vm);
        noise = new Noise(seed);

        layers = FindObjectOfType <LayerStore>().GetLayers(vm);

        Utils.ProfileCall(() =>
        {
            foreach (var layer in layers)
            {
                layer.VmStart(this);
            }
        }, "Initialize layers");
    }
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            rot = new Vector2(
                rot.x + Input.GetAxis("Mouse X") * 3,
                rot.y + Input.GetAxis("Mouse Y") * 3);

            transform.localRotation  = Quaternion.AngleAxis(rot.x, Vector3.up);
            transform.localRotation *= Quaternion.AngleAxis(rot.y, Vector3.left);
        }
        transform.position += transform.forward * 50 * Input.GetAxis("Vertical") * Time.deltaTime;
        transform.position += transform.right * 50 * Input.GetAxis("Horizontal") * Time.deltaTime;

        RaycastHit hit;
        var        mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));

        if (Input.GetMouseButtonDown(0))
        {
            bool adjacent = true;
            if (((Block)blockToPlace).type == Block.Air.type)
            {
                adjacent = false;
            }

            if (Physics.Raycast(Camera.main.transform.position, mousePos - Camera.main.transform.position, out hit, 100))
            {
                Voxelmetric.SetBlock(hit, blockToPlace, adjacent);
            }
        }

        if (Physics.Raycast(Camera.main.transform.position, mousePos - Camera.main.transform.position, out hit, 100))
        {
            selectedBlockText.text = Voxelmetric.GetBlock(hit).ToString();
        }

        if (saveProgress != null)
        {
            saveProgressText.text = SaveStatus();
        }
        else
        {
            saveProgressText.text = "Save";
        }
    }
示例#6
0
    public virtual void LoadBlocks(Voxelmetric vm)
    {
        this.vm = vm;

        idAttributes = new Dictionary <int, BlockType>();
        nameId       = new Dictionary <string, int>();
        idName       = new Dictionary <int, string>();

        AddType(AirAttributes());
        AddType(VoidAttributes());

        var typesToLoad = FindObjectOfType <BlockTypeStore>().GetBlockTypes(vm);

        foreach (var attr in typesToLoad)
        {
            AddType(attr);
        }

        Debug.Log(string.Format("Voxelmetric Block Type Loader loaded {0} block types", idName.Keys.Count));
    }
示例#7
0
 public BlockType[] GetBlockAttrs(Voxelmetric vm)
 {
     return(new BlockType[] {
         new CubeBlockType()
         {
             blockName = "rock",
             id = 2,
             solid = true,
             canBeWalkedOn = true,
             canBeWalkedThrough = false,
             textureName = "rock",
         },
         new CubeBlockType()
         {
             blockName = "dirt",
             id = 3,
             solid = true,
             canBeWalkedOn = false,
             canBeWalkedThrough = false,
             textureName = "dirt",
         },
         new CubeBlockType()
         {
             blockName = "grass",
             id = 4,
             solid = true,
             canBeWalkedOn = true,
             canBeWalkedThrough = true,
             textureName = "grass",
         },
         new CubeBlockType()
         {
             blockName = "sand",
             id = 5,
             solid = true,
             canBeWalkedOn = true,
             canBeWalkedThrough = true,
             textureName = "sand",
         }
     });
 }
    public virtual void LoadTextures(Voxelmetric vm)
    {
        var resourceTextures = Resources.LoadAll <Texture2D>(pathToTextureResources);
        var atlas            = new ImmutableTextureAtlas(resourceTextures, 8192);

        for (int i = 0; i < resourceTextures.Length; i++)
        {
            var textureName = resourceTextures[i].name;
            var nameLength  = textureName.Length;
            if (!string.IsNullOrEmpty(textureName))
            {
                var rootTextureName = textureName;
                var openIndex       = textureName.IndexOf('[');
                if (openIndex > 0 && openIndex < textureName.Length - 2 && textureName.IndexOf(']') == textureName.Length - 1)
                {
                    // TODO: Direction-based texture naming, like this:
                    // var dir = textureName.Substring(openIndex + 1, textureName.Length - (openIndex + 2));
                    rootTextureName = textureName.Substring(0, openIndex);
                }
                TextureSet tex;
                if (textureSets.ContainsKey(rootTextureName))
                {
                    tex = textureSets[rootTextureName];
                    tex.AddTexture(atlas.GetTextureRect(rootTextureName));;
                }
                else
                {
                    tex = new TextureSet(rootTextureName);
                    tex.AddTexture(atlas.GetTextureRect(rootTextureName));
                    AddTexture(tex);
                }
            }
        }


        mainAtlas            = atlas;
        material.mainTexture = (Texture2D)atlas;
        #if UNITY_EDITOR
        Debug.Log(string.Format("Voxelmetric Texture Set Loader loaded {0} textures into {1} texture sets.", resourceTextures.Length, textureSets.Keys.Count));
        #endif
    }
示例#9
0
    public virtual void LoadTextures(Voxelmetric vm)
    {
        var       resourceTextures = Resources.LoadAll <Texture2D>(pathToTextureResources);
        Texture2D packedTextures   = new Texture2D(64, 64)
        {
            filterMode = FilterMode.Point
        };

        Rect[] rects = packedTextures.PackTextures(resourceTextures, 0, 8192, false);

        for (int i = 0; i < resourceTextures.Length; i++)
        {
            TextureSet tex = new TextureSet(resourceTextures[i].name);
            tex.AddTexture(rects[i]);
            AddTexture(tex);
        }

        tilesheet            = packedTextures;
        material.mainTexture = packedTextures;

        Debug.Log(string.Format("Voxelmetric Texture Set Loader loaded {0} textures into {1} texture sets.", rects.Length, textureSets.Keys.Count));
    }
示例#10
0
 public override BlockType[] GetBlockTypes(Voxelmetric vm)
 {
     return(new BlockType[] {
         new Cube2dBlockType {
             blockName = "rock",
             id = 2,
             solid = true,
             canBeWalkedOn = true,
             canBeWalkedThrough = false,
             textureName = "rock",
         },
         new Cube2dBlockType {
             blockName = "dirt",
             id = 3,
             solid = true,
             canBeWalkedOn = true,
             canBeWalkedThrough = false,
             textureName = "dirt",
         },
         new FoliageCube2dBlockType {
             blockName = "grass",
             id = 4,
             solid = true,
             canBeWalkedOn = true,
             canBeWalkedThrough = false,
             textureName = "dirt",
             foliageTextureName = "grass"
         },
         new Cube2dBlockType {
             blockName = "sand",
             id = 5,
             solid = true,
             canBeWalkedOn = true,
             canBeWalkedThrough = true,
             textureName = "sand",
         }
     });
 }
示例#11
0
 public void SaveAll()
 {
     saveProgress = Voxelmetric.SaveAll();
 }
示例#12
0
        void Update()
        {
            if (saveProgress != null && saveProgress.GetProgress() >= 100)
            {
                saveProgress = null;
            }

            // Roatation
            if (Input.GetMouseButton(1))
            {
                rot = new Vector2(
                    rot.x + Input.GetAxis("Mouse X") * 3,
                    rot.y + Input.GetAxis("Mouse Y") * 3
                    );

                cam.transform.localRotation  = Quaternion.AngleAxis(rot.x, Vector3.up);
                cam.transform.localRotation *= Quaternion.AngleAxis(rot.y, Vector3.left);
            }



            // Movement
            float speedModificator = 1f;

            if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
            {
                speedModificator = 2f;
            }
            else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
            {
                speedModificator = 0.25f;
            }

            cam.transform.position += cam.transform.forward * 40f * speedModificator * Input.GetAxis("Vertical") * Time.deltaTime;
            cam.transform.position += cam.transform.right * 40f * speedModificator * Input.GetAxis("Horizontal") * Time.deltaTime;

            // Screenspace mouse cursor coordinates
            Vector3 mousePos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));

            if (world != null)
            {
                Block block = world.blockProvider.GetBlock(blockToPlace);

                VmRaycastHit hit = VmRaycast.Raycast(new Ray(cam.transform.position, mousePos - cam.transform.position), world, 100, block.type == BlockProvider.AIR_TYPE);

                // Display the type of the selected block
                if (selectedBlockText != null)
                {
                    selectedBlockText.text = Voxelmetric.GetBlock(world, ref hit.vector3Int).DisplayName;
                }

                // Save current world status
                if (saveProgressText != null)
                {
                    saveProgressText.text = saveProgress != null?SaveStatus() : "Save";
                }

                if (eventSystem != null && !eventSystem.IsPointerOverGameObject())
                {
                    if (hit.block.type != BlockProvider.AIR_TYPE)
                    {
                        bool       adjacent = block.type != BlockProvider.AIR_TYPE;
                        Vector3Int blockPos = adjacent ? hit.adjacentPos : hit.vector3Int;
                        Debug.DrawLine(cam.transform.position, blockPos, Color.red);
                    }

                    // Clicking voxel blocks
                    if (Input.GetMouseButtonDown(0))
                    {
                        if (hit.block.type != BlockProvider.AIR_TYPE)
                        {
                            bool       adjacent = block.type != BlockProvider.AIR_TYPE;
                            Vector3Int blockPos = adjacent ? hit.adjacentPos : hit.vector3Int;
                            Voxelmetric.SetBlockData(world, ref blockPos, new BlockData(block.type, block.solid));
                        }
                    }

                    // Pathfinding
                    if (Input.GetKeyDown(KeyCode.I))
                    {
                        if (hit.block.type != BlockProvider.AIR_TYPE)
                        {
                            bool adjacent = block.type != BlockProvider.AIR_TYPE;
                            pfStart = adjacent ? hit.adjacentPos : hit.vector3Int;
                        }
                    }

                    if (Input.GetKeyDown(KeyCode.O))
                    {
                        if (hit.block.type != BlockProvider.AIR_TYPE)
                        {
                            bool adjacent = block.type != BlockProvider.AIR_TYPE;
                            pfStop = adjacent ? hit.adjacentPos : hit.vector3Int;
                        }
                    }

                    if (Input.GetKeyDown(KeyCode.P))
                    {
                        pf = new PathFinder(pfStart, pfStop, world, 0);
                    }

                    if (pf != null && pf.path.Count != 0)
                    {
                        for (int i = 0; i < pf.path.Count - 1; i++)
                        {
                            Vector3 p0 = (Vector3)pf.path[i] + Env.halfBlockOffset;
                            Vector3 p1 = (Vector3)pf.path[i + 1] + Env.halfBlockOffset;
                            Debug.DrawLine(p0, p1, Color.red);
                        }
                    }
                }

                // Test of ranged block setting
                if (Input.GetKeyDown(KeyCode.T))
                {
                    Action <ModifyBlockContext> action = context => { Debug.Log("Action performed"); };

                    Vector3Int fromPos = new Vector3Int(-44, -44, -44);
                    Vector3Int toPos   = new Vector3Int(44, 44, 44);
                    Voxelmetric.SetBlockData(world, fromPos, toPos, BlockProvider.airBlock, action);
                }
            }
        }
示例#13
0
 public abstract BlockType[] GetBlockTypes(Voxelmetric vm);
示例#14
0
 public override void Initialize(Voxelmetric vm)
 {
     base.Initialize(vm);
     textureSet = vm.components.textureLoader.GetByName(textureName);
 }
示例#15
0
 public abstract TerrainLayer[] GetLayers(Voxelmetric vm);
示例#16
0
 public void SetVm(Voxelmetric vm)
 {
     this.vm = vm;
 }
示例#17
0
    void FixedUpdate()
    {
        //Scroll to change block
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            blockToPlaceIndex += 1;
        }
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            blockToPlaceIndex -= 1;
        }
        //Wrap around
        if (blockToPlaceIndex < 0)
        {
            blockToPlaceIndex = blockToPlace.Length - 1;
        }
        if (blockToPlaceIndex > blockToPlace.Length)
        {
            blockToPlaceIndex = 0;
        }

        //Keypress to change block
        if (Input.GetKey("1"))
        {
            blockToPlaceIndex = 0;
        }
        else if (Input.GetKey("2"))
        {
            blockToPlaceIndex = 1;
        }
        else if (Input.GetKey("3"))
        {
            blockToPlaceIndex = 2;
        }
        else if (Input.GetKey("4"))
        {
            blockToPlaceIndex = 3;
        }
        else if (Input.GetKey("5"))
        {
            blockToPlaceIndex = 4;
        }

        if (Input.GetButtonDown("PrimaryFire"))
        {
            RaycastHit hit;
            if (Physics.Raycast(transform.position, transform.forward, out hit, range))
            {
                Voxelmetric.SetBlock(hit, "air", false);
            }
        }

        if (Input.GetButtonDown("SecondaryFire"))
        {
            RaycastHit hit;
            if (Physics.Raycast(transform.position, transform.forward, out hit, range))
            {
                Voxelmetric.SetBlock(hit, blockToPlace[blockToPlaceIndex], true);
            }
        }
    }
示例#18
0
 public virtual void Initialize(Voxelmetric vm)
 {
     noise   = new Noise();
     this.vm = vm;
 }
示例#19
0
 public override void Initialize(Voxelmetric vm)
 {
     base.Initialize(vm);
 }
示例#20
0
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            rot = new Vector2(
                rot.x + Input.GetAxis("Mouse X") * 3,
                rot.y + Input.GetAxis("Mouse Y") * 3);

            transform.localRotation  = Quaternion.AngleAxis(rot.x, Vector3.up);
            transform.localRotation *= Quaternion.AngleAxis(rot.y, Vector3.left);
        }
        transform.position += transform.forward * 50 * Input.GetAxis("Vertical") * Time.deltaTime;
        transform.position += transform.right * 50 * Input.GetAxis("Horizontal") * Time.deltaTime;

        RaycastHit hit;
        var        mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));

        if (Input.GetMouseButtonDown(0))
        {
            bool adjacent = true;
            if (((Block)blockToPlace).type == Block.Air.type)
            {
                adjacent = false;
            }

            if (Physics.Raycast(Camera.main.transform.position, mousePos - Camera.main.transform.position, out hit, 100))
            {
                Voxelmetric.SetBlock(hit, blockToPlace, adjacent);
            }
        }

        if (Physics.Raycast(Camera.main.transform.position, mousePos - Camera.main.transform.position, out hit, 100))
        {
            selectedBlockText.text = Voxelmetric.GetBlock(hit).ToString();
        }

        if (saveProgress != null)
        {
            saveProgressText.text = SaveStatus();
        }
        else
        {
            saveProgressText.text = "Save";
        }

        if (Input.GetKeyDown(KeyCode.I))
        {
            if (Physics.Raycast(Camera.main.transform.position, mousePos - Camera.main.transform.position, out hit, 100))
            {
                pfStart = Voxelmetric.GetBlockPos(hit);
            }
        }

        if (Input.GetKeyDown(KeyCode.O))
        {
            if (Physics.Raycast(Camera.main.transform.position, mousePos - Camera.main.transform.position, out hit, 100))
            {
                pfStop = Voxelmetric.GetBlockPos(hit);
            }
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            pf = new PathFinder(pfStart, pfStop, World.instance, 2);
            Debug.Log(pf.path.Count);
        }

        if (pf != null && pf.path.Count != 0)
        {
            for (int i = 0; i < pf.path.Count - 1; i++)
            {
                Debug.DrawLine(pf.path[i].Add(0, 1, 0), pf.path[i + 1].Add(0, 1, 0));
            }
        }
    }
示例#21
0
 public virtual void Initialize(Voxelmetric vm)
 {
     this.vm = vm;
 }
示例#22
0
 /// <summary>
 /// Used to access the block's attributes.
 /// </summary>
 public BlockType GetBlockType(Voxelmetric vm)
 {
     return(vm.components.blockLoader.BlockAttr(id));
 }