137 lines
4.1 KiB
C#
137 lines
4.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
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 GlobalEnums.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 (Vector2.Distance(transform.position, posIndex) > 0.01f)
|
|
transform.position = Vector2.Lerp(transform.position, posIndex, SC_GameVariables.Instance.gemSpeed * Time.deltaTime);
|
|
else
|
|
{
|
|
transform.position = new Vector3(posIndex.x, posIndex.y, 0);
|
|
scGameLogic.SetGem(posIndex.x, posIndex.y, this);
|
|
}
|
|
if (mousePressed && Input.GetMouseButtonUp(0))
|
|
{
|
|
mousePressed = false;
|
|
if (scGameLogic.CurrentState == GlobalEnums.GameState.move)
|
|
{
|
|
finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
CalculateAngle();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetupGem(SC_GameLogic _ScGameLogic,Vector2Int _Position)
|
|
{
|
|
posIndex = _Position;
|
|
scGameLogic = _ScGameLogic;
|
|
}
|
|
|
|
//Not every gem needs this
|
|
private void OnMouseDown()
|
|
{
|
|
if (scGameLogic.CurrentState == GlobalEnums.GameState.move)
|
|
{
|
|
firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
mousePressed = true;
|
|
}
|
|
}
|
|
|
|
//Not every gem needs this
|
|
private void CalculateAngle()
|
|
{
|
|
swipeAngle = Mathf.Atan2(finalTouchPosition.y - firstTouchPosition.y, finalTouchPosition.x - firstTouchPosition.x);
|
|
swipeAngle = swipeAngle * 180 / Mathf.PI;
|
|
|
|
if (Vector3.Distance(firstTouchPosition, finalTouchPosition) > .5f)
|
|
MovePieces();
|
|
}
|
|
|
|
//Not every gem needs this, maybe
|
|
private void MovePieces()
|
|
{
|
|
previousPos = posIndex;
|
|
|
|
if (swipeAngle < 45 && swipeAngle > -45 && posIndex.x < SC_GameVariables.Instance.rowsSize - 1)
|
|
{
|
|
otherGem = scGameLogic.GetGem(posIndex.x + 1, posIndex.y);
|
|
otherGem.posIndex.x--;
|
|
posIndex.x++;
|
|
|
|
}
|
|
else if (swipeAngle > 45 && swipeAngle <= 135 && posIndex.y < SC_GameVariables.Instance.colsSize - 1)
|
|
{
|
|
otherGem = scGameLogic.GetGem(posIndex.x, posIndex.y + 1);
|
|
otherGem.posIndex.y--;
|
|
posIndex.y++;
|
|
}
|
|
else if (swipeAngle < -45 && swipeAngle >= -135 && posIndex.y > 0)
|
|
{
|
|
otherGem = scGameLogic.GetGem(posIndex.x, posIndex.y - 1);
|
|
otherGem.posIndex.y++;
|
|
posIndex.y--;
|
|
}
|
|
else if (swipeAngle > 135 || swipeAngle < -135 && posIndex.x > 0)
|
|
{
|
|
otherGem = scGameLogic.GetGem(posIndex.x - 1, posIndex.y);
|
|
otherGem.posIndex.x++;
|
|
posIndex.x--;
|
|
}
|
|
|
|
scGameLogic.SetGem(posIndex.x,posIndex.y, this);
|
|
scGameLogic.SetGem(otherGem.posIndex.x, otherGem.posIndex.y, otherGem);
|
|
|
|
StartCoroutine(CheckMoveCo());
|
|
}
|
|
|
|
//Why are we checking matches on the Gem itself
|
|
public IEnumerator CheckMoveCo()
|
|
{
|
|
scGameLogic.SetState(GlobalEnums.GameState.wait);
|
|
|
|
yield return new WaitForSeconds(.5f);
|
|
scGameLogic.FindAllMatches();
|
|
|
|
if (otherGem != null)
|
|
{
|
|
if (isMatch == false && otherGem.isMatch == false)
|
|
{
|
|
otherGem.posIndex = posIndex;
|
|
posIndex = previousPos;
|
|
|
|
scGameLogic.SetGem(posIndex.x, posIndex.y, this);
|
|
scGameLogic.SetGem(otherGem.posIndex.x, otherGem.posIndex.y, otherGem);
|
|
|
|
yield return new WaitForSeconds(.5f);
|
|
scGameLogic.SetState(GlobalEnums.GameState.move);
|
|
}
|
|
else
|
|
{
|
|
scGameLogic.DestroyMatches();
|
|
}
|
|
}
|
|
}
|
|
}
|