/// <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); } }