private IEnumerator Cycle(MovingChargedObject mco) { bool isFirst = true; while (true) { if (isFirst) { isFirst = false; yield return(new WaitForSeconds(Random.Range(0, GameSettings.magnetInterval))); } if (mco == null) { break; } else { if (isSplashMenu || !GameManager.GetGameManager().GetIsPaused()) { ApplyMagneticForce(mco); } yield return(new WaitForSeconds(GameSettings.magnetInterval)); } } }
public ChargedObject MakeChargedObject(ChargedObjectSettings chargedObjectSettings, bool addToSandboxHistory = false) { createCounter++; GameObject newObject = Instantiate(SandboxManager.GetSandboxPrefabs()[chargedObjectSettings.shape]); newObject.name = "sandbox object " + createCounter; newObject.transform.parent = GetRegionManager().gameObject.transform; ChargedObject newChargedObject = newObject.AddComponent <ChargedObject>(); newChargedObject.UpdateValues(chargedObjectSettings); if (chargedObjectSettings.canMove) { MovingChargedObject mco = newObject.AddComponent <MovingChargedObject>(); mco.UpdateValues(chargedObjectSettings); mco.SetFrozenPosition(GameManager.GetGameManager().GetIsPaused()); } newObject.transform.position = chargedObjectSettings.position; GetRegionManager().AddChargedObject(newChargedObject); if (addToSandboxHistory) { AddToHistory(newChargedObject, chargedObjectSettings); } if (!GetChargedObjects().ContainsKey(newChargedObject)) { GetChargedObjects().Add(newChargedObject, chargedObjectSettings); } return(newChargedObject); }
private MovingChargedObject MakeMovingChargedObject(float charge, float mass, Vector3 position, GameObject prefab) { GameObject go = MakeChargedObject(spherePrefab, charge, position); MovingChargedObject mco = go.AddComponent <MovingChargedObject>(); mco.mass = mass; return(mco); }
public void AddChargedObject(ChargedObject chargedObject) { GetChargedObjects().Add(chargedObject); chargedObject.UpdateAppearance(); MovingChargedObject mco = chargedObject.gameObject.GetComponent <MovingChargedObject>(); if (mco != null) { GetMovingChargedObjects().Add(mco); if (hasAppliedStartVelocity) { mco.ApplyStartVelocity(); } StartCoroutine(Cycle(mco)); } }
private void ApplyMagneticForce(MovingChargedObject mco) { Vector3 newForce = new Vector3(0, 0, 0); foreach (ChargedObject chargedObject in GetChargedObjects()) { if (chargedObject == null) { string stuff = ""; foreach (ChargedObject co in GetChargedObjects()) { if (co != null) { stuff += " '" + co.gameObject + "'"; } else { stuff += " null"; } } Debug.Log("null thingy weird! " + GetChargedObjects().Count + " " + stuff); } if (mco.GetChargedObject() == chargedObject || chargedObject.ignoreOtherMovingChargedObjects) { continue; } float distance = Vector3.Distance(mco.transform.position, chargedObject.gameObject.transform.position); float force = 1000 * mco.GetCharge() * chargedObject.charge / Mathf.Pow(distance, 2); Vector3 direction; direction = mco.transform.position - chargedObject.transform.position; direction.Normalize(); newForce += force * direction * GameSettings.magnetInterval; } //if two charged particles occupy the same space, the newForce is (NaN,NaN,NaN) and AddForce throws an error if (float.IsNaN(newForce.x)) { newForce = Vector3.zero; } mco.AddForce(newForce); }
private void MakeNetDisruption(Vector3 center) { Debug.Log("MakeNetDisruption"); MakeNet(center, 5, 5, 3); int spread = 5; for (int i = -1; i < 2; i += 2) { for (int j = -1; j < 2; j += 2) { int height = Random.Range(60, 130); Vector3 position = center + new Vector3(i * spread, height, j * spread); MovingChargedObject mco = MakeMovingChargedObject(GetRandomNonZero(5) * 15, Random.Range(20, 100), position); mco.startVelocity = new Vector3(0, -Random.Range(2, 5), 0); } } }
private void RemakeCursorGameObject() { shouldRemakeCursor = false; Destroy(cursorGameObject); ChargedObjectSettings chargedObjectSettings = GetChargedObjectSettingsFromUI(); cursorGameObject = Instantiate(SandboxManager.GetSandboxPrefabs()[sandboxShape]); ChargedObject co = cursorGameObject.AddComponent <ChargedObject>(); MovingChargedObject mco = cursorGameObject.AddComponent <MovingChargedObject>(); co.enabled = false; mco.enabled = false; co.UpdateValues(chargedObjectSettings); mco.UpdateValues(chargedObjectSettings); cursorGameObject.transform.position = new Vector3(0, -100000, 0); ParentChildFunctions.SetCollidersOfChildren(cursorGameObject, false, true); }
public void DestroyChargedObject(ChargedObject chargedObject) { MovingChargedObject movingChargedObject = chargedObject.gameObject.GetComponent <MovingChargedObject>(); if (movingChargedObject != null) { if (GetMovingChargedObjects().Contains(movingChargedObject)) { GetMovingChargedObjects().Remove(movingChargedObject); } else { Debug.LogError("DestroyChargedObject called but RegionManager does not have this mco."); } } GetChargedObjects().Remove(chargedObject); Destroy(chargedObject.gameObject); Destroy(chargedObject); Destroy(movingChargedObject); }
private void ApplyMagneticForce(MovingChargedObject mco) { Vector3 newForce = new Vector3(0, 0, 0); foreach (ChargedObject chargedObject in GetChargedObjects()) { if (chargedObject == null) { string stuff = ""; foreach (ChargedObject co in GetChargedObjects()) if (co != null) stuff += " '" + co.gameObject + "'"; else stuff += " null"; Debug.Log("null thingy weird! " + GetChargedObjects().Count + " " + stuff); } if (mco.GetChargedObject() == chargedObject || chargedObject.ignoreOtherMovingChargedObjects) continue; float distance = Vector3.Distance(mco.transform.position, chargedObject.gameObject.transform.position); float force = 1000 * mco.GetCharge() * chargedObject.charge / Mathf.Pow(distance, 2); Vector3 direction; direction = mco.transform.position - chargedObject.transform.position; direction.Normalize(); newForce += force * direction * GameSettings.magnetInterval; } //if two charged particles occupy the same space, the newForce is (NaN,NaN,NaN) and AddForce throws an error if (float.IsNaN(newForce.x)) newForce = Vector3.zero; mco.AddForce(newForce); }
private IEnumerator Cycle(MovingChargedObject mco) { bool isFirst = true; while (true) { if (isFirst) { isFirst = false; yield return new WaitForSeconds(Random.Range(0, GameSettings.magnetInterval)); } if (mco == null) { break; } else { if (isSplashMenu || !GameManager.GetGameManager().GetIsPaused()) ApplyMagneticForce(mco); yield return new WaitForSeconds(GameSettings.magnetInterval); } } }