private RaycastHit GetMeshHit(Vector3 touchPosition) { int layerMask = 1 << LayerMask.NameToLayer("UI"); if (!Physics.Raycast(guiCam.ScreenPointToRay(touchPosition), out var hitInfo, float.PositiveInfinity, layerMask)) { return(hitInfo); } Renderer component = hitInfo.transform.GetComponent <Renderer>(); MeshCollider meshCollider = hitInfo.collider as MeshCollider; JigsawPiece component2 = hitInfo.collider.gameObject.GetComponent <JigsawPiece>(); if (component2 == null || component == null || meshCollider == null) { return(default(RaycastHit)); } return(hitInfo); }
private IEnumerator checkIfPuzzleSolved(JigsawPiece scriptObj, float delayTime) { yield return(new WaitForSeconds(delayTime)); solveCount++; if (solveCount >= pieceCount) { scriptObj.PixelMouseExit(); isSolved = true; dispatcher.DispatchEvent(default(JigsawEvents.AllPiecesSolved)); if (string.IsNullOrEmpty(switchTo)) { playAudioEvent(audioPuzzleSolved); } else { audioSetSwitchEvent(audioPuzzleSolved, switchTo, switchGameObject); } } }
private RaycastHit GetPixelHit(Vector3 touchPosition) { GameObject gameObject = null; RaycastHit result = default(RaycastHit); int layerMask = 1 << LayerMask.NameToLayer("UI"); int num = Physics.RaycastNonAlloc(guiCam.ScreenPointToRay(touchPosition), raycastHits, float.PositiveInfinity, layerMask); if (num == 0) { return(result); } for (int i = 0; i < num; i++) { RaycastHit raycastHit = raycastHits[i]; Renderer component = raycastHit.transform.GetComponent <Renderer>(); MeshCollider meshCollider = raycastHit.collider as MeshCollider; JigsawPiece component2 = raycastHit.collider.gameObject.GetComponent <JigsawPiece>(); if (component2 == null || component == null || component.sharedMaterial == null || component.sharedMaterial.mainTexture == null || meshCollider == null) { continue; } Texture2D texture2D = component.material.mainTexture as Texture2D; Vector2 textureCoord = raycastHit.textureCoord; textureCoord.x *= texture2D.width; textureCoord.y *= texture2D.height; try { float a = texture2D.GetPixel((int)textureCoord.x, (int)textureCoord.y).a; if (a > 0.5f && (gameObject == null || raycastHit.transform.gameObject.transform.position.z < gameObject.transform.position.z)) { gameObject = raycastHit.transform.gameObject; result = raycastHit; } } catch (UnityException) { } } return(result); }
private void Update() { if (!hasInitalizedPuzzle || isSolved) { return; } Vector3 touchPosition; RaycastHit hitInfo; Vector3 vector; switch (getTouchPhase(out touchPosition)) { case TouchPhaseExtended.Began: oldPos = touchPosition; if (IsMeshPuzzle) { focusHit = GetMeshHit(touchPosition); } else { focusHit = GetPixelHit(touchPosition); } if (focusHit.collider != null) { focusObj = focusHit.collider.gameObject; } else { focusObj = null; } if (focusObj != oldFocus) { if (oldFocus != null) { JigsawPiece component = oldFocus.GetComponent <JigsawPiece>(); component.PixelMouseExit(); } if (focusObj != null) { JigsawPiece component = focusObj.GetComponent <JigsawPiece>(); component.PixelMouseOver(); } oldFocus = focusObj; } if (focusObj != null && bkgArtworkColl.Raycast(guiCam.ScreenPointToRay(touchPosition), out hitInfo, 500f)) { pickupOffset = focusObj.transform.position - hitInfo.point; pickupOffset.z = 0f; LayerToTop(focusObj.name); } break; case TouchPhaseExtended.Moved: if (focusObj != null && bkgArtworkColl.Raycast(guiCam.ScreenPointToRay(touchPosition), out hitInfo, 500f)) { JigsawPiece component = focusObj.GetComponent <JigsawPiece>(); vector = hitInfo.point + pickupOffset + shadowLiftOffset; vector.z = 0f; if (hitInfo.point.y > areaDividerObj.transform.position.y) { focusObj.transform.SetParent(solveContainer, worldPositionStays: false); } else { focusObj.transform.SetParent(startContainer, worldPositionStays: false); } if (vector.x < xMin) { vector.x = xMin; } else if (vector.x > xMax) { vector.x = xMax; } if (vector.y < yMin) { vector.y = yMin; } else if (vector.y > yMax) { vector.y = yMax; } component.PixelMouseDrag(vector); } break; case TouchPhaseExtended.Ended: { if (!(focusObj != null) || !SolvePositions.ContainsKey(focusObj.name)) { break; } Vector3 vector2 = SolvePositions[focusObj.name]; vector2.z = 0f; float num = Vector2.Distance(focusObj.transform.position, vector2); JigsawPiece component = focusObj.GetComponent <JigsawPiece>(); if (num <= SnapDistance + shadowDistanceAdjust + snapModifier) { snapModifier -= snapDecay; if (snapModifier < 0f) { snapModifier = 0f; } component.Lock(vector2); playAudioEvent(audioPuzzlePieceDrop); LayerToLocked(focusObj.name); if (ParticlesLockPiece != null) { GameObject gameObject = Object.Instantiate(ParticlesLockPiece, PieceParticlePosition, Quaternion.identity); gameObject.transform.SetParent(focusObj.transform, worldPositionStays: false); gameObject.transform.localScale = PieceParticleScale; gameObject.layer = LayerMask.NameToLayer("UI"); if (ParticlesLockPiece != null) { ParticleSystem component2 = ParticlesLockPiece.GetComponent <ParticleSystem>(); if (component2 != null) { solveEffectsDelay = component2.main.duration; } } } CoroutineRunner.Start(checkIfPuzzleSolved(component, solveEffectsDelay), this, "checkIfPuzzleSolved"); } else { vector = focusObj.transform.position; component = focusObj.GetComponent <JigsawPiece>(); component.PixelMouseDrop(vector - shadowLiftOffset); } break; } case TouchPhaseExtended.Stationary: case TouchPhaseExtended.Canceled: break; case TouchPhaseExtended.Mouse: if (oldPos == touchPosition) { break; } oldPos = touchPosition; if (IsMeshPuzzle) { focusHit = GetMeshHit(touchPosition); } else { focusHit = GetPixelHit(touchPosition); } if (focusHit.collider != null) { if (IsMeshPuzzle) { focusObj = focusHit.collider.gameObject; } else { focusObj = focusHit.collider.gameObject; } } else { focusObj = null; } if (!(focusObj != oldFocus)) { break; } if (oldFocus != null) { JigsawPiece component = oldFocus.GetComponent <JigsawPiece>(); if (component != null) { component.PixelMouseExit(); } } if (focusObj != null) { JigsawPiece component = focusObj.GetComponent <JigsawPiece>(); if (component != null) { component.PixelMouseOver(); } } oldFocus = focusObj; break; case TouchPhaseExtended.NoEvent: break; } }
public void PuzzleInit() { dispatcher.DispatchEvent(new UIDisablerEvents.DisableUIElement("Joystick")); rectTransform = GetComponent <RectTransform>(); rectTransform.localPosition = new Vector3(0f, 0f, -100f); focusObj = null; hasInitalizedPuzzle = false; isSolved = false; pieceCount = 0; solveCount = 0; snapModifier = 1f; snapDecay = snapModifier / (float)snapAssistDuration; shadowLiftOffset = new Vector3(0f - shadowOffset.x, 0f - shadowOffset.y, 0f) * 0.1f; shadowDistanceAdjust = Vector2.Distance(Vector2.zero, shadowLiftOffset) * 0.5f; SolvePositions.Clear(); StartPositions.Clear(); bkgArtworkObj = base.gameObject.transform.Find("Background/Bkg artwork").gameObject; if (bkgArtworkObj == null) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- could not find background artwork"); return; } bkgArtworkColl = bkgArtworkObj.GetComponent <Collider>(); if (bkgArtworkColl == null) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- could not find collider on background artwork"); return; } solveContainer = base.gameObject.transform.Find("Solve Positions"); if (solveContainer == null) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- could not find solve container"); } else { foreach (Transform item in solveContainer) { GameObject gameObject = item.transform.gameObject; if (SolvePositions.ContainsKey(gameObject.name)) { Log.LogError(null, $"O_o\t JigsawController.Start: Found duplicate puzzle piece '{gameObject.name}'. Please make them unique"); return; } SolvePositions.Add(gameObject.name, gameObject.transform.position); Object.Destroy(gameObject); } } startContainer = base.gameObject.transform.Find("Start Positions"); if (startContainer == null) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- could not find startContainer object"); return; } foreach (Transform item2 in startContainer) { GameObject gameObject = item2.transform.gameObject; if (StartPositions.ContainsKey(gameObject.name)) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- Found duplicate puzzle piece named {gameObject.name}'. Please make them unique"); return; } StartPositions.Add(gameObject.name, gameObject.transform.position); GameObject gameObject2 = null; if (!IsMeshPuzzle) { gameObject2 = Object.Instantiate(gameObject, Vector3.zero, Quaternion.identity); JigsawPiece component = gameObject2.GetComponent <JigsawPiece>(); if (component != null) { Object.Destroy(component); } } JigsawPiece jigsawPiece = gameObject.AddComponent <JigsawPiece>(); if (!IsMeshPuzzle) { gameObject2.transform.SetParent(gameObject.transform, worldPositionStays: false); gameObject2.transform.localScale = Vector3.one; gameObject2.transform.localPosition = new Vector3(shadowOffset.x * solveContainer.localScale.x * shadowScaleAdjustment, shadowOffset.y * solveContainer.localScale.y * shadowScaleAdjustment, 0.5f); Renderer component2 = gameObject2.GetComponent <Renderer>(); if (component2.material.HasProperty("_Color")) { component2.material.color = ShadowColor; } Collider component3 = gameObject2.GetComponent <Collider>(); if (component3 != null) { Object.Destroy(component3); } gameObject2.name = $"Shadow ({jigsawPiece.Id})"; } float num = minigameTweenTime * 0.95f; jigsawPiece.Init(MouseoverColor, SuccessColor, TweenTimeMin, TweenTimeMax, DelayMin + num, DelayMax + num, PieceTweenTime, PieceDelay, easeType, pieceEaseType); if (gameObject.GetComponent <Collider>() == null) { gameObject.AddComponent <MeshCollider>(); } if (gameObject.GetComponent <Rigidbody>() == null) { Rigidbody rigidbody = gameObject.AddComponent <Rigidbody>(); rigidbody.isKinematic = true; } gameObject.GetComponent <JigsawPiece>().Appear(); layers.Add(gameObject.name); layerObjects.Add(gameObject.name, gameObject); } areaDividerObj = base.gameObject.transform.Find("Background/Area Divider").gameObject; if (areaDividerObj == null) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- could not find area divider"); return; } areaDividerObj.SetActive(value: false); completedArtworkObj = base.gameObject.transform.Find("Completed/Completed Artwork").gameObject; if (completedArtworkObj == null) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- could not find completed artwork"); return; } completedArtworkObj.SetActive(value: true); JigsawBackground jigsawBackground = completedArtworkObj.AddComponent <JigsawBackground>(); jigsawBackground.Init(SuccessColor, ParticlesSolvePuzzle, ArtworkTweenTime, ArtworkSolveDelay, ArtworkParticlePosition, ArtworkParticleScale, solveEaseType); completedArtworkObj.SetActive(value: false); if (StartPositions.Count != SolvePositions.Count) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- the number of start positions and solve positions must match ({StartPositions.Count} != {SolvePositions.Count})"); return; } pieceCount = StartPositions.Count; guiCam = GameObject.FindGameObjectWithTag(UIConstants.Tags.UI_HUD).GetComponentInChildren <Camera>(); if (guiCam == null) { Log.LogError(null, $"O_o\t JigsawController.Start: ERROR -- Can't find the GUI camera"); return; } CalculateScreenExtents(); hasInitalizedPuzzle = true; }