コード例 #1
0
    /**
     * Get the id based on its position
     * @return int id
     */
    public virtual int GetID()
    {
        Vector3 position = this.tile.GetPosition();
        int     x        = Mathf.RoundToInt(position.x);
        int     z        = Mathf.RoundToInt(position.z);

        Tile topLeftTile     = MapGeneratorBehaviour.IsTileExists(x - 1, z + 1) ? MapGeneratorBehaviour.Tiles[x - 1][z + 1].GetComponent <TileBehaviour>().GetTile() : null;
        Tile topTile         = MapGeneratorBehaviour.IsTileExists(x, z + 1) ? MapGeneratorBehaviour.Tiles[x][z + 1].GetComponent <TileBehaviour>().GetTile() : null;
        Tile topRightTile    = MapGeneratorBehaviour.IsTileExists(x + 1, z + 1) ? MapGeneratorBehaviour.Tiles[x + 1][z + 1].GetComponent <TileBehaviour>().GetTile() : null;
        Tile leftTile        = MapGeneratorBehaviour.IsTileExists(x - 1, z) ? MapGeneratorBehaviour.Tiles[x - 1][z].GetComponent <TileBehaviour>().GetTile() : null;
        Tile rightTile       = MapGeneratorBehaviour.IsTileExists(x + 1, z) ? MapGeneratorBehaviour.Tiles[x + 1][z].GetComponent <TileBehaviour>().GetTile() : null;
        Tile bottomLeftTile  = MapGeneratorBehaviour.IsTileExists(x - 1, z - 1) ? MapGeneratorBehaviour.Tiles[x - 1][z - 1].GetComponent <TileBehaviour>().GetTile() : null;
        Tile bottomTile      = MapGeneratorBehaviour.IsTileExists(x, z - 1) ? MapGeneratorBehaviour.Tiles[x][z - 1].GetComponent <TileBehaviour>().GetTile() : null;
        Tile bottomRightTile = MapGeneratorBehaviour.IsTileExists(x + 1, z - 1) ? MapGeneratorBehaviour.Tiles[x + 1][z - 1].GetComponent <TileBehaviour>().GetTile() : null;

        // 8-bit binary
        Char[] binary = new char[8];

        // check top left
        if (topLeftTile != null && topLeftTile.GetType() == TileType.Water)
        {
            binary[7] = '1';
        }
        else
        {
            binary[7] = '0';
        }

        // check top tile
        if (topTile != null && topTile.GetType() == TileType.Water)
        {
            binary[6] = '1';
        }
        else
        {
            binary[6] = '0';
        }

        // check top tile
        if (topRightTile != null && topRightTile.GetType() == TileType.Water)
        {
            binary[5] = '1';
        }
        else
        {
            binary[5] = '0';
        }

        // check left tile
        if (leftTile != null && leftTile.GetType() == TileType.Water)
        {
            binary[4] = '1';
        }
        else
        {
            binary[4] = '0';
        }

        // check right tile
        if (rightTile != null && rightTile.GetType() == TileType.Water)
        {
            binary[3] = '1';
        }
        else
        {
            binary[3] = '0';
        }

        // check bottom left tile
        if (bottomLeftTile != null && bottomLeftTile.GetType() == TileType.Water)
        {
            binary[2] = '1';
        }
        else
        {
            binary[2] = '0';
        }

        // check bottom left tile
        if (bottomTile != null && bottomTile.GetType() == TileType.Water)
        {
            binary[1] = '1';
        }
        else
        {
            binary[1] = '0';
        }

        // check bottom right tile
        if (bottomRightTile != null && bottomRightTile.GetType() == TileType.Water)
        {
            binary[0] = '1';
        }
        else
        {
            binary[0] = '0';
        }

        // Convert binary to int
        int value = (int)TileID.Center;

        int start = 0;

        for (int i = binary.Length - 1; i > -1; i--)
        {
            if (binary[i].Equals('1'))
            {
                value = value + (int)Math.Pow(2, start);
            }

            start = start + 1;
        }

        Debug.Log(value);


        return(value);
    }
    /**
     * Sand out sharp edges
     * @return void
     */
    public IEnumerator SandGrassEdges()
    {
        while (this.generated && !this.finished)
        {
            GameObject tiles = GameObject.Find("Tiles");

            foreach (GameObject grassTile in this.grassTiles)
            {
                GrassTileBehaviour grassTileBehaviour = grassTile.GetComponent <GrassTileBehaviour>();
                Tile tile = grassTileBehaviour.GetTile();

                int positionX = Mathf.RoundToInt(tile.GetPosition().x);
                int positionZ = Mathf.RoundToInt(tile.GetPosition().z);

                GameObject topTileGameObject = IsTileExists(tile.GetPosition().x, tile.GetPosition().z + 1) ? Tiles[positionX][positionZ + 1] : null;
                Tile       topTile           = (topTileGameObject != null) ? topTileGameObject.GetComponent <TileBehaviour>().GetTile() : null;

                GameObject leftTileGameObject = IsTileExists(tile.GetPosition().x - 1, tile.GetPosition().z) ? Tiles[positionX - 1][positionZ] : null;
                Tile       leftTile           = (leftTileGameObject != null) ? leftTileGameObject.GetComponent <TileBehaviour>().GetTile() : null;

                GameObject bottomTileGameObject = IsTileExists(tile.GetPosition().x, tile.GetPosition().z - 1) ? Tiles[positionX][positionZ - 1] : null;
                Tile       bottomTile           = (bottomTileGameObject != null) ? bottomTileGameObject.GetComponent <TileBehaviour>().GetTile() : null;

                GameObject rightTileGameObject = IsTileExists(tile.GetPosition().x + 1, tile.GetPosition().z) ? Tiles[positionX + 1][positionZ] : null;
                Tile       rightTile           = (rightTileGameObject != null) ? rightTileGameObject.GetComponent <TileBehaviour>().GetTile() : null;

                GameObject edgeTileGameObject = null;
                Tile       cornerTile;

                Vector3 tilePosition = tile.GetPosition();

                // left and top
                if (
                    leftTile != null &&
                    topTile != null &&
                    rightTile != null &&
                    bottomTile != null &&
                    leftTile.GetType() == TileType.Water &&
                    topTile.GetType() == TileType.Water &&
                    rightTile.GetType() == TileType.Grass &&
                    bottomTile.GetType() == TileType.Grass
                    )
                {
                    edgeTileGameObject = MonoBehaviour.Instantiate(
                        Resources.Load("Prefabs/Development/Tiles/Grass/Top Left Grass Tile", typeof(GameObject))
                        ) as GameObject;
                }
                // right and top
                else if (
                    rightTile != null &&
                    topTile != null &&
                    leftTile != null &&
                    topTile != null &&
                    rightTile.GetType() == TileType.Water &&
                    topTile.GetType() == TileType.Water &&
                    leftTile.GetType() == TileType.Grass &&
                    topTile.GetType() == TileType.Grass
                    )
                {
                    edgeTileGameObject = MonoBehaviour.Instantiate(
                        Resources.Load("Prefabs/Development/Tiles/Grass/Top Right Grass Tile", typeof(GameObject))
                        ) as GameObject;
                }
                // left and bottom
                else if (
                    leftTile != null &&
                    bottomTile != null &&
                    rightTile != null &&
                    topTile != null &&
                    leftTile.GetType() == TileType.Water &&
                    bottomTile.GetType() == TileType.Water &&
                    rightTile.GetType() == TileType.Grass &&
                    topTile.GetType() == TileType.Grass
                    )
                {
                    edgeTileGameObject = MonoBehaviour.Instantiate(
                        Resources.Load("Prefabs/Development/Tiles/Grass/Bottom Left Grass Tile", typeof(GameObject))
                        ) as GameObject;
                }
                // right and bottom
                else if (
                    rightTile != null &&
                    bottomTile != null &&
                    leftTile != null &&
                    topTile != null &&
                    rightTile.GetType() == TileType.Water &&
                    bottomTile.GetType() == TileType.Water &&
                    leftTile.GetType() == TileType.Grass &&
                    topTile.GetType() == TileType.Grass
                    )
                {
                    edgeTileGameObject = MonoBehaviour.Instantiate(
                        Resources.Load("Prefabs/Development/Tiles/Grass/Bottom Right Grass Tile", typeof(GameObject))
                        ) as GameObject;
                }

                if (edgeTileGameObject != null)
                {
                    cornerTile = tile;

                    TileBehaviour cornerTileBehaviour = edgeTileGameObject.GetComponent <TileBehaviour>();

                    cornerTile.SetType(TileType.Grass);
                    cornerTileBehaviour.SetTile(cornerTile);

                    edgeTileGameObject.transform.position = tilePosition;
                    edgeTileGameObject.transform.SetParent(tiles.transform);


                    Tiles[Mathf.RoundToInt(tilePosition.x)][Mathf.RoundToInt(tilePosition.z)] = edgeTileGameObject;

                    MonoBehaviour.Destroy(grassTile);
                }
            }

            // Clear out the grass tiles list
            this.grassTiles.Clear();

            // Set finished flag to true
            this.finished = true;

            yield return(null);
        }

        yield return(null);
    }