Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
NoeBrt committed Apr 7, 2024
1 parent 7143fef commit 2796e0f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 60 deletions.
2 changes: 2 additions & 0 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1431,8 +1431,10 @@ MonoBehaviour:
isPause: 1
CellHistory: []
cubePrefab: {fileID: 6936488134860841744, guid: abfd3ec33900d3e46a861d8fe3b450f9, type: 3}
isHistoryActive: 0
gridMesh: {fileID: 1728370620}
offset: -0.025
historyBound: -55
--- !u!1 &1178169789
GameObject:
m_ObjectHideFlags: 0
Expand Down
30 changes: 23 additions & 7 deletions Assets/Scripts/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,25 @@ public class Cell : MonoBehaviour

public Color mouseOverCubeColor = new Color(0.95f, 0.95f, 0.95f, 1f);

public int countAliveNeighbors;

public List<GameObject> cellHistory;
private float historyBound = -55f;


// Start is called before the first frame update
void Start()
{
cameraController = Camera.main.GetComponent<CameraController>();
spriteRenderer = GetComponent<SpriteRenderer>();

cellHistory = new List<GameObject>();
historyBound = GridManager.instance.historyBound;
}

// Update is called once per frame
public void UpdateCell()
{


UpdateHistory();
UpdateNeighbors();
UpdateCellState();


}
public void UpdateCubeRender()
{
Expand Down Expand Up @@ -174,6 +172,24 @@ private void Update()

UpdateCubeRender();


}
private void UpdateHistory()
{
cellHistory.ForEach(cell => cell.transform.position -= new Vector3(0, 0, 1f));

bool isHistoryActive = GridManager.instance.isHistoryActive;
GameObject firstElement = cellHistory.FirstOrDefault();
if (cellHistory.Count > 0 && firstElement.transform.position.z < historyBound)
{
cellHistory.RemoveAt(0);
Destroy(firstElement);
}
if (state == StateEnum.ALIVE && isHistoryActive)
{
GameObject previousCell = Instantiate(cubePrefab, cube.transform.position - new Vector3(0, 0, 1f), Quaternion.identity);
cellHistory.Add(previousCell);
}
}


Expand Down
114 changes: 61 additions & 53 deletions Assets/Scripts/GridManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ public class GridManager : MonoBehaviour
float deltaT = 0.2f;

public bool isPause = true;
public List<GameObject> CellHistory = new List<GameObject>();

public Cell[,] Cells { get; set; }

public GameObject cubePrefab;

bool isHistoryActive = false;
public bool isHistoryActive = false;

public GameObject gridMesh;
public float offset = -0.025f;

public float historyBound = -55f;
CameraController cameraController;
void initGrid()
{
Cells = new Cell[width, height];
Expand All @@ -36,7 +34,7 @@ void initGrid()
for (int y = 0; y < height; y++)
{
GameObject newCell = Instantiate(cellPrefab,
new Vector3(x + (cellPrefab.transform.localScale.x / 2) - (width / 2)-offset, y + (cellPrefab.transform.localScale.y / 2) - (height / 2)-offset, 0),
new Vector3(x + (cellPrefab.transform.localScale.x / 2) - (width / 2) - offset, y + (cellPrefab.transform.localScale.y / 2) - (height / 2) - offset, 0),
Quaternion.identity);
Cell cell = newCell.GetComponent<Cell>();
cell.position = new int[2] { x, y };
Expand All @@ -46,6 +44,7 @@ void initGrid()
}
void Start()
{
cameraController = Camera.main.GetComponent<CameraController>();

initGrid();
StartCoroutine(UpdateCellsCoroutine());
Expand All @@ -72,45 +71,45 @@ IEnumerator UpdateCellsCoroutine()
{
if (!isPause)
{
for (int i = 0; i < CellHistory.Count; i++)
{
GameObject cell = CellHistory[i];
cell.transform.position = cell.transform.position - new Vector3(0, 0, 1);
if (cell.transform.position.z < -55)
{
CellHistory.Remove(cell);
Destroy(cell);
}
}
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Cells[x, y].UpdateCell();
if (Cells[x, y].state == StateEnum.ALIVE)
{
if (isHistoryActive){
GameObject previousCell = Instantiate(cubePrefab, Cells[x, y].cube.transform.position - new Vector3(0, 0, 1f), Quaternion.identity);
CellHistory.Add(previousCell);}
}

}
}

for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Cells[x, y].state = Cells[x, y].nextState;
}
}
UpdateCellsAndHandleHistory();
ApplyNextStatesToCells();
}

// Wait for deltaT seconds before the next update
yield return new WaitForSeconds(deltaT);
}
}

private void UpdateCellsAndHandleHistory()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Cells[x, y].UpdateCell();
}
}
}

private void ApplyNextStatesToCells()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Cells[x, y].state = Cells[x, y].nextState;
}
yield return new WaitForSeconds(deltaT); // Attendre deltaT seconde avant la prochaine mise à jour
}
}


private void Update()
{
HandleKeyboardInput();
UpdateCellHistoryVisibility();
}

private void HandleKeyboardInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Expand All @@ -119,33 +118,42 @@ private void Update()
if (Input.GetKeyDown(KeyCode.Z))
{
isHistoryActive = !isHistoryActive;

}
if (Input.GetKeyDown(KeyCode.G))
{
gridMesh.SetActive(!gridMesh.activeSelf);

}
if (Input.GetKeyDown(KeyCode.R))
{
CellHistory.ForEach(cell => Destroy(cell));
CellHistory.Clear();
foreach (Cell cell in Cells)
{
cell.state = StateEnum.DEAD;
cell.nextState = StateEnum.DEAD;
}
ResetCells();
}
if(!Camera.main.GetComponent<CameraController>().isProfile && !Camera.main.GetComponent<CameraController>().startTransition )
}

private void ResetCells()
{
foreach (var cell in Cells)
{
CellHistory.ForEach(cell => cell.SetActive(false));
cell.state = StateEnum.DEAD;
cell.nextState = StateEnum.DEAD;
cell.cellHistory.ForEach(Destroy);
cell.cellHistory.Clear();
}
if (!Camera.main.GetComponent<CameraController>().isProfile && Camera.main.GetComponent<CameraController>().startTransition)
}

private void UpdateCellHistoryVisibility()
{
if (!cameraController.isProfile)
{
CellHistory.ForEach(cell => cell.SetActive(isHistoryActive));
bool shouldShowCells = cameraController.startTransition && isHistoryActive;
foreach (var cell in Cells)
{
cell.cellHistory.ForEach(previousCell => previousCell.SetActive(shouldShowCells));
}
}
}




}

}

0 comments on commit 2796e0f

Please sign in to comment.