mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-06-10 22:20:40 +02:00
feat: json loader (#37)
This commit is contained in:
18
Assets/Scripts/Level.cs
Normal file
18
Assets/Scripts/Level.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class Level
|
||||
{
|
||||
public string JsonName { get; set; }
|
||||
|
||||
public string name;
|
||||
public string musicName;
|
||||
public int totalJumps;
|
||||
public int totalAttempts;
|
||||
public int order;
|
||||
|
||||
public static Level CreateFromJSON(string jsonString)
|
||||
{
|
||||
return JsonUtility.FromJson<Level>(jsonString);
|
||||
}
|
||||
}
|
2
Assets/Scripts/Level.cs.meta
Normal file
2
Assets/Scripts/Level.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d646b99f5a21a91c88669a5e4d774f06
|
58
Assets/Scripts/LevelsLoader.cs
Normal file
58
Assets/Scripts/LevelsLoader.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
public class LevelsLoader : MonoBehaviour
|
||||
{
|
||||
public List<Level> levels = new();
|
||||
public Level levelCurrent;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
LoadAllLevels();
|
||||
levelCurrent = levels[0];
|
||||
}
|
||||
|
||||
private void LoadAllLevels()
|
||||
{
|
||||
TextAsset[] levelFiles = Resources.LoadAll<TextAsset>("Levels");
|
||||
foreach (TextAsset jsonTextFile in levelFiles)
|
||||
{
|
||||
Level level = Level.CreateFromJSON(jsonTextFile.text);
|
||||
level.JsonName = jsonTextFile.name;
|
||||
levels.Add(level);
|
||||
}
|
||||
levels.Sort((x, y) => x.order.CompareTo(y.order));
|
||||
}
|
||||
|
||||
private void SaveLevelCurrent()
|
||||
{
|
||||
string json = JsonUtility.ToJson(levelCurrent, true) + "\n";
|
||||
File.WriteAllText(Path.Combine(Application.dataPath, "Resources", "Levels", levelCurrent.JsonName + ".json"), json);
|
||||
}
|
||||
|
||||
public void NextLevel()
|
||||
{
|
||||
int currentIndex = levels.IndexOf(levelCurrent);
|
||||
levelCurrent = levels[(currentIndex + 1) % levels.Count];
|
||||
}
|
||||
|
||||
public void PreviousLevel()
|
||||
{
|
||||
int currentIndex = levels.IndexOf(levelCurrent);
|
||||
levelCurrent = levels[(currentIndex - 1 + levels.Count) % levels.Count];
|
||||
}
|
||||
|
||||
public void IncreaseTotalJumps()
|
||||
{
|
||||
levelCurrent.totalJumps += 1;
|
||||
SaveLevelCurrent();
|
||||
}
|
||||
|
||||
public void IncreaseTotalAttempts()
|
||||
{
|
||||
levelCurrent.totalAttempts += 1;
|
||||
SaveLevelCurrent();
|
||||
}
|
||||
}
|
2
Assets/Scripts/LevelsLoader.cs.meta
Normal file
2
Assets/Scripts/LevelsLoader.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c3543e79f987af40bbd4a51c0a334c3
|
8
Assets/Scripts/LevelsSelect.meta
Normal file
8
Assets/Scripts/LevelsSelect.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 946cccb716439eccfa13f90e2fe2abe1
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
10
Assets/Scripts/LevelsSelect/LevelHomeButton.cs
Normal file
10
Assets/Scripts/LevelsSelect/LevelHomeButton.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class LevelHomeButton : MonoBehaviour
|
||||
{
|
||||
public void GoToHome()
|
||||
{
|
||||
SceneManager.LoadScene("HomeScene");
|
||||
}
|
||||
}
|
2
Assets/Scripts/LevelsSelect/LevelHomeButton.cs.meta
Normal file
2
Assets/Scripts/LevelsSelect/LevelHomeButton.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef2acea012085a3bb9a981fc334afaec
|
10
Assets/Scripts/LevelsSelect/LevelNameButton.cs
Normal file
10
Assets/Scripts/LevelsSelect/LevelNameButton.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class LevelNameButton : MonoBehaviour
|
||||
{
|
||||
public void PlayLevel()
|
||||
{
|
||||
SceneManager.LoadScene("LevelScene");
|
||||
}
|
||||
}
|
2
Assets/Scripts/LevelsSelect/LevelNameButton.cs.meta
Normal file
2
Assets/Scripts/LevelsSelect/LevelNameButton.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3617637b5b720115a8f1a856cb43f8a5
|
19
Assets/Scripts/LevelsSelect/LevelNameText.cs
Normal file
19
Assets/Scripts/LevelsSelect/LevelNameText.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class LevelNameText : MonoBehaviour
|
||||
{
|
||||
public Text levelNameText;
|
||||
public LevelsLoader levelsLoader;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
|
||||
levelNameText.text = levelsLoader.levelCurrent.name;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
levelNameText.text = levelsLoader.levelCurrent.name;
|
||||
}
|
||||
}
|
2
Assets/Scripts/LevelsSelect/LevelNameText.cs.meta
Normal file
2
Assets/Scripts/LevelsSelect/LevelNameText.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9d2aa88c034d40f187d6ff84705da65
|
16
Assets/Scripts/LevelsSelect/LevelNextButton.cs
Normal file
16
Assets/Scripts/LevelsSelect/LevelNextButton.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class LevelNextButton : MonoBehaviour
|
||||
{
|
||||
public LevelsLoader levelsLoader;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
|
||||
}
|
||||
|
||||
public void NextLevel()
|
||||
{
|
||||
levelsLoader.NextLevel();
|
||||
}
|
||||
}
|
2
Assets/Scripts/LevelsSelect/LevelNextButton.cs.meta
Normal file
2
Assets/Scripts/LevelsSelect/LevelNextButton.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c34ef065650ba0802be580828cc0d474
|
16
Assets/Scripts/LevelsSelect/LevelPreviousButton.cs
Normal file
16
Assets/Scripts/LevelsSelect/LevelPreviousButton.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class LevelPreviousButton : MonoBehaviour
|
||||
{
|
||||
public LevelsLoader levelsLoader;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
|
||||
}
|
||||
|
||||
public void PreviousLevel()
|
||||
{
|
||||
levelsLoader.PreviousLevel();
|
||||
}
|
||||
}
|
2
Assets/Scripts/LevelsSelect/LevelPreviousButton.cs.meta
Normal file
2
Assets/Scripts/LevelsSelect/LevelPreviousButton.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c53cba3260b30d02b79e69a3198546c
|
20
Assets/Scripts/MainMenu.cs
Normal file
20
Assets/Scripts/MainMenu.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class MainMenu : MonoBehaviour
|
||||
{
|
||||
public void LaunchGame()
|
||||
{
|
||||
SceneManager.LoadSceneAsync("SelectLevelScene");
|
||||
}
|
||||
|
||||
public void OpenSettings()
|
||||
{
|
||||
// SceneManager.LoadSceneAsync(?);
|
||||
}
|
||||
|
||||
public void QuitGame()
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
2
Assets/Scripts/MainMenu.cs.meta
Normal file
2
Assets/Scripts/MainMenu.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1cd6b406f139414086355a222af717e
|
@ -1,11 +1,13 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.IO;
|
||||
|
||||
public class PlayerScript : MonoBehaviour
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
public Rigidbody2D rigidBody;
|
||||
public GameObject playerObject;
|
||||
public ParticleSystem particle;
|
||||
public LevelsLoader levelsLoader;
|
||||
|
||||
public bool isColliding = true;
|
||||
public AudioSource audioSource;
|
||||
@ -15,6 +17,12 @@ public class PlayerScript : MonoBehaviour
|
||||
|
||||
public void Start()
|
||||
{
|
||||
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
|
||||
levelsLoader.IncreaseTotalAttempts();
|
||||
|
||||
audioSource.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.levelCurrent.musicName));
|
||||
audioSource.Play();
|
||||
|
||||
var mainModule = particle.main;
|
||||
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
|
||||
particle.transform.parent = null;
|
||||
@ -55,6 +63,7 @@ public class PlayerScript : MonoBehaviour
|
||||
{
|
||||
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, 0);
|
||||
rigidBody.AddForce(Vector2.up * 26.6581f, ForceMode2D.Impulse);
|
||||
levelsLoader.IncreaseTotalJumps();
|
||||
}
|
||||
|
||||
private bool IsJumping()
|
@ -1,6 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraScript : MonoBehaviour
|
||||
public class PlayerCamera : MonoBehaviour
|
||||
{
|
||||
public GameObject playerObject;
|
||||
|
Reference in New Issue
Block a user