Advertisement
Grognard_87

Untitled

Feb 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. public class EnemySpawner01 : MonoBehaviour
  2. {
  3. public GameObject enemyPrefab;
  4. public float width = 10;
  5. public float height = 5;
  6. public float speed = 5.0f;
  7. public float padding = 0;
  8. public float SpawnDelay = 1;
  9. public float Timer;
  10. public Transform TrainingBoss;
  11.  
  12. private int direction = 1;
  13. private float boundaryRightEdge, boundaryLeftEdge;
  14. void Start()
  15. {
  16. Camera camera = Camera.main;
  17. float distance = transform.position.z - camera.transform.position.z;
  18. boundaryLeftEdge = camera.ViewportToWorldPoint(new Vector3(0, 0, distance)).x + padding;
  19. boundaryRightEdge = camera.ViewportToWorldPoint(new Vector3(1, 1, distance)).x - padding;
  20. SpawnUntilFull();
  21. }
  22. void SpawnEnemies()
  23. {
  24. foreach (Transform child in transform)
  25. {
  26. GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
  27. enemy.transform.parent = child;
  28. }
  29. }
  30. void SpawnUntilFull()
  31. {
  32. Transform FreePosition = NextFreePosition();
  33. if (FreePosition)
  34. {
  35. GameObject enemy = Instantiate(enemyPrefab, FreePosition.position, Quaternion.identity) as GameObject;
  36. enemy.transform.parent = FreePosition;
  37. }
  38. if (NextFreePosition())
  39. {
  40. Invoke("SpawnUntilFull", SpawnDelay);
  41. }
  42. }
  43. void OnDrawGizmos()
  44. {
  45. float xmin, xmax, ymin, ymax;
  46. xmin = transform.position.x - 0.5f * width;
  47. xmax = transform.position.x + 0.5f * width;
  48. ymin = transform.position.y - 0.5f * height;
  49. ymax = transform.position.y + 0.5f * height;
  50. Gizmos.DrawLine(new Vector3(xmin, ymin, 0), new Vector3(xmin, ymax));
  51. Gizmos.DrawLine(new Vector3(xmin, ymax, 0), new Vector3(xmax, ymax));
  52. Gizmos.DrawLine(new Vector3(xmax, ymax, 0), new Vector3(xmax, ymin));
  53. Gizmos.DrawLine(new Vector3(xmax, ymin, 0), new Vector3(xmin, ymin));
  54. }
  55. void Update()
  56. {
  57. float formationRightEdge = transform.position.x + 0.5f * width;
  58. float formationLeftEdge = transform.position.x - 0.5f * width;
  59. if (formationRightEdge > boundaryRightEdge)
  60. {
  61. direction = -1;
  62. }
  63. if (formationLeftEdge < boundaryLeftEdge)
  64. {
  65. direction = 1;
  66. }
  67. transform.position += new Vector3(direction * speed * Time.deltaTime, 0, 0);
  68. if (AllMembersDead())
  69. {
  70. SpawnUntilFull();
  71. }
  72.  
  73. if (Timer > 0)
  74. {
  75. Timer -= Time.deltaTime;
  76. }
  77. else
  78. {
  79. Destroy(gameObject);
  80. if (TrainingBoss.gameObject.activeInHierarchy == false)
  81. {
  82. TrainingBoss.gameObject.SetActive(true);
  83. }
  84. else
  85. {
  86. TrainingBoss.gameObject.SetActive(false);
  87. }
  88. }
  89.  
  90. }
  91. Transform NextFreePosition()
  92. {
  93. foreach (Transform childPositionGameObject in transform)
  94. {
  95. if (childPositionGameObject.childCount == 0)
  96. {
  97. return childPositionGameObject;
  98. }
  99. }
  100. return null;
  101. }
  102.  
  103. bool AllMembersDead()
  104. {
  105. foreach (Transform childPositionGameObject in transform)
  106. {
  107. if (childPositionGameObject.childCount > 0)
  108. {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement