Guest User

Generate Infinite

a guest
Oct 5th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. class Tile
  5. {
  6. public GameObject theTile;
  7. public float creationTime;
  8.  
  9. public Tile(GameObject t, float ct)
  10. {
  11. theTile = t;
  12. creationTime = ct;
  13. }
  14. }
  15.  
  16.  
  17. public class GenerateInfinite : MonoBehaviour {
  18.  
  19. public GameObject plane;
  20. public GameObject player;
  21.  
  22. int planeSize = 10;
  23. int halfTilesX = 10;
  24. int halfTilesZ = 10;
  25.  
  26. Vector3 startPos;
  27.  
  28. Hashtable tiles = new Hashtable();
  29.  
  30. void Start ()
  31. {
  32. this.gameObject.transform.position = Vector3.zero;
  33. startPos = Vector3.zero;
  34.  
  35. float updateTime = Time.realtimeSinceStartup;
  36.  
  37. for (int x = -halfTilesX; x < halfTilesX; x++)
  38. {
  39. for (int z = -halfTilesZ; z < halfTilesZ; z++)
  40. {
  41. Vector3 pos = new Vector3 ((x * planeSize + startPos.x),
  42. 0,
  43. (z * planeSize + startPos.z));
  44. GameObject t = (GameObject)Instantiate (plane, pos, Quaternion.identity);
  45.  
  46. string tilename = "Tile_" + ((int)(pos.x)).ToString () + "_" + ((int)(pos.z)).ToString ();
  47. t.name = tilename;
  48. Tile tile = new Tile (t, updateTime);
  49. tiles.Add (tilename, tile);
  50. }
  51. }
  52. }
  53.  
  54.  
  55. void Update ()
  56. {
  57. //Determine how far the player has moved since last update
  58. int xMove = (int)(player.transform.position.x - startPos.x);
  59. int zMove = (int)(player.transform.position.z - startPos.z);
  60.  
  61. if (Mathf.Abs (xMove) >= planeSize || Mathf.Abs (zMove) >= planeSize)
  62. {
  63. float updateTime = Time.realtimeSinceStartup;
  64.  
  65. //Force integer position and round to nearest tilesize
  66. int playerX = (int)(Mathf.Floor(player.transform.position.x/planeSize) * planeSize);
  67. int playerZ = (int)(Mathf.Floor(player.transform.position.z/planeSize) * planeSize);
  68.  
  69. for (int x = -halfTilesX; x < halfTilesX; x++)
  70. {
  71. for (int z = -halfTilesZ; z < halfTilesZ; z++)
  72. {
  73. Vector3 pos = new Vector3 ((x * planeSize + playerX),
  74. 0,
  75. (z * planeSize + playerZ));
  76.  
  77. string tilename = "Tile_" + ((int)(pos.x)).ToString () + "_" + ((int)(pos.z)).ToString ();
  78.  
  79. //If the tile isnt already in the hashtable (created), make a new one
  80. if (!tiles.ContainsKey (tilename))
  81. {
  82. GameObject t = (GameObject)Instantiate (plane, pos, Quaternion.identity);
  83. t.name = tilename;
  84. Tile tile = new Tile (t, updateTime);
  85. tiles.Add (tilename, tile);
  86. }
  87.  
  88. //Tile already exists, update the timestamp
  89. else
  90. {
  91. (tiles [tilename] as Tile).creationTime = updateTime;
  92. }
  93. }
  94. }
  95.  
  96. //Destroy all tiles not just created or updated
  97. //and put new tiles and tiles to be kept in a new hashtable
  98. Hashtable newTerrain = new Hashtable();
  99. foreach (Tile tls in tiles.Values)
  100. {
  101. if (tls.creationTime != updateTime) {
  102. //Delete gameobject
  103. Destroy (tls.theTile);
  104. }
  105.  
  106. else
  107. {
  108. newTerrain.Add (tls.theTile.name, tls);
  109. }
  110. }
  111.  
  112. //Copy new hashtable contents to the working hashtable
  113. tiles = newTerrain;
  114.  
  115. startPos = player.transform.position;
  116. }
  117. }
  118. }
Add Comment
Please, Sign In to add comment