Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class DecorationSpawner : MonoBehaviour
  6. {
  7. [System.Serializable]
  8. public class decoInfo
  9. {
  10. public List<Decoration> decorationReferences;
  11. public int decorationCount = 10;
  12. public float minimalDistanceBetweenDecoration = 10.0f;
  13. }
  14.  
  15. public float minX = 0.0f;
  16. public float maxX = 180.0f;
  17. public float minZ = 0.0f;
  18. public float maxZ = 180.0f;
  19.  
  20. public List<decoInfo> decorations = new List<decoInfo>();
  21.  
  22. private float Scale = 0.5f;
  23.  
  24. public void RemoveDecoration()
  25. {
  26. foreach (Decoration item in Object.FindObjectsOfType<Decoration>())
  27. {
  28. DestroyImmediate(item.gameObject);
  29. }
  30. }
  31.  
  32. public void SpawnDecoration()
  33. {
  34. foreach (decoInfo item in decorations)
  35. {
  36. for (int i = 0; i < item.decorationCount; i++)
  37. {
  38. //Object newProp = Instantiate(item.decorationReferences[(int)Random.Range(0.0f, (float)item.decorationReferences.Count)]);
  39. int iObject = Random.Range(0, item.decorationReferences.Count);
  40.  
  41. bool bPlaced = false;
  42. int counter = 0;
  43. while ((bPlaced == false) && (counter < 10))
  44. {
  45. item.decorationReferences[iObject].transform.position = new Vector3(Random.Range(minX, maxX), 250, Random.Range(minZ, maxZ));
  46.  
  47. RaycastHit hit;
  48. Physics.Raycast(item.decorationReferences[iObject].transform.position, Vector3.down, out hit);
  49.  
  50. if (hit.transform.gameObject == this.transform.gameObject)
  51. {
  52. if (item.decorationReferences[iObject].isAllowed(hit.point.y))
  53. {
  54. item.decorationReferences[iObject].transform.position = new Vector3(item.decorationReferences[iObject].transform.position.x,
  55. item.decorationReferences[iObject].getRandomAltitude(hit.point.y),
  56. item.decorationReferences[iObject].transform.position.z);
  57.  
  58. GameObject tObject = Instantiate(item.decorationReferences[iObject].gameObject, item.decorationReferences[iObject].transform.position, item.decorationReferences[iObject].getRandomOrientation(hit.normal)) as GameObject;
  59.  
  60. tObject.transform.localScale = new Vector3(Scale, Scale, Scale);
  61. bPlaced = true;
  62. }
  63. else counter++;
  64. }
  65. else counter++;
  66. }
  67. }
  68. }
  69. }
  70.  
  71. // Use this for initialization
  72. void Start()
  73. {
  74.  
  75. }
  76.  
  77. // Update is called once per frame
  78. void Update()
  79. {
  80.  
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement