starOfWords/Assets/Scripts/LevelMapScene.cs

215 lines
6.4 KiB
C#
Raw Normal View History

using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LevelMapScene : MonoBehaviour
{
[Header("━━━ UI ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")]
public TMP_Text infoText;
public TMP_Text totalScoreText;
public Image infoBackground;
[Header("━━━ INFO HIDE ━━━━━━━━━━━━━━━━━━━━━━━━━")]
public float infoHideDelay = 5f;
[Header("━━━ AUDIO ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")]
public AudioSource bgMusic;
public AudioSource clickSound;
[Header("━━━ SCENES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━")]
public Button backButton;
public string backSceneName = "modsec 1";
[Header("Her level ayrı sahneye gidiyorsa AÇIK bırak (GameScene_Level1, GameScene_Level2 ...)")]
public bool usePerLevelScene = true;
[Header("Ayrı sahne kullanılıyorsa bu ön ek + levelID kullanılır")]
public string levelScenePrefix = "GameScene_Level"; // -> GameScene_Level1
[Header("Tüm leveller TEK sahneye gidiyorsa burayı kullan (usePerLevelScene KAPALI iken)")]
public string singleGameSceneName = "GameScene";
[Header("━━━ PLANETS / LEVEL BUTTONS ━━━━━━━━━━")]
public Button[] levelButtons;
[Header("━━━ LEVEL LOCK SETTINGS ━━━━━━━━━━━━━━")]
public bool useScoreLock = true;
private int playerTotalScore;
private Coroutine hideInfoCoroutine;
private readonly int[] requiredScores = new int[20]
{
0, 1000, 2000, 3000, 4000,
5000, 6000, 7000, 8000, 9000,
10000, 11000, 12000, 13000, 14000,
15000, 16000, 17000, 18000, 19000
};
void Start()
{
playerTotalScore = PlayerPrefs.GetInt("TotalScore", 0);
if (totalScoreText != null)
totalScoreText.text = "Your Total Score: " + playerTotalScore;
if (infoText != null)
{
infoText.text = "";
infoText.alpha = 0f;
}
if (infoBackground != null)
{
infoBackground.gameObject.SetActive(false);
infoBackground.color = new Color(1f, 1f, 1f, 0.70f);
}
if (bgMusic != null)
{
bgMusic.loop = true;
bgMusic.Play();
}
SetupLevelButtons();
if (backButton != null)
{
backButton.onClick.RemoveAllListeners();
backButton.onClick.AddListener(GoBack);
}
}
void SetupLevelButtons()
{
if (levelButtons == null || levelButtons.Length == 0)
{
Debug.LogError("LevelMapScene: Level Buttons atanmadı.");
return;
}
int buttonCount = Mathf.Min(levelButtons.Length, requiredScores.Length);
for (int i = 0; i < buttonCount; i++)
{
int levelID = i + 1;
if (levelButtons[i] == null)
{
Debug.LogWarning("LevelMapScene: Level button boş index: " + i);
continue;
}
levelButtons[i].interactable = true;
levelButtons[i].onClick.RemoveAllListeners();
levelButtons[i].onClick.AddListener(() => OnLevelSelect(levelID));
}
}
void OnLevelSelect(int levelID)
{
if (clickSound != null)
clickSound.Play();
if (levelID < 1 || levelID > requiredScores.Length)
{
Debug.LogError("Geçersiz levelID: " + levelID);
return;
}
int neededScore = requiredScores[levelID - 1];
// ❌ KİLİTLİ
if (useScoreLock && playerTotalScore < neededScore)
{
ShowInfo(
"LEVEL LOCKED\n\n" +
"Level: " + levelID + "\n" +
"Your Total Score: " + playerTotalScore + "\n" +
"Required Score: " + neededScore,
new Color32(255, 70, 70, 255));
return;
}
// ✔ AÇIK → sahneyi belirle
string sceneToLoad = usePerLevelScene
? (levelScenePrefix + levelID) // GameScene_Level1
: singleGameSceneName; // GameScene
// Sahne Build Settings'te var mı? Kontrol et
if (!Application.CanStreamedLevelBeLoaded(sceneToLoad))
{
ShowInfo(
"SAHNE BULUNAMADI\n\n" +
"'" + sceneToLoad + "'\n" +
"Build Settings'e ekli değil!",
new Color32(255, 170, 50, 255));
Debug.LogError("Sahne yüklenemiyor: '" + sceneToLoad +
"'. File > Build Settings içine eklediğinden ve adının birebir aynı olduğundan emin ol.");
return;
}
ShowInfo(
"LEVEL OPENED\n\n" +
"Level: " + levelID + "\n" +
"Loading...",
new Color32(70, 255, 100, 255));
PlayerPrefs.SetInt("SelectedLevel", levelID);
PlayerPrefs.SetInt("SelectedStoryLevel", levelID);
PlayerPrefs.SetString("LastPlayedMod", "Mod1");
PlayerPrefs.Save();
StartCoroutine(LoadGameSceneDelay(sceneToLoad));
}
void ShowInfo(string message, Color color)
{
if (infoBackground != null)
infoBackground.gameObject.SetActive(true);
if (infoText != null)
{
infoText.alpha = 1f;
infoText.color = color;
infoText.text = message;
}
if (hideInfoCoroutine != null)
StopCoroutine(hideInfoCoroutine);
hideInfoCoroutine = StartCoroutine(HideInfoAfterDelay());
}
IEnumerator LoadGameSceneDelay(string sceneToLoad)
{
yield return new WaitForSeconds(0.35f);
SceneManager.LoadScene(sceneToLoad);
}
IEnumerator HideInfoAfterDelay()
{
yield return new WaitForSeconds(infoHideDelay);
if (infoText != null)
{
infoText.text = "";
infoText.alpha = 0f;
}
if (infoBackground != null)
infoBackground.gameObject.SetActive(false);
}
void GoBack()
{
if (clickSound != null)
clickSound.Play();
if (!string.IsNullOrWhiteSpace(backSceneName))
SceneManager.LoadScene(backSceneName);
}
}