Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. public void Generate()
  2. {
  3. Vector3 tmpPos;
  4. GameObject tree;
  5.  
  6. /*
  7. System.Random r = new System.Random();
  8. for (int i = 0; i < 100; i++)
  9. {
  10. print((float)RandomExtensions.NextGaussian(r, this.waterMu, this.waterSigma));
  11. }
  12. * */
  13.  
  14. System.DateTime d = System.DateTime.Now;
  15.  
  16. if (this.waterTrees > 0 || this.dryTrees > 0 )
  17. {
  18. this.waterTreesQueue = new List<GameObject>(this.waterTrees);
  19. this.dryTreesQueue = new List<GameObject>(this.dryTrees);
  20.  
  21. SpawnWater();
  22. SpawnDry();
  23. print("creation: " + (System.DateTime.Now - d));
  24. int rows = Mathf.RoundToInt(Mathf.Sqrt(this.waterTrees + this.dryTrees));
  25. int columns = rows;
  26.  
  27. int indexX = Mathf.RoundToInt((this.gridWidth / rows));
  28. int indexY = Mathf.RoundToInt((this.gridHeight / columns));
  29. int startX = Mathf.RoundToInt((indexX / 2) - (this.gridWidth/2));
  30. int startY = Mathf.RoundToInt((indexY / 2) - (this.gridHeight/2));
  31.  
  32. int randomLimitsX = Mathf.RoundToInt(indexX / 3);
  33. int randomLimitsY = Mathf.RoundToInt(indexY / 3);
  34.  
  35. System.DateTime gt = System.DateTime.Now;
  36. for (int i = 0; i < rows; i++)
  37. {
  38. for (int j = 0; j < columns; j++)
  39. {
  40. tmpPos = new Vector3(this.gridCenter.x + (Random.Range(-1 * randomLimitsX, randomLimitsX) + (startX + indexX * i)), 0f, this.gridCenter.y + Random.Range(-1 * randomLimitsY, randomLimitsY) + (startY + indexY * j));
  41. float randVal = Random.value;
  42.  
  43. if (this.dryTreesQueue.Count == 0 || randVal < .5f)
  44. {
  45. if (this.waterTreesQueue.Count > 0)
  46. {
  47. tree = (GameObject)this.waterTreesQueue[0];
  48. this.waterTreesQueue.RemoveAt(0);
  49. tree.transform.position = tmpPos;
  50. }
  51. }
  52. else if (this.waterTreesQueue.Count == 0 || randVal > .5f)
  53. {
  54. if (this.dryTreesQueue.Count > 0)
  55. {
  56. tree = (GameObject)this.dryTreesQueue[0];
  57. this.dryTreesQueue.RemoveAt(0);
  58. tree.transform.position = tmpPos;
  59. }
  60. }
  61. }
  62. }
  63.  
  64. print("reposition: " + (System.DateTime.Now - gt));
  65.  
  66. // clean up
  67. foreach (GameObject item in this.waterTreesQueue)
  68. {
  69. Destroy(item);
  70. }
  71. foreach (GameObject item in this.dryTreesQueue)
  72. {
  73. Destroy(item);
  74. }
  75. }
  76. else
  77. {
  78. this.errorText.text = "# of trees == 0";
  79. }
  80.  
  81. print("Elapsed: " + (System.DateTime.Now - d));
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement