private void AddScore(Collision collision) { float points = baseValue; points += collision.relativeVelocity.magnitude * velocityMultiplier; Rigidbody rb = collision.gameObject.GetComponent <Rigidbody>(); if (rb != null) { points += rb.mass * massMultiplier; } ChargedObject co = collision.gameObject.GetComponent <ChargedObject>(); if (co != null) { points += co.charge * chargeMultiplier; if (co.charge > 0) { points += co.charge * positiveChargeMultiplier; } if (co.charge < 0) { points += co.charge * negativeChargeMultiplier; } } if (GameManager.GetGameManager() != null) { GameManager.GetGameManager().AddScore(points); } }
public static void ApplyForces(ChargedObject self, List <ChargedObject> others) { others.ForEach((other) => { CalcCoulombForce(self, other); CalcLorentzForce(self, other); }); }
private static void CalcLorentzForce(ChargedObject self, ChargedObject other) { var distance = Vector3.Distance(self.transform.position, other.transform.position); var direction = self.transform.position - other.transform.position; direction.Normalize(); var magneticInduction = Vector3.Cross(other.Rigidbody.velocity, direction) * (PhysicsConstants.LORENTZ_FOEF * other.Charge / Mathf.Pow(distance, 2f)); var force = self.Charge * Vector3.Cross(self.Rigidbody.velocity, magneticInduction); if (Math.Abs(Vector3.SqrMagnitude(force)) <= PhysicsConstants.ACCURACY) { return; } if (!isValidForce(force)) { return; } self.Rigidbody.AddForce(force * Time.fixedDeltaTime); self.LorentzForce += force; }
private static void CalcCoulombForce(ChargedObject self, ChargedObject other) { var distance = Vector3.Distance(self.transform.position, other.transform.position); var force = PhysicsConstants.COULOMB_KOEF * self.Charge * other.Charge / Mathf.Pow(distance, 2f); var direction = self.transform.position - other.transform.position; direction.Normalize(); var forceWithDirection = force * direction; forceWithDirection.x = (float)Math.Round(forceWithDirection.x, 0); forceWithDirection.y = (float)Math.Round(forceWithDirection.y, 0); forceWithDirection.z = (float)Math.Round(forceWithDirection.z, 0); if (Math.Abs(Vector3.SqrMagnitude(forceWithDirection)) <= PhysicsConstants.ACCURACY) { return; } if (!isValidForce(forceWithDirection)) { return; } self.Rigidbody.AddForce(forceWithDirection * Time.fixedDeltaTime); self.CoulombForce += forceWithDirection; }
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); }
public void SendMessageToolChangedCharge(ChargedObject chargedObject) { //if a charge was changed in sandbox mode before starting the sim, then sandbox history must be updated with the tool use if (!GameManager.GetGameManager().HasSimulationBegun()) { UpdateHistory(chargedObject); } }
public ChargedObject GetChargedObject() { if (chargedObject == null) { chargedObject = GetComponent <ChargedObject>(); } return(chargedObject); }
private void FindChargedObjects() { foreach (GameObject go in ParentChildFunctions.GetAllChildren(gameObject, false)) { ChargedObject co = go.GetComponent <ChargedObject>(); if (co != null) { AddChargedObject(co); } } }
private void UpdateHistory(ChargedObject chargedObject) { if (GetChargedObjects().ContainsKey(chargedObject)) { //Debug.Log("udpating history " + chargedObject.charge); GetChargedObjects()[chargedObject].charge = chargedObject.charge; } else { //Debug.Log("UpdateHistory does not contain key"); } }
void OnCollisionEnter(Collision collision) { ChargedObject chargedObject = collision.gameObject.GetComponent <ChargedObject>(); if (chargedObject != null) { AddScore(collision); SetRenderVisibility(true); MakeLightning(collision.transform.position); GetRegionManager().DestroyChargedObject(chargedObject); } }
private void ModifyChargeClick(GameObject clickedObject) { if (clickedObject == null) { return; } ChargedObject co = clickedObject.GetComponent <ChargedObject>(); Tools tool = GetGameplayUI().GetSelectedTool(); if (co != null) { if (co.isLocked) { Debug.Log("prompt: cannot change locked charged object"); } else if (!infiniteCharges && toolCharges[(int)tool] < 1) { Debug.Log("prompt: tool is depleted"); SoundManager.PlaySound(GameSounds.depleted); } else { SoundManager.PlaySound(GameSounds.click); toolCharges[(int)tool]--; if (tool == Tools.add) { co.charge += 1; } if (tool == Tools.subtract) { co.charge -= 1; } if (tool == Tools.divide) { co.charge /= 2; } if (tool == Tools.multiply) { co.charge *= 2; } co.charge = Mathf.Clamp(co.charge, -GameSettings.maximumCharge, GameSettings.maximumCharge); co.UpdateAppearance(); if (GameManager.GetGameManager().isSandboxMode) { GetSandboxManager().SendMessageToolChangedCharge(co); } } GetGameplayUI().UpdateSelectedToolAppearance(); } }
private GameObject MakeChargedObject(GameObject prefab, float charge, Vector3 position) { GameObject go = Instantiate(prefab, position, Quaternion.Euler(Vector3.up)) as GameObject; allChargedObjects.Add(go); go.transform.parent = splashObjectsContainer.transform; ChargedObject co = go.AddComponent <ChargedObject>(); co.charge = charge; co.UpdateAppearance(); return(go); }
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)); } }
public void CreateClick(bool addToSandboxHistory) { SoundManager.PlaySound(GameSounds.click); ChargedObjectSettings chargedObjectSettings = GetCreateObjectUI().GetChargedObjectSettingsFromUI(); if (OverlapsWithCoordHistory(chargedObjectSettings) && GameManager.GetGameManager().GetIsPaused()) { SoundManager.PlaySound(GameSounds.refuse); } else { ChargedObject newChargedObject = MakeChargedObject(chargedObjectSettings, addToSandboxHistory); } }
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)); } }
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); }
public void DeleteClick(GameObject clickObject) { ChargedObject co = clickObject.GetComponent <ChargedObject>(); if (co != null) { ChargedObjectSettings cos = co.GetChargedObjectSettings(); if (cos != null && sandboxHistory.Contains(cos)) { RemoveFromHistory(cos); } GetRegionManager().DestroyChargedObject(co); SoundManager.PlaySound(GameSounds.click); } }
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); }
public ChargedObject GetChargedObject() { if (chargedObject == null) chargedObject = GetComponent<ChargedObject>(); return chargedObject; }
private void UpdateHistory(ChargedObject chargedObject) { if (GetChargedObjects().ContainsKey(chargedObject)) { //Debug.Log("udpating history " + chargedObject.charge); GetChargedObjects()[chargedObject].charge = chargedObject.charge; } else { //Debug.Log("UpdateHistory does not contain key"); } }
private void AddToHistory(ChargedObject chargedObject, ChargedObjectSettings chargedObjectSettings) { sandboxHistory.Add(chargedObjectSettings); coordinateHistory.Add(chargedObjectSettings.position, chargedObjectSettings); GetChargedObjects().Add(chargedObject, chargedObjectSettings); }
public void SendMessageToolChangedCharge(ChargedObject chargedObject) { //if a charge was changed in sandbox mode before starting the sim, then sandbox history must be updated with the tool use if (!GameManager.GetGameManager().HasSimulationBegun()) UpdateHistory(chargedObject); }
private void AddToHistory(ChargedObject chargedObject, ChargedObjectSettings chargedObjectSettings) { sandboxHistory.Add(chargedObjectSettings); coordinateHistory.Add(chargedObjectSettings.position, chargedObjectSettings); GetChargedObjects().Add(chargedObject, chargedObjectSettings); }