コード例 #1
0
    void RandomlyModifyMap()
    {
        //We'll retrieve the bounds of our map in order to pick a nice random block
        int x = UnityEngine.Random.Range(0, BlockUtilities.GetMapWidth(createdMap));
        int y = UnityEngine.Random.Range(0, BlockUtilities.GetMapHeight(createdMap));

        //The depth is a little different from the width/height, as depth may be negative
        int z = UnityEngine.Random.Range(BlockUtilities.GetMapLowerDepth(createdMap), BlockUtilities.GetMapUpperDepth(createdMap));

        //Here we'll flip a figurative coin to decide if we'll add or remove
        if (UnityEngine.Random.Range(0, 2) == 1)
        {
            //We'll remove a block in this case
            BlockUtilities.RemoveBlockFromMap(createdMap, x, y, z, true, false);
        }
        else
        {
            //Again, we'll create a block
            Block b = GameObject.Instantiate(blockPrefab) as Block;

            //And then add it to the map
            //We'll randomise on addition
            //And pending our CleanMap parameter, we'll refresh the surrounding blocks
            BlockUtilities.AddBlockToMap(createdMap, b, true, 0, true, x, y, z, false, true);
        }
    }
コード例 #2
0
    public void Dig()
    {
        //Set the facing direction

        Direction facing = Direction.None;

        //You'll need to change this if you're using some sort of smoothing
        //this is just to show the dealio
        Vector3 forward = transform.forward;

        if (forward == frontDirection)
        {
            facing = Direction.Down;
        }
        else if (forward == backDirection)
        {
            facing = Direction.Up;
        }
        else if (forward == leftDirection)
        {
            facing = Direction.Left;
        }
        else if (forward == rightDirection)
        {
            facing = Direction.Right;
        }

        int d_x = y, d_y = x;

        switch (facing)
        {
        case Direction.Down: {
            d_x = this.x;
            d_y = this.y + 1;
            break;
        }

        case Direction.Up: {
            d_x = this.x;
            d_y = this.y - 1;
            break;
        }

        case Direction.Right: {
            d_x = this.x + 1;
            d_y = this.y;
            break;
        }

        case Direction.Left: {
            d_x = this.x - 1;
            d_y = this.y;
            break;
        }

        case Direction.None: {
            return;
        }
        }

        //We need to do a few things here
        if (streamingMap != null)
        {
            if (d_x < 0 || d_x >= streamingMap.width ||
                d_y < 0 || d_y >= streamingMap.height)
            {
                return;
            }
        }
        else
        {
            if (d_x < 0 || d_x >= BlockUtilities.GetMapWidth(parentMap) ||
                d_y < 0 || d_y >= BlockUtilities.GetMapHeight(parentMap))
            {
                return;
            }
        }

        //Looks like we can do it!
        //You will probably want to implement a "Diggable" / "Not Diggable" logic here
        //But for now - we dig!

        BlockUtilities.RemoveBlockFromMap(parentMap, d_x, d_y, 0, false, false);

        if (streamingMap != null)
        {
            streamingMap.SetBlockPrefabAt(null, d_x, d_y, 0);
        }
    }