void Start() { Settings = FindObjectOfType <CSettingsContainer>(); m_arr_Playground = new CCellBehaviour[Settings.Dimensions.x, Settings.Dimensions.y]; // Fill Playground for (int x = 0; x < m_arr_Playground.GetLength(0); x++) { for (int y = 0; y < m_arr_Playground.GetLength(1); y++) { GameObject go = Instantiate(m_cellPrefab, gameObject.transform); // Position cells go.transform.localPosition = new Vector3(x, -y); CCellBehaviour cellBehaviour = go.GetComponent <CCellBehaviour>(); cellBehaviour.m_ArrPos = new Vector2Int(x, y); // Inform cell about its position m_arr_Playground[x, y] = cellBehaviour; } } // Add neighbors to the cells foreach (CCellBehaviour cell in m_arr_Playground) { AddNeighbors(cell.m_ArrPos.x, cell.m_ArrPos.y, cell); } m_arrFilled = true; // The playgound has been populated Render(); // Startrender of playground }
/// <summary> /// Adds a new neighbor to each others neighbor list /// </summary> /// <param name="_neighbor"> The neighbor cell to be added </param> public void AddNeighbor(CCellBehaviour _neighbor) { if (!m_list_neighbors.Contains(_neighbor)) // Prevents duplicates { m_list_neighbors.Add(_neighbor); // Add neighbor to this cells list _neighbor.AddNeighbor(this); // Add this cell to the neighbors list } }
/// <summary> /// Add neighbors to the cell in the playgound array with full bounds checks /// </summary> /// <param name="_x"> Cells x-position on playground </param> /// <param name="_y"> Cells y-position on playground </param> /// <param name="_cell"> Cell which will get neighbors added </param> private void AddNeighbors(int _x, int _y, CCellBehaviour _cell) { try { // Left if (!(_x - 1 < 0)) { _cell.AddNeighbor(m_arr_Playground[_x - 1, _y]); } else { _cell.AddNeighbor(m_arr_Playground[m_arr_Playground.GetLength(0) - 1, _y]); } // Right if (!(_x + 1 >= m_arr_Playground.GetLength(0))) { _cell.AddNeighbor(m_arr_Playground[_x + 1, _y]); } else { _cell.AddNeighbor(m_arr_Playground[0, _y]); } // Top if (!(_y - 1 < 0)) { _cell.AddNeighbor(m_arr_Playground[_x, _y - 1]); } else { _cell.AddNeighbor(m_arr_Playground[_x, m_arr_Playground.GetLength(1) - 1]); } // Bottom if (!(_y + 1 >= m_arr_Playground.GetLength(1))) { _cell.AddNeighbor(m_arr_Playground[_x, _y + 1]); } else { _cell.AddNeighbor(m_arr_Playground[_x, 0]); } // Top Left if (!(_x - 1 < 0 || _y - 1 < 0)) { _cell.AddNeighbor(m_arr_Playground[_x - 1, _y - 1]); } else if (!(_x - 1 < 0)) { _cell.AddNeighbor(m_arr_Playground[_x - 1, m_arr_Playground.GetLength(1) - 1]); } else if (!(_y - 1 < 0)) { _cell.AddNeighbor(m_arr_Playground[m_arr_Playground.GetLength(0) - 1, _y - 1]); } else { _cell.AddNeighbor(m_arr_Playground[m_arr_Playground.GetLength(0) - 1, m_arr_Playground.GetLength(1) - 1]); } // Top Right if (!(_x + 1 >= m_arr_Playground.GetLength(0) || _y - 1 < 0)) { _cell.AddNeighbor(m_arr_Playground[_x + 1, _y - 1]); } else if (!(_x + 1 >= m_arr_Playground.GetLength(0))) { _cell.AddNeighbor(m_arr_Playground[_x + 1, m_arr_Playground.GetLength(1) - 1]); } else if (!(_y - 1 < 0)) { _cell.AddNeighbor(m_arr_Playground[0, _y - 1]); } else { _cell.AddNeighbor(m_arr_Playground[0, m_arr_Playground.GetLength(1) - 1]); } // Bottom Left if (!(_x - 1 < 0 || _y + 1 >= m_arr_Playground.GetLength(1))) { _cell.AddNeighbor(m_arr_Playground[_x - 1, _y + 1]); } else if (!(_x - 1 < 0)) { _cell.AddNeighbor(m_arr_Playground[_x - 1, 0]); } else if (!(_y + 1 >= m_arr_Playground.GetLength(1))) { _cell.AddNeighbor(m_arr_Playground[m_arr_Playground.GetLength(0) - 1, _y + 1]); } else { _cell.AddNeighbor(m_arr_Playground[m_arr_Playground.GetLength(0) - 1, 0]); } // Bottom Right if (!(_x + 1 >= m_arr_Playground.GetLength(0) || _y + 1 >= m_arr_Playground.GetLength(1))) { _cell.AddNeighbor(m_arr_Playground[_x + 1, _y + 1]); } else if (!(_x + 1 >= m_arr_Playground.GetLength(0))) { _cell.AddNeighbor(m_arr_Playground[_x + 1, 0]); } else if (!(_y + 1 >= m_arr_Playground.GetLength(1))) { _cell.AddNeighbor(m_arr_Playground[0, _y + 1]); } else { _cell.AddNeighbor(m_arr_Playground[0, 0]); } } catch (System.Exception e) // Catch errors of assignement (esp. null references) { Debug.Log(_cell.m_ArrPos.x + "," + _cell.m_ArrPos.y); } }