Преглед изворни кода

Random generated map works on each start

Priz пре 4 дана
родитељ
комит
1fe9ab077b

+ 1 - 1
GAMEN3-ValleyRunner_SanTi036/Assets/Scenes/Game.unity

@@ -3156,7 +3156,7 @@ MonoBehaviour:
   - lod: 4
     visibleDstThreshold: 500
     useForCollider: 0
-  viewer: {fileID: 0}
+  viewer: {fileID: 1708832120}
   mapMaterial: {fileID: 2100000, guid: b0872f84cf3c7441d893168677410c73, type: 2}
 --- !u!114 &1078513059
 MonoBehaviour:

+ 41 - 4
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/MapGen/EndlessTerrain.cs

@@ -24,10 +24,26 @@ public class EndlessTerrain : MonoBehaviour {
 	Dictionary<Vector2, TerrainChunk> terrainChunkDictionary = new Dictionary<Vector2, TerrainChunk>();
 	static List<TerrainChunk> terrainChunksVisibleLastUpdate = new List<TerrainChunk>();
 
-	void Start() {
+    void Awake()
+    {
+        ResetTerrain();
+    }
+
+
+
+    void Start() {
 		mapGenerator = FindObjectOfType<MapGenerator> ();
 
-		maxViewDst = detailLevels [detailLevels.Length - 1].visibleDstThreshold;
+        if (mapGenerator == null)
+        {
+            Debug.LogError("MapGenerator not found!");
+            return;
+        }
+
+        viewerPosition = Vector2.zero;
+        viewerPositionOld = Vector2.one * 9999f; // force update
+
+        maxViewDst = detailLevels [detailLevels.Length - 1].visibleDstThreshold;
 		chunkSize = MapGenerator.mapChunkSize - 1;
 		chunksVisibleInViewDst = Mathf.RoundToInt(maxViewDst / chunkSize);
 
@@ -175,9 +191,15 @@ public class EndlessTerrain : MonoBehaviour {
 			return meshObject.activeSelf;
 		}
 
-	}
+        public void Dispose()
+        {
+            Object.Destroy(meshObject);
+        }
+
 
-	class LODMesh {
+    }
+
+    class LODMesh {
 
 		public Mesh mesh;
 		public bool hasRequestedMesh;
@@ -211,4 +233,19 @@ public class EndlessTerrain : MonoBehaviour {
 		public bool useForCollider;
 	}
 
+    public void ResetTerrain()
+    {
+        // Destroy chunk GameObjects
+        foreach (var chunk in terrainChunkDictionary.Values)
+        {
+            chunk.Dispose();
+        }
+
+        terrainChunkDictionary.Clear();
+        terrainChunksVisibleLastUpdate.Clear();
+    }
+
+
+
 }
+

+ 18 - 4
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/MapGen/MapGenerator.cs

@@ -40,11 +40,25 @@ public class MapGenerator : MonoBehaviour {
 	Queue<MapThreadInfo<MapData>> mapDataThreadInfoQueue = new Queue<MapThreadInfo<MapData>>();
 	Queue<MapThreadInfo<MeshData>> meshDataThreadInfoQueue = new Queue<MapThreadInfo<MeshData>>();
 
-	void Awake() {
-		falloffMap = FalloffGenerator.GenerateFalloffMap (mapChunkSize);
-	}
+    void Awake()
+    {
+
+        if (WorldSettings.generateNewWorld)
+        {
+            seed = WorldSettings.seed;
+        }
+
+        falloffMap = FalloffGenerator.GenerateFalloffMap(mapChunkSize);
+    }
+
+    void Start()
+    {
+        WorldSettings.generateNewWorld = false;
+    }
+
+
 
-	public static int mapChunkSize {
+    public static int mapChunkSize {
 		get {
 			if (instance == null) {
 				instance = FindObjectOfType<MapGenerator> ();

+ 9 - 1
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/MenuManager.cs

@@ -8,20 +8,28 @@ public class MenuManager : MonoBehaviour
 
     public void StartNewGame()
     {
+        WorldSettings.generateNewWorld = true;
+        WorldSettings.seed = Random.Range(int.MinValue, int.MaxValue);
+
         LoadingManager.sceneToLoad = "Game";
         LoadingManager.loadSave = false;  // No loading for new game
+
         SceneManager.LoadScene("LoadingScreen");
     }
 
     public void LoadGame()
     {
+        WorldSettings.generateNewWorld = false;
+
         LoadingManager.sceneToLoad = "Game";
-        LoadingManager.loadSave = true;   // Load the last save
+        LoadingManager.loadSave = true; // load the last save
+
         SceneManager.LoadScene("LoadingScreen");
     }
 
 
 
+
     public void OpenSettings()
     {
         if (SettingsPanel != null)

+ 7 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/WorldSettings.cs

@@ -0,0 +1,7 @@
+using UnityEngine;
+
+public static class WorldSettings
+{
+    public static bool generateNewWorld = false;
+    public static int seed;
+}

+ 2 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/WorldSettings.cs.meta

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

+ 1 - 1
GAMEN3-ValleyRunner_SanTi036/Assets/Skybox.mat

@@ -112,7 +112,7 @@ Material:
     - _DstBlend: 0
     - _DstBlendAlpha: 0
     - _EnvironmentReflections: 1
-    - _Exposure: 0.966106
+    - _Exposure: 0.9679918
     - _GlossMapScale: 0
     - _Glossiness: 0
     - _GlossyReflections: 0