/// <summary> /// Calculate the resized rect /// </summary> /// <param name="canvasRect">Rect to be transformed</param> /// <returns>Resized Rect</returns> public static Rect SafeToSafeCanvas(Rect canvasRect) { // should calculate the safe area size on canvas return(new Rect { // Set the Rect min position min = AplicationFuncs.ScreenToCanvasPos(ScreenPos: Screen.safeArea.min, canvasRect: canvasRect), // Set the max postion max = AplicationFuncs.ScreenToCanvasPos(ScreenPos: Screen.safeArea.max, canvasRect: canvasRect) }); }
///<Summary> /// Init Singleton instance ///</Summary> public void InitSingleton() { // Set as enable this.IsEnable = true; // calculate the canvas proportion this.inputCanvasRect = this.GetComponent <RectTransform>(); this.SafeZoneFixedToCanvas = AplicationFuncs.SafeToSafeCanvas(canvasRect: inputCanvasRect.rect); Debug.Log(SafeZoneFixedToCanvas); // calculate the area for joyDetection JoyDetectionArea = new Rect(SafeZoneFixedToCanvas.xMin, SafeZoneFixedToCanvas.yMin, SafeZoneFixedToCanvas.width * ScreenPercentDetect, SafeZoneFixedToCanvas.height); // get the joyStick REffs, knowing that has a child that is a joyBack and a child of that been the knob // back part of the joy this.joyBackImg = this.transform.GetChild(0).GetComponent <RawImage>(); this.joyBackRect = this.joyBackImg.GetComponent <RectTransform>(); // knoob part of the joy this.JoyKnobImg = this.joyBackImg.transform.GetChild(0).GetComponent <RawImage>(); this.joyKnobRect = this.JoyKnobImg.GetComponent <RectTransform>(); // Calculate the maximus radius for the joy based on size this.JoyMaxRadius = this.joyBackRect.rect.width * 0.5f - this.joyKnobRect.rect.width * 0.5f; // calculate the default related position, validating the values making joy visivel on safe area all the time this.RelatedDefaultPosition = new Vector2( JoyDetectionArea.position.x + ((JoyDetectionArea.width * positionPercent.x) < (joyBackRect.rect.width / 2f) ? (joyBackRect.rect.width / 2f) : (JoyDetectionArea.width * positionPercent.x)), JoyDetectionArea.position.y + ((JoyDetectionArea.height * positionPercent.y) < (joyBackRect.rect.height / 2f) ? (joyBackRect.rect.height / 2f) : (JoyDetectionArea.height * positionPercent.y))); // get refference to the jump button components this.jumpBtnImg = this.transform.GetChild(1).GetComponent <Image>(); this.jumpBtn = this.jumpBtnImg.GetComponent <Button>(); this.jumpBtnRct = this.jumpBtnImg.GetComponent <RectTransform>(); // calculate the jump button position based on margin and placed in safe area this.jumpBtnRct.anchoredPosition = new Vector2( SafeZoneFixedToCanvas.xMax - (SafeZoneFixedToCanvas.width * JumpPercentMargin.x + jumpBtnRct.rect.width * 0.5f), SafeZoneFixedToCanvas.yMin + SafeZoneFixedToCanvas.height * JumpPercentMargin.y + jumpBtnRct.rect.height * 0.5f); // Set joy to invisible IsJoyStickVisible = false; // Debug thats ready InformationPanel.DebugConsoleInput("Input Manager valid and Online!"); }
/// <summary> /// Collect data from the main objects, get the joint and check if have a valid target /// </summary> public void InitParts() { // Setting to the stangin pos IsStunted = false; // stores the reference for the standoUp Joint StandJoint = GetComponent <ConfigurableJoint>(); StandJoint.connectedMassScale = BaseMassScale; // ref to the main rigidbody mainRB = GetComponent <Rigidbody>(); // change the current gravity mainRB.useGravity = false; // set the character mass mainRB.mass = CharacterMass; // store a reference to the capsule colide this.targetSphereCollider = this.GetComponent <CapsuleCollider>(); // evaluate all the elements in list foreach (var bPart in bodyParts) { // evaluate if the main go exist if (bPart.partGO) { // checks if has a configurable joint if (bPart.partGO.GetComponent <ConfigurableJoint>()) { // stores the ref for the joint bPart.partJoint = bPart.partGO.GetComponent <ConfigurableJoint>(); // evaluate the target GO if (!bPart.partTarget) { // if not setted properly, debug and close Debug.LogError($"GO -> {bPart.partGO.name} doesnt have a valid target!"); // close aplication AplicationFuncs.CloseApp(); } } else { //if fails and its in editor, stop and debug Debug.LogError($"GO -> {bPart.partGO.name} doesnt have a Configurable joint!"); // close aplication AplicationFuncs.CloseApp(); } } } }
/// <summary> /// initial setup of this controller /// </summary> private void InitController() { // TO REMOVE - - - - - Application.targetFrameRate = 140; QualitySettings.vSyncCount = 0; // Get the reff for the physics controller physicsCharacter = GetComponentInChildren <PhysicsCharacterController>(); // get the ref of the target animator playerTargetAnimator = GetComponentInChildren <Animator>(); // Validate the reff // if fails to find the physics controller if (!physicsCharacter || !playerTargetAnimator) { // Close the aplication AplicationFuncs.CloseApp(); return; } // Debug information and initialize the parts InformationPanel.DebugConsoleInput("Physics System connected!"); physicsCharacter.InitParts(); // Define the maximum velocity physicsCharacter.SetCharacterMaxSpeed(characterMaxVelocity); // define the kill speed physicsCharacter.SetCharacterKillVelocity(this.killVelocity); // Stunted - - - - - // regist the for the stunted event physicsCharacter.stuntedDelegate += StuntedCallback; // init vars stunted = false; // Grab controller - - - - - // get ref to controller this.playerGrabController = this.GetComponentInChildren <GrabController>(); // Init with enable tag this.playerGrabController.Init(controllerState: true, lHand: ref LeftHandGO, rHand: ref RightHandGO); // Regists for jump delegate InputManager.Instance.jumpDelegate += () => { physicsCharacter.Jump(this.JumpValue); }; // regist the controller to the action event InputManager.Instance.actionDelegate += playerGrabController.ActionCallback; }
/// <summary> /// Evaluates, draw and calculte the input on screen /// </summary> private void OnScreenController() { // Check if exists toutchs or the joysitck is desable if (Input.touchCount == 0f) { return; } // Evaluate all the touches foreach (var touch in Input.touches) { // check if exist a toutch beeing tracked if (this.IsJoyStickVisible && touch.fingerId == ValidJoyTouchID) { // if the touch ended if (touch.phase == TouchPhase.Ended) {// set the joy invisible IsJoyStickVisible = false; // jump to the next touch break; } // if the joy is visible, a toutch is been tracked and not ended, update the // calculate the delta vector // clamp the maximu magnitude to the maximun calculated radius var tempDeltaDirection = Vector2.ClampMagnitude((touch.position - this.StartPosition), this.JoyMaxRadius); // Set the knob positio base on the calculated delta joyKnobRect.anchoredPosition = (this.joyBackRect.rect.size * 0.5f) + tempDeltaDirection; // Determinate the amount of input the joy makes (-1 to 1 scale) this.InputVector.Set(tempDeltaDirection.x / this.JoyMaxRadius, tempDeltaDirection.y / this.JoyMaxRadius); } // if is inside of the joy Detection area and is a new touch else if (JoyDetectionArea.Contains(AplicationFuncs.ScreenToCanvasPos(touch.position, inputCanvasRect.rect)) && touch.phase == TouchPhase.Began) { // set the joy to visible IsJoyStickVisible = true; // Store the information of this new valid touch this.ValidJoyTouchID = touch.fingerId; this.StartPosition = touch.position; // place the joy on the touch position this.joyBackRect.anchoredPosition = AplicationFuncs.ScreenToCanvasPos(touch.position, inputCanvasRect.rect); } } }