Пример #1
0
    public bool CanSpawnBox(Box.BoxType type)
    {
        var neededFreeBoxes = type.GetNeededSpace();

        for (int y = 0; y < Height; y++)
        {
            var continousFreeBoxes = 0;
            for (int x = 0; x < Width; x++)
            {
                var free = IsSlotFree(x, y);
                if (!free && continousFreeBoxes >= neededFreeBoxes)
                {
                    return(true);
                }
                else if (free)
                {
                    continousFreeBoxes++;
                }
                else
                {
                    continousFreeBoxes = 0;
                }
            }
            if (continousFreeBoxes >= neededFreeBoxes)
            {
                return(true);
            }
        }
        return(false);
    }
Пример #2
0
    public Vector2Int GetFreeSlot(Box.BoxType type)
    {
        // TODO: maybe select a random free slot?
        var slotX = 0;
        var slotY = 0;

        var neededFreeBoxes = type.GetNeededSpace();


        var foundFree = false;

        for (int y = 0; y < Height; y++)
        {
            var continousFreeBoxes = 0;
            var firstSlotX         = 0;
            for (int x = 0; x < Width; x++)
            {
                var free = IsSlotFree(x, y);
                if (!free && continousFreeBoxes >= neededFreeBoxes)
                {
                    foundFree = true;
                    slotX     = firstSlotX;
                    slotY     = y;
                    break;
                }
                else if (free)
                {
                    continousFreeBoxes++;
                }
                else
                {
                    continousFreeBoxes = 0;
                    firstSlotX         = x + 1;             // The next one might be the first free one
                }
            }
            if (foundFree)
            {
                break;
            }
            if (continousFreeBoxes >= neededFreeBoxes)
            {
                slotX = firstSlotX;
                slotY = y;
                break;
            }
        }

        return(new Vector2Int(slotX, slotY));
    }
Пример #3
0
 public void SpawnBox(Box.BoxType type, int referenceCount)
 {
     // TODO: maybe make this random?
     foreach (var shelf in shelves)
     {
         if (shelf.GetFreeSpace() >= type.GetNeededSpace())
         {
             if (shelf.CanSpawnBox(type))
             {
                 shelf.GetComponent <ShelfBoxSpawner>().SpawnBox(type, shelf.GetFreeSlot(type), referenceCount);
                 return;
             }
         }
     }
     Debug.LogError("CANNOT PLACE BOX " + type);
     CannotPlaceBoxEvent.Raise();
     // TriggerGameOver();
 }