feat: json loader (#37)

This commit is contained in:
2025-02-17 12:27:09 +01:00
committed by GitHub
parent 51404e5cc6
commit 827c867052
89 changed files with 1143 additions and 888 deletions

18
Assets/Scripts/Level.cs Normal file
View 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);
}
}

View File

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

View 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();
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4c3543e79f987af40bbd4a51c0a334c3

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 946cccb716439eccfa13f90e2fe2abe1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelHomeButton : MonoBehaviour
{
public void GoToHome()
{
SceneManager.LoadScene("HomeScene");
}
}

View File

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

View File

@ -0,0 +1,10 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelNameButton : MonoBehaviour
{
public void PlayLevel()
{
SceneManager.LoadScene("LevelScene");
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3617637b5b720115a8f1a856cb43f8a5

View 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;
}
}

View File

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

View 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();
}
}

View File

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

View 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();
}
}

View File

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

View 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();
}
}

View File

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

View File

@ -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()

View File

@ -1,6 +1,6 @@
using UnityEngine;
public class CameraScript : MonoBehaviour
public class PlayerCamera : MonoBehaviour
{
public GameObject playerObject;