Ejemplo n.º 1
0
    private void Generate()
    {
        // Setup the map of the environment tiles according to the specified width and height
        // Generate tiles from the list of accessible and inaccessible prefabs using a random
        // and the specified accessible percentage
        mMap       = new EnvironmentTile[Size.x][];
        properties = new Properties[Size.x][];

        int     halfWidth  = Size.x / 2;
        int     halfHeight = Size.y / 2;
        Vector3 position   = new Vector3(-(halfWidth * TileSize), 0.0f, -(halfHeight * TileSize));
        bool    start      = false;

        // Initial map value setup
        for (int x = 0; x < Size.x; ++x)
        {
            mMap[x]       = new EnvironmentTile[Size.y];
            properties[x] = new Properties[Size.y];
            for (int y = 0; y < Size.y; ++y)
            {
                if (x == Size.x / 2 && y == Size.y / 2)
                {
                    start = true;
                }
                bool isAccessible            = start || Random.value < AccessiblePercentage;
                List <EnvironmentTile> tiles = isAccessible ? AccessibleTiles : InaccessibleTiles;
                if (tiles == AccessibleTiles)
                {
                    properties[x][y].listType = 0;
                }
                else
                {
                    properties[x][y].listType = 1;
                }

                properties[x][y].tileType = Random.Range(0, tiles.Count);

                if (properties[x][y].listType == 1 && properties[x][y].tileType > 20)
                { // Is it unbreakabable?
                    properties[x][y].tileType -= 21;
                    properties[x][y].listType  = 2;
                    tiles = UnbreakableTiles;
                }
                else if (properties[x][y].listType == 1 && properties[x][y].tileType == 20)
                { // Grass tile should just be picked up, not broken down
                    properties[x][y].listType = 3;
                }

                EnvironmentTile prefab = tiles[properties[x][y].tileType];
                EnvironmentTile tile   = Instantiate(prefab, position, Quaternion.identity, transform);            // Instantiate it
                // Give it all its properties
                tile.Position = new Vector3(position.x + (TileSize / 2), TileHeight, position.z + (TileSize / 2)); // Give it a position
                if (properties[x][y].listType == 0)
                {
                    tile.IsAccessible = true; // Make the tile something you can move on
                    if (properties[x][y].tileType > 0)
                    {
                        Transform[] grass = tile.GetComponentsInChildren <Transform>();
                        for (int i = 1; i < grass.Length; ++i)
                        {
                            grass[i].Rotate(0.0f, Random.Range(0.0f, 360.0f), 0.0f);
                        }
                    }
                }
                else
                {
                    tile.IsAccessible = false; // Or not
                    if (properties[x][y].listType == 1 || properties[x][y].listType == 2)
                    {
                        tile.child.Rotate(0.0f, Random.Range(0.0f, 360.0f), 0.0f, Space.World);
                    }
                }

                tile.gameObject.name = string.Format("Tile({0},{1})", x, y); // Name it

                properties[x][y].pos.x = tile.Position.x;
                properties[x][y].pos.y = tile.Position.z;

                tile.xID = x;
                tile.yID = y;

                mMap[x][y] = tile;
                mAll.Add(tile);

                if (start)
                {
                    playerTile          = tile.GetComponent <EnvironmentTile>();
                    Start               = tile;
                    mMap[x][y].occupant = GameObject.FindGameObjectWithTag("Player");
                }

                position.z += TileSize;
                start       = false;
            }
            position.x += TileSize;
            position.z  = -(halfHeight * TileSize);
        }

        GetComponentInParent <Game>().GetComponentInChildren <DoomsdayTimer>().enabled = true;

        //-----------------------------------------------------------------
        // NPC setup
        //-------
        for (int x = 0; x < Size.x; x++)
        { // Prepare for chickens
            for (int y = 0; y < Size.y; y++)
            {
                if (Random.value > 0.999f)
                {                       // It has to be really low because otherwise it gets super laggy
                    GameObject chicken; // This is all just the general creation process
                    chicken = Instantiate(NPC[0], GetComponentInParent <Game>().transform);
                    chicken.transform.position = new Vector3(mMap[x][y].Position.x, 2.0f, mMap[x][y].Position.z);
                    chicken.GetComponent <CharacterManager>().currentTile = mMap[x][y];
                    chicken.GetComponent <Character>().CurrentPosition    = mMap[x][y];
                    mMap[x][y].occupant = chicken;
                    // Give it a tile too, so I can keep track of it for later
                    chicken.GetComponent <CharacterManager>().currentTile.xID = x;
                    chicken.GetComponent <CharacterManager>().currentTile.yID = y;
                }
            }
        }
    }