Beispiel #1
0
 /// <summary>
 /// Add an element in the grid, we assume the positions are in the grid
 /// </summary>
 public void Add(int x, int y, IGridElement element)
 {
     if (_grid[x, y] != null)
     {
         throw new ArgumentException($"Can't add an element to grid at ({x};{y}), position is not empty");
     }
     _grid[x, y] = element;
 }
    public void RemoveElement(IGridElement element)
    {
        foreach (var pos in element.gridPositions)
        {
            elements[pos.x, pos.y] = null;
        }

        element.gridPositions = null;
    }
    public bool AddElementPositionOwnership(IGridElement element, int x, int y)
    {
        if (elements[x, y] != null)
        {
            Debug.LogWarning("the position already has an owner");
            return(false);
        }

        elements[x, y] = element;
        element.gridPositions.Add(new Vector2Int(x, y));
        return(true);
    }
    public void AddElement(IGridElement element, int x, int y)
    {
        if (elements[x, y] != null)
        {
            Debug.LogWarning("The position " + x + ", " + y + " is already taken");
            return;
        }

        elements[x, y]        = element;
        element.gridPositions = new List <Vector2Int>();
        element.gridPositions.Add(new Vector2Int(x, y));
    }
    public void ClearAllAdditionalElementPositions(IGridElement element)
    {
        if (element.gridPositions == null)
        {
            return;
        }

        var posCount = element.gridPositions.Count;

        for (int i = 1; i < posCount; i++) //start at one to skip the element position
        {
            var(x, y)      = (element.gridPositions[i].x, element.gridPositions[i].y);
            elements[x, y] = null;
        }

        element.gridPositions.RemoveRange(1, posCount - 1);
    }
    public bool RemoveElementPositionOwnership(IGridElement element, int x, int y)
    {
        if (elements[x, y] != element)
        {
            Debug.LogWarning("the element has not the ownership of this position");
            return(false);
        }

        elements[x, y] = null;
        var index = element.gridPositions.FindIndex(p => p.x == x && p.y == y);

        if (index != -1)
        {
            element.gridPositions.RemoveAt(index);
        }

        return(true);
    }
Beispiel #7
0
 public void AddToGrid(int x, int y, IGridElement element)
 => _grid.Add(x, y, element);