using System.Collections; using System.Collections.Generic; using Enums; using UnityEngine; //This is a SINGULAR gem public class SC_Gem : MonoBehaviour { [HideInInspector] public Vector2Int posIndex; private Vector2 firstTouchPosition; private Vector2 finalTouchPosition; private bool mousePressed; private float swipeAngle = 0; private SC_Gem otherGem; public GemType type; public bool isMatch = false; private Vector2Int previousPos; public GameObject destroyEffect; public int scoreValue = 10; public int blastSize = 1; private SC_GameLogic scGameLogic; void Update() { //if the current position doesnt match the index in GameBoard, animate it to move to the correct position //else, we update the GameBoard with the current possition, do we need this every frame???? if (Vector2.Distance(this.transform.position, this.posIndex) > 0.01f) this.transform.position = Vector2.Lerp(this.transform.position, this.posIndex, SC_GameVariables.Instance.gemSpeed * Time.deltaTime); else { this.transform.position = new Vector3(this.posIndex.x, this.posIndex.y, 0); this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this); } if (this.mousePressed && Input.GetMouseButtonUp(0)) { this.mousePressed = false; if (this.scGameLogic.CurrentState == GameState.Move) { this.finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); CalculateAngle(); } } } public void SetupGem(SC_GameLogic _ScGameLogic,Vector2Int _Position) { this.posIndex = _Position; this.scGameLogic = _ScGameLogic; } //Not every gem needs this private void OnMouseDown() { if (this.scGameLogic.CurrentState == GameState.Move) { this.firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); this.mousePressed = true; } } //Not every gem needs this private void CalculateAngle() { this.swipeAngle = Mathf.Atan2(this.finalTouchPosition.y - this.firstTouchPosition.y, this.finalTouchPosition.x - this.firstTouchPosition.x); this.swipeAngle = this.swipeAngle * 180 / Mathf.PI; if (Vector3.Distance(this.firstTouchPosition, this.finalTouchPosition) > .5f) MovePieces(); } //Not every gem needs this, maybe private void MovePieces() { this.previousPos = this.posIndex; if (this.swipeAngle < 45 && this.swipeAngle > -45 && this.posIndex.x < SC_GameVariables.Instance.rowsSize - 1) { this.otherGem = this.scGameLogic.GetGem(this.posIndex.x + 1, this.posIndex.y); this.otherGem.posIndex.x--; this.posIndex.x++; } else if (this.swipeAngle > 45 && this.swipeAngle <= 135 && this.posIndex.y < SC_GameVariables.Instance.colsSize - 1) { this.otherGem = this.scGameLogic.GetGem(this.posIndex.x, this.posIndex.y + 1); this.otherGem.posIndex.y--; this.posIndex.y++; } else if (this.swipeAngle < -45 && this.swipeAngle >= -135 && this.posIndex.y > 0) { this.otherGem = this.scGameLogic.GetGem(this.posIndex.x, this.posIndex.y - 1); this.otherGem.posIndex.y++; this.posIndex.y--; } else if (this.swipeAngle > 135 || this.swipeAngle < -135 && this.posIndex.x > 0) { this.otherGem = this.scGameLogic.GetGem(this.posIndex.x - 1, this.posIndex.y); this.otherGem.posIndex.x++; this.posIndex.x--; } this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this); this.scGameLogic.SetGem(this.otherGem.posIndex.x, this.otherGem.posIndex.y, this.otherGem); StartCoroutine(CheckMoveCo()); } //Why are we checking matches on the Gem itself public IEnumerator CheckMoveCo() { this.scGameLogic.SetState(GameState.Wait); yield return new WaitForSeconds(.5f); this.scGameLogic.FindAllMatches(); if (this.otherGem != null) { if (this.isMatch == false && this.otherGem.isMatch == false) { this.otherGem.posIndex = this.posIndex; this.posIndex = this.previousPos; this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this); this.scGameLogic.SetGem(this.otherGem.posIndex.x, this.otherGem.posIndex.y, this.otherGem); yield return new WaitForSeconds(.5f); this.scGameLogic.SetState(GameState.Move); } else { this.scGameLogic.DestroyMatches(); } } } }