149 lines
4.6 KiB
C#
149 lines
4.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class PuzzleManager : MonoBehaviour
|
|
{
|
|
public GameObject tilePrefab; // Prefab untuk tile
|
|
public Sprite[] tileSprites; // Array berisi potongan gambar
|
|
public int gridSize = 3; // Ukuran grid (3x3)
|
|
public float tileSize = 100f; // Ukuran tile
|
|
|
|
private GameObject[,] grid; // Grid untuk menyimpan tile
|
|
private Vector2Int emptyTilePos; // Posisi tile kosong
|
|
|
|
void Start()
|
|
{
|
|
InitializeGrid();
|
|
ShuffleTiles();
|
|
}
|
|
|
|
void InitializeGrid()
|
|
{
|
|
grid = new GameObject[gridSize, gridSize];
|
|
emptyTilePos = new Vector2Int(gridSize - 1, gridSize - 1); // Tile kosong di pojok kanan bawah
|
|
|
|
for (int y = 0; y < gridSize; y++)
|
|
{
|
|
for (int x = 0; x < gridSize; x++)
|
|
{
|
|
// Buat tile
|
|
GameObject tile = Instantiate(tilePrefab, transform);
|
|
tile.GetComponent<Image>().sprite = tileSprites[y * gridSize + x];
|
|
tile.GetComponent<Button>().onClick.AddListener(() => OnTileClick(tile));
|
|
|
|
// Atur posisi tile
|
|
RectTransform rectTransform = tile.GetComponent<RectTransform>();
|
|
rectTransform.anchoredPosition = new Vector2(x * tileSize, -y * tileSize);
|
|
|
|
// Simpan tile ke grid
|
|
grid[x, y] = tile;
|
|
|
|
// Nonaktifkan tile kosong
|
|
if (x == emptyTilePos.x && y == emptyTilePos.y)
|
|
{
|
|
tile.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ShuffleTiles()
|
|
{
|
|
// Acak posisi tile
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
Vector2Int[] possibleMoves = GetPossibleMoves();
|
|
int randomIndex = Random.Range(0, possibleMoves.Length);
|
|
MoveTile(possibleMoves[randomIndex]);
|
|
}
|
|
}
|
|
|
|
void OnTileClick(GameObject tile)
|
|
{
|
|
// Cari posisi tile yang diklik
|
|
Vector2Int tilePos = FindTilePosition(tile);
|
|
|
|
// Cek apakah tile bersebelahan dengan tile kosong
|
|
if (IsAdjacent(tilePos, emptyTilePos))
|
|
{
|
|
// Geser tile ke posisi kosong
|
|
MoveTile(tilePos);
|
|
}
|
|
}
|
|
|
|
Vector2Int FindTilePosition(GameObject tile)
|
|
{
|
|
for (int y = 0; y < gridSize; y++)
|
|
{
|
|
for (int x = 0; x < gridSize; x++)
|
|
{
|
|
if (grid[x, y] == tile)
|
|
{
|
|
return new Vector2Int(x, y);
|
|
}
|
|
}
|
|
}
|
|
return new Vector2Int(-1, -1); // Tidak ditemukan
|
|
}
|
|
|
|
bool IsAdjacent(Vector2Int pos1, Vector2Int pos2)
|
|
{
|
|
// Cek apakah dua tile bersebelahan
|
|
return Mathf.Abs(pos1.x - pos2.x) + Mathf.Abs(pos1.y - pos2.y) == 1;
|
|
}
|
|
|
|
void MoveTile(Vector2Int tilePos)
|
|
{
|
|
// Geser tile ke posisi kosong
|
|
GameObject tile = grid[tilePos.x, tilePos.y];
|
|
grid[emptyTilePos.x, emptyTilePos.y] = tile;
|
|
grid[tilePos.x, tilePos.y] = null;
|
|
|
|
// Update posisi tile
|
|
RectTransform rectTransform = tile.GetComponent<RectTransform>();
|
|
rectTransform.anchoredPosition = new Vector2(emptyTilePos.x * tileSize, -emptyTilePos.y * tileSize);
|
|
|
|
// Update posisi kosong
|
|
emptyTilePos = tilePos;
|
|
|
|
// Cek apakah puzzle sudah selesai
|
|
CheckWin();
|
|
}
|
|
|
|
Vector2Int[] GetPossibleMoves()
|
|
{
|
|
// Dapatkan posisi tile yang bisa digerakkan
|
|
List<Vector2Int> moves = new List<Vector2Int>();
|
|
|
|
if (emptyTilePos.x > 0) moves.Add(new Vector2Int(emptyTilePos.x - 1, emptyTilePos.y));
|
|
if (emptyTilePos.x < gridSize - 1) moves.Add(new Vector2Int(emptyTilePos.x + 1, emptyTilePos.y));
|
|
if (emptyTilePos.y > 0) moves.Add(new Vector2Int(emptyTilePos.x, emptyTilePos.y - 1));
|
|
if (emptyTilePos.y < gridSize - 1) moves.Add(new Vector2Int(emptyTilePos.x, emptyTilePos.y + 1));
|
|
|
|
return moves.ToArray();
|
|
}
|
|
|
|
void CheckWin()
|
|
{
|
|
// Cek apakah semua tile berada di posisi yang benar
|
|
for (int y = 0; y < gridSize; y++)
|
|
{
|
|
for (int x = 0; x < gridSize; x++)
|
|
{
|
|
if (grid[x, y] != null)
|
|
{
|
|
int correctIndex = y * gridSize + x;
|
|
if (grid[x, y].GetComponent<Image>().sprite != tileSprites[correctIndex])
|
|
{
|
|
return; // Masih ada tile yang salah
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Jika semua tile benar, tampilkan pesan menang
|
|
Debug.Log("You Win!");
|
|
}
|
|
} |