65 lines
1.5 KiB
C#
65 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public class Tile : MonoBehaviour
|
|
{
|
|
public int X, Y;
|
|
public int RotationState;
|
|
public int SolutionRotation;
|
|
public Sprite DisconnectedSprite, ConnectedSprite;
|
|
public bool locked = false;
|
|
|
|
public int[] baseConnections;
|
|
public int graphicRotationOffset = 0;
|
|
|
|
private SpriteRenderer spriteRenderer;
|
|
|
|
void Start()
|
|
{
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
}
|
|
|
|
void OnMouseDown()
|
|
{
|
|
if (locked) return;
|
|
RotateClockwise();
|
|
}
|
|
|
|
public void RotateClockwise()
|
|
{
|
|
RotationState = (RotationState + 1) % 4;
|
|
ApplyRotation(RotationState);
|
|
GameManager.instance.CheckConnections();
|
|
}
|
|
|
|
public void ApplyRotation(int rot)
|
|
{
|
|
RotationState = rot;
|
|
int finalRotation = (rot + graphicRotationOffset) % 4;
|
|
transform.rotation = Quaternion.Euler(0, 0, -90 * finalRotation);
|
|
}
|
|
|
|
public void RandomRotate()
|
|
{
|
|
int rot = Random.Range(0, 4);
|
|
ApplyRotation(rot);
|
|
}
|
|
|
|
public void SetConnectionState(bool connected)
|
|
{
|
|
if (spriteRenderer == null)
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
spriteRenderer.sprite = connected ? ConnectedSprite : DisconnectedSprite;
|
|
}
|
|
|
|
public bool HasConnection(int dir)
|
|
{
|
|
foreach (int baseDir in baseConnections)
|
|
{
|
|
if ((baseDir + RotationState) % 4 == dir)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|