/// <summary> /// Resets the controls to the default values /// </summary> public void ResetControls() { audioSource.Play(); dialog.OnResult += (Dialog.DialogResult result) => { if (result == Dialog.DialogResult.Yes) { changingControl = null; ControlsChanged = false; SaveSystem.ResetControls(); GetControls(false); okButton.gameObject.SetActive(false); applyButton.gameObject.SetActive(false); cancelButton.gameObject.SetActive(false); if (Game.Instance != null) { Game.Instance.GetControls(); } } }; dialog.Open(Dialog.DialogType.YesNo, "Are you sure that you want to reset the controls?"); }
/// <summary> /// Gets the button text for the control /// </summary> /// <param name="control">The control to get the text for</param> /// <returns>The button text for the control</returns> private TMP_Text GetButtonText(SavedControl control) { int index = controls.IndexOf(control) + 1; Transform controlTransform = template.transform.parent.GetChild(index); Transform buttonTransform = controlTransform.GetChild(1); TMP_Text buttonText = buttonTransform.GetChild(0).GetComponent <TMP_Text>(); return(buttonText); }
/// <summary> /// Change the key for the control /// </summary> /// <param name="control">The contol to change</param> private void ChangeControl(SavedControl control) { audioSource.Play(); TMP_Text buttonText = GetButtonText(control); CancelChange(); changingControl = control; buttonText.text = "Press a key..."; editingText.gameObject.SetActive(true); }
/// <summary> /// Checks the users input /// </summary> private void CheckUserInput() { if (!Input.anyKeyDown || changingControl is null) { return; } foreach (KeyCode code in Enum.GetValues(typeof(KeyCode))) { if (code == KeyCode.Mouse0 || code == KeyCode.Mouse1) { continue; } if (Input.GetKeyDown(code)) { audioSource.Play(); if (code == KeyCode.Escape) { CancelChange(); return; } bool keyTaken = controls.Where(x => x != changingControl).Any(x => x.Key == code); if (keyTaken) { dialog.OnResult += (_) => CancelChange(); dialog.Open(Dialog.DialogType.Ok, $"The key <b>{code}</b> is already taken by another control"); } else { TMP_Text buttonText = GetButtonText(changingControl); changingControl.Key = code; changingControl = null; buttonText.text = code.ToString(); editingText.gameObject.SetActive(false); } } } }
/// <summary> /// Checks the user input and moves the tetromino down /// </summary> private void CheckUserInput() { // This method checks the keys that the player can press to manipulate the position of the tetromino // The options here will be left, right, up and down // Left and right will move the tetromino 1 unit to the left or right // Down will move the tetromino 1 unit down // Up will rotate the tetromino #if UNITY_IOS if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { previousUnitPosition = new Vector2(touch.position.x, touch.position.y); } else if (touch.phase == TouchPhase.Moved) { Vector2 touchDeltaPosition = touch.deltaPosition; direction = touchDeltaPosition.normalized; if (Mathf.Abs(touch.position.x - previousUnitPosition.x) >= Game.Instance.TouchSensitivityHorizontal && direction.x < 0 && touch.deltaPosition.y > -10 && touch.deltaPosition.y < 10) { // Move Left MoveLeft(); previousUnitPosition = touch.position; moved = true; } else if (Mathf.Abs(touch.position.x - previousUnitPosition.x) >= Game.Instance.TouchSensitivityHorizontal && direction.x > 0 && touch.deltaPosition.y > -10 && touch.deltaPosition.y < 10) { // Move Right MoveRight(); previousUnitPosition = touch.position; moved = true; } else if (Mathf.Abs(touch.position.y - previousUnitPosition.y) >= Game.Instance.TouchSensitivityVertical && direction.y < 0 && touch.deltaPosition.x > -10 && touch.deltaPosition.x < 10) { // Move Down MoveDown(); previousUnitPosition = touch.position; moved = true; } } else if (touch.phase == TouchPhase.Ended) { if (!moved && touch.position.x > Screen.width / 4) { Rotate(); } moved = false; } } if (Time.time - fallTimer >= Game.Instance.FallSpeed) { MoveDown(); } #else SavedControl moveLeft = Game.Instance.Controls.FirstOrDefault(x => x.ControlType == SavedControl.Type.MoveLeft); SavedControl moveRight = Game.Instance.Controls.FirstOrDefault(x => x.ControlType == SavedControl.Type.MoveRight); SavedControl moveDown = Game.Instance.Controls.FirstOrDefault(x => x.ControlType == SavedControl.Type.MoveDown); SavedControl rotate = Game.Instance.Controls.FirstOrDefault(x => x.ControlType == SavedControl.Type.Rotate); SavedControl moveToBottom = Game.Instance.Controls.FirstOrDefault(x => x.ControlType == SavedControl.Type.MoveToBottom); SavedControl save = Game.Instance.Controls.FirstOrDefault(x => x.ControlType == SavedControl.Type.SaveTetromino); if (Input.GetKeyUp(moveRight.Key) || Input.GetKeyUp(moveLeft.Key)) { movedImmediateHorizontal = false; horizontalTimer = 0; buttonDownWaitTimerHorizontal = 0; } if (Input.GetKeyUp(moveDown.Key)) { movedImmediateVertical = false; verticalTimer = 0; buttonDownWaitTimerVertical = 0; } if (Input.GetKey(moveRight.Key)) { MoveRight(); } if (Input.GetKey(moveLeft.Key)) { MoveLeft(); } if (Input.GetKeyDown(rotate.Key)) { Rotate(); } if (Input.GetKey(moveDown.Key) || Time.time - fallTimer >= Game.Instance.FallSpeed) { MoveDown(); } if (Input.GetKeyDown(moveToBottom.Key)) { MoveToBottom(); } #endif }