Net-Game/Assets/Scripts/GameManager.cs
2025-03-28 08:33:16 -04:00

135 lines
3.0 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
[Header("Game Settings")]
public int width = 5;
public int height = 5;
[Header("Prefabs and UI")]
public GameObject[] tilePrefabs;
public TMP_InputField seedInput;
public TMP_Dropdown sizeDropdown;
[Header("Buttons")]
public Button newGameButton;
public Button restartGameButton;
public Button solveButton;
private int currentSeed;
private Tile[,] tiles;
void Awake()
{
instance = this;
newGameButton.onClick.AddListener(NewGame);
restartGameButton.onClick.AddListener(RestartGame);
solveButton.onClick.AddListener(SolveGame);
}
void Start()
{
NewGame();
}
public void NewGame()
{
UpdateMapSizeFromDropdown(); // Get map size from dropdown
currentSeed = Random.Range(0, int.MaxValue);
seedInput.text = currentSeed.ToString();
GenerateMap();
}
public void RestartGame()
{
UpdateMapSizeFromDropdown(); // Ensure regeneration based on current difficulty
int.TryParse(seedInput.text, out currentSeed);
GenerateMap();
}
public void GenerateMap()
{
ClearExistingTiles();
tiles = new Tile[width, height];
MapGenerator.Generate(tiles, tilePrefabs, width, height, currentSeed);
CheckConnections();
}
private void ClearExistingTiles()
{
if (tiles != null)
{
foreach (Tile tile in tiles)
{
if (tile != null)
Destroy(tile.gameObject);
}
}
tiles = null;
}
public void CheckConnections()
{
bool solved = Solver.CheckSolved(tiles, width, height);
if (solved)
Debug.Log("Puzzle Solved!");
}
public void SolveGame()
{
RestoreSolution();
CheckConnections();
}
public void RestoreSolution()
{
foreach (var tile in tiles)
{
tile.ApplyRotation(tile.SolutionRotation);
tile.SetConnectionState(true); // Update texture when restoring
}
CheckConnections();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.J))
JumbleTiles();
}
public void JumbleTiles()
{
foreach (var tile in tiles)
{
if (!tile.locked)
tile.RandomRotate();
}
CheckConnections();
}
private void UpdateMapSizeFromDropdown()
{
switch (sizeDropdown.value)
{
case 0: // Easy
width = height = 5;
break;
case 1: // Medium
width = height = 8;
break;
case 2: // Hard
width = height = 11;
break;
default:
width = height = 5;
break;
}
}
}