Updated project to unity 5, finalized terrain generation
This commit is contained in:
28
Game/Assets/Scripts/Unity/InitializeScript.cs
Normal file
28
Game/Assets/Scripts/Unity/InitializeScript.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using TransportGame.Business;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Utils;
|
||||
using UnityEngine;
|
||||
|
||||
public class InitializeScript : MonoBehaviour
|
||||
{
|
||||
// Use this for initialization
|
||||
public void Start()
|
||||
{
|
||||
// Load configuration
|
||||
Logger.Info("Loading configuration...");
|
||||
ConfigurationManager.LoadConfiguration();
|
||||
Logger.Info("Finished loading configuration.");
|
||||
|
||||
// Load biomes
|
||||
Logger.Info("Loading biomes...");
|
||||
BiomeManager.LoadBiomes();
|
||||
Logger.Info("Finished loading biomes.");
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
}
|
||||
}
|
90
Game/Assets/Scripts/Unity/TerrainGeneratorScript.cs
Normal file
90
Game/Assets/Scripts/Unity/TerrainGeneratorScript.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using TransportGame.Utils;
|
||||
using System.Threading;
|
||||
using TransportGame.Generator;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Business;
|
||||
|
||||
public class TerrainGeneratorScript : MonoBehaviour
|
||||
{
|
||||
private Map map = null;
|
||||
|
||||
public int TerrainWidth = 1024;
|
||||
public int TerrainHeight = 1024;
|
||||
public GameObject WaterObject;
|
||||
|
||||
// Use this for initialization
|
||||
void Start()
|
||||
{
|
||||
StartCoroutine(GenerateMap());
|
||||
}
|
||||
|
||||
private void GenerateTerrainThread()
|
||||
{
|
||||
TerrainGenerator generator = new TerrainGenerator();
|
||||
map = generator.Generate(TerrainWidth, TerrainHeight);
|
||||
}
|
||||
|
||||
private Mesh GenerateWater()
|
||||
{
|
||||
Mesh water = new Mesh();
|
||||
water.name = "water";
|
||||
water.vertices = new[] {
|
||||
new Vector3(0, map.WaterLevel, 0),
|
||||
new Vector3(0, map.WaterLevel, map.Height),
|
||||
new Vector3(map.Width, map.WaterLevel, 0),
|
||||
new Vector3(map.Width, map.WaterLevel, map.Height)
|
||||
};
|
||||
water.triangles = new[] { 0, 1, 2, 2, 1, 3 };
|
||||
water.uv = new[] {
|
||||
new Vector2(0, 0),
|
||||
new Vector2(0, 1),
|
||||
new Vector2(1, 0),
|
||||
new Vector2(1, 1)
|
||||
};
|
||||
water.RecalculateNormals();
|
||||
|
||||
return water;
|
||||
}
|
||||
|
||||
private IEnumerator GenerateMap()
|
||||
{
|
||||
// Wait for the map generation thread
|
||||
foreach (var i in Task.RunAsync(GenerateTerrainThread))
|
||||
yield return i;
|
||||
|
||||
// Generate terrain data
|
||||
TerrainData terrainData = new TerrainData();
|
||||
terrainData.heightmapResolution = Mathf.Max(map.Height, map.Width) + 1;
|
||||
terrainData.size = new Vector3(map.Width, map.Biome.Height, map.Height);
|
||||
terrainData.SetDetailResolution(1024, 8);
|
||||
terrainData.SetHeights(0, 0, map.Heights);
|
||||
terrainData.name = "Generated Terrain Data";
|
||||
yield return null;
|
||||
|
||||
// Create terrain object
|
||||
GameObject terrain = Terrain.CreateTerrainGameObject(terrainData);
|
||||
terrain.name = "Generated Terrain";
|
||||
yield return null;
|
||||
|
||||
Terrain terrainComp = terrain.GetComponent<Terrain>();
|
||||
terrainComp.heightmapPixelError = 1;
|
||||
yield return null;
|
||||
|
||||
// Set water
|
||||
if (WaterObject != null)
|
||||
{
|
||||
MeshFilter waterMesh = WaterObject.GetComponent<MeshFilter>();
|
||||
waterMesh.mesh = GenerateWater();
|
||||
}
|
||||
|
||||
// Set up textures
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user