Ejemplo n.º 1
0
    public void Generate()
    {
        gameObject.GetChildren().ForEach(child => Destroy(child));
        // Generate ground
        var ground = Instantiate(GroundPrefab, Vector3.zero, Quaternion.identity);

        //ground.transform.localScale = new Vector3(Width / 10, 1, Height / 10);
        //this.NextFrame(() => ground.transform.localScale = new Vector3(Width / 10, 1, Height / 10));
        SceneManager.MoveGameObjectToScene(ground, gameObject.scene);
        ground.transform.parent = transform;
        //ground.GetComponent<MeshCollider>().

        // Generate doors
        for (var i = 0; i < 4; i++)
        {
            Doors[i] = false;
            if (UnityEngine.Random.value < DoorProbability)
            {
                Doors[i] = true;
            }
        }
        if (InDoor != Orientation.None)
        {
            Doors[(int)InDoor] = true;
        }

        var wallNorth = GenerateWall(NorthDoor, Width);

        wallNorth.transform.parent   = gameObject.transform;
        wallNorth.transform.position = new Vector3(0, 0, Height / 2);
        wallNorth.transform.rotation = Quaternion.FromToRotation(Vector3.right, Vector3.forward);

        /*var wallSouth = GenerateWall(SouthDoor, Width);
         * wallSouth.transform.parent = gameObject.transform;
         * wallSouth.transform.position = new Vector3(0, 0, -Height / 2);
         * wallSouth.transform.rotation = Quaternion.FromToRotation(Vector3.right, Vector3.back);*/

        var wallEast = GenerateWall(EastDoor, Height);

        wallEast.transform.parent   = gameObject.transform;
        wallEast.transform.position = new Vector3(Width / 2, 0, 0);
        wallEast.transform.rotation = Quaternion.FromToRotation(Vector3.right, Vector3.right);

        var wallWest = GenerateWall(WestDoor, Height);

        wallWest.transform.parent   = gameObject.transform;
        wallWest.transform.position = new Vector3(-Width / 2, 0, 0);
        wallWest.transform.rotation = Quaternion.FromToRotation(Vector3.right, Vector3.left);

        // Generate map nodes
        mapSizeX   = Mathf.CeilToInt((Width / 2 - NodeSize / 2) / NodeSize) * 2 + 1;
        mapOffsetX = mapSizeX / 2;
        mapSizeY   = Mathf.CeilToInt((Height / 2 - NodeSize / 2) / NodeSize) * 2 + 1;
        mapOffsetY = mapSizeY / 2;
        map        = new MapNode[mapSizeX, mapSizeY];
        RangeX     = new RangeInt(
            Mathf.FloorToInt((-Width / 2 + NodeSize / 2) / NodeSize),
            Mathf.FloorToInt((Width / 2 + NodeSize / 2) / NodeSize)
            );
        RangeY = new RangeInt(
            Mathf.FloorToInt((-Height / 2 + NodeSize / 2) / NodeSize),
            Mathf.FloorToInt((Height / 2 + NodeSize / 2) / NodeSize)
            );
        // Init map nodes
        for (var y = 0; y < mapSizeY; y++)
        {
            for (var x = 0; x < mapSizeX; x++)
            {
                map[x, y] = new MapNode()
                {
                    Type = MapNodeType.Empty
                }
            }
        }
        ;
        ForEach((node, x, y) =>
        {
            if (y == RangeY.start || y == RangeY.end || x == RangeX.start || x == RangeX.end)
            {
                node.Type = MapNodeType.Wall;
            }
            node.Center = new Vector3(x * NodeSize, 0, y * NodeSize);
        });

        // Generate obstacles
        var obstacleCount = Mathf.FloorToInt(GenerateObstacle * Mathf.Sqrt(UnityEngine.Random.value));

        GetNodes()
        .Where(node => node.Type == MapNodeType.Empty)
        .RandomTake(obstacleCount)
        .ForEach(node =>
        {
            var obstacle = Utility.Instantiate(
                Obstacles
                .WeightedRandomTake(1)
                .Select(item => item.Object as GameObject)
                .FirstOrDefault(),
                gameObject);

            obstacle.transform.position = node.Center;
            node.Type = MapNodeType.Obstacle;
        });

        Generated = true;
    }