public void SwapGems(Gem selectedGem) { if (selectedGem.gemStatus != Data.GemStatus.Ready) { return; } selectedGem.ToggleSelector(); if (m_currentGem == null) { m_currentGem = selectedGem; } else if (m_currentGem == selectedGem) { m_currentGem = null; } else if (!m_currentGem.IsAdjacent(selectedGem)) { m_currentGem.ToggleSelector(); m_currentGem = selectedGem; } else { m_currentGem.gemStatus = Data.GemStatus.Moving; selectedGem.gemStatus = Data.GemStatus.Moving; Swap(m_currentGem, selectedGem); m_currentGem.ToggleSelector(); selectedGem.ToggleSelector(); m_currentGem = null; } }
public bool IsAdjacent(Gem otherGem) { if (otherGem != null) { if (this.x == otherGem.x) { if (this.y == otherGem.y + 1 || this.y == otherGem.y - 1) return true; } if (this.y == otherGem.y) { if (this.x == otherGem.x + 1 || this.x == otherGem.x - 1) return true; } } return false; }
private void Swap(Gem gem1, Gem gem2) { //Swap X, Y m_gems[gem1.x, gem1.y] = gem2; m_gems[gem2.x, gem2.y] = gem1; int tempX = gem1.x; int tempY = gem1.y; gem1.x = gem2.x; gem1.y = gem2.y; gem2.x = tempX; gem2.y = tempY; //Swap position Vector3 gem1Position = gem1.transform.position; Vector3 gem2Position = gem2.transform.position; StartCoroutine(MoveGem(gem1, gem1Position, gem2Position)); StartCoroutine(MoveGem(gem2, gem2Position, gem1Position, CheckBoard)); }
IEnumerator MoveGem(Gem gem, Vector3 from, Vector3 to, Action callback = null) { gem.gemStatus = Data.GemStatus.Moving; float time = Time.time; while (Vector3.Distance(gem.transform.position, to) > 0.01f) { gem.transform.position = Vector3.Lerp(from, to, (Time.time - time) * 1.2f); yield return null; } gem.transform.position = to; gem.gemStatus = Data.GemStatus.Ready; if (callback != null) { callback.Invoke(); } }
public bool IsMatch(Gem otherGem) { return gemType == otherGem.gemType; }