mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-06-10 22:20:40 +02:00
feat: edit a level (#64)
This commit is contained in:
@ -4,6 +4,7 @@ using UnityEngine.SceneManagement;
|
||||
|
||||
public class NormalGameMode : IGameMode
|
||||
{
|
||||
public bool editMode { get; set; } = false;
|
||||
private const float HorizontalSpeed = 8.6f;
|
||||
private const float JumpForce = 26.6581f;
|
||||
private const KeyCode JumpKey = KeyCode.Space;
|
||||
@ -18,6 +19,7 @@ public class NormalGameMode : IGameMode
|
||||
|
||||
if (player.IsColliding && Input.GetKey(JumpKey) && !isRotating)
|
||||
{
|
||||
Debug.Log("Player is Jumping");
|
||||
Jump(player);
|
||||
}
|
||||
|
||||
@ -89,7 +91,18 @@ public class NormalGameMode : IGameMode
|
||||
|
||||
if (collision.gameObject.CompareTag("Kill"))
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
if (editMode)
|
||||
{
|
||||
GameObject spawn = new GameObject("AutoSpawnPoint");
|
||||
spawn.transform.position = new Vector3(-16, -3, 0f);
|
||||
player.transform.position = spawn.transform.position;
|
||||
player.RigidBody.linearVelocity = Vector2.zero;
|
||||
player.SpeedMultiplier = 1f;
|
||||
}
|
||||
else
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
}
|
||||
|
||||
if (collision.gameObject.CompareTag("Win"))
|
||||
|
153
Assets/Scripts/JSONLevelEditor.cs
Normal file
153
Assets/Scripts/JSONLevelEditor.cs
Normal file
@ -0,0 +1,153 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using SimpleFileBrowser;
|
||||
using TMPro;
|
||||
|
||||
[System.Serializable]
|
||||
public class BlockData
|
||||
{
|
||||
public string prefabName;
|
||||
public Vector3 position;
|
||||
public float rotationZ;
|
||||
public Vector3 scale;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class LevelData
|
||||
{
|
||||
public string levelName;
|
||||
public List<BlockData> blocks = new List<BlockData>();
|
||||
}
|
||||
|
||||
public class JSONLevelEditor : MonoBehaviour
|
||||
{
|
||||
[Header("UI")]
|
||||
public TMP_Dropdown levelDropdown;
|
||||
public Button loadButton;
|
||||
public Button saveButton;
|
||||
public TMP_Text statusText;
|
||||
|
||||
[Header("Editor")]
|
||||
public Transform editRoot; // où on instancie les blocs
|
||||
public List<GameObject> blockPrefabs; // à remplir dans l’inspecteur (mêmes noms que dans JSON)
|
||||
|
||||
private LevelData currentLevel;
|
||||
private string currentJsonPath;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Remplir la dropdown avec les JSON disponibles dans Resources/Levels
|
||||
var assets = Resources.LoadAll<TextAsset>("Levels");
|
||||
levelDropdown.options.Clear();
|
||||
foreach (var txt in assets)
|
||||
levelDropdown.options.Add(new TMP_Dropdown.OptionData(txt.name));
|
||||
levelDropdown.RefreshShownValue();
|
||||
|
||||
loadButton.onClick.AddListener(OnLoadClicked);
|
||||
saveButton.onClick.AddListener(OnSaveClicked);
|
||||
UpdateStatus("Prêt.", Color.white);
|
||||
}
|
||||
|
||||
private void OnLoadClicked()
|
||||
{
|
||||
string lvlName = levelDropdown.options[levelDropdown.value].text;
|
||||
TextAsset json = Resources.Load<TextAsset>($"Levels/{lvlName}");
|
||||
if (json == null)
|
||||
{
|
||||
UpdateStatus($"Niveau '{lvlName}' introuvable.", Color.red);
|
||||
return;
|
||||
}
|
||||
|
||||
currentJsonPath = Path.Combine(Application.dataPath, "Resources/Levels", lvlName + ".json");
|
||||
currentLevel = JsonUtility.FromJson<LevelData>(json.text);
|
||||
if (currentLevel == null)
|
||||
{
|
||||
UpdateStatus("Impossible de parser le JSON.", Color.red);
|
||||
return;
|
||||
}
|
||||
|
||||
ClearEditRoot();
|
||||
foreach (var bd in currentLevel.blocks)
|
||||
{
|
||||
var prefab = blockPrefabs.Find(p => p.name == bd.prefabName);
|
||||
if (prefab == null) continue;
|
||||
var go = Instantiate(prefab, editRoot);
|
||||
go.transform.localPosition = bd.position;
|
||||
go.transform.localEulerAngles = new Vector3(0, 0, bd.rotationZ);
|
||||
go.transform.localScale = bd.scale;
|
||||
// vous pouvez ajouter un script pour manipuler ce 'go' dans l’éditeur
|
||||
}
|
||||
|
||||
UpdateStatus($"Niveau '{lvlName}' chargé.", Color.green);
|
||||
}
|
||||
|
||||
private void OnSaveClicked()
|
||||
{
|
||||
if (currentLevel == null)
|
||||
{
|
||||
UpdateStatus("Aucun niveau chargé.", Color.red);
|
||||
return;
|
||||
}
|
||||
|
||||
// Reconstruire le LevelData depuis les enfants de editRoot
|
||||
currentLevel.blocks.Clear();
|
||||
foreach (Transform child in editRoot)
|
||||
{
|
||||
var bd = new BlockData
|
||||
{
|
||||
prefabName = child.name.Replace("(Clone)", ""),
|
||||
position = child.localPosition,
|
||||
rotationZ = child.localEulerAngles.z,
|
||||
scale = child.localScale
|
||||
};
|
||||
currentLevel.blocks.Add(bd);
|
||||
}
|
||||
|
||||
// Demande path de sauvegarde
|
||||
StartCoroutine(DoSaveDialog());
|
||||
}
|
||||
|
||||
private IEnumerator DoSaveDialog()
|
||||
{
|
||||
yield return FileBrowser.WaitForSaveDialog(
|
||||
FileBrowser.PickMode.Files, false, null, "json",
|
||||
"Save JSON", "Save");
|
||||
|
||||
if (!FileBrowser.Success)
|
||||
{
|
||||
UpdateStatus("Sauvegarde annulée.", Color.red);
|
||||
yield break;
|
||||
}
|
||||
|
||||
string dest = FileBrowser.Result[0];
|
||||
if (!dest.EndsWith(".json")) dest += ".json";
|
||||
|
||||
string jsonText = JsonUtility.ToJson(currentLevel, true);
|
||||
try
|
||||
{
|
||||
File.WriteAllText(dest, jsonText);
|
||||
UpdateStatus($"Sauvegardé : {dest}", Color.green);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError(e);
|
||||
UpdateStatus("Erreur lors de la sauvegarde.", Color.red);
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearEditRoot()
|
||||
{
|
||||
for (int i = editRoot.childCount - 1; i >= 0; i--)
|
||||
DestroyImmediate(editRoot.GetChild(i).gameObject);
|
||||
}
|
||||
|
||||
private void UpdateStatus(string msg, Color col)
|
||||
{
|
||||
if (statusText == null) return;
|
||||
statusText.text = msg;
|
||||
statusText.color = col;
|
||||
}
|
||||
}
|
2
Assets/Scripts/JSONLevelEditor.cs.meta
Normal file
2
Assets/Scripts/JSONLevelEditor.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a995a69086a0184d830aa300b3c2843
|
@ -5,6 +5,8 @@ using System.IO;
|
||||
public class LevelLoader : MonoBehaviour
|
||||
{
|
||||
public LevelsLoader levelsLoader;
|
||||
public bool editMode;
|
||||
public bool createMode;
|
||||
public AudioSource audioSource;
|
||||
public Text progressionText;
|
||||
private readonly float groundY = -6.034f;
|
||||
@ -49,27 +51,36 @@ public class LevelLoader : MonoBehaviour
|
||||
|
||||
instance.transform.localScale = new Vector3(newScaleX, newScaleY, originalScale.z);
|
||||
}
|
||||
|
||||
GameObject groundPrefab = GetPrefab("Ground");
|
||||
GameObject groundInstance = Instantiate(groundPrefab, new Vector3(current.LastX / 2, groundY, 0), Quaternion.identity);
|
||||
float groundWidth = current.LastX;
|
||||
groundInstance.transform.localScale = new Vector3(groundWidth / 5f * 2, 1, 1);
|
||||
|
||||
if (!editMode)
|
||||
{
|
||||
GameObject groundPrefab = GetPrefab("Ground");
|
||||
GameObject groundInstance = Instantiate(groundPrefab, new Vector3(current.LastX / 2, groundY, 0), Quaternion.identity);
|
||||
float groundWidth = current.LastX;
|
||||
groundInstance.transform.localScale = new Vector3(groundWidth / 5f * 2, 1, 1);
|
||||
}
|
||||
Instantiate(GetPrefab("WinnerWall"), new Vector3(current.LastX, 0, 0), Quaternion.Euler(0, 0, 90));
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
|
||||
levelsLoader.IncreaseTotalAttempts();
|
||||
createMode = PlayerPrefs.GetInt("CreateMode", 0) == 1;
|
||||
if (!createMode)
|
||||
{
|
||||
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
|
||||
levelsLoader.IncreaseTotalAttempts();
|
||||
|
||||
LoadAudio();
|
||||
LoadElements();
|
||||
LoadElements();
|
||||
if (!editMode)
|
||||
LoadAudio();
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
Level current = levelsLoader.levelCurrent;
|
||||
progressionText.text = current.ProgressionPercent + "%";
|
||||
if (!editMode)
|
||||
{
|
||||
Level current = levelsLoader.levelCurrent;
|
||||
progressionText.text = current.ProgressionPercent + "%";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ public class LevelHomeButton : MonoBehaviour
|
||||
{
|
||||
public void GoToHome()
|
||||
{
|
||||
PlayerPrefs.SetInt("CreateMode", 0);
|
||||
PlayerPrefs.SetInt("EditMode", 0);
|
||||
SceneManager.LoadScene("HomeScene");
|
||||
}
|
||||
}
|
||||
|
@ -7,4 +7,9 @@ public class LevelNameButton : MonoBehaviour
|
||||
{
|
||||
SceneManager.LoadScene("LevelScene");
|
||||
}
|
||||
public void EditLevel()
|
||||
{
|
||||
PlayerPrefs.SetInt("CreateMode", 0);
|
||||
SceneManager.LoadScene("LevelEditorScene");
|
||||
}
|
||||
}
|
||||
|
@ -23,13 +23,19 @@ public class MainMenu : MonoBehaviour
|
||||
SceneManager.LoadSceneAsync("LevelEditorScene");
|
||||
}
|
||||
|
||||
public void CreateVoidLevel()
|
||||
{
|
||||
PlayerPrefs.SetInt("CreateMode", 1);
|
||||
SceneManager.LoadScene("LevelEditorScene");
|
||||
}
|
||||
|
||||
public void EditorChoice()
|
||||
{
|
||||
SceneManager.LoadSceneAsync("EditorChoiceScene");
|
||||
}
|
||||
|
||||
public void CreateLevel()
|
||||
public void EditLevel()
|
||||
{
|
||||
SceneManager.LoadSceneAsync("CreateLevelScene");
|
||||
SceneManager.LoadSceneAsync("SelectLevelToEditScene");
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ public class TestManager : MonoBehaviour
|
||||
else
|
||||
{
|
||||
gameMode = new NormalGameMode();
|
||||
((NormalGameMode)gameMode).editMode = true;
|
||||
currentPlayer.ChangeGameMode(gameMode);
|
||||
currentPlayer.SpeedMultiplier = 0f;
|
||||
|
||||
@ -68,12 +69,15 @@ public class TestManager : MonoBehaviour
|
||||
}
|
||||
|
||||
if (editorUI != null)
|
||||
{
|
||||
Debug.LogError("editor UI null");
|
||||
editorUI.SetActive(false);
|
||||
}
|
||||
|
||||
currentPlayer.transform.position = spawnPoint.position;
|
||||
currentPlayer.RigidBody.linearVelocity = Vector2.zero;
|
||||
currentPlayer.SpeedMultiplier = 1f;
|
||||
currentPlayer.SpriteRenderer.sprite = Resources.Load<Sprite>("Shapes/BaseSquare");
|
||||
// currentPlayer.SpriteRenderer.sprite = Resources.Load<Sprite>("Shapes/BaseSquare");
|
||||
|
||||
currentPlayer.ChangeGameMode(gameMode);
|
||||
isTesting = true;
|
||||
|
Reference in New Issue
Block a user