feat: dynamically create elements levels (#39)

This commit is contained in:
2025-03-03 10:36:38 +01:00
committed by GitHub
parent ce92ba53cc
commit e2bbf26929
13 changed files with 608 additions and 476 deletions

View File

@ -1,4 +1,21 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
[System.Serializable]
public class LevelElement
{
public enum Type
{
Obstacle = 0,
Spike = 1
}
public Type type;
public float x;
public float y;
}
[System.Serializable]
public class Level
@ -11,6 +28,8 @@ public class Level
public string musicName;
public int order;
public List<LevelElement> elements;
public static Level CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<Level>(jsonString);

View File

@ -0,0 +1,58 @@
using UnityEngine;
using System.IO;
public class LevelLoader : MonoBehaviour
{
public LevelsLoader levelsLoader;
public AudioSource audioSource;
public GameObject obstaclePrefab;
public GameObject spikePrefab;
void Start()
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
levelsLoader.IncreaseTotalAttempts();
audioSource.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.levelCurrent.musicName));
audioSource.Play();
obstaclePrefab = Resources.Load<GameObject>("Prefabs/Obstacle");
spikePrefab = Resources.Load<GameObject>("Prefabs/Spike");
Level current = levelsLoader.levelCurrent;
// Debug.Log("Level: " + current.name);
// for (int i = 0; i < current.elements.Count; i++)
// {
// LevelElement element = current.elements[i];
// Debug.Log("Element: " + element.type + " " + element.x + " " + element.y);
// }
for (int index = 0; index < current.elements.Count; index++)
{
LevelElement element = current.elements[index];
GameObject prefab = obstaclePrefab;
if (element.type == LevelElement.Type.Spike)
{
prefab = spikePrefab;
}
Instantiate(prefab, new Vector3(element.x, element.y, 0), Quaternion.identity);
}
// // Obstacle
// // x=-6.684, y=-2.897, 0
// // scale=0.96055, 0.2326, 1
// Instantiate(obstaclePrefab, new Vector3(-6.684f, -2.897f, 0), Quaternion.identity);
// // Spike
// // -3.06, -2.93
// // scale=0.15, 0.15, 1
// Instantiate(spikePrefab, new Vector3(-3.06f, -2.93f, 0), Quaternion.identity);
}
void Update()
{
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 017ea60a517f31bf3af976010911be25

View File

@ -23,6 +23,7 @@ public class LevelsLoader : MonoBehaviour
foreach (TextAsset jsonTextFileStats in levelStatsFiles)
{
LevelStat levelStat = LevelStat.CreateFromJSON(jsonTextFileStats.text);
levelStat.JsonName = jsonTextFileStats.name;
levelStatsMap[levelStat.JsonName] = levelStat;
}

View File

@ -10,7 +10,6 @@ public class Player : MonoBehaviour
public LevelsLoader levelsLoader;
public bool isColliding = true;
public AudioSource audioSource;
private bool hasStarted = false;
private bool canJump = true;
@ -18,10 +17,6 @@ public class Player : 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;
@ -56,7 +51,6 @@ public class Player : MonoBehaviour
}
UpdateParticlePositionAndRotation();
UpdateParticleSystemSpeed();
}
private void Jump()
@ -84,12 +78,6 @@ public class Player : MonoBehaviour
particle.transform.rotation = Quaternion.Euler(0, 0, 150.464f);
}
private void UpdateParticleSystemSpeed()
{
var velocityOverLifetime = particle.velocityOverLifetime;
velocityOverLifetime.x = rigidBody.linearVelocity.x;
}
public void OnCollisionEnter2D(Collision2D collision)
{
isColliding = true;