void Update() { if (status == STATUS.NORMAL) { // for each possible match between one magnet on the moving part and those on the base foreach (MagnetMatch match in matches) { // for each of the possible base magnets foreach (Magnet baseMagnet in match.baseMagnets) { // keeps monitoring position and rotation difference until the connection threshold is reached if (checkDistance(baseMagnet, match.movingMagnet) && checkRotation(baseMagnet, match.movingMagnet)) { // disables the moving part collider to avoid collisions while joining magnets match.movingMagnet.transform.parent.GetComponent <Collider> ().enabled = false; baseMagnetToJoin = baseMagnet; movingMagnetToJoin = match.movingMagnet; status = STATUS.JOINED; break; } } } } else if (status == STATUS.JOINED) { joinMagnets(); } }
// Whenever two magnets have been joined, the moving part is hidden and a corresponding static base part takes // its place, so as to avoid displaying imperfect connections with space in between, wrong angles, etc. private void HandleMagnetsJoined(GameObject joinedPart, Magnet baseMagnetJoined) { magnetController.enabled = false; AudioSource.PlayClipAtPoint(lockSound, Camera.main.transform.position); // the moving part is immediately hidden and removed from the game joinedPart.SetActive(false); // searches for a correspondance between the base magnet which has been connected and the array of // possible matches, so that it can reach the relevant disabled base part and enable it GameObject basePart = null; foreach (BaseObjectByMagnet baseObjectByMagnet in baseObjectsByMagnet) { if (baseObjectByMagnet.baseMagnet == baseMagnetJoined) { basePart = baseObjectByMagnet.baseObject; break; } } basePart.SetActive(true); // if all the base parts are now active, the game is over bool allBasePartsActive = true; foreach (Transform child in baseObject.transform) { if (!child.gameObject.activeSelf) { allBasePartsActive = false; break; } } if (allBasePartsActive) { GameWon(); } }
// Checks rotation difference threshold between magnets // FIXME: with a different controller, the Y axis should be considered too... private bool checkRotation(Magnet baseMagnet, Magnet movingMagnet) { Vector3 difference = baseMagnet.transform.eulerAngles - movingMagnet.transform.eulerAngles; float absX = Mathf.Abs(difference.x); bool xOK = false; if ((absX - 0) < rotationThreshold.x || (360 - absX) < rotationThreshold.x) { xOK = true; } float absZ = Mathf.Abs(difference.z); bool zOK = false; if ((absZ - 0) < rotationThreshold.z || (360 - absZ) < rotationThreshold.z) { zOK = true; } return(xOK && zOK); }
// Checks distance threshold between magnets private bool checkDistance(Magnet baseMagnet, Magnet movingMagnet) { float distance = Vector3.Distance(baseMagnet.transform.position, movingMagnet.transform.position); return(Mathf.Abs(distance) <= distanceThreshold); }
public void OnDisable() { matches = new List <MagnetMatch> (); baseMagnetToJoin = null; movingMagnetToJoin = null; }
void Start() { status = STATUS.NORMAL; baseMagnetToJoin = null; movingMagnetToJoin = null; }
private void HandleMagnetsJoined(GameObject joinedPart, Magnet baseMagnetJoined) { selected = null; zoom = 3f; }