public void Start() { Instance = this; BombModule = GetComponent <KMBombModule>(); Audio = GetComponent <KMAudio>(); Submission = Screen.Traverse <TextMesh>("Submission"); SubmissionMaterial = Submission.GetComponent <Renderer>().material; moduleID = idCounter++; // Setup the buttons for (int i = 0; i < 12; i++) { KMSelectable selectable = Numberpad[i]; selectable.AddInteract(() => { if (NotInteractable) { return(false); } Audio.PlaySoundAtTransform("ButtonPress", transform); UserInput += selectable.gameObject.Traverse <TextMesh>("ButtonText").text; return(true); }); } ClearButton.AddInteract(() => { if (NotInteractable) { return(false); } Audio.PlaySoundAtTransform("ButtonPress", transform); UserInput = null; return(true); }); SubmitButton.AddInteract(() => { if (NotInteractable) { return(false); } if (string.IsNullOrEmpty(UserInput)) { CustomDisplayText = "UNDEFINED"; } Audio.PlaySoundAtTransform("ButtonPress", transform); decimal result; if ((CurrentEquation.ValueUndefined && string.IsNullOrEmpty(UserInput)) || (!CurrentEquation.ValueUndefined && decimal.TryParse(UserInput, out result) && result == CurrentEquation.Value)) { isSolved = true; StartCoroutine(SolveAnimation()); } else { StartCoroutine(StrikeAnimation()); } return(true); }); // Generate up to 1000 equations until we get one that doesn't throw an exception. for (int i = 0; i < 1000; i++) { try { CurrentEquation = Equation.Generate(); decimal EquationValue = CurrentEquation.Value; } catch (DivideByZeroException) { // Catch any division by zero, to mark the equation as having an undefined value. CurrentEquation.ValueUndefined = true; } catch (OverflowException) { // The numbers on this module can get really big, too big for the decimal type: // "The highest possible value for an operand is 125,000,000,000 (125 trillion), which is a "?" with no shape, a "|" facing west, and a "C" exponent. // If all three operands are this combination, and they're all connected by multiplication only, the total adds up to... // 1,953,125,000,000,000,000,000,000,000,000,000 / 1.953125 x 10 ^ 33 / 1.953125 decillion" - Lumbud84 CurrentEquation = null; } catch (Exception exception) { Log("Unexpected exception occured, solving module: "); Debug.LogException(exception); BombModule.HandlePass(); } if (CurrentEquation != null) { break; } } // If we weren't able to generate a valid equation, force solve. if (CurrentEquation == null) { Log("Unable to generate a valid equation, solving module."); BombModule.HandlePass(); } Log("Equation:\n" + CurrentEquation.LogMessage); CurrentEquation.Display(Screen.Traverse("Equation")); }
public void Start() { Instance = this; NeedyModule = GetComponent <KMNeedyModule>(); Audio = GetComponent <KMAudio>(); OperandGameObject = OperandScreen.Traverse("Operand"); Submission = SubmissionScreen.Traverse <TextMesh>("Submission"); moduleID = idCounter++; // Setup the buttons for (int i = 0; i < 12; i++) { KMSelectable selectable = Numberpad[i]; selectable.AddInteract(() => { if (NotInteractable) { return(false); } Audio.PlaySoundAtTransform("ButtonPress", transform); UserInput += selectable.gameObject.Traverse <TextMesh>("ButtonText").text; return(true); }); } ClearButton.AddInteract(() => { if (NotInteractable) { return(false); } Audio.PlaySoundAtTransform("ButtonPress", transform); UserInput = null; return(true); }); SubmitButton.AddInteract(() => { if (NotInteractable) { return(false); } decimal result; if (decimal.TryParse(UserInput, out result) && result == CurrentOperand.Value) { Audio.PlaySoundAtTransform("Solve", transform); } else { Audio.PlaySoundAtTransform("Strike", transform); NeedyModule.HandleStrike(); } isSolved = true; NeedyModule.HandlePass(); SetDisplayVisiblity(false); return(true); }); NeedyModule.OnNeedyActivation = () => { isSolved = false; UserInput = null; // Generate up to 1000 operands until we get one that doesn't throw an exception. for (int i = 0; i < 1000; i++) { try { CurrentOperand = Operand.Generate(); decimal OperandValue = CurrentOperand.Value; } catch (OverflowException) { // An overflow probably can't happen with a single operand, but it doesn't hurt. CurrentOperand = null; } catch (Exception exception) { Log("Unexpected exception occured, solving module: "); Debug.LogException(exception); NeedyModule.HandlePass(); return; } if (CurrentOperand != null) { break; } } // If we weren't able to generate a valid operand, force solve. if (CurrentOperand == null) { Log("Unable to generate a valid operand, solving module."); NeedyModule.HandlePass(); return; } Log("Operand:\n" + CurrentOperand.LogMessage.PrefixLines(" - ")); CurrentOperand.Display(OperandGameObject); SetDisplayVisiblity(true); }; NeedyModule.OnNeedyDeactivation = () => SetDisplayVisiblity(false); NeedyModule.OnTimerExpired = () => { NeedyModule.HandleStrike(); SetDisplayVisiblity(false); }; }