GridSystem.cs

mail@pastecode.io avatar
unknown
csharp
2 months ago
5.1 kB
9
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class GridSystem<T> : Singleton<GridSystem<T>>
{
    private Vector2Int dimensions = new Vector2Int(1,1);

    private T[,] data;

    public Vector2Int Dimensions 
    {
        get 
        {
            return dimensions;
        }
    }
    

    private bool isReady;
    public bool IsReady 
    {   
        get 
        {
            return isReady;
        }
    }

    // Initialize data array 
    public void InitializeGrid(Vector2Int dimensions) 
    {
        if (dimensions.x < 1 || dimensions.y < 1)
            Debug.LogError("Dimensions must be (each) bigger than 0.");

        this.dimensions = dimensions;
        data = new T[dimensions.x, dimensions.y];
        isReady= true;

    }

    // Clear the entire grid 
    public void Clear() 
    {
        data = new T[dimensions.x, dimensions.y];
    }

    // bounds check 
    public bool CheckBounds(int x, int y) 
    {
        if (!isReady)
            Debug.LogError("Grid is not initialized yet, it is not ready.");
        return x>=0 && x < dimensions.x && y >= 0 &&  y < dimensions.y;

    }
    public bool CheckBounds(Vector2Int position)
    {
        return CheckBounds(position.x, position.y);
    }

    // check if (x,y) on grid is empty or  not
    public bool IsEmpty(int x, int y)
    {
        if (!CheckBounds(x,y))
            Debug.LogError("(" + x + ", " + y + ") are not on the grid." );

        // return data[x,y]==null;
        return EqualityComparer<T>.Default.Equals(data[x,y], default(T));
    
    }
    public bool IsEmpty(Vector2Int position)
    {
        return IsEmpty(position.x, position.y);
    }

    // Put an item on grid

    public bool PutItemAt(T item, int x, int y, bool allowOverwrite = false) {
        if (!CheckBounds(x,y))
            Debug.LogError("(" + x + ", " + y + ") are not on the grid." );

        if (!allowOverwrite && !IsEmpty(x,y))
        {  
            return false;
        }

        data[x,y] = item;
        return true; // indicate that this process is completed. 


    }
    public bool PutItemAt(T item, Vector2Int position, bool allowOverwrite = false)
    {
        return PutItemAt(item, position.x, position.y, allowOverwrite);
    }

    // get an item

    public T GetItemAt(int x, int y)
    {
        if (!CheckBounds(x,y))
            Debug.LogError("(" + x + ", " + y + ") are not on the grid." );

        return data[x,y];
    }

    public T GetItemAt(Vector2Int position)
    {
        return GetItemAt(position.x, position.y);
    }

    // remove an item

    public T RemoveItemAt(int x, int y)
    {
        if (!CheckBounds(x,y))
            Debug.LogError("(" + x + ", " + y + ") are not on the grid." );

        T returner = data[x,y];
        data[x,y] = default(T);
        return returner;
    }

    public T RemoveItemAt(Vector2Int position)
    {
        return RemoveItemAt(position.x, position.y);
    }


    public bool MoveItemTo(int x1, int y1, int x2, int y2, bool allowOverwrite = false) {
        if (!CheckBounds(x1,y1))
            Debug.LogError("(" + x1 + ", " + y1 + ") are not on the grid." );

        if (!CheckBounds(x2,y2))
            Debug.LogError("(" + x2 + ", " + y2 + ") are not on the grid." );

        if (!allowOverwrite && !IsEmpty(x2,y2))
        {  
            return false;
        }


        data[x2,y2] = RemoveItemAt(x1,y1);
        return true; // indicate that this process is completed. 
    }
    public bool MoveItemTo(Vector2Int position1, Vector2Int position2, bool allowOverwrite = false)
    {
        return MoveItemTo(position1.x, position1.y, position2.x,position2.y, allowOverwrite);
    }


    // swap 2 items on grid 
    public void SwapItemsAt(int x1, int y1, int x2, int y2)
    {
        if (!CheckBounds(x1,y2))
            Debug.LogError("(" + x1 + ", " + y1 + ") are not on the grid." );
        if (!CheckBounds(x2,y2))
            Debug.LogError("(" + x2 + ", " + y2 + ") are not on the grid." );

        T temp = data[x1,y1];
        data[x1,y1] = data[x2,y2];
        data[x2,y2] = temp;
    }

    public void SwapItemsAt(Vector2Int position1, Vector2Int position2)
    {
        SwapItemsAt(position1.x, position1.y,position2.x,position2.y);
    }
    // Clear the entire grid 




    // Convert grid data to string 
    public override string ToString()
    {
        string s = "";

        for(int y = dimensions.y - 1; y != -1; --y)
        {
            s += "[ ";

            for(int x = 0; x != dimensions.x; ++x)
            {
                if(IsEmpty(x, y))
                    s += "x";
                else
                    s += data[x, y].ToString();

                if(x != dimensions.x - 1)
                    s += ", ";
            }
            s += " ]\n";
        }
        return s;
    }






}
Leave a Comment