492 lines
11 KiB
C#
492 lines
11 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Boo.Lang;
|
|
using UnityEngine;
|
|
|
|
[Serializable]
|
|
public class RuntimeTerrains : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
internal sealed class _0024GenerateUpdate_0024837 : GenericGenerator<object>
|
|
{
|
|
[Serializable]
|
|
internal sealed class _0024 : GenericGeneratorEnumerator<object>, IEnumerator
|
|
{
|
|
internal RuntimeTerrains _0024self__0024838;
|
|
|
|
public _0024(RuntimeTerrains self_)
|
|
{
|
|
_0024self__0024838 = self_;
|
|
}
|
|
|
|
public override bool MoveNext()
|
|
{
|
|
int result;
|
|
switch (_state)
|
|
{
|
|
default:
|
|
_0024self__0024838.frames = 1f / Time.deltaTime;
|
|
if ((bool)_0024self__0024838.tc_script2)
|
|
{
|
|
goto case 2;
|
|
}
|
|
goto IL_008c;
|
|
case 2:
|
|
if (_0024self__0024838.tc_script2.generate)
|
|
{
|
|
_0024self__0024838.tc_script2.generate_output(_0024self__0024838.tc_script2.prelayers[0]);
|
|
result = (YieldDefault(2) ? 1 : 0);
|
|
break;
|
|
}
|
|
goto IL_008c;
|
|
case 1:
|
|
{
|
|
result = 0;
|
|
break;
|
|
}
|
|
IL_008c:
|
|
YieldDefault(1);
|
|
goto case 1;
|
|
}
|
|
return (byte)result != 0;
|
|
}
|
|
}
|
|
|
|
internal RuntimeTerrains _0024self__0024839;
|
|
|
|
public _0024GenerateUpdate_0024837(RuntimeTerrains self_)
|
|
{
|
|
_0024self__0024839 = self_;
|
|
}
|
|
|
|
public override IEnumerator<object> GetEnumerator()
|
|
{
|
|
return new _0024(_0024self__0024839);
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
internal sealed class _0024GenerateStart_0024840 : GenericGenerator<object>
|
|
{
|
|
[Serializable]
|
|
internal sealed class _0024 : GenericGeneratorEnumerator<object>, IEnumerator
|
|
{
|
|
internal RuntimeTerrains _0024self__0024841;
|
|
|
|
public _0024(RuntimeTerrains self_)
|
|
{
|
|
_0024self__0024841 = self_;
|
|
}
|
|
|
|
public override bool MoveNext()
|
|
{
|
|
int result;
|
|
switch (_state)
|
|
{
|
|
default:
|
|
_0024self__0024841.generating = true;
|
|
_0024self__0024841.currentOutput = 0;
|
|
goto IL_00a7;
|
|
case 2:
|
|
if (_0024self__0024841.tc_script2.generate)
|
|
{
|
|
result = (YieldDefault(2) ? 1 : 0);
|
|
break;
|
|
}
|
|
if (_0024self__0024841.tc_script2.splat_output)
|
|
{
|
|
_0024self__0024841.tc_script2.stitch_splatmap();
|
|
}
|
|
goto IL_00a7;
|
|
case 1:
|
|
{
|
|
result = 0;
|
|
break;
|
|
}
|
|
IL_00a7:
|
|
if (_0024self__0024841.SelectCloneOutput())
|
|
{
|
|
_0024self__0024841.tc_script2.generate = true;
|
|
_0024self__0024841.StartCoroutine(_0024self__0024841.GenerateUpdate());
|
|
goto case 2;
|
|
}
|
|
_0024self__0024841.GenerateStop();
|
|
YieldDefault(1);
|
|
goto case 1;
|
|
}
|
|
return (byte)result != 0;
|
|
}
|
|
}
|
|
|
|
internal RuntimeTerrains _0024self__0024842;
|
|
|
|
public _0024GenerateStart_0024840(RuntimeTerrains self_)
|
|
{
|
|
_0024self__0024842 = self_;
|
|
}
|
|
|
|
public override IEnumerator<object> GetEnumerator()
|
|
{
|
|
return new _0024(_0024self__0024842);
|
|
}
|
|
}
|
|
|
|
private GameObject TerrainComposer;
|
|
|
|
private terraincomposer_save tc_script;
|
|
|
|
private GameObject TerrainComposerClone;
|
|
|
|
private terraincomposer_save tc_script2;
|
|
|
|
private float frames;
|
|
|
|
private int currentOutput;
|
|
|
|
public bool moveTerrainsWithCamera;
|
|
|
|
public float updateCheckTime;
|
|
|
|
public Transform mainCamera;
|
|
|
|
public bool generateOnStart;
|
|
|
|
public bool createTerrainsOnTheFly;
|
|
|
|
public bool autoSpeed;
|
|
|
|
public int targetFrames;
|
|
|
|
public int generateSpeed;
|
|
|
|
public bool heightmapOutput;
|
|
|
|
public bool splatOutput;
|
|
|
|
public bool treeOutput;
|
|
|
|
public bool grassOutput;
|
|
|
|
public bool objectOutput;
|
|
|
|
public int seed;
|
|
|
|
public bool randomSeed;
|
|
|
|
public bool randomizeHeightmapOutput;
|
|
|
|
public Vector2 randomizeHeightmapRange;
|
|
|
|
public bool randomizeTreeOutput;
|
|
|
|
public Vector2 randomizeTreeRange;
|
|
|
|
public bool randomizeGrassOutput;
|
|
|
|
public Vector2 randomizeGrassRange;
|
|
|
|
public bool randomizeObjectOutput;
|
|
|
|
public Vector2 randomizeObjectRange;
|
|
|
|
private float terrainSize;
|
|
|
|
private float totalSize;
|
|
|
|
private Vector3[] initPos;
|
|
|
|
private Vector3 oldPos;
|
|
|
|
private float terrainSizeHalf;
|
|
|
|
private float relativePos;
|
|
|
|
private float newPos;
|
|
|
|
private float offset;
|
|
|
|
private GUIStyle myStyle;
|
|
|
|
private System.Collections.Generic.List<terrain_class> terrains;
|
|
|
|
private bool resetActive;
|
|
|
|
private bool generateStart;
|
|
|
|
private bool generating;
|
|
|
|
public RuntimeTerrains()
|
|
{
|
|
updateCheckTime = 0.5f;
|
|
generateOnStart = true;
|
|
autoSpeed = true;
|
|
targetFrames = 90;
|
|
generateSpeed = 500;
|
|
seed = 10;
|
|
randomizeHeightmapRange = new Vector2(0f, 1000f);
|
|
randomizeTreeRange = new Vector2(0f, 1000f);
|
|
randomizeGrassRange = new Vector2(0f, 1000f);
|
|
randomizeObjectRange = new Vector2(0f, 1000f);
|
|
terrains = new System.Collections.Generic.List<terrain_class>();
|
|
}
|
|
|
|
public virtual void Start()
|
|
{
|
|
TerrainComposer = GameObject.Find("TerrainComposer_Save");
|
|
tc_script = (terraincomposer_save)TerrainComposer.GetComponent(typeof(terraincomposer_save));
|
|
tc_script.heightmap_output = false;
|
|
tc_script.splat_output = false;
|
|
myStyle = new GUIStyle();
|
|
if (createTerrainsOnTheFly)
|
|
{
|
|
CreateTerrains();
|
|
}
|
|
if (moveTerrainsWithCamera)
|
|
{
|
|
if (mainCamera == null)
|
|
{
|
|
Debug.Log("Assign the Main Camera");
|
|
Debug.Break();
|
|
}
|
|
StartMoveTerrains();
|
|
InvokeRepeating("UpdateMoveTerrain", 0f, updateCheckTime);
|
|
}
|
|
if (generateOnStart)
|
|
{
|
|
StartCoroutine(GenerateStart());
|
|
}
|
|
}
|
|
|
|
public virtual void CreateTerrains()
|
|
{
|
|
tc_script.create_terrain(tc_script.terrains[0], tc_script.terrainTiles);
|
|
tc_script.fit_terrain_tiles(tc_script.terrains[0], true);
|
|
ParentTerrains();
|
|
}
|
|
|
|
public virtual void UpdateMoveTerrain()
|
|
{
|
|
if (!generating)
|
|
{
|
|
resetActive = false;
|
|
generateStart = false;
|
|
ResetActive();
|
|
UpdateTerrainPositionsX();
|
|
UpdateTerrainPositionsZ();
|
|
if (generateStart)
|
|
{
|
|
StartCoroutine(GenerateStart());
|
|
}
|
|
}
|
|
}
|
|
|
|
public virtual void ResetActive()
|
|
{
|
|
if (!resetActive)
|
|
{
|
|
for (int i = 0; i < tc_script.terrains.Count; i++)
|
|
{
|
|
tc_script.terrains[i].active = false;
|
|
}
|
|
resetActive = true;
|
|
}
|
|
}
|
|
|
|
public virtual IEnumerator GenerateUpdate()
|
|
{
|
|
return new _0024GenerateUpdate_0024837(this).GetEnumerator();
|
|
}
|
|
|
|
public virtual IEnumerator GenerateStart()
|
|
{
|
|
return new _0024GenerateStart_0024840(this).GetEnumerator();
|
|
}
|
|
|
|
public virtual void GenerateStop()
|
|
{
|
|
UnityEngine.Object.Destroy(TerrainComposerClone);
|
|
if (currentOutput == 5)
|
|
{
|
|
for (int i = 0; i < terrains.Count; i++)
|
|
{
|
|
terrains[i].terrain.gameObject.SetActive(true);
|
|
}
|
|
terrains.Clear();
|
|
generating = false;
|
|
}
|
|
}
|
|
|
|
public virtual bool SelectCloneOutput()
|
|
{
|
|
tc_script.disable_outputs();
|
|
if (randomSeed)
|
|
{
|
|
seed = (int)(Time.realtimeSinceStartup * 2000f);
|
|
}
|
|
int result;
|
|
if (currentOutput == 0)
|
|
{
|
|
currentOutput = 1;
|
|
if (heightmapOutput)
|
|
{
|
|
tc_script.heightmap_output = true;
|
|
CreateClone();
|
|
if (randomizeHeightmapOutput)
|
|
{
|
|
tc_script2.randomize_layer_offset(layer_output_enum.heightmap, randomizeHeightmapRange, seed);
|
|
}
|
|
result = 1;
|
|
goto IL_017b;
|
|
}
|
|
}
|
|
if (currentOutput == 1)
|
|
{
|
|
tc_script.terrains[0].SetAllNeighbors(tc_script.terrains);
|
|
currentOutput = 2;
|
|
if (splatOutput)
|
|
{
|
|
tc_script.splat_output = true;
|
|
CreateClone();
|
|
result = 1;
|
|
goto IL_017b;
|
|
}
|
|
}
|
|
if (currentOutput == 2)
|
|
{
|
|
currentOutput = 3;
|
|
if (treeOutput)
|
|
{
|
|
tc_script.tree_output = true;
|
|
CreateClone();
|
|
result = 1;
|
|
goto IL_017b;
|
|
}
|
|
}
|
|
if (currentOutput == 3)
|
|
{
|
|
currentOutput = 4;
|
|
if (grassOutput)
|
|
{
|
|
tc_script.grass_output = true;
|
|
CreateClone();
|
|
result = 1;
|
|
goto IL_017b;
|
|
}
|
|
}
|
|
if (currentOutput == 4)
|
|
{
|
|
currentOutput = 5;
|
|
if (objectOutput)
|
|
{
|
|
tc_script.object_output = true;
|
|
CreateClone();
|
|
result = 1;
|
|
goto IL_017b;
|
|
}
|
|
}
|
|
result = 0;
|
|
goto IL_017b;
|
|
IL_017b:
|
|
return (byte)result != 0;
|
|
}
|
|
|
|
public virtual void CreateClone()
|
|
{
|
|
if ((bool)tc_script2)
|
|
{
|
|
GenerateStop();
|
|
}
|
|
TerrainComposerClone = UnityEngine.Object.Instantiate(TerrainComposer);
|
|
TerrainComposerClone.name = "<Generating>";
|
|
tc_script2 = (terraincomposer_save)TerrainComposerClone.GetComponent(typeof(terraincomposer_save));
|
|
tc_script2.script_base = tc_script;
|
|
tc_script2.auto_speed = autoSpeed;
|
|
tc_script2.generate_speed = generateSpeed;
|
|
tc_script2.target_frame = targetFrames;
|
|
tc_script2.runtime = true;
|
|
tc_script2.generate_begin();
|
|
}
|
|
|
|
public virtual void TerrainsFlush()
|
|
{
|
|
for (int i = 0; i < tc_script.terrains.Count; i++)
|
|
{
|
|
tc_script.terrains[i].terrain.Flush();
|
|
}
|
|
}
|
|
|
|
public virtual void ParentTerrains()
|
|
{
|
|
GameObject gameObject = new GameObject();
|
|
gameObject.name = "_Terrains";
|
|
for (int i = 0; i < tc_script.terrains.Count; i++)
|
|
{
|
|
tc_script.terrains[i].terrain.transform.parent = gameObject.transform;
|
|
}
|
|
}
|
|
|
|
public virtual void StartMoveTerrains()
|
|
{
|
|
terrainSize = tc_script.terrains[0].size.x;
|
|
totalSize = tc_script.terrains[0].tiles.x * terrainSize;
|
|
terrainSizeHalf = terrainSize / 2f;
|
|
initPos = new Vector3[tc_script.terrains.Count];
|
|
for (int i = 0; i < tc_script.terrains.Count; i++)
|
|
{
|
|
initPos[i] = tc_script.terrains[i].terrain.transform.position;
|
|
}
|
|
offset = terrainSize / 2f;
|
|
if (totalSize / terrainSize % 2f != 0f)
|
|
{
|
|
}
|
|
UpdateTerrainPositionsX();
|
|
UpdateTerrainPositionsZ();
|
|
if (generateStart)
|
|
{
|
|
StartCoroutine(GenerateStart());
|
|
}
|
|
}
|
|
|
|
public virtual void UpdateTerrainPositionsX()
|
|
{
|
|
for (int i = 0; i < tc_script.terrains.Count; i++)
|
|
{
|
|
relativePos = mainCamera.transform.position.x - initPos[i].x;
|
|
newPos = Mathf.Round((relativePos - offset) / totalSize) * totalSize + initPos[i].x;
|
|
if (tc_script.terrains[i].terrain.transform.position.x != newPos)
|
|
{
|
|
tc_script.terrains[i].terrain.transform.position = new Vector3(newPos, tc_script.terrains[i].terrain.transform.position.y, tc_script.terrains[i].terrain.transform.position.z);
|
|
tc_script.terrains[i].terrain.gameObject.SetActive(false);
|
|
terrains.Add(tc_script.terrains[i]);
|
|
tc_script.terrains[i].active = true;
|
|
generateStart = true;
|
|
}
|
|
}
|
|
oldPos.x = Mathf.Round(mainCamera.transform.position.x / terrainSize) * terrainSize;
|
|
}
|
|
|
|
public virtual void UpdateTerrainPositionsZ()
|
|
{
|
|
for (int i = 0; i < tc_script.terrains.Count; i++)
|
|
{
|
|
relativePos = mainCamera.transform.position.z - initPos[i].z;
|
|
newPos = Mathf.Round((relativePos - offset) / totalSize) * totalSize + initPos[i].z;
|
|
if (tc_script.terrains[i].terrain.transform.position.z != newPos)
|
|
{
|
|
tc_script.terrains[i].terrain.transform.position = new Vector3(tc_script.terrains[i].terrain.transform.position.x, tc_script.terrains[i].terrain.transform.position.y, newPos);
|
|
tc_script.terrains[i].terrain.gameObject.SetActive(false);
|
|
tc_script.terrains[i].SetAllNeighbors(tc_script.terrains);
|
|
terrains.Add(tc_script.terrains[i]);
|
|
tc_script.terrains[i].active = true;
|
|
generateStart = true;
|
|
}
|
|
}
|
|
oldPos.z = Mathf.Round(mainCamera.transform.position.z / terrainSize) * terrainSize;
|
|
}
|
|
|
|
public virtual void Main()
|
|
{
|
|
}
|
|
}
|