public static void SetAroundCells(this LifeGameCell centerCell, LifeGameCell rightCell, LifeGameCell lowerRightCell, LifeGameCell lowerCell, LifeGameCell lowerLeftCell) { centerCell.ConnectRight(rightCell); centerCell.ConnectLowerRight(lowerRightCell); centerCell.ConnectLower(lowerCell); centerCell.ConnectLowerLeft(lowerLeftCell); }
void Update() { ///再生中だったら if (isPlayed) { LifeActivity(); generationNum++; } if (Input.GetMouseButtonDown(0) && !isPlayed) { clickedCell = null; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { clickedCell = hit.collider.gameObject.GetComponent <LifeGameCell>(); } if (clickedCell == null) { return; } if (clickedCell.CellState == LifeGameCell.CellStates.dead) { clickedCell.CellState = LifeGameCell.CellStates.alive; } else { clickedCell.CellState = LifeGameCell.CellStates.dead; } } /// 生存しているセルの数のTextを更新 if (m_aliveCells != null) { m_aliveCells.text = "生存数" + allCells.ToString() + "個"; } /// 世代を表示するTextを更新 if (m_generationText != null) { m_generationText.text = generationNum.ToString() + "世代"; } }
public static void ConnectLowerLeft(this LifeGameCell upperRightCell, LifeGameCell lowerLeftCell) { upperRightCell.LowerLeft = lowerLeftCell; lowerLeftCell.UpperRight = upperRightCell; }
public static void ConnectLower(this LifeGameCell upperCell, LifeGameCell lowerCell) { upperCell.Lower = lowerCell; lowerCell.Upper = upperCell; }
public static void ConnectRight(this LifeGameCell leftCell, LifeGameCell rightCell) { leftCell.Right = rightCell; rightCell.Left = leftCell; }
/// <summary> /// 対象のセルの周りのセルを確認する /// </summary> /// <param name="x"> 対象のセルのX座標 </param> /// <param name="y"> 対象のセルのX座標 </param> /// <param name="cell"> 対象のセル </param> void GetNeighborCells(int x, int y, LifeGameCell cell) { cell.neighborCells = 0; ///左上 if (x == 0 && y == 0) { if (lifecells[x + 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } ///左下 else if (x == 0 && y == m_rows - 1) { if (lifecells[x + 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } ///左端 else if (x == 0 && y > 0 && y < m_rows - 1) { if (lifecells[x, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } ///右上 else if (x == m_columns - 1 && y == 0) { if (lifecells[x - 1, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } ///右下 else if (x == m_columns - 1 && y == m_rows - 1) { if (lifecells[x - 1, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } ///右端 else if (x == m_columns - 1 && y > 0 && y < m_rows - 1) { if (lifecells[x - 1, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } ///端にない場合 else if (x > 0 && x < m_columns - 1 && y > 0 && y < m_rows - 1) { if (lifecells[x + 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } ///一番上の列 else if (x > 0 && x < m_columns - 1 && y == 0) { if (lifecells[x + 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y + 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } ///一番下の列 else if (x > 0 && x < m_columns - 1 && y == m_rows - 1) { if (lifecells[x + 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x + 1, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x - 1, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } if (lifecells[x, y - 1].CellState == LifeGameCell.CellStates.alive) { cell.neighborCells++; } } }