Create All Needed Scripts

This commit is contained in:
2025-12-14 09:23:38 +08:00
parent 980b26dd5e
commit 6fe70bd113
53 changed files with 1988 additions and 195 deletions

View File

@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using Enums;
using UnityEngine;
//This is a SINGULAR gem
@@ -14,7 +15,7 @@ public class SC_Gem : MonoBehaviour
private float swipeAngle = 0;
private SC_Gem otherGem;
public GlobalEnums.GemType type;
public GemType type;
public bool isMatch = false;
private Vector2Int previousPos;
public GameObject destroyEffect;
@@ -25,19 +26,22 @@ public class SC_Gem : MonoBehaviour
void Update()
{
if (Vector2.Distance(transform.position, posIndex) > 0.01f)
transform.position = Vector2.Lerp(transform.position, posIndex, SC_GameVariables.Instance.gemSpeed * Time.deltaTime);
//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
{
transform.position = new Vector3(posIndex.x, posIndex.y, 0);
scGameLogic.SetGem(posIndex.x, posIndex.y, this);
this.transform.position = new Vector3(this.posIndex.x, this.posIndex.y, 0);
this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this);
}
if (mousePressed && Input.GetMouseButtonUp(0))
if (this.mousePressed && Input.GetMouseButtonUp(0))
{
mousePressed = false;
if (scGameLogic.CurrentState == GlobalEnums.GameState.move)
this.mousePressed = false;
if (this.scGameLogic.CurrentState == GameState.Move)
{
finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
this.finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
CalculateAngle();
}
}
@@ -45,63 +49,63 @@ public class SC_Gem : MonoBehaviour
public void SetupGem(SC_GameLogic _ScGameLogic,Vector2Int _Position)
{
posIndex = _Position;
scGameLogic = _ScGameLogic;
this.posIndex = _Position;
this.scGameLogic = _ScGameLogic;
}
//Not every gem needs this
private void OnMouseDown()
{
if (scGameLogic.CurrentState == GlobalEnums.GameState.move)
if (this.scGameLogic.CurrentState == GameState.Move)
{
firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePressed = true;
this.firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
this.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;
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(firstTouchPosition, finalTouchPosition) > .5f)
if (Vector3.Distance(this.firstTouchPosition, this.finalTouchPosition) > .5f)
MovePieces();
}
//Not every gem needs this, maybe
private void MovePieces()
{
previousPos = posIndex;
this.previousPos = this.posIndex;
if (swipeAngle < 45 && swipeAngle > -45 && posIndex.x < SC_GameVariables.Instance.rowsSize - 1)
if (this.swipeAngle < 45 && this.swipeAngle > -45 && this.posIndex.x < SC_GameVariables.Instance.rowsSize - 1)
{
otherGem = scGameLogic.GetGem(posIndex.x + 1, posIndex.y);
otherGem.posIndex.x--;
posIndex.x++;
this.otherGem = this.scGameLogic.GetGem(this.posIndex.x + 1, this.posIndex.y);
this.otherGem.posIndex.x--;
this.posIndex.x++;
}
else if (swipeAngle > 45 && swipeAngle <= 135 && posIndex.y < SC_GameVariables.Instance.colsSize - 1)
else if (this.swipeAngle > 45 && this.swipeAngle <= 135 && this.posIndex.y < SC_GameVariables.Instance.colsSize - 1)
{
otherGem = scGameLogic.GetGem(posIndex.x, posIndex.y + 1);
otherGem.posIndex.y--;
posIndex.y++;
this.otherGem = this.scGameLogic.GetGem(this.posIndex.x, this.posIndex.y + 1);
this.otherGem.posIndex.y--;
this.posIndex.y++;
}
else if (swipeAngle < -45 && swipeAngle >= -135 && posIndex.y > 0)
else if (this.swipeAngle < -45 && this.swipeAngle >= -135 && this.posIndex.y > 0)
{
otherGem = scGameLogic.GetGem(posIndex.x, posIndex.y - 1);
otherGem.posIndex.y++;
posIndex.y--;
this.otherGem = this.scGameLogic.GetGem(this.posIndex.x, this.posIndex.y - 1);
this.otherGem.posIndex.y++;
this.posIndex.y--;
}
else if (swipeAngle > 135 || swipeAngle < -135 && posIndex.x > 0)
else if (this.swipeAngle > 135 || this.swipeAngle < -135 && this.posIndex.x > 0)
{
otherGem = scGameLogic.GetGem(posIndex.x - 1, posIndex.y);
otherGem.posIndex.x++;
posIndex.x--;
this.otherGem = this.scGameLogic.GetGem(this.posIndex.x - 1, this.posIndex.y);
this.otherGem.posIndex.x++;
this.posIndex.x--;
}
scGameLogic.SetGem(posIndex.x,posIndex.y, this);
scGameLogic.SetGem(otherGem.posIndex.x, otherGem.posIndex.y, otherGem);
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());
}
@@ -109,27 +113,27 @@ public class SC_Gem : MonoBehaviour
//Why are we checking matches on the Gem itself
public IEnumerator CheckMoveCo()
{
scGameLogic.SetState(GlobalEnums.GameState.wait);
this.scGameLogic.SetState(GameState.Wait);
yield return new WaitForSeconds(.5f);
scGameLogic.FindAllMatches();
this.scGameLogic.FindAllMatches();
if (otherGem != null)
if (this.otherGem != null)
{
if (isMatch == false && otherGem.isMatch == false)
if (this.isMatch == false && this.otherGem.isMatch == false)
{
otherGem.posIndex = posIndex;
posIndex = previousPos;
this.otherGem.posIndex = this.posIndex;
this.posIndex = this.previousPos;
scGameLogic.SetGem(posIndex.x, posIndex.y, this);
scGameLogic.SetGem(otherGem.posIndex.x, otherGem.posIndex.y, otherGem);
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);
scGameLogic.SetState(GlobalEnums.GameState.move);
this.scGameLogic.SetState(GameState.Move);
}
else
{
scGameLogic.DestroyMatches();
this.scGameLogic.DestroyMatches();
}
}
}