public Grid( int width, int height, float cellSize, Vector3 originPosition, Func <Grid <TGridObject>, int, int, TGridObject> createDefaultGridObject ) { _width = width; _height = height; _cellSize = cellSize; _originPosition = originPosition; _gridArray = new TGridObject[width, height]; _debugTextArray = new TextMesh[width, height]; _mainCamera = Camera.main; Debug.Log($"{nameof(Grid)} - Grid width {_width} and height {_height}"); for (var x = 0; x < _gridArray.GetLength(0); x++) { for (var y = 0; y < _gridArray.GetLength(1); y++) { _gridArray[x, y] = createDefaultGridObject(this, x, y); } } OnGridObjectChanged += (sender, eventArgs) => { _debugTextArray[eventArgs.x, eventArgs.y].text = _gridArray[eventArgs.x, eventArgs.y]?.ToString(); }; }
public Grid(int width, int height, float cellSize, Vector3 originPosition, Func<Grid<TGridObject>, int, int, TGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } bool showDebug = false; if (showDebug) { TextMesh[,] debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { //debugTextArray[x, y] = UtilsClass.CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 30, Color.white, TextAnchor.MiddleCenter); debugTextArray[x, y] = CreateWorldText(null, gridArray[x, y]?.ToString(), GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, fontSize, 0.03f, Color.white, TextAnchor.MiddleCenter, TextAlignment.Center); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); OnGridObjectChanged += (object sender, OnGridObjectChangedEventArgs eventArgs) => { debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString(); }; } }
public Grid(int width, int height, float cellSize, Vector2 origin, Func <Grid <TGridObject>, int, int, TGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.origin = origin; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { //debug //Debug.Log(x + ", " + y); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); //initialize grid objects gridArray[x, y] = createGridObject(this, x, y); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); }
public PlayGrid(int width, int height, float cellSize, Vector3 originPosition, Func <PlayGrid <TGridObject>, int, int, TGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); }
public Grid(int width, int height, float cellSize, Vector3 originPosition) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; if (SHOW_DEBUG) { debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { debugTextArray[x, y] = UtilsClass.CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * 0.5f, 40, Color.white, TextAnchor.MiddleCenter); debugTextArray[x, y].characterSize = 0.07f; debugTextArray[x, y].GetComponent <MeshRenderer>().sortingLayerName = "Text"; Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); } }
public grid(int width, int height, float cellSize, Vector3 originPosition, Func <grid <TGridObject>, int, int, TGridObject> createCells) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; //TextMesh[,] debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createCells(this, x, y); //debugTextArray[x, y] = UtilsClass.CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 10, Color.white, TextAnchor.MiddleCenter); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); OnGridObjectChanged += (object sender, OnGridObjectChangedEventArgs e) => { //debugTextArray[e.x, e.y].text = gridArray[e.x, e.y]?.ToString(); }; }
public Grid(int width, int height, float cellsize, Vector3 oriPosition, Func <Grid <TGridObject>, int, int, TGridObject> createGridObject) { this.width = width; this.height = height; this.cellsize = cellsize; this.oriPosition = oriPosition; gridArray = new TGridObject[width, height]; debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); if (isDrawLine) { debugTextArray[x, y] = EveryFunction.CreateWorldText(gridArray[x, y].ToString(), null, GetWorldPosition(x, y) + new Vector3(cellsize, cellsize) * 0.5f, 45, Color.white, TextAnchor.MiddleCenter, TextAlignment.Center); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } } if (isDrawLine) { Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); } }
public Grid(int width, int height, float CellSize, int depth, Vector3 OriginPos, Func <Grid <TGridObject>, int, int, TGridObject> CreateObject) { this.width = width; this.height = height; this.CellSize = CellSize; this.DepthOfGrid = depth; this.OriginPos = OriginPos; gridArray = new TGridObject[width, height]; debugValues = new TextMesh[width, height]; for (int i = 0; i < gridArray.GetLength(0); i++) { for (int j = 0; j < gridArray.GetLength(1); j++) { gridArray[i, j] = CreateObject(this, i, j); } } Debug.Log("Initiated a grid with Width = " + width.ToString() + " Cells and height = " + height.ToString() + " Cells"); for (int i = 0; i < gridArray.GetLength(0); i++) { for (int j = 0; j < gridArray.GetLength(1); j++) { Vector3 CurrentWorldPosition = GetWorldPosition(i, j); CurrentWorldPosition.z = depth; //debugValues[i,j]=UtilsClass.CreateWorldText(gridArray[i, j]?.ToString(), null, CurrentWorldPosition+new Vector3(CellSize/2,CellSize/2,0), 20, Color.white); Helper.DrawRectangle(CurrentWorldPosition, CellSize); } } }
public Grid(int width, int height, float cellSize, Vector3 originPosition, Func <Grid <TGridObject>, int, int, TGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } bool showDebug = true; if (showDebug) { TextMesh[,] debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { debugTextArray[x, y] = CreateWorldText(null, gridArray[x, y]?.ToString(), GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * 0.5f, 60, Color.white, TextAnchor.MiddleCenter, TextAlignment.Center, 0); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); } }
public Grid(int n_largeur, int n_hauteur, float n_dimCell, Vector3 n_origine, Func <Grid <TGridObject>, int, int, TGridObject> createGridObjet) { largeur = n_largeur; hauteur = n_hauteur; dimCell = n_dimCell; origine = n_origine; listNode = new TGridObject[largeur, hauteur]; for (int x = 0; x < listNode.GetLength(0); x++) { for (int y = 0; y < listNode.GetLength(1); y++) { listNode[x, y] = createGridObjet(this, x, y); } } for (int x = 0; x < largeur; x++) { for (int y = 0; y < hauteur; y++) { Debug.DrawLine(Position(x, y), Position(x + 1, y), Color.white, 100f); Debug.DrawLine(Position(x, y), Position(x, y + 1), Color.white, 100f); } } Debug.DrawLine(Position(0, hauteur), Position(largeur, hauteur), Color.white, 100f); Debug.DrawLine(Position(largeur, 0), Position(largeur, hauteur), Color.white, 100f); }
public GridMesh(int width, int height, float cellSize, Vector3 originPosition, Func <GridMesh <TGridObject>, int, int, TGridObject> createGridObject, bool showDebug = false) { Width = width; Height = height; CellSize = cellSize; _originPosition = originPosition; _gridArray = new TGridObject[width, height]; for (int i = 0; i < _gridArray.GetLength(0); i++) { for (int j = 0; j < _gridArray.GetLength(1); j++) { _gridArray[i, j] = createGridObject(this, i, j); } } if (showDebug) { _debugTextMeshes = new TextMesh[width, height]; DrawMesh(); } }
public GridCM(int width, int height, float cellSize, Vector3 originPosition, Func <GridCM <TGridObject>, int, int, bool, TGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; int layerId = 9; int layerMask = 1 << layerId; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { Vector3 center = GetWorldPosition(x, y); var ground = Physics2D.OverlapBox(new Vector2(center.x + 0.5f, center.y + 0.5f), new Vector2(0.9f, 0.9f), 0f, layerMask); bool walkable = !ground; gridArray[x, y] = createGridObject(this, x, y, walkable); // if(!walkable){ // Debug.Log("Node " + x + " " + y + " is unwalkable"); // } // else{ // Debug.Log("Node " + x + " " + y + " is walkable"); // } Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); }
public MapGrid(int width, int height, float cellSize, Vector3 originPosition, Func <MapGrid <TGridObject>, int, int, TGridObject> initObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; pathNodes = new PathNode[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int z = 0; z < gridArray.GetLength(1); z++) { gridArray[x, z] = initObject(this, x, z); pathNodes[x, z] = new PathNode(pathNodes, x, z); } } // if (DebugStore.debugMode) // { // for (int x = 0; x < gridArray.GetLength(0); x++) // { // for (int y = 0; y < gridArray.GetLength(1); y++) // { // Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); // Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); // } // } // Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); // Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); // } }
private TextMesh[,] debugTextArray; //debug purposes public Grid(int width, int height, float cellSize, Vector3 originPosition) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; bool showDebug = true; if (showDebug) { debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { //Debug.Log(x + "," + y); debugTextArray[x, y] = UtilsClass.CreateWorldText(gridArray[x, y].ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * 0.5f, 20, Color.white, TextAnchor.MiddleCenter); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); } //SetValue(2, 1, 56); }
/// <summary> /// Constructor /// </summary> /// <param name="width">The width of the grid</param> /// <param name="height">The height of the grid</param> /// <param name="cellWidth">The width of each cell in the grid</param> /// <param name="cellHeight">The height of each cell in the grid</param> /// <param name="createGridObject">Function that creates the TGridObject</param> /// <param name="originPostion">The buttom left corner of the grid in world space</param> /// <param name="is2D">Is the grid in 2D world</param> public GenericGrid( int width, int height, int cellWidth, int cellHeight, Func <GenericGrid <TGridObject>, int, int, TGridObject> createGridObject, Vector3 originPostion = default(Vector3), bool is2D = false) { this.width = width; this.height = height; this.cellWidth = cellWidth; this.cellHeight = cellHeight; this.is2D = is2D; this.origin = originPostion; gridArray = new TGridObject[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { gridArray[x, y] = createGridObject(this, x, y); } } debugArray = new TextMesh[width, height]; OnGridValueChanged += (object sender, OnGridValueChangedEventArgs e) => { if (debugArray[e.x, e.y] != null) { debugArray[e.x, e.y].text = gridArray[e.x, e.y].ToString(); } }; }
public Grid(int width, int height, float cellSize, Vector3 originPosition, Func <TGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; grid = new TGridObject[width, height]; debugTextArray = new TextMesh[width, height]; for (int x = 0; x < grid.GetLength(0); x++) { Debug.Log("here"); for (int y = 0; y < grid.GetLength(1); y++) { grid[x, y] = createGridObject(); } } //Debugging code // for(int x = 0; x < grid.GetLength(0); x++) // { // for(int y = 0; y < grid.GetLength(1); y++) // { // Debug.Log(grid[x,y].ToString()); // Debug.DrawLine(GetWorldPosition(x,y), GetWorldPosition(x, y+1), Color.white, 100.0f); // Debug.DrawLine(GetWorldPosition(x,y), GetWorldPosition(x+1, y), Color.white, 100.0f); // } // } // Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width,height), Color.white, 100.0f); // Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width,height), Color.white, 100.0f); }
public Grid(int width, int height, float cellSize, Vector2 originPos, Func <Grid <TGridObject>, int, int, TGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPos = originPos; gridArray = new TGridObject[width, height]; debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); //debugTextArray[x,y] = CreateWorldText(gridArray[x,y].ToString(), null, GetWorldPos(x,y) + new Vector2(cellSize,cellSize)*0.5f, 20,Color.white,TextAnchor.MiddleCenter); Debug.DrawLine(GetWorldPos(x, y), GetWorldPos(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPos(x, y), GetWorldPos(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPos(0, height), GetWorldPos(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPos(width, 0), GetWorldPos(width, height), Color.white, 100f); OnGridValueChanged += (object sender, OnGridValueChangedEventArgs eventArgs) => { debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y].ToString(); }; }
public void init(int width, int height, float cellSize, Vector3 originPosition) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; gridArray = new TGridObject[width, height]; }
public Grid(Vector2Int gridSize, float cellSize, Vector2 parentPosition) { _gridSize = gridSize; _cellSize = cellSize; _cellCountX = (int)(_gridSize.x / _cellSize); _cellCountY = (int)(_gridSize.y / _cellSize); _originPosition = parentPosition - new Vector2(_gridSize.x, _gridSize.y) * .5f; _gridArray = new TGridObject[_cellCountX, _cellCountY]; }
public Grid(int width, int height, Func <Grid <TGridObject>, int, int, bool, TGridObject> createGridObject) { this.width = width; this.height = height; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y, true); } } }
public CustomGrid(int width, int height, float cellSize, Vector3 originPosition, Func <CustomGrid <TGridObject>, int, int, TGridObject> createGridObject, bool alignOnZ) { this.width = width; this.height = height; this.cellSize = cellSize; if (alignOnZ) { this.originPosition = new Vector3(originPosition.x - ((width * 0.5f) * cellSize), originPosition.y, originPosition.z - ((height * 0.5f) * cellSize)); } else { this.originPosition = new Vector3(originPosition.x - ((width * 0.5f) * cellSize), originPosition.y - ((height * 0.5f) * cellSize), originPosition.z); } this.alignOnZ = alignOnZ; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int z = 0; z < gridArray.GetLength(1); z++) { gridArray[x, z] = createGridObject(this, x, z); } } bool showDebug = true; if (showDebug && alignOnZ) { TextMesh[,] debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int z = 0; z < gridArray.GetLength(1); z++) { debugTextArray[x, z] = Misc.MiscFunctions.CreateWorldText(gridArray[x, z]?.ToString(), null, GetWorldPosition(x, z) + new Vector3(cellSize, 0, cellSize) * 0.5f, 12, Color.white, TextAnchor.MiddleCenter); Debug.DrawLine(GetWorldPosition(x, z), GetWorldPosition(x, z + 1), Color.white, 100); Debug.DrawLine(GetWorldPosition(x, z), GetWorldPosition(x + 1, z), Color.white, 100); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100); OnGridValueChanged += (object sender, OnGridValueChangedEventArgs eventArgs) => { debugTextArray[eventArgs.x, eventArgs.z].text = gridArray[eventArgs.x, eventArgs.z]?.ToString(); }; } //SetValue(0, 1, 56); }
public Grid(int sizeX, int sizeY, float cellSize, Vector3 origin, GridPivot pivot, GridDrawPlane drawPlane, bool debug = false) { _size = new Vector2Int(sizeX, sizeY); _cells = new TGridObject[sizeX, sizeY]; _cellSize = cellSize; _origin = origin; _pivot = pivot; _drawPlane = drawPlane; if (debug) { DrawDebug(); } }
public Grid(int width, int height, Func <TGridObject> createGridObject) { this.width = width; this.height = height; GridArray = new TGridObject[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { GridArray[x, y] = createGridObject(); } } }
public TileGrid(int width, int height, float cellSize, Vector3 originOffset, Func <TileGrid <TGridObject>, int, int, TGridObject> createGridObject) { _width = width; _height = height; _cellSize = cellSize; _originOffset = originOffset; _gridArray = new TGridObject[width, height]; for (int x = 0; x < _gridArray.GetLength(0); x++) { for (int y = 0; y < _gridArray.GetLength(1); y++) { _gridArray[x, y] = createGridObject(this, x, y); } } }
public Grid(int x, int y, float cellSize, Vector2 origin, GameObject spawnObject, Func <Grid <TGridObject>, int, int, KeyCode, GameObject, TGridObject> CreateGridObject, float rowOffset = 0f) { gridObjects = new TGridObject[x, y]; this.cellSize = cellSize; this.rowOffset = rowOffset; this.origin = origin; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { gridObjects[i, j] = CreateGridObject(this, i, j, KeyCode.None, spawnObject); } } }
/// <summary> /// /// </summary> /// <param name="width">Width of the grid</param> /// <param name="height">Height of the grid</param> /// <param name="cellSize">Each cells size on world space</param> /// <param name="originPosition">Where the grid should be placed</param> /// <param name="createGridObject">How should I create this value?</param> /// <param name="showDebug">Enable built-in debug text?</param> /// <param name="debugColor">Color of the built-in debug text</param> /// <param name="fontSize">Size of the debug text (which uses ToString() of the class)</param> public Grid(int width, int height, float cellSize, Vector2 originPosition, Func <Grid <TGridObject>, int, int, TGridObject> createGridObject, bool showDebug = false, Color?debugColor = null, int fontSize = 20) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; this.showDebug = showDebug; this.createGridObject = createGridObject; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } if (showDebug) { debugTextArray = new TextMesh[width, height]; if (debugColor == null) { debugColor = Color.black; } for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { debugTextArray[x, y] = UtilsClass.CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorldPosition(x, y) + new Vector2(cellSize, cellSize) * 0.5f, fontSize, debugColor, textAnchor: TextAnchor.MiddleCenter); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), (Color)debugColor, float.MaxValue); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), (Color)debugColor, float.MaxValue); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), (Color)debugColor, float.MaxValue); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), (Color)debugColor, float.MaxValue); OnGridValueChanged += (object sender, OnGridValueChangedEventArgs eventArgs) => { debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString(); }; } }
public Grid(int width, int height, float cellSize, Func <Grid <TGridObject>, int, int, TGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } }
//Constructor public JUCLGrid(int width, int height, float cellSize, Vector3 originPosition, Func <JUCLGrid <TGridObject>, int, int, TGridObject> createGridObject, bool showDebug, float debugHeight) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; this.showDebug = showDebug; this.debugHeight = debugHeight; gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } //Debug mode if (showDebug == true) { //Create text TextMeshPro[,] debugTextArray = new TextMeshPro[width, height]; //For every tile for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { //Create text representation of value debugTextArray[x, y] = JUCLTextUtils.CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, 0, cellSize) * 0.5f, 2, Color.white, TextAlignmentOptions.Center); //Draw debug grid lines Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } //Debug grid lines Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); //Event for when value is changed OnGridValueChanged += (object sender, OnGridValueChangedEventArgs eventArgs) => { debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString(); }; } }
//constructor, also takes in a Func parameter to create any manner of custom object public Grid(int width, int height, float cellSize, Vector3 originPosition, Func <Grid <TGridObject>, int, int, TGridObject> createGridObject) { //set variables this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; //instantiate gridArray gridArray = new TGridObject[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } bool showDebug = false; if (showDebug) { //Array that contains text for each grid square TextMesh[,] debugTextArray = new TextMesh[width, height]; //create said text for grid and draw lines around each square (find a better way to draw these) for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { debugTextArray[x, y] = CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 30, Color.white, TextAnchor.MiddleCenter); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); OnGridObjectChanged += (object sender, OnGridObjectChangedEventArgs eventArgs) => { debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString(); }; } }
//constuctor public GenericGrid(int width, int height, float cellSizeX, float cellSizeY, Vector3 originPosition, Func <GenericGrid <TGridObject>, int, int, TGridObject> createGridObject, bool ShowGrid = true) { this.width = width; this.height = height; this.cellSizeX = cellSizeX; this.cellSizeY = cellSizeY; this.originPosition = originPosition; gridArray = new TGridObject[this.width, this.height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } }