// ---------------- public void ControlByTouch() { if (this.touchCtrl == null) { return; } // Get controls' references... TouchStick stickWalk = this.touchCtrl.GetStick(DemoFppGameCS.STICK_WALK); TouchZone zoneAim = this.touchCtrl.GetZone(DemoFppGameCS.ZONE_AIM); TouchZone zoneFire = this.touchCtrl.GetZone(DemoFppGameCS.ZONE_FIRE); TouchZone zoneZoom = this.touchCtrl.GetZone(DemoFppGameCS.ZONE_ZOOM); TouchZone zoneReload = this.touchCtrl.GetZone(DemoFppGameCS.ZONE_RELOAD); // If Walk stick is pressed... if (stickWalk.Pressed()) { // ... use it's unnormalized direction vector to control walking. Vector2 moveVec = stickWalk.GetVec(); this.SetWalkSpeed(moveVec.y, moveVec.x); } // Stop walking when stick is released... else { this.SetWalkSpeed(0, 0); } // Firing... // Set weapons trigger by getting the Fire zone pressed state // (include mid-frame press) this.SetTriggerState(zoneFire.UniPressed(true, false)); // Reload on Reload zone press (including mid-frame press and release situations). if (zoneReload.JustUniPressed(true, true)) { this.ReloadWeapon(); } // Toggle zoom mode, when tapped on zoom-zone... if (zoneZoom.JustTapped()) { this.zoomActive = !this.zoomActive; } // Zoom dragging... if (this.zoomActive && zoneZoom.UniPressed(false, false)) { // Store inital zoom factor... if (!this.isZoomDragging) { this.isZoomDragging = true; this.zoomFactorRaw = this.zoomFactor; } // Change zoom factor by RAW vertical unified-touch drag delta // queried in centimeters and scaled... this.zoomFactorRaw += (this.zoomFactorPerCm * zoneZoom.GetUniDragDelta(TouchCoordSys.SCREEN_CM, true).y); this.zoomFactor = Mathf.Clamp(this.zoomFactorRaw, 0, 1); } else { this.isZoomDragging = false; } // Aim, when either aim or fire zone if pressed by at least one finger // (ignoring mid-frame presses and releases)... if (zoneAim.UniPressed(false, false) || zoneFire.UniPressed(false, false)) { // If just started aiming, store initial aim angles... if (!this.isAiming) { this.isAiming = true; this.aimVertRaw = this.aimVert; this.aimHorzRaw = this.aimHorz; } // Get aim delta adding aim-zone and fire-zone's // unified-touch RAW drag deltas in centimeters... Vector2 aimDelta = zoneAim.GetUniDragDelta(TouchCoordSys.SCREEN_CM, true) + zoneFire.GetUniDragDelta(TouchCoordSys.SCREEN_CM, true); // Apply aim-sensitivity and speed... aimDelta *= Mathf.Lerp(0.1f, 1.0f, this.aimSensitivity); aimDelta.x *= this.horzAimSpeed; aimDelta.y *= this.vertAimSpeed; // Add calculated delta to current our raw, non-clamped aim angles // and pass them to Aim() function. // By keeping separate, non-clamped angles we prevent that // uncomfortable effect when dragging past the limit and back again. this.aimHorzRaw += aimDelta.x; this.aimVertRaw += aimDelta.y; // this.Aim(this.aimHorzRaw, this.aimVertRaw); } else { this.isAiming = false; } // When double tapped the aim zone - level horizonal aim angle... if (zoneAim.JustDoubleTapped()) { this.Aim(this.aimHorz, 0); } }
// --------------- private void Update() { // If controller's layout changed, reset menu's width... if ((this.ctrl != null) && this.ctrl.LayoutChanged()) { this.menu.SetWindowSize(Screen.width, this.ctrl.GetDPI()); } // Fade-in phase... if (this.fadeInTimer.Enabled) { this.fadeInTimer.Update(Time.deltaTime); if (this.fadeInTimer.Completed) { this.fadeInTimer.Disable(); } } // Control... else { if (Input.GetKeyUp(KeyCode.Escape)) { Application.LoadLevel(EXIT_SCREEN_SCENE_NAME); return; //Application.Quit(); } // Get first and the only one touch zone (no need for IDs)... TouchZone zone = this.ctrl.GetZone(0); // Handle tap... if (zone.JustTapped()) { this.menu.OnTap(); } // Handle unified-touch press... if (zone.JustUniPressed()) { this.menu.OnPress(); } // If unified-touch moved, use it's drag delta... if (zone.UniDragged()) { this.menu.Move(-zone.GetUniDragDelta(TouchCoordSys.SCREEN_PX).x); } // On unfied-touch release, get released drag velocity to detect those quick swipes... else if (zone.JustUniReleased()) { this.menu.OnRelease(-zone.GetReleasedUniDragVel().x); } } // If swipe-menu completed - go to selected mode... this.menu.UpdateMenu(); if (this.menu.JustCompleted()) { switch (this.menu.GetCurItem()) { case ITEM_FPP: this.StartDemo(FPP_DEMO_SCENE_NAME); return; case ITEM_RPG: this.StartDemo(RPG_DEMO_SCENE_NAME); return; case ITEM_MAP: this.StartDemo(MAP_DEMO_SCENE_NAME); return; case ITEM_DSS: this.StartDemo(DUAL_STICK_DEMO_SCENE_NAME); return; } } }
// ---------------------- // Update() // ---------------------- void Update() { if (this.ctrl != null) { // ---------------------- // Stick and Zone Variables // ---------------------- TouchStick walkStick = this.ctrl.GetStick(STICK_WALK); TouchZone actionZone = this.ctrl.GetZone(ZONE_ACTION); TouchZone screenZone = this.ctrl.GetZone(ZONE_SCREEN); // ---------------------- // Input Handling Code // ---------------------- // ---------------- // Stick 'Walk'... // ---------------- if (walkStick.Pressed()) { Vector2 walkVec = walkStick.GetVec(); float walkTilt = walkStick.GetTilt(); float walkAngle = walkStick.GetAngle(); TouchStick.StickDir walkDir = walkStick.GetDigitalDir(true); Vector3 walkWorldVec = walkStick.GetVec3d(false, 0); // Your code here. } // ---------------- // Zone 'Action'... // ---------------- if (actionZone.JustUniPressed()) { Vector2 actionPressStartPos = actionZone.GetUniStartPos(TouchCoordSys.SCREEN_PX); } // Uni-touch pressed... if (actionZone.UniPressed()) { float actionUniDur = actionZone.GetMultiTouchDuration(); Vector2 actionUniPos = actionZone.GetMultiPos(); Vector2 actionUniDragDelta = actionZone.GetMultiDragDelta(); Vector2 actionUniRawDrawDelta = actionZone.GetMultiDragDelta(true); } // Uni-Touch Just Released if (actionZone.JustUniReleased()) { Vector2 actionUniRelStartPos = actionZone.GetReleasedUniStartPos(); Vector2 actionUniRelEndPos = actionZone.GetReleasedUniEndPos(); int actionUniRelStartBox = TouchZone.GetBoxPortion(2, 2, actionZone.GetReleasedUniStartPos(TouchCoordSys.SCREEN_NORMALIZED)); int actionUniRelEndBox = TouchZone.GetBoxPortion(2, 2, actionZone.GetReleasedUniEndPos(TouchCoordSys.SCREEN_NORMALIZED)); Vector2 actionUniRelDragVel = actionZone.GetReleasedUniDragVel(); Vector2 actionUniRelDragVec = actionZone.GetReleasedUniDragVec(); } // ---------------- // Zone 'SCREEN'... // ---------------- if (screenZone.JustMultiPressed()) { Vector2 screenMultiPressStartPos = screenZone.GetMultiStartPos(TouchCoordSys.SCREEN_PX); } if (screenZone.JustUniPressed()) { Vector2 screenPressStartPos = screenZone.GetUniStartPos(TouchCoordSys.SCREEN_PX); } // Multi-Pressed... if (screenZone.MultiPressed()) { float screenMultiDur = screenZone.GetMultiTouchDuration(); Vector2 screenMultiPos = screenZone.GetMultiPos(); Vector2 screenMultiDragDelta = screenZone.GetMultiDragDelta(); Vector2 screenMultiRawDrawDelta = screenZone.GetMultiDragDelta(true); // Multi-touch drag... if (screenZone.JustMultiDragged()) { } if (screenZone.MultiDragged()) { } // Just Twisted... if (screenZone.JustTwisted()) { } // Twisted... if (screenZone.Twisted()) { float screenTwistDelta = screenZone.GetTwistDelta(); float screenTwistTotal = screenZone.GetTotalTwist(); } // Just Pinched... if (screenZone.JustPinched()) { } // Pinched... if (screenZone.Pinched()) { float screenPinchRelScale = screenZone.GetPinchRelativeScale(); float screenPinchAbsScale = screenZone.GetPinchScale(); float screenFingerDist = screenZone.GetPinchDist(); } } // Uni-touch pressed... if (screenZone.UniPressed()) { float screenUniDur = screenZone.GetMultiTouchDuration(); Vector2 screenUniPos = screenZone.GetMultiPos(); Vector2 screenUniDragDelta = screenZone.GetMultiDragDelta(); Vector2 screenUniRawDrawDelta = screenZone.GetMultiDragDelta(true); // Just Uni-touch dragged... if (screenZone.JustUniDragged()) { } // Uni-Touch drag... if (screenZone.UniDragged()) { } } // Multi-Touch Just Released if (screenZone.JustMultiReleased()) { Vector2 screenMultiRelStartPos = screenZone.GetReleasedMultiStartPos(); Vector2 screenMultiRelEndPos = screenZone.GetReleasedMultiEndPos(); int screenMultiRelStartBox = TouchZone.GetBoxPortion(2, 2, screenZone.GetReleasedMultiStartPos(TouchCoordSys.SCREEN_NORMALIZED)); int screenMultiRelEndBox = TouchZone.GetBoxPortion(2, 2, screenZone.GetReleasedMultiEndPos(TouchCoordSys.SCREEN_NORMALIZED)); Vector2 screenMultiRelDragVel = screenZone.GetReleasedMultiDragVel(); Vector2 screenMultiRelDragVec = screenZone.GetReleasedMultiDragVec(); // Released multi-touch was dragged... if (screenZone.ReleasedMultiDragged()) { } // Released multi-touch was twisted... if (screenZone.ReleasedTwisted()) { float screenRelTwistAngle = screenZone.GetReleasedTwistAngle(); float screenRelTwistVel = screenZone.GetReleasedTwistVel(); } // Released multi-touch was pinched... if (screenZone.ReleasedPinched()) { float screenRelPinchStartDist = screenZone.GetReleasedPinchStartDist(); float screenRelPinchEndDist = screenZone.GetReleasedPinchEndDist(); float screenRelPinchScale = screenZone.GetReleasedPinchScale(); } } // Uni-Touch Just Released if (screenZone.JustUniReleased()) { Vector2 screenUniRelStartPos = screenZone.GetReleasedUniStartPos(); Vector2 screenUniRelEndPos = screenZone.GetReleasedUniEndPos(); int screenUniRelStartBox = TouchZone.GetBoxPortion(2, 2, screenZone.GetReleasedUniStartPos(TouchCoordSys.SCREEN_NORMALIZED)); int screenUniRelEndBox = TouchZone.GetBoxPortion(2, 2, screenZone.GetReleasedUniEndPos(TouchCoordSys.SCREEN_NORMALIZED)); Vector2 screenUniRelDragVel = screenZone.GetReleasedUniDragVel(); Vector2 screenUniRelDragVec = screenZone.GetReleasedUniDragVec(); // Released uni-touch was dragged... if (screenZone.ReleasedUniDragged()) { } } // Double multi-finger tap... if (screenZone.JustMultiDoubleTapped()) { Vector2 screenMultiDblTapPos = screenZone.GetMultiTapPos(); } else // Simple multi-finger tap... if (screenZone.JustMultiTapped()) { Vector2 screenMultiTapPos = screenZone.GetMultiTapPos(); } else // Double one-finger tap... if (screenZone.JustDoubleTapped()) { Vector2 screenDblTapPos = screenZone.GetTapPos(); } else // Simple one-finger tap... if (screenZone.JustTapped()) { Vector2 screenTapPos = screenZone.GetTapPos(); } } }
// -------------------- public void ControlByTouch(TouchController ctrl, DemoRpgGameCS game) { TouchStick stickWalk = ctrl.GetStick(DemoRpgGameCS.STICK_WALK); TouchZone zoneScreen = ctrl.GetZone(DemoRpgGameCS.ZONE_SCREEN), zoneAction = ctrl.GetZone(DemoRpgGameCS.ZONE_ACTION), zoneFire = ctrl.GetZone(DemoRpgGameCS.ZONE_FIRE); // Get stick's normalized direction in world space relative to camera's angle... Vector3 moveWorldDir = stickWalk.GetVec3d(TouchStick.Vec3DMode.XZ, true, game.camOrbitalAngle); // Get stick's angle in world space by adding it to camera's angle.. float stickWorldAngle = stickWalk.GetAngle() + game.camOrbitalAngle; // Get walking speed from stick's current tilt... float speed = stickWalk.GetTilt(); // Get gun's trigger state by checking Fire zone's state (including mid-frame press) bool gunTriggerState = zoneFire.UniPressed(true, false); // Check if Action should be performed - either by pressing the Action button or // by tapping on the right side of the screen... bool performAction = zoneAction.JustUniPressed(true, true) || zoneScreen.JustTapped(); if ((this.charaState == CharaState.IDLE) || (this.charaState == CharaState.WALK) || (this.charaState == CharaState.RUN)) { if (speed > this.walkStickThreshold) { float moveSpeed = 0; // Walk... if (speed < this.runStickThreshold) { moveSpeed = this.walkSpeed; if (this.charaState != CharaState.WALK) { this.StartWalking(); } // Set animation speed... this.charaAnim[this.ANIM_WALK_F].speed = this.walkAnimSpeed; } // Run! else { moveSpeed = this.runSpeed; if (this.charaState != CharaState.RUN) { this.StartRunning(); } // Set animation speed... this.charaAnim[this.ANIM_RUN_F].speed = this.runAnimSpeed; } // Update player's angle... this.angle = DampAngle(this.angle, stickWorldAngle, this.angleSmoothingTime, this.turnMaxSpeed, Time.deltaTime); // Move player's collider... this.charaCtrl.Move(moveWorldDir * moveSpeed * Time.deltaTime); } else if ((this.charaState == CharaState.WALK) || (this.charaState == CharaState.RUN)) { // Stop... this.StartIdle(); } // Perform Action... if (performAction) { this.PerformUseAction(); } } // Every other state than USE... if (this.charaState != CharaState.USE) { if (this.gun != null) { this.gun.SetTriggerState(gunTriggerState); } if (performAction) { this.PerformUseAction(); } } // USE state updates... else { if (this.gun != null) { this.gun.SetTriggerState(false); } this.useElapsed += Time.deltaTime; if (this.useElapsed > this.useAnimDuration) { this.StartIdle(); } } }