Beispiel #1
0
    private void Fill(FleetPlacement fleet, ShipsInventory inventory)
    {
        foreach (ShipsInventory.ShipsCell item in inventory.Ships)
        {
            for (int i = 0; i < item.count; i++)
            {
                ShipOrientations direction = Random.Range(0, 2) == 0 ? ShipOrientations.Horizontal : ShipOrientations.Vertical;
                int length = item.ship.Length;

                var lines   = direction == ShipOrientations.Horizontal ? GetFreeHorizontalCells(length) : GetFreeVerticalCells(length);
                int basePos = GetRandomIndexFromLineList(lines);

                int listIndex = Random.Range(0, lines[basePos].lists.Count);
                var list      = lines[basePos].lists[listIndex];

                int secondPos = list[Random.Range(0, (list.Count - length) + 1)];

                FleetPlacement.ShipPlacement data = new FleetPlacement.ShipPlacement();
                data.Orientation = direction;
                data.Position    = direction == ShipOrientations.Horizontal ? new Vector2Int(secondPos, basePos) : new Vector2Int(basePos, secondPos);
                data.ShipData    = item.ship;

                fleet.Placements.Add(data);

                FillShipPosition(direction, length, direction == ShipOrientations.Horizontal ? secondPos : basePos, direction == ShipOrientations.Horizontal ? basePos : secondPos);
            }
        }
    }
Beispiel #2
0
    private void FillShipPosition(ShipOrientations orientation, int length, int x, int y)
    {
        int minLimitX = Mathf.Clamp(x - 1, 0, _width - 1);
        int maxLimitX = orientation == ShipOrientations.Horizontal ? Mathf.Clamp(x + length, 0, _width - 1) : Mathf.Clamp(x + 1, 0, _width - 1);

        int minLimitY = Mathf.Clamp(y - 1, 0, _height - 1);
        int maxLimitY = orientation == ShipOrientations.Horizontal ? Mathf.Clamp(y + 1, 0, _height - 1) : Mathf.Clamp(y + length, 0, _height - 1);

        for (int i = minLimitX; i <= maxLimitX; i++)
        {
            for (int j = minLimitY; j <= maxLimitY; j++)
            {
                _field[i, j] = FillTypes.Ship;
            }
        }
    }