示例#1
0
    private void BuildBlueprint(Blueprint blueprint)
    {
        int           yRotation = GameController.Random.Next(0, 4) * 2;
        QuaternionInt rotation  = new QuaternionInt(0, yRotation, 0);
        Coord         size      = blueprint.RandomSize();

        Coord offset = Blueprint.RotationOffset(yRotation);

        bool validPosition = false;
        int  tries         = 0;

        Coord bottomLeft = new Coord();

        while (!validPosition && tries < 1000)
        {
            int bottomLeftX = GameController.Random.Next(0, location.size - (rotation * size).x.Abs());
            int bottomLeftZ = GameController.Random.Next(0, location.size - (rotation * size).z.Abs());
            int floorHeight = location.heightMap[bottomLeftX, bottomLeftZ];
            bottomLeft = new Coord(bottomLeftX, floorHeight, bottomLeftZ);

            validPosition = true;

            for (int x = -1; x <= size.x; x++)
            {
                for (int y = 0; y < size.y; y++)
                {
                    for (int z = -1; z <= size.z; z++)
                    {
                        Coord tileCoord = new Coord(x, y, z);
                        Coord tilePos   = bottomLeft + rotation * tileCoord + offset;

                        if (!(location.GetTileFree(tilePos) && location.GetHeight(tilePos) == floorHeight))
                        {
                            validPosition = false;
                        }
                        if (!validPosition)
                        {
                            break;
                        }
                    }
                    if (!validPosition)
                    {
                        break;
                    }
                }
                if (!validPosition)
                {
                    break;
                }
            }

            tries++;
        }

        if (validPosition)
        {
            blueprint.GenerateBuilding(location, bottomLeft, size, rotation);
        }
        else
        {
            Debug.Log($"Could not find valid position for {blueprint}");
        }
    }