public static void Connect(Atom start, Atom end) { if (start == end) return; if (start.neighbours.Contains(end)) { foreach (var connection1 in start.connections.Where(connection1 => connection1.a == end || connection1.b == end)) { connection1.lifeTimeLeft = MaxLifeTime; return; } return; } var connection = pool.GetObject(); connection.Reset(); start.neighbours.Add(end); end.neighbours.Add(start); start.connections.Add(connection); end.connections.Add(connection); connection.a = start; connection.b = end; }
public static void DisconnectAllNeighbours(Atom atom) { var tempConnections = atom.connections.ToArray(); for (var index = 0; index < tempConnections.Length; index++) { Disconnect(tempConnections[index]); } }
private void UpdateConnections() { //Connection.DisconnectAllNeighbours(this); Atom[] atoms = new Atom[0]; float colliderSize = 1.28f*2f; UpdateAtoms(ref atoms, transform.position, colliderSize*2f); if (atoms.Length < MinNeighbours) { UpdateAtoms(ref atoms, transform.position, colliderSize * 4f); if (atoms.Length < MinNeighbours) { UpdateAtoms(ref atoms, transform.position, colliderSize * 6f); if (atoms.Length < MinNeighbours) { UpdateAtoms(ref atoms, transform.position, colliderSize * 8f); if (atoms.Length < MinNeighbours) { UpdateAtoms(ref atoms, transform.position, colliderSize * 10f); if (atoms.Length < MinNeighbours) { UpdateAtoms(ref atoms, transform.position, colliderSize * 12f); if (atoms.Length < MinNeighbours) { UpdateAtoms(ref atoms, transform.position, colliderSize * 15f); if (atoms.Length < MinNeighbours) { UpdateAtoms(ref atoms, transform.position, colliderSize * 20f); } } } } } } } foreach (var atom in atoms) { Connection.Connect(this, atom); } }
private static void UpdateAtoms(ref Atom[] atoms, Vector3 position, float radius) { _cachedAtomList.Clear(); //int count = atoms.Length; for (int index = 0; index < atoms.Length; index++) { _cachedAtomList.Add(atoms[index]); } foreach (var colliderInCircle in Physics2D.OverlapCircleAll(position, radius)) { //if (count > 8) //{ //break; //} var atom = colliderInCircle.GetComponent<Atom>(); if (atom != null && !_cachedAtomList.Contains(atom)) { _cachedAtomList.Add(atom); //count++; } } atoms = _cachedAtomList.ToArray(); }