feat: import JSON level (#48)

This commit is contained in:
djelalb
2025-03-24 19:00:31 +01:00
committed by GitHub
parent 58f774fc7e
commit d57cc647aa
11 changed files with 1488 additions and 1 deletions

View File

@ -63,6 +63,5 @@ public class ShipGameMode : IGameMode
public void OnCollisionExit(Player player, Collision2D collision)
{
// rien pour l'instant
}
}

View File

@ -0,0 +1,128 @@
using UnityEngine;
using System.IO;
using System.Collections;
using SimpleFileBrowser;
using UnityEngine.SceneManagement;
using TMPro;
public class JSONImporter : MonoBehaviour
{
public TMP_Text statusText;
private void Awake()
{
if (statusText == null)
{
GameObject statusObj = GameObject.Find("StatusText");
if (statusObj != null)
{
statusText = statusObj.GetComponent<TMP_Text>();
if (statusText != null)
{
Debug.Log("✅ StatusText found and assigned automatically!");
}
else
{
Debug.LogError("⚠️ 'StatusText' was found but does not have a TMP_Text component!");
}
}
else
{
Debug.LogError("⚠️ No GameObject named 'StatusText' found in the scene. Please create a TextMeshPro element and name it 'StatusText'.");
}
}
}
private void Start()
{
if (statusText != null)
{
statusText.text = "Ready to import...";
statusText.color = Color.white;
}
else
{
Debug.LogError("statusText is not assigned!");
}
}
public void ImportJSON()
{
Debug.Log("Button clicked, starting import...");
if (statusText != null)
{
statusText.text = "Importing...";
statusText.color = Color.yellow;
}
StartCoroutine(ShowFileBrowser());
}
private IEnumerator ShowFileBrowser()
{
yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.Files, false, null, null, "Select JSON File", "Load");
if (FileBrowser.Success)
{
string sourcePath = FileBrowser.Result[0];
if (Path.GetExtension(sourcePath).ToLower() != ".json")
{
UpdateStatus("Invalid file. Please select a JSON file.", Color.red);
yield break;
}
string fileName = Path.GetFileName(sourcePath);
string destinationPath = Path.Combine(Application.dataPath, "Resources/Levels", fileName);
bool success = false;
try
{
File.Copy(sourcePath, destinationPath, true);
success = true;
}
catch (IOException e)
{
Debug.LogError("Error copying file: " + e.Message);
}
if (success)
{
UpdateStatus("Import successful!", Color.green);
}
else
{
UpdateStatus("Import error.", Color.red);
}
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
LevelsLoader loader = Object.FindAnyObjectByType<LevelsLoader>();
if (loader != null)
{
loader.RefreshLevels();
}
}
else
{
UpdateStatus("No file selected.", Color.red);
}
yield return null;
}
private void UpdateStatus(string message, Color color)
{
if (statusText != null)
{
statusText.text = message;
statusText.color = color;
statusText.gameObject.SetActive(false);
statusText.gameObject.SetActive(true);
Canvas.ForceUpdateCanvases();
}
else
{
Debug.LogError("statusText is NULL!");
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: baf97ea8555b6214299a38be9fe1724f

View File

@ -111,4 +111,12 @@ public class LevelsLoader : MonoBehaviour
return clampedPercentage;
}
public void RefreshLevels()
{
levels.Clear();
LoadAllLevels();
if (levels.Count > 0)
levelCurrent = levels[0];
}
}

View File

@ -8,6 +8,11 @@ public class MainMenu : MonoBehaviour
SceneManager.LoadSceneAsync("SelectLevelScene");
}
public void OpenImport()
{
SceneManager.LoadSceneAsync("ImportScene");
}
public void OpenSettings()
{
// SceneManager.LoadSceneAsync(?);