137 lines
3.7 KiB
C#
137 lines
3.7 KiB
C#
using UnityEngine;
|
|
|
|
public class Tile : MonoBehaviour
|
|
{
|
|
public int X, Y;
|
|
public int RotationState;
|
|
public int SolutionRotation;
|
|
public byte SolutionConnections; // Bitmask of solution connections (R=1, U=2, L=4, D=8)
|
|
public byte Barriers; // Bitmask of barrier connections
|
|
public Sprite DisconnectedSprite, ConnectedSprite;
|
|
public bool locked = false;
|
|
|
|
public bool isCenterTile = false; // Used for center tile in the grid
|
|
|
|
public int[] baseConnections; // Base connection directions (0=R, 1=U, 2=L, 3=D)
|
|
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 RotateCounterClockwise()
|
|
{
|
|
RotationState = (RotationState + 3) % 4; // +3 is equivalent to -1 mod 4
|
|
ApplyRotation(RotationState);
|
|
GameManager.instance.CheckConnections();
|
|
}
|
|
|
|
public void Rotate180()
|
|
{
|
|
RotationState = (RotationState + 2) % 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>();
|
|
|
|
if (isCenterTile)
|
|
{
|
|
// Center tile should always be connected
|
|
connected = true;
|
|
}
|
|
spriteRenderer.sprite = connected ? ConnectedSprite : DisconnectedSprite;
|
|
}
|
|
|
|
public void ToggleLock()
|
|
{
|
|
locked = !locked;
|
|
// Optional: Add visual feedback for locked state
|
|
}
|
|
|
|
public bool HasConnection(int dir)
|
|
{
|
|
// Check if there's a barrier in this direction
|
|
if ((Barriers & (1 << dir)) != 0)
|
|
return false;
|
|
|
|
// Get the actual connection direction after rotation
|
|
int rotatedDir = (dir - RotationState + 4) % 4;
|
|
|
|
// Check if this connection exists in the base connections
|
|
foreach (int baseDir in baseConnections)
|
|
{
|
|
if (baseDir == rotatedDir)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool IsCorrectlyOriented()
|
|
{
|
|
// Get the current connections based on rotation
|
|
byte currentConnections = 0;
|
|
foreach (int baseDir in baseConnections)
|
|
{
|
|
int actualDir = (baseDir + RotationState) % 4;
|
|
currentConnections |= (byte)(1 << actualDir);
|
|
}
|
|
|
|
return currentConnections == SolutionConnections;
|
|
}
|
|
|
|
public int GetConnectionCount()
|
|
{
|
|
return baseConnections.Length;
|
|
}
|
|
|
|
public string GetTileType()
|
|
{
|
|
int count = GetConnectionCount();
|
|
switch (count)
|
|
{
|
|
case 1: return "End";
|
|
case 2:
|
|
// Check if opposite directions (straight) or adjacent (corner)
|
|
int dir1 = baseConnections[0];
|
|
int dir2 = baseConnections[1];
|
|
if ((dir1 + 2) % 4 == dir2) return "Straight";
|
|
return "Corner";
|
|
case 3: return "TCorner";
|
|
case 4: return "Cross";
|
|
default: return "Empty";
|
|
}
|
|
}
|
|
} |