add complex algorithm unity project
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public enum BezierCurveType
|
||||
{
|
||||
Linear,
|
||||
Quadratic,
|
||||
Cubic
|
||||
}
|
||||
|
||||
public class BezierCurves : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private BezierCurveType _bezierCurveType;
|
||||
[SerializeField] private Color _color = Color.red;
|
||||
[SerializeField] private Transform[] _positions = new Transform[0];
|
||||
[SerializeField, Min(1)] private int _separations = 1;
|
||||
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.color = _color;
|
||||
|
||||
switch (_bezierCurveType)
|
||||
{
|
||||
case BezierCurveType.Linear:
|
||||
DrawLinearBezierCurve();
|
||||
break;
|
||||
case BezierCurveType.Quadratic:
|
||||
DrawQuadraticBezierCurve();
|
||||
break;
|
||||
case BezierCurveType.Cubic:
|
||||
DrawCubicBezierCurve();
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
for (int i = 0; i < _positions.Length; i++)
|
||||
{
|
||||
Gizmos.DrawSphere(_positions[i].position, 0.25f);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawLinearBezierCurve()
|
||||
{
|
||||
if (_positions.Length < 2) return;
|
||||
|
||||
Vector3 p0 = _positions[0].position;
|
||||
Vector3 p1 = _positions[1].position;
|
||||
|
||||
// Gizmos.DrawLine(p0, p1);
|
||||
|
||||
for (int i = 0; i < _separations; i++)
|
||||
{
|
||||
float t0 = (float)i / _separations;
|
||||
float t1 = (float)(i + 1) / _separations;
|
||||
|
||||
Gizmos.DrawLine(LinearCurvePoint(p0, p1, t0), LinearCurvePoint(p0, p1, t1));
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawQuadraticBezierCurve()
|
||||
{
|
||||
if (_positions.Length < 3) return;
|
||||
|
||||
Vector3 p0 = _positions[0].position;
|
||||
Vector3 p1 = _positions[1].position;
|
||||
Vector3 p2 = _positions[2].position;
|
||||
|
||||
for (int i = 0; i < _separations; i++)
|
||||
{
|
||||
float t0 = (float)i / _separations;
|
||||
float t1 = (float)(i + 1) / _separations;
|
||||
|
||||
Gizmos.DrawLine(QuadraticCurvePoint(p0, p1, p2, t0), QuadraticCurvePoint(p0, p1, p2, t1));
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawCubicBezierCurve()
|
||||
{
|
||||
if (_positions.Length < 4) return;
|
||||
|
||||
Vector3 p0 = _positions[0].position;
|
||||
Vector3 p1 = _positions[1].position;
|
||||
Vector3 p2 = _positions[2].position;
|
||||
Vector3 p3 = _positions[3].position;
|
||||
|
||||
for (int i = 0; i < _separations; i++)
|
||||
{
|
||||
float t0 = (float)i / _separations;
|
||||
float t1 = (float)(i + 1) / _separations;
|
||||
|
||||
Gizmos.DrawLine(CubicCurvePoint(p0, p1, p2, p3, t0), CubicCurvePoint(p0, p1, p2, p3, t1));
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 LinearCurvePoint(Vector3 p0, Vector3 p1, float t) => (1 - t) * p0 + t * p1;
|
||||
|
||||
private Vector3 QuadraticCurvePoint(Vector3 p0, Vector3 p1, Vector3 p2, float t) =>
|
||||
LinearCurvePoint(LinearCurvePoint(p0, p1, t), LinearCurvePoint(p1, p2, t), t);
|
||||
|
||||
private Vector3 CubicCurvePoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) =>
|
||||
LinearCurvePoint(QuadraticCurvePoint(p0, p1, p2, t), QuadraticCurvePoint(p1, p2, p3, t), t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1cfb24ff081f4b3f835bd9a2f727f7c2
|
||||
timeCreated: 1730365567
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57013a2083f54f939804c3473dae39d9
|
||||
timeCreated: 1730369902
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace DefaultNamespace.GameOfLife
|
||||
{
|
||||
[RequireComponent(typeof(MeshRenderer))]
|
||||
public class Cell : MonoBehaviour, IPointerClickHandler
|
||||
{
|
||||
[SerializeField] private Material _aliveMaterial;
|
||||
[SerializeField] private Material _deadMaterial;
|
||||
|
||||
private bool _isAlive;
|
||||
|
||||
public bool IsAlive
|
||||
{
|
||||
get => _isAlive;
|
||||
set
|
||||
{
|
||||
_isAlive = value;
|
||||
_meshRenderer.material = _isAlive ? _aliveMaterial : _deadMaterial;
|
||||
}
|
||||
}
|
||||
|
||||
private MeshRenderer _meshRenderer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_meshRenderer = GetComponent<MeshRenderer>();
|
||||
IsAlive = false;
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
IsAlive = !IsAlive;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7becbdb21488469fa938aa758663fcda
|
||||
timeCreated: 1730369913
|
||||
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace DefaultNamespace.GameOfLife
|
||||
{
|
||||
public class GameOfLife : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Cell _cellPrefab;
|
||||
[SerializeField, Range(10, 999)] private int _rows = 50;
|
||||
[SerializeField, Range(10, 999)] private int _columns = 50;
|
||||
|
||||
private Cell[] _cells;
|
||||
private int CalculateIndex(int x, int y) => x + y * _columns;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_cells = new Cell[_rows * _columns];
|
||||
|
||||
for (int x = 0; x < _columns; x++)
|
||||
{
|
||||
for (int y = 0; y < _rows; y++)
|
||||
{
|
||||
Cell cell = Instantiate(_cellPrefab, transform);
|
||||
cell.transform.position = new Vector3(x, y, 0.0f);
|
||||
cell.name = $"{x:000} / {y:000}";
|
||||
|
||||
_cells[CalculateIndex(x, y)] = cell;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SimulateStep()
|
||||
{
|
||||
// collect new states
|
||||
bool[] newStates = new bool[_rows * _columns];
|
||||
|
||||
for (int x = 0; x < _columns; x++)
|
||||
{
|
||||
for (int y = 0; y < _rows; y++)
|
||||
{
|
||||
// apply rules
|
||||
int index = CalculateIndex(x, y);
|
||||
bool cellState = _cells[index].IsAlive;
|
||||
int neighbors = CountNeighbors(x, y);
|
||||
|
||||
if (cellState)
|
||||
{
|
||||
// cell is alive
|
||||
// rule 1: less than two neighbors => die
|
||||
if (neighbors < 2) cellState = false;
|
||||
|
||||
// rule 2 : equal to two or three neighbors => stay alive
|
||||
if (neighbors is 2 or 3) cellState = true;
|
||||
|
||||
// rule 3: more than three neighbors => die
|
||||
if (neighbors > 3) cellState = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// cell is dead
|
||||
// rule 4: equal to three neighbors => birth
|
||||
if (neighbors == 3) cellState = true;
|
||||
}
|
||||
|
||||
newStates[index] = cellState;
|
||||
}
|
||||
}
|
||||
|
||||
// transfer new states
|
||||
for (int i = 0; i < _cells.Length; i++)
|
||||
{
|
||||
_cells[i].IsAlive = newStates[i];
|
||||
}
|
||||
}
|
||||
|
||||
private int CountNeighbors(int x, int y)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
for (int dx = -1; dx <= 1; dx++)
|
||||
{
|
||||
for (int dy = -1; dy <= 1; dy++)
|
||||
{
|
||||
// exception: own cell
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
|
||||
int currentX = x + dx;
|
||||
int currentY = y + dy;
|
||||
|
||||
// exception: borders
|
||||
if (currentX < 0 || currentX >= _columns) continue;
|
||||
if (currentY < 0 || currentY >= _rows) continue;
|
||||
|
||||
if (_cells[CalculateIndex(currentX, currentY)].IsAlive) count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
private void ResetGrid()
|
||||
{
|
||||
for (int i = 0; i < _cells.Length; i++)
|
||||
{
|
||||
_cells[i].IsAlive = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void FillGridRandomly()
|
||||
{
|
||||
for (int i = 0; i < _cells.Length; i++)
|
||||
{
|
||||
_cells[i].IsAlive = Random.value < 0.1f;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSimulateStepButtonClick() => SimulateStep();
|
||||
|
||||
public void OnResetButtonClick()
|
||||
{
|
||||
ResetGrid();
|
||||
OnStopSimulationButtonClick();
|
||||
}
|
||||
|
||||
public void OnStartSimulationButtonClick() => InvokeRepeating(nameof(SimulateStep), 0.0f, 0.5f);
|
||||
public void OnStopSimulationButtonClick() => CancelInvoke();
|
||||
|
||||
public void OnFillGridRandomlyButtonClick() => FillGridRandomly();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49cb0230767d434b93f45ea3290f01a3
|
||||
timeCreated: 1730370459
|
||||
Reference in New Issue
Block a user